ScenarioRepoCurveBuilder.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.  *
  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>ScenarioRepoCurveBuilder</i> implements the Construction of the Scenario Repo Curve using the Input
  80.  * Instruments and their Quotes.
  81.  *
  82.  *  <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  86.  *      <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>
  87.  *      <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>
  88.  *  </ul>
  89.  * <br><br>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class ScenarioRepoCurveBuilder {

  94.     /**
  95.      * Create an Instance of the Custom Splined Repo Curve
  96.      *
  97.      * @param strName Curve Name
  98.      * @param dtSpot The Spot Date
  99.      * @param comp The Underlying Repo Component
  100.      * @param aiDate Array of the Dates
  101.      * @param adblRepo Array of the Repo Rates
  102.      * @param scbc The Segment Custom Builder Control
  103.      *
  104.      * @return The Instance of the Custom Splined Repo Curve
  105.      */

  106.     public static final org.drip.state.repo.RepoCurve CustomSplineRepoCurve (
  107.         final java.lang.String strName,
  108.         final org.drip.analytics.date.JulianDate dtSpot,
  109.         final org.drip.product.definition.Component comp,
  110.         final int[] aiDate,
  111.         final double[] adblRepo,
  112.         final org.drip.spline.params.SegmentCustomBuilderControl scbc)
  113.     {
  114.         if (null == strName || null == dtSpot || strName.isEmpty() || null == aiDate || null == adblRepo)
  115.             return null;

  116.         int iNumInstrument = aiDate.length;
  117.         int[] aiBasisPredictorOrdinate = new int[iNumInstrument + 1];
  118.         double[] adblBasisResponseValue = new double[iNumInstrument + 1];
  119.         org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC = new
  120.             org.drip.spline.params.SegmentCustomBuilderControl[iNumInstrument];

  121.         if (0 == iNumInstrument || iNumInstrument != adblRepo.length) return null;

  122.         for (int i = 0; i <= iNumInstrument; ++i) {
  123.             aiBasisPredictorOrdinate[i] = 0 == i ? dtSpot.julian() : aiDate[i - 1];

  124.             adblBasisResponseValue[i] = 0 == i ? adblRepo[0] : adblRepo[i - 1];

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

  127.         try {
  128.             return new org.drip.state.curve.BasisSplineRepoCurve (comp, new
  129.                 org.drip.spline.grid.OverlappingStretchSpan
  130.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.CreateCalibratedStretchEstimator
  131.                         (strName, aiBasisPredictorOrdinate, adblBasisResponseValue, aSCBC, null,
  132.                             org.drip.spline.stretch.BoundarySettings.NaturalStandard(),
  133.                                 org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE)));
  134.         } catch (java.lang.Exception e) {
  135.             e.printStackTrace();
  136.         }

  137.         return null;
  138.     }

  139.     /**
  140.      * Create an Instance of the Cubic Polynomial Splined Repo Curve
  141.      *
  142.      * @param strName Curve Name
  143.      * @param dtSpot The Spot Date
  144.      * @param comp The Underlying Repo Component
  145.      * @param aiDate Array of the Dates
  146.      * @param adblRepo Array of the Repo Rates
  147.      *
  148.      * @return The Instance of the Basis Curve
  149.      */

  150.     public static final org.drip.state.repo.RepoCurve CubicPolynomialRepoCurve (
  151.         final java.lang.String strName,
  152.         final org.drip.analytics.date.JulianDate dtSpot,
  153.         final org.drip.product.definition.Component comp,
  154.         final int[] aiDate,
  155.         final double[] adblRepo)
  156.     {
  157.         try {
  158.             return CustomSplineRepoCurve (strName, dtSpot, comp, aiDate, adblRepo, new
  159.                 org.drip.spline.params.SegmentCustomBuilderControl
  160.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL, new
  161.                         org.drip.spline.basis.PolynomialFunctionSetParams (4),
  162.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  163.         } catch (java.lang.Exception e) {
  164.             e.printStackTrace();
  165.         }

  166.         return null;
  167.     }

  168.     /**
  169.      * Create an Instance of the Quartic Polynomial Splined Repo Curve
  170.      *
  171.      * @param strName Curve Name
  172.      * @param dtSpot The Spot Date
  173.      * @param comp The Underlying Repo Component
  174.      * @param aiDate Array of the Dates
  175.      * @param adblRepo Array of the Repo Rates
  176.      *
  177.      * @return The Instance of the Splined Repo Curve
  178.      */

  179.     public static final org.drip.state.repo.RepoCurve QuarticPolynomialRepoCurve (
  180.         final java.lang.String strName,
  181.         final org.drip.analytics.date.JulianDate dtSpot,
  182.         final org.drip.product.definition.Component comp,
  183.         final int[] aiDate,
  184.         final double[] adblRepo)
  185.     {
  186.         try {
  187.             return CustomSplineRepoCurve (strName, dtSpot, comp, aiDate, adblRepo, new
  188.                 org.drip.spline.params.SegmentCustomBuilderControl
  189.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL, new
  190.                         org.drip.spline.basis.PolynomialFunctionSetParams (5),
  191.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  192.         } catch (java.lang.Exception e) {
  193.             e.printStackTrace();
  194.         }

  195.         return null;
  196.     }

  197.     /**
  198.      * Create an Instance of the Kaklis-Pandelis Splined Repo Curve
  199.      *
  200.      * @param strName Curve Name
  201.      * @param dtSpot The Spot Date
  202.      * @param comp The Underlying Repo Component
  203.      * @param aiDate Array of the Dates
  204.      * @param adblRepo Array of the Repo Rates
  205.      *
  206.      * @return The Instance of the Splined Repo Curve
  207.      */

  208.     public static final org.drip.state.repo.RepoCurve KaklisPandelisRepoCurve (
  209.         final java.lang.String strName,
  210.         final org.drip.analytics.date.JulianDate dtSpot,
  211.         final org.drip.product.definition.Component comp,
  212.         final int[] aiDate,
  213.         final double[] adblRepo)
  214.     {
  215.         try {
  216.             return CustomSplineRepoCurve (strName, dtSpot, comp, aiDate, adblRepo, new
  217.                 org.drip.spline.params.SegmentCustomBuilderControl
  218.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KAKLIS_PANDELIS, new
  219.                         org.drip.spline.basis.KaklisPandelisSetParams (2),
  220.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  221.         } catch (java.lang.Exception e) {
  222.             e.printStackTrace();
  223.         }

  224.         return null;
  225.     }

  226.     /**
  227.      * Create an Instance of the KLK Hyperbolic Splined Repo Curve
  228.      *
  229.      * @param strName Curve Name
  230.      * @param dtSpot The Spot Date
  231.      * @param comp The Underlying Repo Component
  232.      * @param aiDate Array of the Dates
  233.      * @param adblRepo Array of the Repo Rates
  234.      * @param dblTension The Tension Parameter
  235.      *
  236.      * @return The Instance of the Splined Repo Curve
  237.      */

  238.     public static final org.drip.state.repo.RepoCurve KLKHyperbolicRepoCurve (
  239.         final java.lang.String strName,
  240.         final org.drip.analytics.date.JulianDate dtSpot,
  241.         final org.drip.product.definition.Component comp,
  242.         final int[] aiDate,
  243.         final double[] adblRepo,
  244.         final double dblTension)
  245.     {
  246.         try {
  247.             return CustomSplineRepoCurve (strName, dtSpot, comp, aiDate, adblRepo, new
  248.                 org.drip.spline.params.SegmentCustomBuilderControl
  249.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_HYPERBOLIC_TENSION,
  250.                         new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  251.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  252.         } catch (java.lang.Exception e) {
  253.             e.printStackTrace();
  254.         }

  255.         return null;
  256.     }

  257.     /**
  258.      * Create an Instance of the KLK Rational Linear Splined Repo Curve
  259.      *
  260.      * @param strName Curve Name
  261.      * @param dtSpot The Spot Date
  262.      * @param comp The Underlying Repo Component
  263.      * @param aiDate Array of the Dates
  264.      * @param adblRepo Array of the Repo Rates
  265.      * @param dblTension The Tension Parameter
  266.      *
  267.      * @return The Instance of the Repo Curve
  268.      */

  269.     public static final org.drip.state.repo.RepoCurve KLKRationalLinearRepoCurve (
  270.         final java.lang.String strName,
  271.         final org.drip.analytics.date.JulianDate dtSpot,
  272.         final org.drip.product.definition.Component comp,
  273.         final int[] aiDate,
  274.         final double[] adblRepo,
  275.         final double dblTension)
  276.     {
  277.         try {
  278.             return CustomSplineRepoCurve (strName, dtSpot, comp, aiDate, adblRepo, new
  279.                 org.drip.spline.params.SegmentCustomBuilderControl
  280.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_RATIONAL_LINEAR_TENSION,
  281.                 new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  282.                     org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  283.         } catch (java.lang.Exception e) {
  284.             e.printStackTrace();
  285.         }

  286.         return null;
  287.     }

  288.     /**
  289.      * Create an Instance of the KLK Rational Quadratic Splined Repo Curve
  290.      *
  291.      * @param strName Curve Name
  292.      * @param dtSpot The Spot Date
  293.      * @param comp The Underlying Repo Component
  294.      * @param aiDate Array of the Dates
  295.      * @param adblRepo Array of the Repo Rates
  296.      * @param dblTension The Tension Parameter
  297.      *
  298.      * @return The Instance of the Repo Curve
  299.      */

  300.     public static final org.drip.state.repo.RepoCurve KLKRationalQuadraticRepoCurve (
  301.         final java.lang.String strName,
  302.         final org.drip.analytics.date.JulianDate dtSpot,
  303.         final org.drip.product.definition.Component comp,
  304.         final int[] aiDate,
  305.         final double[] adblRepo,
  306.         final double dblTension)
  307.     {
  308.         try {
  309.             return CustomSplineRepoCurve (strName, dtSpot, comp, aiDate, adblRepo, new
  310.                 org.drip.spline.params.SegmentCustomBuilderControl
  311.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_RATIONAL_QUADRATIC_TENSION,
  312.                 new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  313.                     org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  314.         } catch (java.lang.Exception e) {
  315.             e.printStackTrace();
  316.         }

  317.         return null;
  318.     }

  319.     /**
  320.      * Construct a Repo Curve using the Flat Repo Rate
  321.      *
  322.      * @param dtSpot Spot Date
  323.      * @param comp Repo Component
  324.      * @param dblRepoRate The Flat Repo Rate
  325.      *
  326.      * @return The Flat Repo Rate Curve
  327.      */

  328.     public static final org.drip.state.repo.RepoCurve FlatRateRepoCurve (
  329.         final org.drip.analytics.date.JulianDate dtSpot,
  330.         final org.drip.product.definition.Component comp,
  331.         final double dblRepoRate)
  332.     {
  333.         if (null == dtSpot) return null;

  334.         int iEpochDate = dtSpot.julian();

  335.         try {
  336.             return new org.drip.state.nonlinear.FlatForwardRepoCurve (iEpochDate, comp, new int[]
  337.                 {iEpochDate}, new double[] {dblRepoRate});
  338.         } catch (java.lang.Exception e) {
  339.             e.printStackTrace();
  340.         }

  341.         return null;
  342.     }
  343. }