BasisSplineDeterministicVolatility.java

  1. package org.drip.state.curve;

  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>BasisSplineDeterministicVolatility</i> extends the BasisSplineTermStructure for the specific case of
  81.  * the Implementation of the Deterministic Volatility Term Structure.
  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/curve/README.md">Basis Spline Based Latent States</a></li>
  89.  *  </ul>
  90.  * <br><br>
  91.  *
  92.  * @author Lakshmi Krishnamurthy
  93.  */

  94. public class BasisSplineDeterministicVolatility extends org.drip.state.volatility.VolatilityCurve {
  95.     private org.drip.spline.grid.Span _spanImpliedVolatility = null;

  96.     /**
  97.      * BasisSplineDeterministicVolatility Constructor
  98.      *
  99.      * @param iEpochDate The Epoch Date
  100.      * @param label Latent State Label
  101.      * @param strCurrency The Currency
  102.      * @param spanImpliedVolatility The Implied Volatility Span
  103.      *
  104.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  105.      */

  106.     public BasisSplineDeterministicVolatility (
  107.         final int iEpochDate,
  108.         final org.drip.state.identifier.CustomLabel label,
  109.         final java.lang.String strCurrency,
  110.         final org.drip.spline.grid.Span spanImpliedVolatility)
  111.         throws java.lang.Exception
  112.     {
  113.         super (iEpochDate, label, strCurrency);

  114.         if (null == (_spanImpliedVolatility = spanImpliedVolatility))
  115.             throw new java.lang.Exception ("BasisSplineDeterministicVolatility ctr: Invalid Inputs");
  116.     }

  117.     @Override public double impliedVol (
  118.         final int iDate)
  119.         throws java.lang.Exception
  120.     {
  121.         double dblSpanLeft = _spanImpliedVolatility.left();

  122.         if (dblSpanLeft >= iDate) return _spanImpliedVolatility.calcResponseValue (dblSpanLeft);

  123.         double dblSpanRight = _spanImpliedVolatility.right();

  124.         if (dblSpanRight <= iDate) return _spanImpliedVolatility.calcResponseValue (dblSpanRight);

  125.         return _spanImpliedVolatility.calcResponseValue (iDate);
  126.     }

  127.     @Override public double node (
  128.         final int iDate)
  129.         throws java.lang.Exception
  130.     {
  131.         double dblImpliedVol = impliedVol (iDate);

  132.         return java.lang.Math.sqrt (dblImpliedVol * dblImpliedVol + 2. * dblImpliedVol * (iDate -
  133.             epoch().julian()) / 365.25 * _spanImpliedVolatility.calcResponseValueDerivative (iDate, 1));
  134.     }

  135.     @Override public double vol (
  136.         final int iDate)
  137.         throws java.lang.Exception
  138.     {
  139.         return node (iDate);
  140.     }

  141.     @Override public double nodeDerivative (
  142.         final int iDate,
  143.         final int iOrder)
  144.         throws java.lang.Exception
  145.     {
  146.         if (!org.drip.numerical.common.NumberUtil.IsValid (iDate))
  147.             throw new java.lang.Exception
  148.                 ("BasisSplineDeterministicVolatility::nodeDerivative => Invalid Inputs");

  149.         org.drip.function.definition.R1ToR1 au = new org.drip.function.definition.R1ToR1
  150.             (null) {
  151.             @Override public double evaluate (
  152.                 double dblX)
  153.                 throws java.lang.Exception
  154.             {
  155.                 return node ((int) dblX);
  156.             }
  157.         };

  158.         return au.derivative (iDate, iOrder);
  159.     }
  160. }