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

  79. /**
  80.  * <i>ScenarioBasisCurveBuilder</i> implements the construction of the scenario basis curve using the input
  81.  * instruments and their quotes.
  82.  *
  83.  *  <br><br>
  84.  *  <ul>
  85.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  86.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  87.  *      <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>
  88.  *      <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>
  89.  *  </ul>
  90.  * <br><br>
  91.  *
  92.  * @author Lakshmi Krishnamurthy
  93.  */

  94. public class ScenarioBasisCurveBuilder {

  95.     /**
  96.      * Create an Instance of the Custom Splined Basis Curve
  97.      *
  98.      * @param strName Curve Name
  99.      * @param dtSpot The Spot Date
  100.      * @param friReference Reference Leg FRI
  101.      * @param friDerived Derived Leg FRI
  102.      * @param bBasisOnReference TRUE - The Basis Quote is on the Reference Leg
  103.      * @param astrTenor Array of the Tenors
  104.      * @param adblBasis Array of the Basis Spreads
  105.      * @param scbc The Segment Custom Builder Control
  106.      *
  107.      * @return The Instance of the Basis Curve
  108.      */

  109.     public static final org.drip.state.basis.BasisCurve CustomSplineBasisCurve (
  110.         final java.lang.String strName,
  111.         final org.drip.analytics.date.JulianDate dtSpot,
  112.         final org.drip.state.identifier.ForwardLabel friReference,
  113.         final org.drip.state.identifier.ForwardLabel friDerived,
  114.         final boolean bBasisOnReference,
  115.         final java.lang.String[] astrTenor,
  116.         final double[] adblBasis,
  117.         final org.drip.spline.params.SegmentCustomBuilderControl scbc)
  118.     {
  119.         if (null == strName || null == dtSpot || strName.isEmpty() || null == astrTenor || null == adblBasis)
  120.             return null;

  121.         int iNumTenor = astrTenor.length;
  122.         int[] aiBasisPredictorOrdinate = new int[iNumTenor + 1];
  123.         double[] adblBasisResponseValue = new double[iNumTenor + 1];
  124.         org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC = new
  125.             org.drip.spline.params.SegmentCustomBuilderControl[iNumTenor];

  126.         if (0 == iNumTenor || iNumTenor != adblBasis.length) return null;

  127.         for (int i = 0; i <= iNumTenor; ++i) {
  128.             if (0 != i) {
  129.                 java.lang.String strTenor = astrTenor[i - 1];

  130.                 if (null == strTenor || strTenor.isEmpty()) return null;

  131.                 org.drip.analytics.date.JulianDate dtMaturity = dtSpot.addTenor (strTenor);

  132.                 if (null == dtMaturity) return null;

  133.                 aiBasisPredictorOrdinate[i] = dtMaturity.julian();
  134.             } else
  135.                 aiBasisPredictorOrdinate[i] = dtSpot.julian();

  136.             adblBasisResponseValue[i] = 0 == i ? adblBasis[0] : adblBasis[i - 1];

  137.             if (0 != i) aSCBC[i - 1] = scbc;
  138.         }

  139.         try {
  140.             return new org.drip.state.curve.BasisSplineBasisCurve (friReference, friDerived,
  141.                 bBasisOnReference, new org.drip.spline.grid.OverlappingStretchSpan
  142.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.CreateCalibratedStretchEstimator
  143.                         (strName, aiBasisPredictorOrdinate, adblBasisResponseValue, aSCBC, null,
  144.                             org.drip.spline.stretch.BoundarySettings.NaturalStandard(),
  145.                                 org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE)));
  146.         } catch (java.lang.Exception e) {
  147.             e.printStackTrace();
  148.         }

  149.         return null;
  150.     }

  151.     /**
  152.      * Create an Instance of the Cubic Polynomial Splined Basis Curve
  153.      *
  154.      * @param strName Curve Name
  155.      * @param dtStart The Tenor Start Date
  156.      * @param friReference Reference Leg FRI
  157.      * @param friDerived Derived Leg FRI
  158.      * @param bBasisOnReference TRUE - The Basis Quote is on the Reference Leg
  159.      * @param astrTenor Array of the Tenors
  160.      * @param adblBasis Array of the Basis Spreads
  161.      *
  162.      * @return The Instance of the Basis Curve
  163.      */

  164.     public static final org.drip.state.basis.BasisCurve CubicPolynomialBasisCurve (
  165.         final java.lang.String strName,
  166.         final org.drip.analytics.date.JulianDate dtStart,
  167.         final org.drip.state.identifier.ForwardLabel friReference,
  168.         final org.drip.state.identifier.ForwardLabel friDerived,
  169.         final boolean bBasisOnReference,
  170.         final java.lang.String[] astrTenor,
  171.         final double[] adblBasis)
  172.     {
  173.         try {
  174.             return CustomSplineBasisCurve (strName, dtStart, friReference, friDerived, bBasisOnReference,
  175.                 astrTenor, adblBasis, new org.drip.spline.params.SegmentCustomBuilderControl
  176.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL, new
  177.                         org.drip.spline.basis.PolynomialFunctionSetParams (4),
  178.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  179.         } catch (java.lang.Exception e) {
  180.             e.printStackTrace();
  181.         }

  182.         return null;
  183.     }

  184.     /**
  185.      * Create an Instance of the Quartic Polynomial Splined Basis Curve
  186.      *
  187.      * @param strName Curve Name
  188.      * @param dtStart The Tenor Start Date
  189.      * @param friReference Reference Leg FRI
  190.      * @param friDerived Derived Leg FRI
  191.      * @param bBasisOnReference TRUE - The Basis Quote is on the Reference Leg
  192.      * @param astrTenor Array of the Tenors
  193.      * @param adblBasis Array of the Basis Spreads
  194.      *
  195.      * @return The Instance of the Basis Curve
  196.      */

  197.     public static final org.drip.state.basis.BasisCurve QuarticPolynomialBasisCurve (
  198.         final java.lang.String strName,
  199.         final org.drip.analytics.date.JulianDate dtStart,
  200.         final org.drip.state.identifier.ForwardLabel friReference,
  201.         final org.drip.state.identifier.ForwardLabel friDerived,
  202.         final boolean bBasisOnReference,
  203.         final java.lang.String[] astrTenor,
  204.         final double[] adblBasis)
  205.     {
  206.         try {
  207.             return CustomSplineBasisCurve (strName, dtStart, friReference, friDerived, bBasisOnReference,
  208.                 astrTenor, adblBasis, new org.drip.spline.params.SegmentCustomBuilderControl
  209.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL, new
  210.                         org.drip.spline.basis.PolynomialFunctionSetParams (5),
  211.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  212.         } catch (java.lang.Exception e) {
  213.             e.printStackTrace();
  214.         }

  215.         return null;
  216.     }

  217.     /**
  218.      * Create an Instance of the Kaklis-Pandelis Splined Basis Curve
  219.      *
  220.      * @param strName Curve Name
  221.      * @param dtStart The Tenor Start Date
  222.      * @param friReference Reference Leg FRI
  223.      * @param friDerived Derived Leg FRI
  224.      * @param bBasisOnReference TRUE - The Basis Quote is on the Reference Leg
  225.      * @param astrTenor Array of the Tenors
  226.      * @param adblBasis Array of the Basis Spreads
  227.      *
  228.      * @return The Instance of the Basis Curve
  229.      */

  230.     public static final org.drip.state.basis.BasisCurve KaklisPandelisBasisCurve (
  231.         final java.lang.String strName,
  232.         final org.drip.analytics.date.JulianDate dtStart,
  233.         final org.drip.state.identifier.ForwardLabel friReference,
  234.         final org.drip.state.identifier.ForwardLabel friDerived,
  235.         final boolean bBasisOnReference,
  236.         final java.lang.String[] astrTenor,
  237.         final double[] adblBasis)
  238.     {
  239.         try {
  240.             return CustomSplineBasisCurve (strName, dtStart, friReference, friDerived, bBasisOnReference,
  241.                 astrTenor, adblBasis, new org.drip.spline.params.SegmentCustomBuilderControl
  242.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KAKLIS_PANDELIS,
  243.                         new org.drip.spline.basis.KaklisPandelisSetParams (2),
  244.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  245.         } catch (java.lang.Exception e) {
  246.             e.printStackTrace();
  247.         }

  248.         return null;
  249.     }

  250.     /**
  251.      * Create an Instance of the KLK Hyperbolic Splined Basis Curve
  252.      *
  253.      * @param strName Curve Name
  254.      * @param dtStart The Tenor Start Date
  255.      * @param friReference Reference Leg FRI
  256.      * @param friDerived Derived Leg FRI
  257.      * @param bBasisOnReference TRUE - The Basis Quote is on the Reference Leg
  258.      * @param astrTenor Array of the Tenors
  259.      * @param adblBasis Array of the Basis Spreads
  260.      * @param dblTension The Tension Parameter
  261.      *
  262.      * @return The Instance of the Basis Curve
  263.      */

  264.     public static final org.drip.state.basis.BasisCurve KLKHyperbolicBasisCurve (
  265.         final java.lang.String strName,
  266.         final org.drip.analytics.date.JulianDate dtStart,
  267.         final org.drip.state.identifier.ForwardLabel friReference,
  268.         final org.drip.state.identifier.ForwardLabel friDerived,
  269.         final boolean bBasisOnReference,
  270.         final java.lang.String[] astrTenor,
  271.         final double[] adblBasis,
  272.         final double dblTension)
  273.     {
  274.         try {
  275.             return CustomSplineBasisCurve (strName, dtStart, friReference, friDerived, bBasisOnReference,
  276.                 astrTenor, adblBasis, new org.drip.spline.params.SegmentCustomBuilderControl
  277.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_HYPERBOLIC_TENSION,
  278.                         new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  279.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  280.         } catch (java.lang.Exception e) {
  281.             e.printStackTrace();
  282.         }

  283.         return null;
  284.     }

  285.     /**
  286.      * Create an Instance of the KLK Rational Linear Splined Basis Curve
  287.      *
  288.      * @param strName Curve Name
  289.      * @param dtStart The Tenor Start Date
  290.      * @param friReference Reference Leg FRI
  291.      * @param friDerived Derived Leg FRI
  292.      * @param bBasisOnReference TRUE - The Basis Quote is on the Reference Leg
  293.      * @param astrTenor Array of the Tenors
  294.      * @param adblBasis Array of the Basis Spreads
  295.      * @param dblTension The Tension Parameter
  296.      *
  297.      * @return The Instance of the Basis Curve
  298.      */

  299.     public static final org.drip.state.basis.BasisCurve KLKRationalLinearBasisCurve (
  300.         final java.lang.String strName,
  301.         final org.drip.analytics.date.JulianDate dtStart,
  302.         final org.drip.state.identifier.ForwardLabel friReference,
  303.         final org.drip.state.identifier.ForwardLabel friDerived,
  304.         final boolean bBasisOnReference,
  305.         final java.lang.String[] astrTenor,
  306.         final double[] adblBasis,
  307.         final double dblTension)
  308.     {
  309.         try {
  310.             return CustomSplineBasisCurve (strName, dtStart, friReference, friDerived, bBasisOnReference,
  311.                 astrTenor, adblBasis, new org.drip.spline.params.SegmentCustomBuilderControl
  312.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_RATIONAL_LINEAR_TENSION,
  313.                 new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  314.                     org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  315.         } catch (java.lang.Exception e) {
  316.             e.printStackTrace();
  317.         }

  318.         return null;
  319.     }

  320.     /**
  321.      * Create an Instance of the KLK Rational Quadratic Splined Basis Curve
  322.      *
  323.      * @param strName Curve Name
  324.      * @param dtStart The Tenor Start Date
  325.      * @param friReference Reference Leg FRI
  326.      * @param friDerived Derived Leg FRI
  327.      * @param bBasisOnReference TRUE - The Basis Quote is on the Reference Leg
  328.      * @param astrTenor Array of the Tenors
  329.      * @param adblBasis Array of the Basis Spreads
  330.      * @param dblTension The Tension Parameter
  331.      *
  332.      * @return The Instance of the Basis Curve
  333.      */

  334.     public static final org.drip.state.basis.BasisCurve KLKRationalQuadraticBasisCurve (
  335.         final java.lang.String strName,
  336.         final org.drip.analytics.date.JulianDate dtStart,
  337.         final org.drip.state.identifier.ForwardLabel friReference,
  338.         final org.drip.state.identifier.ForwardLabel friDerived,
  339.         final boolean bBasisOnReference,
  340.         final java.lang.String[] astrTenor,
  341.         final double[] adblBasis,
  342.         final double dblTension)
  343.     {
  344.         try {
  345.             return CustomSplineBasisCurve (strName, dtStart, friReference, friDerived, bBasisOnReference,
  346.                 astrTenor, adblBasis, new org.drip.spline.params.SegmentCustomBuilderControl
  347.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_RATIONAL_QUADRATIC_TENSION,
  348.                 new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  349.                     org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  350.         } catch (java.lang.Exception e) {
  351.             e.printStackTrace();
  352.         }

  353.         return null;
  354.     }
  355. }