LatentStateProcessor.java

  1. package org.drip.service.json;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  *
  13.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  14.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  15.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  16.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  17.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  18.  *      and computational support.
  19.  *  
  20.  *      https://lakshmidrip.github.io/DROP/
  21.  *  
  22.  *  DROP is composed of three modules:
  23.  *  
  24.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  25.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  26.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  27.  *
  28.  *  DROP Product Core implements libraries for the following:
  29.  *  - Fixed Income Analytics
  30.  *  - Loan Analytics
  31.  *  - Transaction Cost Analytics
  32.  *
  33.  *  DROP Portfolio Core implements libraries for the following:
  34.  *  - Asset Allocation Analytics
  35.  *  - Asset Liability Management Analytics
  36.  *  - Capital Estimation Analytics
  37.  *  - Exposure Analytics
  38.  *  - Margin Analytics
  39.  *  - XVA Analytics
  40.  *
  41.  *  DROP Computational Core implements libraries for the following:
  42.  *  - Algorithm Support
  43.  *  - Computation Support
  44.  *  - Function Analysis
  45.  *  - Model Validation
  46.  *  - Numerical Analysis
  47.  *  - Numerical Optimizer
  48.  *  - Spline Builder
  49.  *  - Statistical Learning
  50.  *
  51.  *  Documentation for DROP is Spread Over:
  52.  *
  53.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  54.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  55.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  56.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  57.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  58.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  59.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  60.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  61.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  62.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  63.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  64.  *
  65.  *  Licensed under the Apache License, Version 2.0 (the "License");
  66.  *      you may not use this file except in compliance with the License.
  67.  *  
  68.  *  You may obtain a copy of the License at
  69.  *      http://www.apache.org/licenses/LICENSE-2.0
  70.  *  
  71.  *  Unless required by applicable law or agreed to in writing, software
  72.  *      distributed under the License is distributed on an "AS IS" BASIS,
  73.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  74.  *  
  75.  *  See the License for the specific language governing permissions and
  76.  *      limitations under the License.
  77.  */

  78. /**
  79.  * <i>LatentStateProcessor</i> Sets Up and Executes a JSON Based In/Out Curve Processor.
  80.  *
  81.  * <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/README.md">Environment, Product/Definition Containers, and Scenario/State Manipulation APIs</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/json/README.md">JSON Based Valuation Request Service</a></li>
  87.  *  </ul>
  88.  * <br><br>
  89.  *
  90.  * @author Lakshmi Krishnamurthy
  91.  */

  92. public class LatentStateProcessor {

  93.     static final org.drip.state.discount.MergedDiscountForwardCurve FundingCurve (
  94.         final org.drip.json.simple.JSONObject jsonParameter)
  95.     {
  96.         org.drip.analytics.date.JulianDate dtSpot = org.drip.json.parser.Converter.DateEntry (jsonParameter,
  97.             "SpotDate");

  98.         java.lang.String strCurrency = org.drip.json.parser.Converter.StringEntry (jsonParameter,
  99.             "Currency");

  100.         java.lang.String[] astrDepositMaturityTenor = org.drip.json.parser.Converter.StringArrayEntry
  101.             (jsonParameter, "DepositTenor");

  102.         double[] adblDepositQuote = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter,
  103.             "DepositQuote");

  104.         double[] adblFuturesQuote = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter,
  105.             "FuturesQuote");

  106.         java.lang.String[] astrFixFloatMaturityTenor = org.drip.json.parser.Converter.StringArrayEntry
  107.             (jsonParameter, "FixFloatTenor");

  108.         double[] adblFixFloatQuote = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter,
  109.             "FixFloatQuote");

  110.         return org.drip.service.template.LatentMarketStateBuilder.SmoothFundingCurve (dtSpot, strCurrency,
  111.             astrDepositMaturityTenor, adblDepositQuote, "ForwardRate", adblFuturesQuote, "ForwardRate",
  112.                 astrFixFloatMaturityTenor, adblFixFloatQuote, "SwapRate");
  113.     }

  114.     static final org.drip.state.govvie.GovvieCurve TreasuryCurve (
  115.         final org.drip.json.simple.JSONObject jsonParameter)
  116.     {
  117.         org.drip.analytics.date.JulianDate dtSpot = org.drip.json.parser.Converter.DateEntry (jsonParameter,
  118.             "SpotDate");

  119.         java.lang.String strCode = org.drip.json.parser.Converter.StringEntry (jsonParameter,
  120.             "TreasuryCode");

  121.         java.lang.String[] astrTenor = org.drip.json.parser.Converter.StringArrayEntry (jsonParameter,
  122.             "TreasuryTenor");

  123.         double[] adblYield = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter,
  124.             "TreasuryYield");

  125.         int iNumTenor = null == adblYield ? 0 : adblYield.length;
  126.         double[] adblCoupon = 0 == iNumTenor ? null : new double[iNumTenor];
  127.         org.drip.analytics.date.JulianDate[] adtMaturity = 0 == iNumTenor ? null : new
  128.             org.drip.analytics.date.JulianDate[iNumTenor];
  129.         org.drip.analytics.date.JulianDate[] adtEffective = 0 == iNumTenor ? null : new
  130.             org.drip.analytics.date.JulianDate[iNumTenor];

  131.         for (int i = 0; i < iNumTenor; ++i) {
  132.             adblCoupon[i] = adblYield[i];

  133.             adtMaturity[i] = (adtEffective[i] = dtSpot).addTenor (astrTenor[i]);
  134.         }

  135.         return org.drip.service.template.LatentMarketStateBuilder.GovvieCurve (strCode, dtSpot, adtEffective,
  136.             adtMaturity, adblCoupon, adblYield, "Yield",
  137.                 org.drip.service.template.LatentMarketStateBuilder.SHAPE_PRESERVING);
  138.     }

  139.     static final org.drip.state.credit.CreditCurve CreditCurve (
  140.         final org.drip.json.simple.JSONObject jsonParameter,
  141.         final org.drip.state.discount.MergedDiscountForwardCurve dcFunding)
  142.     {
  143.         if (null == dcFunding) return null;

  144.         java.lang.String strReferenceEntity = org.drip.json.parser.Converter.StringEntry (jsonParameter,
  145.             "ReferenceEntity");

  146.         java.lang.String[] astrCDSMaturityTenor = org.drip.json.parser.Converter.StringArrayEntry
  147.             (jsonParameter, "CDSTenor");

  148.         double[] adblCDSQuote = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter, "CDSQuote");

  149.         return org.drip.service.template.LatentMarketStateBuilder.CreditCurve (dcFunding.epoch(),
  150.             strReferenceEntity, astrCDSMaturityTenor, adblCDSQuote, adblCDSQuote, "FairPremium", dcFunding);
  151.     }

  152.     /**
  153.      * JSON Based in/out Funding Curve Sample
  154.      *
  155.      * @param jsonParameter JSON Funding Curve Request Parameters
  156.      *
  157.      * @return JSON Funding Curve Response
  158.      */

  159.     @SuppressWarnings ("unchecked") static final org.drip.json.simple.JSONObject FundingCurveThunker (
  160.         final org.drip.json.simple.JSONObject jsonParameter)
  161.     {
  162.         org.drip.analytics.date.JulianDate dtSpot = org.drip.json.parser.Converter.DateEntry (jsonParameter,
  163.             "SpotDate");

  164.         java.lang.String strCurrency = org.drip.json.parser.Converter.StringEntry (jsonParameter,
  165.             "Currency");

  166.         java.lang.String[] astrDepositMaturityTenor = org.drip.json.parser.Converter.StringArrayEntry
  167.             (jsonParameter, "DepositTenor");

  168.         double[] adblDepositQuote = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter,
  169.             "DepositQuote");

  170.         double[] adblFuturesQuote = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter,
  171.             "FuturesQuote");

  172.         java.lang.String[] astrFixFloatMaturityTenor = org.drip.json.parser.Converter.StringArrayEntry
  173.             (jsonParameter, "FixFloatTenor");

  174.         double[] adblFixFloatQuote = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter,
  175.             "FixFloatQuote");

  176.         org.drip.state.discount.MergedDiscountForwardCurve dcFunding =
  177.             org.drip.service.template.LatentMarketStateBuilder.SmoothFundingCurve (dtSpot, strCurrency,
  178.                 astrDepositMaturityTenor, adblDepositQuote, "ForwardRate", adblFuturesQuote, "ForwardRate",
  179.                     astrFixFloatMaturityTenor, adblFixFloatQuote, "SwapRate");

  180.         if (null == dcFunding) return null;

  181.         int iNumDeposit = null == adblDepositQuote ? 0 : adblDepositQuote.length;
  182.         int iNumFutures = null == adblFuturesQuote ? 0 : adblFuturesQuote.length;
  183.         int iNumFixFloat = null == adblFixFloatQuote ? 0 : adblFixFloatQuote.length;

  184.         org.drip.json.simple.JSONArray jsonDepositArray = new org.drip.json.simple.JSONArray();

  185.         for (int i = 0; i < iNumDeposit; ++i) {
  186.             org.drip.json.simple.JSONObject jsonDeposit = new org.drip.json.simple.JSONObject();

  187.             jsonDeposit.put ("InstrumentType", "DEPOSIT");

  188.             jsonDeposit.put ("MaturityTenor", astrDepositMaturityTenor[i]);

  189.             jsonDeposit.put ("InstrumentQuote", adblDepositQuote[i]);

  190.             jsonDeposit.put ("CalibrationMeasure", "ForwardRate");

  191.             try {
  192.                 jsonDeposit.put ("DiscountFactor", dcFunding.df (astrDepositMaturityTenor[i]));
  193.             } catch (java.lang.Exception e) {
  194.                 e.printStackTrace();

  195.                 return null;
  196.             }

  197.             jsonDepositArray.add (i, jsonDeposit);
  198.         }

  199.         org.drip.json.simple.JSONArray jsonFuturesArray = new org.drip.json.simple.JSONArray();

  200.         for (int i = 0; i < iNumFutures; ++i) {
  201.             org.drip.json.simple.JSONObject jsonFutures = new org.drip.json.simple.JSONObject();

  202.             jsonFutures.put ("InstrumentType", "FUTURES");

  203.             jsonFutures.put ("InstrumentQuote", adblFuturesQuote[i]);

  204.             jsonFutures.put ("CalibrationMeasure", "ForwardRate");

  205.             try {
  206.                 jsonFutures.put ("DiscountFactor", dcFunding.df ((3 + 3 * i) + "M"));
  207.             } catch (java.lang.Exception e) {
  208.                 e.printStackTrace();

  209.                 return null;
  210.             }

  211.             jsonFuturesArray.add (i, jsonFutures);
  212.         }

  213.         org.drip.json.simple.JSONArray jsonFixFloatArray = new org.drip.json.simple.JSONArray();

  214.         for (int i = 0; i < iNumFixFloat; ++i) {
  215.             org.drip.json.simple.JSONObject jsonFixFloat = new org.drip.json.simple.JSONObject();

  216.             jsonFixFloat.put ("InstrumentType", "FIXFLOAT");

  217.             jsonFixFloat.put ("MaturityTenor", astrFixFloatMaturityTenor[i]);

  218.             jsonFixFloat.put ("InstrumentQuote", adblFixFloatQuote[i]);

  219.             jsonFixFloat.put ("CalibrationMeasure", "SwapRate");

  220.             try {
  221.                 jsonFixFloat.put ("DiscountFactor", dcFunding.df (astrFixFloatMaturityTenor[i]));
  222.             } catch (java.lang.Exception e) {
  223.                 e.printStackTrace();

  224.                 return null;
  225.             }

  226.             jsonFixFloatArray.add (i, jsonFixFloat);
  227.         }

  228.         org.drip.json.simple.JSONObject jsonResponse = new org.drip.json.simple.JSONObject();

  229.         jsonResponse.put ("SpotDate", dtSpot.toString());

  230.         jsonResponse.put ("Currency", strCurrency);

  231.         jsonResponse.put ("FundingLabel", dcFunding.label().fullyQualifiedName());

  232.         jsonResponse.put ("DepositMetrics", jsonDepositArray);

  233.         jsonResponse.put ("FuturesMetrics", jsonFuturesArray);

  234.         jsonResponse.put ("FixFloatMetrics", jsonFixFloatArray);

  235.         return jsonResponse;
  236.     }

  237.     /**
  238.      * JSON Based in/out Credit Curve Sample
  239.      *
  240.      * @param jsonParameter JSON Credit Curve Request Parameters
  241.      *
  242.      * @return JSON Credit Curve Response
  243.      */

  244.     @SuppressWarnings ("unchecked") static final org.drip.json.simple.JSONObject CreditCurveThunker (
  245.         final org.drip.json.simple.JSONObject jsonParameter)
  246.     {
  247.         org.drip.state.discount.MergedDiscountForwardCurve dcFunding = FundingCurve (jsonParameter);

  248.         if (null == dcFunding) return null;

  249.         java.lang.String strReferenceEntity = org.drip.json.parser.Converter.StringEntry (jsonParameter,
  250.             "ReferenceEntity");

  251.         java.lang.String[] astrCDSMaturityTenor = org.drip.json.parser.Converter.StringArrayEntry
  252.             (jsonParameter, "CDSTenor");

  253.         double[] adblCDSQuote = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter, "CDSQuote");

  254.         org.drip.analytics.date.JulianDate dtSpot = dcFunding.epoch();

  255.         org.drip.state.credit.CreditCurve ccSurvivalRecovery =
  256.             org.drip.service.template.LatentMarketStateBuilder.CreditCurve (dtSpot, strReferenceEntity,
  257.                 astrCDSMaturityTenor, adblCDSQuote, adblCDSQuote, "FairPremium", dcFunding);

  258.         if (null == ccSurvivalRecovery) return null;

  259.         int iNumCDS = null == adblCDSQuote ? 0 : adblCDSQuote.length;

  260.         String strLatentStateLabel = ccSurvivalRecovery.label().fullyQualifiedName();

  261.         org.drip.json.simple.JSONArray jsonCDSArray = new org.drip.json.simple.JSONArray();

  262.         for (int i = 0; i < iNumCDS; ++i) {
  263.             org.drip.json.simple.JSONObject jsonCDS = new org.drip.json.simple.JSONObject();

  264.             jsonCDS.put ("ReferenceEntity", strLatentStateLabel);

  265.             jsonCDS.put ("InstrumentType", "CDS");

  266.             jsonCDS.put ("MaturityTenor", astrCDSMaturityTenor[i]);

  267.             jsonCDS.put ("InstrumentQuote", adblCDSQuote[i]);

  268.             jsonCDS.put ("CalibrationMeasure", "FairPremium");

  269.             try {
  270.                 jsonCDS.put ("SurvivalProbability", ccSurvivalRecovery.survival (astrCDSMaturityTenor[i]));

  271.                 jsonCDS.put ("Recovery", ccSurvivalRecovery.recovery (astrCDSMaturityTenor[i]));
  272.             } catch (java.lang.Exception e) {
  273.                 e.printStackTrace();

  274.                 return null;
  275.             }

  276.             jsonCDSArray.add (i, jsonCDS);
  277.         }

  278.         org.drip.json.simple.JSONObject jsonResponse = new org.drip.json.simple.JSONObject();

  279.         jsonResponse.put ("SpotDate", dtSpot.toString());

  280.         jsonResponse.put ("Currency", dcFunding.currency());

  281.         jsonResponse.put ("ReferenceEntity", strReferenceEntity);

  282.         jsonResponse.put ("CreditLabel", strLatentStateLabel);

  283.         jsonResponse.put ("FundingLabel", dcFunding.label().fullyQualifiedName());

  284.         jsonResponse.put ("CDSMetrics", jsonCDSArray);

  285.         return jsonResponse;
  286.     }
  287. }