ScenarioTermStructureBuilder.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>ScenarioTermStructureBuilder</i> implements the construction of the basis spline term structure using
  81.  * the input 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 ScenarioTermStructureBuilder {

  95.     /**
  96.      * Construct a Term Structure Instance using the specified Custom Spline
  97.      *
  98.      * @param strName Name of the the Term Structure Instance
  99.      * @param dtStart The Start Date
  100.      * @param strCurrency Currency
  101.      * @param adblDate Array of Dates
  102.      * @param adblNode Array of Term Structure Nodes
  103.      * @param scbc Segment Custom Builder Parameters
  104.      *
  105.      * @return Instance of the Term Structure
  106.      */

  107.     public static final org.drip.analytics.definition.NodeStructure CustomSplineTermStructure (
  108.         final java.lang.String strName,
  109.         final org.drip.analytics.date.JulianDate dtStart,
  110.         final java.lang.String strCurrency,
  111.         final double[] adblDate,
  112.         final double[] adblNode,
  113.         final org.drip.spline.params.SegmentCustomBuilderControl scbc)
  114.     {
  115.         if (null == strName || strName.isEmpty() || null == dtStart || null == adblDate || null == adblNode
  116.             || null == scbc)
  117.             return null;

  118.         int iNumDate = adblDate.length;
  119.         org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC = new
  120.             org.drip.spline.params.SegmentCustomBuilderControl[iNumDate - 1];

  121.         if (0 == iNumDate || iNumDate != adblNode.length) return null;

  122.         for (int i = 0; i < iNumDate - 1; ++i)
  123.             aSCBC[i] = scbc;

  124.         try {
  125.             return new org.drip.state.curve.BasisSplineTermStructure (dtStart.julian(),
  126.                 org.drip.state.identifier.CustomLabel.Standard (strName), strCurrency, new
  127.                     org.drip.spline.grid.OverlappingStretchSpan
  128.                         (org.drip.spline.stretch.MultiSegmentSequenceBuilder.CreateCalibratedStretchEstimator
  129.                             (strName, adblDate, adblNode, aSCBC, null,
  130.                                 org.drip.spline.stretch.BoundarySettings.NaturalStandard(),
  131.                                     org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE)));
  132.         } catch (java.lang.Exception e) {
  133.             e.printStackTrace();
  134.         }

  135.         return null;
  136.     }

  137.     /**
  138.      * Construct a Term Structure Instance based off of a Cubic Polynomial Spline
  139.      *
  140.      * @param strName Name of the the Term Structure Instance
  141.      * @param dtStart The Start Date
  142.      * @param strCurrency Currency
  143.      * @param astrTenor Array of Tenors
  144.      * @param adblNode Array of Term Structure Nodes
  145.      *
  146.      * @return The Term Structure Instance based off of a Cubic Polynomial Spline
  147.      */

  148.     public static final org.drip.analytics.definition.NodeStructure CubicPolynomialTermStructure (
  149.         final java.lang.String strName,
  150.         final org.drip.analytics.date.JulianDate dtStart,
  151.         final java.lang.String strCurrency,
  152.         final java.lang.String[] astrTenor,
  153.         final double[] adblNode)
  154.     {
  155.         if (null == dtStart || null == astrTenor) return null;

  156.         int iNumTenor = astrTenor.length;
  157.         double[] adblDate = new double[iNumTenor];

  158.         if (0 == iNumTenor) return null;

  159.         for (int i = 0; i < iNumTenor; ++i)
  160.             adblDate[i] = dtStart.addTenor (astrTenor[i]).julian();

  161.         try {
  162.             return CustomSplineTermStructure (strName, dtStart, strCurrency, adblDate, adblNode, new
  163.                 org.drip.spline.params.SegmentCustomBuilderControl
  164.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL, new
  165.                         org.drip.spline.basis.PolynomialFunctionSetParams (4),
  166.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  167.         } catch (java.lang.Exception e) {
  168.             e.printStackTrace();
  169.         }

  170.         return null;
  171.     }

  172.     /**
  173.      * Construct a Term Structure Instance based off of a Quartic Polynomial Spline
  174.      *
  175.      * @param strName Name of the the Term Structure Instance
  176.      * @param dtStart The Start Date
  177.      * @param strCurrency Currency
  178.      * @param astrTenor Array of Tenors
  179.      * @param adblNode Array of Term Structure Nodes
  180.      *
  181.      * @return The Term Structure Instance based off of a Quartic Polynomial Spline
  182.      */

  183.     public static final org.drip.analytics.definition.NodeStructure QuarticPolynomialTermStructure (
  184.         final java.lang.String strName,
  185.         final org.drip.analytics.date.JulianDate dtStart,
  186.         final java.lang.String strCurrency,
  187.         final java.lang.String[] astrTenor,
  188.         final double[] adblNode)
  189.     {
  190.         if (null == dtStart || null == astrTenor) return null;

  191.         int iNumTenor = astrTenor.length;
  192.         double[] adblDate = new double[iNumTenor];

  193.         if (0 == iNumTenor) return null;

  194.         for (int i = 0; i < iNumTenor; ++i)
  195.             adblDate[i] = dtStart.addTenor (astrTenor[i]).julian();

  196.         try {
  197.             return CustomSplineTermStructure (strName, dtStart, strCurrency, adblDate, adblNode, new
  198.                 org.drip.spline.params.SegmentCustomBuilderControl
  199.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL, new
  200.                         org.drip.spline.basis.PolynomialFunctionSetParams (5),
  201.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  202.         } catch (java.lang.Exception e) {
  203.             e.printStackTrace();
  204.         }

  205.         return null;
  206.     }

  207.     /**
  208.      * Construct a Term Structure Instance based off of a Kaklis-Pandelis Polynomial Tension Spline
  209.      *
  210.      * @param strName Name of the the Term Structure Instance
  211.      * @param dtStart The Start Date
  212.      * @param strCurrency Currency
  213.      * @param astrTenor Array of Tenors
  214.      * @param adblNode Array of Term Structure Nodes
  215.      *
  216.      * @return The Term Structure Instance based off of a Kaklis-Pandelis Polynomial Tension Spline
  217.      */

  218.     public static final org.drip.analytics.definition.NodeStructure KaklisPandelisTermStructure (
  219.         final java.lang.String strName,
  220.         final org.drip.analytics.date.JulianDate dtStart,
  221.         final java.lang.String strCurrency,
  222.         final java.lang.String[] astrTenor,
  223.         final double[] adblNode)
  224.     {
  225.         if (null == dtStart || null == astrTenor) return null;

  226.         int iNumTenor = astrTenor.length;
  227.         double[] adblDate = new double[iNumTenor];

  228.         if (0 == iNumTenor) return null;

  229.         for (int i = 0; i < iNumTenor; ++i)
  230.             adblDate[i] = dtStart.addTenor (astrTenor[i]).julian();

  231.         try {
  232.             return CustomSplineTermStructure (strName, dtStart, strCurrency, adblDate, adblNode, new
  233.                 org.drip.spline.params.SegmentCustomBuilderControl
  234.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KAKLIS_PANDELIS, new
  235.                         org.drip.spline.basis.KaklisPandelisSetParams (2),
  236.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  237.         } catch (java.lang.Exception e) {
  238.             e.printStackTrace();
  239.         }

  240.         return null;
  241.     }

  242.     /**
  243.      * Construct a Term Structure Instance based off of a KLK Hyperbolic Tension Spline
  244.      *
  245.      * @param strName Name of the the Term Structure Instance
  246.      * @param dtStart The Start Date
  247.      * @param strCurrency Currency
  248.      * @param astrTenor Array of Tenors
  249.      * @param adblNode Array of Term Structure Nodes
  250.      * @param dblTension Tension
  251.      *
  252.      * @return The Term Structure Instance based off of a KLK Hyperbolic Tension Spline
  253.      */

  254.     public static final org.drip.analytics.definition.NodeStructure KLKHyperbolicTermStructure (
  255.         final java.lang.String strName,
  256.         final org.drip.analytics.date.JulianDate dtStart,
  257.         final java.lang.String strCurrency,
  258.         final java.lang.String[] astrTenor,
  259.         final double[] adblNode,
  260.         final double dblTension)
  261.     {
  262.         if (null == dtStart || null == astrTenor) return null;

  263.         int iNumTenor = astrTenor.length;
  264.         double[] adblDate = new double[iNumTenor];

  265.         if (0 == iNumTenor) return null;

  266.         for (int i = 0; i < iNumTenor; ++i)
  267.             adblDate[i] = dtStart.addTenor (astrTenor[i]).julian();

  268.         try {
  269.             return CustomSplineTermStructure (strName, dtStart, strCurrency, adblDate, adblNode, new
  270.                 org.drip.spline.params.SegmentCustomBuilderControl
  271.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_HYPERBOLIC_TENSION,
  272.                         new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  273.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  274.         } catch (java.lang.Exception e) {
  275.             e.printStackTrace();
  276.         }

  277.         return null;
  278.     }

  279.     /**
  280.      * Construct a Term Structure Instance based off of a KLK Rational Linear Tension Spline
  281.      *
  282.      * @param strName Name of the the Term Structure Instance
  283.      * @param dtStart The Start Date
  284.      * @param strCurrency Currency
  285.      * @param astrTenor Array of Tenors
  286.      * @param adblNode Array of Term Structure Nodes
  287.      * @param dblTension Tension
  288.      *
  289.      * @return The Term Structure Instance based off of a KLK Rational Linear Tension Spline
  290.      */

  291.     public static final org.drip.analytics.definition.NodeStructure KLKRationalLinearTermStructure (
  292.         final java.lang.String strName,
  293.         final org.drip.analytics.date.JulianDate dtStart,
  294.         final java.lang.String strCurrency,
  295.         final java.lang.String[] astrTenor,
  296.         final double[] adblNode,
  297.         final double dblTension)
  298.     {
  299.         if (null == dtStart || null == astrTenor) return null;

  300.         int iNumTenor = astrTenor.length;
  301.         double[] adblDate = new double[iNumTenor];

  302.         if (0 == iNumTenor) return null;

  303.         for (int i = 0; i < iNumTenor; ++i)
  304.             adblDate[i] = dtStart.addTenor (astrTenor[i]).julian();

  305.         try {
  306.             return CustomSplineTermStructure (strName, dtStart, strCurrency, adblDate, adblNode, new
  307.                 org.drip.spline.params.SegmentCustomBuilderControl
  308.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_RATIONAL_LINEAR_TENSION,
  309.                         new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  310.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  311.         } catch (java.lang.Exception e) {
  312.             e.printStackTrace();
  313.         }

  314.         return null;
  315.     }

  316.     /**
  317.      * Construct a Term Structure Instance based off of a KLK Rational Quadratic Tension Spline
  318.      *
  319.      * @param strName Name of the the Term Structure Instance
  320.      * @param dtStart The Start Date
  321.      * @param strCurrency Currency
  322.      * @param astrTenor Array of Tenors
  323.      * @param adblNode Array of Term Structure Nodes
  324.      * @param dblTension Tension
  325.      *
  326.      * @return The Term Structure Instance based off of a KLK Rational Quadratic Tension Spline
  327.      */

  328.     public static final org.drip.analytics.definition.NodeStructure KLKRationalQuadraticTermStructure (
  329.         final java.lang.String strName,
  330.         final org.drip.analytics.date.JulianDate dtStart,
  331.         final java.lang.String strCurrency,
  332.         final java.lang.String[] astrTenor,
  333.         final double[] adblNode,
  334.         final double dblTension)
  335.     {
  336.         if (null == dtStart || null == astrTenor) return null;

  337.         int iNumTenor = astrTenor.length;
  338.         double[] adblDate = new double[iNumTenor];

  339.         if (0 == iNumTenor) return null;

  340.         for (int i = 0; i < iNumTenor; ++i)
  341.             adblDate[i] = dtStart.addTenor (astrTenor[i]).julian();

  342.         try {
  343.             return CustomSplineTermStructure (strName, dtStart, strCurrency, adblDate, adblNode, new
  344.                 org.drip.spline.params.SegmentCustomBuilderControl
  345.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_RATIONAL_QUADRATIC_TENSION,
  346.                         new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  347.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  348.         } catch (java.lang.Exception e) {
  349.             e.printStackTrace();
  350.         }

  351.         return null;
  352.     }
  353. }