R1CIRStochasticEvolver.java

  1. package org.drip.dynamics.meanreverting;

  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.  *
  9.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  10.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  11.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  12.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  13.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  14.  *      and computational support.
  15.  *  
  16.  *      https://lakshmidrip.github.io/DROP/
  17.  *  
  18.  *  DROP is composed of three modules:
  19.  *  
  20.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  21.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  22.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  23.  *
  24.  *  DROP Product Core implements libraries for the following:
  25.  *  - Fixed Income Analytics
  26.  *  - Loan Analytics
  27.  *  - Transaction Cost Analytics
  28.  *
  29.  *  DROP Portfolio Core implements libraries for the following:
  30.  *  - Asset Allocation Analytics
  31.  *  - Asset Liability Management Analytics
  32.  *  - Capital Estimation Analytics
  33.  *  - Exposure Analytics
  34.  *  - Margin Analytics
  35.  *  - XVA Analytics
  36.  *
  37.  *  DROP Computational Core implements libraries for the following:
  38.  *  - Algorithm Support
  39.  *  - Computation Support
  40.  *  - Function Analysis
  41.  *  - Model Validation
  42.  *  - Numerical Analysis
  43.  *  - Numerical Optimizer
  44.  *  - Spline Builder
  45.  *  - Statistical Learning
  46.  *
  47.  *  Documentation for DROP is Spread Over:
  48.  *
  49.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  50.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  51.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  52.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  53.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  54.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  55.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  56.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  57.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  58.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  59.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  60.  *
  61.  *  Licensed under the Apache License, Version 2.0 (the "License");
  62.  *      you may not use this file except in compliance with the License.
  63.  *  
  64.  *  You may obtain a copy of the License at
  65.  *      http://www.apache.org/licenses/LICENSE-2.0
  66.  *  
  67.  *  Unless required by applicable law or agreed to in writing, software
  68.  *      distributed under the License is distributed on an "AS IS" BASIS,
  69.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  70.  *  
  71.  *  See the License for the specific language governing permissions and
  72.  *      limitations under the License.
  73.  */

  74. /**
  75.  * <i>R1CIRStochasticEvolver</i> implements the R<sup>1</sup> Cos-Ingersoll-Ross Stochastic Evolver. The
  76.  *  References are:
  77.  *  
  78.  *  <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Doob, J. L. (1942): The Brownian Movement and Stochastic Equations <i>Annals of Mathematics</i>
  82.  *              <b>43 (2)</b> 351-369
  83.  *      </li>
  84.  *      <li>
  85.  *          Gardiner, C. W. (2009): <i>Stochastic Methods: A Handbook for the Natural and Social Sciences
  86.  *              4<sup>th</sup> Edition</i> <b>Springer-Verlag</b>
  87.  *      </li>
  88.  *      <li>
  89.  *          Kadanoff, L. P. (2000): <i>Statistical Physics: Statics, Dynamics, and Re-normalization</i>
  90.  *              <b>World Scientific</b>
  91.  *      </li>
  92.  *      <li>
  93.  *          Karatzas, I., and S. E. Shreve (1991): <i>Brownian Motion and Stochastic Calculus 2<sup>nd</sup>
  94.  *              Edition</i> <b>Springer-Verlag</b>
  95.  *      </li>
  96.  *      <li>
  97.  *          Risken, H., and F. Till (1996): <i>The Fokker-Planck Equation – Methods of Solution and
  98.  *              Applications</i> <b>Springer</b>
  99.  *      </li>
  100.  *  </ul>
  101.  *
  102.  *  <br><br>
  103.  *  <ul>
  104.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  105.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  106.  *      <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>
  107.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/dynamics/meanreverting/README.md">Mean Reverting Stochastic Process Dynamics</a></li>
  108.  *  </ul>
  109.  *
  110.  * @author Lakshmi Krishnamurthy
  111.  */

  112. public class R1CIRStochasticEvolver
  113.     extends org.drip.dynamics.meanreverting.R1CKLSStochasticEvolver
  114. {

  115.     /**
  116.      * Construct a Weiner Instance of R1CIRStochasticEvolver Process
  117.      *
  118.      * @param meanReversionSpeed The Mean Reversion Speed
  119.      * @param meanReversionLevel The Mean Reversion Level
  120.      * @param volatility The Volatility
  121.      * @param timeWidth Wiener Time Width
  122.      *
  123.      * @return Weiner Instance of R1CIRStochasticEvolver Process
  124.      */

  125.     public static R1CIRStochasticEvolver Wiener (
  126.         final double meanReversionSpeed,
  127.         final double meanReversionLevel,
  128.         final double volatility,
  129.         final double timeWidth)
  130.     {
  131.         try
  132.         {
  133.             return new R1CIRStochasticEvolver (
  134.                 meanReversionSpeed,
  135.                 meanReversionLevel,
  136.                 volatility,
  137.                 new org.drip.dynamics.ito.R1WienerDriver (
  138.                     timeWidth
  139.                 )
  140.             );
  141.         }
  142.         catch (java.lang.Exception e)
  143.         {
  144.             e.printStackTrace();
  145.         }

  146.         return null;
  147.     }

  148.     /**
  149.      * R1CIRStochasticEvolver Constructor
  150.      *
  151.      * @param meanReversionSpeed The Mean Reversion Speed
  152.      * @param meanReversionLevel The Mean Reversion Level
  153.      * @param volatilityCoefficient The Volatility Coefficient
  154.      * @param r1StochasticDriver The Stochastic Driver
  155.      *
  156.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  157.      */

  158.     public R1CIRStochasticEvolver (
  159.         final double meanReversionSpeed,
  160.         final double meanReversionLevel,
  161.         final double volatilityCoefficient,
  162.         final org.drip.dynamics.ito.R1StochasticDriver r1StochasticDriver)
  163.         throws java.lang.Exception
  164.     {
  165.         super (
  166.             org.drip.dynamics.meanreverting.CKLSParameters.CoxIngersollRoss (
  167.                 meanReversionSpeed,
  168.                 meanReversionLevel,
  169.                 volatilityCoefficient
  170.             ),
  171.             r1StochasticDriver
  172.         );
  173.     }

  174.     /**
  175.      * Indicate it the Evolution includes Zero, or is strictly Positive
  176.      *
  177.      * @return TRUE - Evoltuion is Strictly Positive
  178.      */

  179.     public boolean evolutionStrictlyPositive()
  180.     {
  181.         org.drip.dynamics.meanreverting.CKLSParameters cklsParameters = cklsParameters();

  182.         double volatilityCoefficient = cklsParameters.volatilityCoefficient();

  183.         return 2. * cklsParameters.meanReversionSpeed() * cklsParameters.meanReversionLevel() >=
  184.             volatilityCoefficient * volatilityCoefficient;
  185.     }

  186.     /**
  187.      * Compute the Expected Value of x at a time t from a Starting Value x0
  188.      *
  189.      * @param x0 Starting Variate
  190.      * @param t Time
  191.      *
  192.      * @return Expected Value of x
  193.      *
  194.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  195.      */

  196.     public double mean (
  197.         final double x0,
  198.         final double t)
  199.         throws java.lang.Exception
  200.     {
  201.         if (!org.drip.numerical.common.NumberUtil.IsValid (
  202.                 x0
  203.             ) || !org.drip.numerical.common.NumberUtil.IsValid (
  204.                 t
  205.             ) || 0. > t
  206.         )
  207.         {
  208.             throw new java.lang.Exception (
  209.                 "R1CIRStochasticEvolver::mean => Invalid Inputs"
  210.             );
  211.         }

  212.         double timeDecayFactor = java.lang.Math.exp (
  213.             -1. * cklsParameters().meanReversionSpeed() * t
  214.         );

  215.         return x0 * timeDecayFactor + cklsParameters().meanReversionLevel() * (1. - timeDecayFactor);
  216.     }

  217.     /**
  218.      * Compute the Time Variance of x across at a Time Value t
  219.      *
  220.      * @param x0 Starting Variate
  221.      * @param t Time t
  222.      *
  223.      * @return Time Variance of x
  224.      *
  225.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  226.      */

  227.     public double timeVariance (
  228.         final double x0,
  229.         final double t)
  230.         throws java.lang.Exception
  231.     {
  232.         if (!org.drip.numerical.common.NumberUtil.IsValid (
  233.                 t
  234.             ) || 0. > t
  235.         )
  236.         {
  237.             throw new java.lang.Exception (
  238.                 "R1VasicekStochasticEvolver::timeCovariance => Invalid Inputs"
  239.             );
  240.         }

  241.         double volatilityCoefficient = cklsParameters().volatilityCoefficient();

  242.         double meanReversionSpeed = cklsParameters().meanReversionSpeed();

  243.         double timeDecayFactor = java.lang.Math.exp (
  244.             -1. * meanReversionSpeed * t
  245.         );

  246.         double oneMinusTimeDecayFactor = 1. - timeDecayFactor;

  247.         return volatilityCoefficient * volatilityCoefficient / meanReversionSpeed * oneMinusTimeDecayFactor *
  248.         (
  249.             x0 * timeDecayFactor + 0.5 * cklsParameters().meanReversionLevel() * oneMinusTimeDecayFactor
  250.         );
  251.     }

  252.     @Override public org.drip.measure.statistics.PopulationCentralMeasures
  253.         temporalPopulationCentralMeasures (
  254.             final double x0,
  255.             final double t)
  256.     {
  257.         try
  258.         {
  259.             return new org.drip.measure.statistics.PopulationCentralMeasures (
  260.                 mean (
  261.                     x0,
  262.                     t
  263.                 ),
  264.                 timeVariance (
  265.                     x0,
  266.                     t
  267.                 )
  268.             );
  269.         }
  270.         catch (java.lang.Exception e)
  271.         {
  272.             e.printStackTrace();
  273.         }

  274.         return null;
  275.     }

  276.     @Override public org.drip.measure.statistics.PopulationCentralMeasures
  277.         steadyStatePopulationCentralMeasures (
  278.             final double x0)
  279.     {
  280.         double volatility = cklsParameters().volatilityCoefficient();

  281.         try
  282.         {
  283.             return new org.drip.measure.statistics.PopulationCentralMeasures (
  284.                 cklsParameters().meanReversionLevel(),
  285.                 0.5 * volatility * volatility / cklsParameters().meanReversionSpeed()
  286.             );
  287.         }
  288.         catch (java.lang.Exception e)
  289.         {
  290.             e.printStackTrace();
  291.         }

  292.         return null;
  293.     }

  294.     @Override public org.drip.measure.chisquare.R1NonCentral futureValueDistribution (
  295.         final double r0,
  296.         final double t)
  297.     {
  298.         if (!org.drip.numerical.common.NumberUtil.IsValid (
  299.                 r0
  300.             ) || !org.drip.numerical.common.NumberUtil.IsValid (
  301.                 t
  302.             ) || 0. > t
  303.         )
  304.         {
  305.             return null;
  306.         }

  307.         int digammaTermCount = 1000;
  308.         int besselFirstTermCount = 20;

  309.         org.drip.dynamics.meanreverting.CKLSParameters cklsParameters = cklsParameters();

  310.         double ePowerMinusAT = java.lang.Math.exp (
  311.             -1. * cklsParameters.meanReversionSpeed() * t
  312.         );

  313.         org.drip.function.definition.R1ToR1 gammaEstimator =
  314.             new org.drip.specialfunction.gamma.EulerIntegralSecondKind (
  315.                 null
  316.             );

  317.         try
  318.         {
  319.             return new org.drip.measure.chisquare.R1NonCentral (
  320.                 new org.drip.measure.chisquare.R1NonCentralParameters (
  321.                     cklsParameters.meanReversionLevel() * (1. - ePowerMinusAT),
  322.                     r0 * ePowerMinusAT
  323.                 ),
  324.                 gammaEstimator,
  325.                 org.drip.specialfunction.digamma.CumulativeSeriesEstimator.AbramowitzStegun2007 (
  326.                     digammaTermCount
  327.                 ),
  328.                 new org.drip.function.definition.R2ToR1()
  329.                 {
  330.                     @Override public double evaluate (
  331.                         final double s,
  332.                         final double t)
  333.                         throws Exception
  334.                     {
  335.                         return new org.drip.specialfunction.incompletegamma.LowerEulerIntegral (
  336.                             null,
  337.                             t
  338.                         ).evaluate (
  339.                             s
  340.                         );
  341.                     }
  342.                 },
  343.                 org.drip.specialfunction.bessel.ModifiedFirstFrobeniusSeriesEstimator.Standard (
  344.                     gammaEstimator,
  345.                     besselFirstTermCount
  346.                 )
  347.             );
  348.         }
  349.         catch (java.lang.Exception e)
  350.         {
  351.             e.printStackTrace();
  352.         }

  353.         return null;
  354.     }

  355.     @Override public org.drip.dynamics.kolmogorov.R1FokkerPlanckCIR fokkerPlanckGenerator()
  356.     {
  357.         try
  358.         {
  359.             return new org.drip.dynamics.kolmogorov.R1FokkerPlanckCIR (
  360.                 cklsParameters()
  361.             );
  362.         }
  363.         catch (java.lang.Exception e)
  364.         {
  365.             e.printStackTrace();
  366.         }

  367.         return null;
  368.     }
  369. }