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

  95.     /**
  96.      * Construct the Deterministic Volatility 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 aiDate Array of Dates
  102.      * @param adblImpliedVolatility Array of Implied Volatility Nodes
  103.      * @param scbc Segment Custom Builder Parameters
  104.      *
  105.      * @return Instance of the Term Structure
  106.      */

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

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

  121.         if (0 == iNumDate || iNumDate != adblImpliedVolatility.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.BasisSplineDeterministicVolatility (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, aiDate, adblImpliedVolatility, 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 the Deterministic Volatility 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 adblImpliedVolatility Array of Implied Volatility Nodes
  145.      *
  146.      * @return The Deterministic Volatility Term Structure Instance based off of a Cubic Polynomial Spline
  147.      */

  148.     public static final org.drip.state.volatility.VolatilityCurve 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[] adblImpliedVolatility)
  154.     {
  155.         if (null == dtStart || null == astrTenor) return null;

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

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

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

  161.         try {
  162.             return CustomSplineTermStructure (strName, dtStart, strCurrency, aiDate, adblImpliedVolatility,
  163.                 new 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 the Deterministic Volatility Term Structure Instance based off of a Quartic Polynomial
  174.      * `Spline
  175.      *
  176.      * @param strName Name of the the Term Structure Instance
  177.      * @param dtStart The Start Date
  178.      * @param strCurrency Currency
  179.      * @param astrTenor Array of Tenors
  180.      * @param adblImpliedVolatility Array of Implied Volatility Nodes
  181.      *
  182.      * @return The Deterministic Volatility Term Structure Instance based off of a Quartic Polynomial Spline
  183.      */

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

  192.         int iNumTenor = astrTenor.length;
  193.         int[] aiDate = new int[iNumTenor];

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

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

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

  206.         return null;
  207.     }

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

  221.     public static final org.drip.state.volatility.VolatilityCurve KaklisPandelisTermStructure (
  222.         final java.lang.String strName,
  223.         final org.drip.analytics.date.JulianDate dtStart,
  224.         final java.lang.String strCurrency,
  225.         final java.lang.String[] astrTenor,
  226.         final double[] adblImpliedVolatility)
  227.     {
  228.         if (null == dtStart || null == astrTenor) return null;

  229.         int iNumTenor = astrTenor.length;
  230.         int[] aiDate = new int[iNumTenor];

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

  232.         for (int i = 0; i < iNumTenor; ++i)
  233.             aiDate[i] = dtStart.addTenor (astrTenor[i]).julian();

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

  243.         return null;
  244.     }

  245.     /**
  246.      * Construct the Deterministic Volatility Term Structure Instance based off of a KLK Hyperbolic Tension
  247.      *  Spline
  248.      *
  249.      * @param strName Name of the the Term Structure Instance
  250.      * @param dtStart The Start Date
  251.      * @param strCurrency Currency
  252.      * @param astrTenor Array of Tenors
  253.      * @param adblImpliedVolatility Array of Implied Volatility Nodes
  254.      * @param dblTension Tension
  255.      *
  256.      * @return The Deterministic Volatility Term Structure Instance based off of a KLK Hyperbolic Tension
  257.      *  Spline
  258.      */

  259.     public static final org.drip.state.volatility.VolatilityCurve KLKHyperbolicTermStructure (
  260.         final java.lang.String strName,
  261.         final org.drip.analytics.date.JulianDate dtStart,
  262.         final java.lang.String strCurrency,
  263.         final java.lang.String[] astrTenor,
  264.         final double[] adblImpliedVolatility,
  265.         final double dblTension)
  266.     {
  267.         if (null == dtStart || null == astrTenor) return null;

  268.         int iNumTenor = astrTenor.length;
  269.         int[] aiDate = new int[iNumTenor];

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

  271.         for (int i = 0; i < iNumTenor; ++i)
  272.             aiDate[i] = dtStart.addTenor (astrTenor[i]).julian();

  273.         try {
  274.             return CustomSplineTermStructure (strName, dtStart, strCurrency, aiDate, adblImpliedVolatility,
  275.                 new org.drip.spline.params.SegmentCustomBuilderControl
  276.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_HYPERBOLIC_TENSION,
  277.                         new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  278.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  279.         } catch (java.lang.Exception e) {
  280.             e.printStackTrace();
  281.         }

  282.         return null;
  283.     }

  284.     /**
  285.      * Construct the Deterministic Volatility Term Structure Instance based off of a KLK Rational Linear
  286.      *  Tension Spline
  287.      *
  288.      * @param strName Name of the the Term Structure Instance
  289.      * @param dtStart The Start Date
  290.      * @param strCurrency Currency
  291.      * @param astrTenor Array of Tenors
  292.      * @param adblImpliedVolatility Array of Implied Volatility Nodes
  293.      * @param dblTension Tension
  294.      *
  295.      * @return The Deterministic Volatility Term Structure Instance based off of a KLK Rational Linear
  296.      *  Tension Spline
  297.      */

  298.     public static final org.drip.state.volatility.VolatilityCurve KLKRationalLinearTermStructure (
  299.         final java.lang.String strName,
  300.         final org.drip.analytics.date.JulianDate dtStart,
  301.         final java.lang.String strCurrency,
  302.         final java.lang.String[] astrTenor,
  303.         final double[] adblImpliedVolatility,
  304.         final double dblTension)
  305.     {
  306.         if (null == dtStart || null == astrTenor) return null;

  307.         int iNumTenor = astrTenor.length;
  308.         int[] aiDate = new int[iNumTenor];

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

  310.         for (int i = 0; i < iNumTenor; ++i)
  311.             aiDate[i] = dtStart.addTenor (astrTenor[i]).julian();

  312.         try {
  313.             return CustomSplineTermStructure (strName, dtStart, strCurrency, aiDate, adblImpliedVolatility,
  314.                 new org.drip.spline.params.SegmentCustomBuilderControl
  315.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_RATIONAL_LINEAR_TENSION,
  316.                         new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  317.                             org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  318.         } catch (java.lang.Exception e) {
  319.             e.printStackTrace();
  320.         }

  321.         return null;
  322.     }

  323.     /**
  324.      * Construct the Deterministic Volatility Term Structure Instance based off of a KLK Rational Quadratic
  325.      *  Tension Spline
  326.      *
  327.      * @param strName Name of the the Term Structure Instance
  328.      * @param dtStart The Start Date
  329.      * @param strCurrency Currency
  330.      * @param astrTenor Array of Tenors
  331.      * @param adblImpliedVolatility Array of Implied Volatility Nodes
  332.      * @param dblTension Tension
  333.      *
  334.      * @return The Deterministic Volatility Term Structure Instance based off of a KLK Rational Quadratic
  335.      *  Tension Spline
  336.      */

  337.     public static final org.drip.state.volatility.VolatilityCurve
  338.         KLKRationalQuadraticTermStructure (
  339.             final java.lang.String strName,
  340.             final org.drip.analytics.date.JulianDate dtStart,
  341.             final java.lang.String strCurrency,
  342.             final java.lang.String[] astrTenor,
  343.             final double[] adblImpliedVolatility,
  344.             final double dblTension)
  345.     {
  346.         if (null == dtStart || null == astrTenor) return null;

  347.         int iNumTenor = astrTenor.length;
  348.         int[] aiDate = new int[iNumTenor];

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

  350.         for (int i = 0; i < iNumTenor; ++i)
  351.             aiDate[i] = dtStart.addTenor (astrTenor[i]).julian();

  352.         try {
  353.             return CustomSplineTermStructure (strName, dtStart, strCurrency, aiDate, adblImpliedVolatility,
  354.                 new org.drip.spline.params.SegmentCustomBuilderControl
  355.                     (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_RATIONAL_QUADRATIC_TENSION,
  356.                 new org.drip.spline.basis.ExponentialTensionSetParams (dblTension),
  357.                     org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), null, null));
  358.         } catch (java.lang.Exception e) {
  359.             e.printStackTrace();
  360.         }

  361.         return null;
  362.     }

  363.     /**
  364.      * Construct the Flat Constant Forward Volatility Forward Curve
  365.      *
  366.      * @param iEpochDate Epoch Date
  367.      * @param label Forward Volatility Label
  368.      * @param strCurrency Currency
  369.      * @param dblFlatVolatility Flat Volatility
  370.      *
  371.      * @return The Volatility Curve Instance
  372.      */

  373.     public static final org.drip.state.volatility.VolatilityCurve FlatForward (
  374.         final int iEpochDate,
  375.         final org.drip.state.identifier.VolatilityLabel label,
  376.         final java.lang.String strCurrency,
  377.         final double dblFlatVolatility)
  378.     {
  379.         try {
  380.             return new org.drip.state.nonlinear.FlatForwardVolatilityCurve (iEpochDate, label, strCurrency,
  381.                 new int[] {iEpochDate}, new double[] {dblFlatVolatility});
  382.         } catch (java.lang.Exception e) {
  383.             e.printStackTrace();
  384.         }

  385.         return null;
  386.     }
  387. }