ScenarioForwardCurveBuilder.java

  1. package org.drip.state.creator;

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

  81. /**
  82.  * <i>ScenarioForwardCurveBuilder</i> implements the the construction of the scenario Forward curve using the
  83.  * input discount curve instruments, and a wide variety of custom builds. It implements the following
  84.  * functionality:
  85.  *
  86.  *  <br><br>
  87.  *  <ul>
  88.  *      <li>
  89.  *          Non-linear Custom Discount Curve
  90.  *      </li>
  91.  *      <li>
  92.  *          Shape Preserving Discount Curve Builds - Standard Cubic Polynomial/Cubic KLK Hyperbolic Tension,
  93.  *              and other Custom Builds
  94.  *      </li>
  95.  *      <li>
  96.  *          Smoothing Local/Control Custom Build - DC/Forward/Zero Rate LSQM's
  97.  *      </li>
  98.  *      <li>
  99.  *          "Industry Standard Methodologies" - DENSE/DUALDENSE/CUSTOMDENSE and Hagan-West Forward
  100.  *              Interpolator Schemes
  101.  *      </li>
  102.  *  </ul>
  103.  *
  104.  *  <br><br>
  105.  *  <ul>
  106.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  107.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  108.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/README.md">Latent State Inference and Creation Utilities</a></li>
  109.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/creator/README.md">Scenario State Curve/Surface Builders</a></li>
  110.  *  </ul>
  111.  * <br><br>
  112.  *
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class ScenarioForwardCurveBuilder {

  116.     /**
  117.      * Build the Shape Preserving Forward Curve using the Custom Parameters
  118.      *
  119.      * @param llsc The Linear Latent State Calibrator Instance
  120.      * @param aStretchSpec Array of the Latent State Stretches
  121.      * @param fri The Floating Rate Index
  122.      * @param valParam Valuation Parameters
  123.      * @param pricerParam Pricer Parameters
  124.      * @param csqs Market Parameters
  125.      * @param quotingParam Quoting Parameters
  126.      * @param dblEpochResponse The Starting Response Value
  127.      *
  128.      * @return Instance of the Shape Preserving Discount Curve
  129.      */

  130.     public static final org.drip.state.forward.ForwardCurve ShapePreservingForwardCurve (
  131.         final org.drip.state.inference.LinearLatentStateCalibrator llsc,
  132.         final org.drip.state.inference.LatentStateStretchSpec[] aStretchSpec,
  133.         final org.drip.state.identifier.ForwardLabel fri,
  134.         final org.drip.param.valuation.ValuationParams valParam,
  135.         final org.drip.param.pricer.CreditPricerParams pricerParam,
  136.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  137.         final org.drip.param.valuation.ValuationCustomizationParams quotingParam,
  138.         final double dblEpochResponse)
  139.     {
  140.         if (null == llsc) return null;

  141.         try {
  142.             org.drip.state.forward.ForwardCurve fc = new org.drip.state.curve.BasisSplineForwardRate (fri,
  143.                 llsc.calibrateSpan (aStretchSpec, dblEpochResponse, valParam, pricerParam, quotingParam,
  144.                     csqs));

  145.             return fc.setCCIS (new org.drip.analytics.input.LatentStateShapePreservingCCIS (llsc,
  146.                 aStretchSpec, valParam, pricerParam, quotingParam, csqs)) ? fc : null;
  147.         } catch (java.lang.Exception e) {
  148.             e.printStackTrace();
  149.         }

  150.         return null;
  151.     }

  152.     /**
  153.      * Construct an instance of the Shape Preserver of the desired basis type, using the specified basis set
  154.      *  builder parameters.
  155.      *
  156.      * @param strName Curve Name
  157.      * @param fri The Floating Rate Index
  158.      * @param valParams Valuation Parameters
  159.      * @param pricerParam Pricer Parameters
  160.      * @param csqs Market Parameters
  161.      * @param quotingParam Quoting Parameters
  162.      * @param strBasisType The Basis Type
  163.      * @param fsbp The Function Set Basis Parameters
  164.      * @param aCalibComp Array of Calibration Components
  165.      * @param strManifestMeasure The Calibration Manifest Measure
  166.      * @param adblQuote Array of Calibration Quotes
  167.      * @param dblEpochResponse The Stretch Start DF
  168.      *
  169.      * @return Instance of the Shape Preserver of the desired basis type
  170.      */

  171.     public static final org.drip.state.forward.ForwardCurve ShapePreservingForwardCurve (
  172.         final java.lang.String strName,
  173.         final org.drip.state.identifier.ForwardLabel fri,
  174.         final org.drip.param.valuation.ValuationParams valParams,
  175.         final org.drip.param.pricer.CreditPricerParams pricerParam,
  176.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  177.         final org.drip.param.valuation.ValuationCustomizationParams quotingParam,
  178.         final java.lang.String strBasisType,
  179.         final org.drip.spline.basis.FunctionSetBuilderParams fsbp,
  180.         final org.drip.product.definition.CalibratableComponent[] aCalibComp,
  181.         final java.lang.String strManifestMeasure,
  182.         final double[] adblQuote,
  183.         final double dblEpochResponse)
  184.     {
  185.         if (null == strName || strName.isEmpty() || null == strBasisType || strBasisType.isEmpty() || null ==
  186.             valParams || null == fsbp || null == strManifestMeasure || strManifestMeasure.isEmpty())
  187.             return null;

  188.         int iNumQuote = null == adblQuote ? 0 : adblQuote.length;
  189.         int iNumComp = null == aCalibComp ? 0 : aCalibComp.length;

  190.         if (0 == iNumComp || iNumComp != iNumQuote) return null;

  191.         try {
  192.             org.drip.state.identifier.ForwardLabel forwardLabel = null;

  193.             if (aCalibComp[0] instanceof org.drip.product.rates.DualStreamComponent)
  194.                 forwardLabel = ((org.drip.product.rates.DualStreamComponent)
  195.                     aCalibComp[0]).derivedStream().forwardLabel();
  196.             else {
  197.                 org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.identifier.ForwardLabel>
  198.                     mapForwardLabel = aCalibComp[0].forwardLabel();

  199.                 if (null != mapForwardLabel && 0 != mapForwardLabel.size())
  200.                     forwardLabel = mapForwardLabel.get ("BASE");
  201.             }

  202.             org.drip.state.representation.LatentStateSpecification[] aLSS = new
  203.                 org.drip.state.representation.LatentStateSpecification[] {new
  204.                     org.drip.state.representation.LatentStateSpecification
  205.                         (org.drip.analytics.definition.LatentStateStatic.LATENT_STATE_FORWARD,
  206.                             org.drip.analytics.definition.LatentStateStatic.FORWARD_QM_FORWARD_RATE,
  207.                                 forwardLabel)};

  208.             org.drip.state.inference.LatentStateSegmentSpec[] aSegmentSpec = new
  209.                 org.drip.state.inference.LatentStateSegmentSpec[iNumComp];

  210.             for (int i = 0; i < iNumComp; ++i) {
  211.                 org.drip.product.calib.ProductQuoteSet pqs = aCalibComp[i].calibQuoteSet (aLSS);

  212.                 if (null == pqs || !pqs.set (strManifestMeasure, adblQuote[i])) return null;

  213.                 aSegmentSpec[i] = new org.drip.state.inference.LatentStateSegmentSpec (aCalibComp[i], pqs);
  214.             }

  215.             org.drip.state.inference.LatentStateStretchSpec[] aStretchSpec = new
  216.                 org.drip.state.inference.LatentStateStretchSpec[] {new
  217.                     org.drip.state.inference.LatentStateStretchSpec (strName, aSegmentSpec)};

  218.             org.drip.state.inference.LinearLatentStateCalibrator llsc = new
  219.                 org.drip.state.inference.LinearLatentStateCalibrator (new
  220.                     org.drip.spline.params.SegmentCustomBuilderControl (strBasisType, fsbp,
  221.                         org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), new
  222.                             org.drip.spline.params.ResponseScalingShapeControl (true, new
  223.                                 org.drip.function.r1tor1.QuadraticRationalShapeControl (0.)), null),
  224.                                     org.drip.spline.stretch.BoundarySettings.FinancialStandard(),
  225.                                         org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE, null, null);

  226.             return ShapePreservingForwardCurve (llsc, aStretchSpec, fri, valParams, pricerParam, csqs,
  227.                 quotingParam, dblEpochResponse);
  228.         } catch (java.lang.Exception e) {
  229.             e.printStackTrace();
  230.         }

  231.         return null;
  232.     }

  233.     /**
  234.      * Construct an Instance of the Flat Forward Rate Forward Curve
  235.      *
  236.      * @param dtStart The Forward Curve Start Date
  237.      * @param fri The Floating Rate Index
  238.      * @param dblFlatForwardRate The Flat Forward Rate
  239.      *
  240.      * @return Instance of the Flat Forward Rate Forward Curve
  241.      */

  242.     public static final org.drip.state.forward.ForwardCurve FlatForwardForwardCurve (
  243.         final org.drip.analytics.date.JulianDate dtStart,
  244.         final org.drip.state.identifier.ForwardLabel fri,
  245.         final double dblFlatForwardRate)
  246.     {
  247.         try {
  248.             return new org.drip.state.nonlinear.FlatForwardForwardCurve (dtStart, fri, dblFlatForwardRate);
  249.         } catch (java.lang.Exception e) {
  250.             e.printStackTrace();
  251.         }

  252.         return null;
  253.     }
  254. }