ContinuousForwardRateEvolver.java

  1. package org.drip.dynamics.lmm;

  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>ContinuousForwardRateEvolver</i> sets up and implements the Multi-Factor No-arbitrage Dynamics of the
  80.  * Rates State Quantifiers traced from the Evolution of the Continuously Compounded Forward Rate as
  81.  * formulated in:
  82.  *
  83.  *  <br><br>
  84.  *  <ul>
  85.  *      <li>
  86.  *          Goldys, B., M. Musiela, and D. Sondermann (1994): <i>Log-normality of Rates and Term Structure
  87.  *              Models</i> <b>The University of New South Wales</b>
  88.  *      </li>
  89.  *      <li>
  90.  *          Musiela, M. (1994): <i>Nominal Annual Rates and Log-normal Volatility Structure</i> <b>The
  91.  *              University of New South Wales</b>
  92.  *      </li>
  93.  *      <li>
  94.  *          Brace, A., D. Gatarek, and M. Musiela (1997): The Market Model of Interest Rate Dynamics
  95.  *              <i>Mathematical Finance</i> <b>7 (2)</b> 127-155
  96.  *      </li>
  97.  *  </ul>
  98.  *
  99.  *  <br><br>
  100.  *  <ul>
  101.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  102.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  103.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/dynamics/README.md">HJM, Hull White, LMM, and SABR Dynamic Evolution Models</a></li>
  104.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/dynamics/lmm/README.md">LMM Based Latent State Evolution</a></li>
  105.  *  </ul>
  106.  *  
  107.  * @author Lakshmi Krishnamurthy
  108.  */

  109. public class ContinuousForwardRateEvolver implements org.drip.dynamics.evolution.PointStateEvolver {
  110.     private org.drip.dynamics.hjm.MultiFactorVolatility _mfv = null;
  111.     private org.drip.state.identifier.ForwardLabel _lslForward = null;
  112.     private org.drip.state.identifier.FundingLabel _lslFunding = null;

  113.     private double volatilityRandomDotProduct (
  114.         final int iViewDate,
  115.         final int iTargetDate,
  116.         final int iViewTimeIncrement)
  117.         throws java.lang.Exception
  118.     {
  119.         double dblViewTimeIncrementSQRT = java.lang.Math.sqrt (iViewTimeIncrement);

  120.         org.drip.sequence.random.PrincipalFactorSequenceGenerator pfsg = _mfv.msg();

  121.         double[] adblMultivariateRandom = pfsg.random();

  122.         double dblVolatilityRandomDotProduct = 0.;

  123.         int iNumFactor = pfsg.numFactor();

  124.         for (int i = 0; i < iNumFactor; ++i)
  125.             dblVolatilityRandomDotProduct += _mfv.weightedFactorPointVolatility (i, iViewDate, iTargetDate) *
  126.                 adblMultivariateRandom[i] * dblViewTimeIncrementSQRT;

  127.         return dblVolatilityRandomDotProduct;
  128.     }

  129.     private double volatilityRandomDotDerivative (
  130.         final int iViewDate,
  131.         final int iTargetDate,
  132.         final int iViewTimeIncrement,
  133.         final boolean bTerminal)
  134.         throws java.lang.Exception
  135.     {
  136.         org.drip.function.definition.R1ToR1 pointVolatilityFunctionR1ToR1 = new
  137.             org.drip.function.definition.R1ToR1 (null) {
  138.             @Override public double evaluate (
  139.                 final double dblX)
  140.                 throws java.lang.Exception
  141.             {
  142.                 return bTerminal ? volatilityRandomDotProduct (iViewDate, (int) dblX, iViewTimeIncrement) :
  143.                     volatilityRandomDotProduct ((int) dblX, iTargetDate, iViewTimeIncrement);
  144.             }
  145.         };

  146.         return pointVolatilityFunctionR1ToR1.derivative (bTerminal ? iTargetDate : iViewDate, 1);
  147.     }

  148.     /**
  149.      * ContinuousForwardRateEvolver Constructor
  150.      *
  151.      * @param lslFunding The Funding Latent State Label
  152.      * @param lslForward The Forward Latent State Label
  153.      * @param mfv The Multi-Factor Volatility Instance
  154.      * @param auInitialInstantaneousForwardRate The Instantaneous Forward Rate Function
  155.      *
  156.      * @throws java.lang.Exception Thrown if Inputs are Invalid
  157.      */

  158.     public ContinuousForwardRateEvolver (
  159.         final org.drip.state.identifier.FundingLabel lslFunding,
  160.         final org.drip.state.identifier.ForwardLabel lslForward,
  161.         final org.drip.dynamics.hjm.MultiFactorVolatility mfv,
  162.         final org.drip.function.definition.R1ToR1 auInitialInstantaneousForwardRate)
  163.         throws java.lang.Exception
  164.     {
  165.         if (null == (_lslFunding = lslFunding) || null == (_lslForward = lslForward) || null == (_mfv = mfv))
  166.             throw new java.lang.Exception ("ContinuousForwardRateEvolver ctr: Invalid Inputs");
  167.     }

  168.     /**
  169.      * Retrieve the Funding Label
  170.      *
  171.      * @return The Funding Label
  172.      */

  173.     public org.drip.state.identifier.FundingLabel fundingLabel()
  174.     {
  175.         return _lslFunding;
  176.     }

  177.     /**
  178.      * Retrieve the Forward Label
  179.      *
  180.      * @return The Forward Label
  181.      */

  182.     public org.drip.state.identifier.ForwardLabel forwardLabel()
  183.     {
  184.         return _lslForward;
  185.     }

  186.     /**
  187.      * Retrieve the Multi-factor Volatility Instance
  188.      *
  189.      * @return The Multi-factor Volatility Instance
  190.      */

  191.     public org.drip.dynamics.hjm.MultiFactorVolatility mfv()
  192.     {
  193.         return _mfv;
  194.     }

  195.     @Override public org.drip.dynamics.lmm.ContinuousForwardRateUpdate evolve (
  196.         final int iSpotDate,
  197.         final int iViewDate,
  198.         final int iSpotTimeIncrement,
  199.         final org.drip.dynamics.evolution.LSQMPointUpdate lsqmPrev)
  200.     {
  201.         if (iSpotDate > iViewDate || (null != lsqmPrev && !(lsqmPrev instanceof
  202.             org.drip.dynamics.lmm.ContinuousForwardRateUpdate)))
  203.             return null;

  204.         org.drip.dynamics.lmm.ContinuousForwardRateUpdate bgmPrev =
  205.             (org.drip.dynamics.lmm.ContinuousForwardRateUpdate) lsqmPrev;

  206.         double dblDContinuousForwardDXTerminalPrev = bgmPrev.dContinuousForwardDXTerminal();

  207.         double dblDContinuousForwardDXInitialPrev = bgmPrev.dContinuousForwardDXInitial();

  208.         try {
  209.             double dblDiscountFactorPrev = bgmPrev.discountFactor();

  210.             double dblSpotRateIncrement = dblDContinuousForwardDXInitialPrev * iSpotTimeIncrement +
  211.                 volatilityRandomDotDerivative (iSpotDate, iViewDate, iSpotTimeIncrement, false);

  212.             double dblContinuousForwardIncrement = (dblDContinuousForwardDXTerminalPrev + 0.5 *
  213.                 _mfv.pointVolatilityModulusDerivative (iSpotDate, iViewDate, 1, true)) * iSpotTimeIncrement +
  214.                     volatilityRandomDotDerivative (iSpotDate, iViewDate, iSpotTimeIncrement, true);

  215.             double dblContinuousForwardRate = bgmPrev.continuousForwardRate() +
  216.                 dblContinuousForwardIncrement;

  217.             double dblSpotRate = bgmPrev.spotRate() + dblSpotRateIncrement;

  218.             double dblDiscountFactorIncrement = dblDiscountFactorPrev * ((dblSpotRate -
  219.                 dblContinuousForwardRate) * iSpotTimeIncrement - volatilityRandomDotProduct (iSpotDate,
  220.                     iViewDate, iSpotTimeIncrement));

  221.             return org.drip.dynamics.lmm.ContinuousForwardRateUpdate.Create (_lslFunding, _lslForward,
  222.                 iSpotDate, iSpotDate + iSpotTimeIncrement, iViewDate, dblContinuousForwardRate,
  223.                     dblContinuousForwardIncrement, dblSpotRate, dblSpotRateIncrement, dblDiscountFactorPrev +
  224.                         dblDiscountFactorIncrement, dblDiscountFactorIncrement,
  225.                             dblDContinuousForwardDXInitialPrev, dblDContinuousForwardDXTerminalPrev);
  226.         } catch (java.lang.Exception e) {
  227.             e.printStackTrace();
  228.         }

  229.         return null;
  230.     }

  231.     /**
  232.      * Compute the Realized Zero Coupon Bond Forward Price
  233.      *
  234.      * @param iSpotDate The Spot Date
  235.      * @param iForwardDate The Forward Date
  236.      * @param iMaturityDate The Maturity Date
  237.      * @param dblSpotPrice The Spot Price
  238.      * @param dblSpotForwardReinvestmentAccrual The Continuously Re-invested Accruing Bank Account
  239.      *
  240.      * @return The Realized Zero Coupon Bond Forward Price
  241.      *
  242.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  243.      */

  244.     public double zeroCouponForwardPrice (
  245.         final int iSpotDate,
  246.         final int iForwardDate,
  247.         final int iMaturityDate,
  248.         final double dblSpotPrice,
  249.         final double dblSpotForwardReinvestmentAccrual)
  250.         throws java.lang.Exception
  251.     {
  252.         if (iSpotDate > iForwardDate || iForwardDate > iMaturityDate ||
  253.             !org.drip.numerical.common.NumberUtil.IsValid (dblSpotPrice) ||
  254.                 !org.drip.numerical.common.NumberUtil.IsValid (dblSpotForwardReinvestmentAccrual))
  255.             throw new java.lang.Exception
  256.                 ("ContinuousForwardRateEvolver::zeroCouponForwardPrice => Invalid Inputs");

  257.         int iPeriodIncrement = iForwardDate - iSpotDate;

  258.         return dblSpotPrice / dblSpotForwardReinvestmentAccrual * java.lang.Math.exp (-1. *
  259.             (volatilityRandomDotProduct (iSpotDate, iForwardDate, iPeriodIncrement) + 0.5 * iPeriodIncrement
  260.                 * _mfv.pointVolatilityModulus (iSpotDate, iForwardDate)));
  261.     }
  262. }