CIRFutureValueDistribution.java

  1. package org.drip.sample.ckls;

  2. import org.drip.dynamics.meanreverting.R1CIRStochasticEvolver;
  3. import org.drip.measure.chisquare.R1NonCentral;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.service.env.EnvManager;

  6. /*
  7.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  8.  */

  9. /*!
  10.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  12.  *
  13.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  14.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  15.  *      credit, commodity, equity, FX, and structured products.
  16.  *  
  17.  *      https://lakshmidrip.github.io/DROP/
  18.  *  
  19.  *  DROP is composed of three modules:
  20.  *  
  21.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  22.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  23.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  24.  *
  25.  *  DROP Analytics Core implements libraries for the following:
  26.  *  - Fixed Income Analytics
  27.  *  - Asset Backed Analytics
  28.  *  - XVA Analytics
  29.  *  - Exposure and Margin Analytics
  30.  *
  31.  *  DROP Portfolio Core implements libraries for the following:
  32.  *  - Asset Allocation Analytics
  33.  *  - Transaction Cost Analytics
  34.  *
  35.  *  DROP Numerical Core implements libraries for the following:
  36.  *  - Statistical Learning Library
  37.  *  - Numerical Optimizer Library
  38.  *  - Machine Learning Library
  39.  *  - Spline Builder Library
  40.  *
  41.  *  Documentation for DROP is Spread Over:
  42.  *
  43.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  44.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  45.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  46.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  47.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  48.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  49.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  50.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  51.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  52.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  53.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  54.  *
  55.  *  Licensed under the Apache License, Version 2.0 (the "License");
  56.  *      you may not use this file except in compliance with the License.
  57.  *  
  58.  *  You may obtain a copy of the License at
  59.  *      http://www.apache.org/licenses/LICENSE-2.0
  60.  *  
  61.  *  Unless required by applicable law or agreed to in writing, software
  62.  *      distributed under the License is distributed on an "AS IS" BASIS,
  63.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  64.  *  
  65.  *  See the License for the specific language governing permissions and
  66.  *      limitations under the License.
  67.  */

  68. /**
  69.  * <i>CIRFutureValueDistribution</i> demonstrates the Computation of the Future Value Distribution from an
  70.  *  Evolving R<sup>1</sup> Cox-Ingersoll-Ross Process. The References are:
  71.  *  
  72.  *  <br><br>
  73.  *  <ul>
  74.  *      <li>
  75.  *          Bogoliubov, N. N., and D. P. Sankevich (1994): N. N. Bogoliubov and Statistical Mechanics
  76.  *              <i>Russian Mathematical Surveys</i> <b>49 (5)</b> 19-49
  77.  *      </li>
  78.  *      <li>
  79.  *          Holubec, V., K. Kroy, and S. Steffenoni (2019): Physically Consistent Numerical Solver for
  80.  *              Time-dependent Fokker-Planck Equations <i>Physical Review E</i> <b>99 (4)</b> 032117
  81.  *      </li>
  82.  *      <li>
  83.  *          Kadanoff, L. P. (2000): <i>Statistical Physics: Statics, Dynamics, and Re-normalization</i>
  84.  *              <b>World Scientific</b>
  85.  *      </li>
  86.  *      <li>
  87.  *          Ottinger, H. C. (1996): <i>Stochastic Processes in Polymeric Fluids</i> <b>Springer-Verlag</b>
  88.  *              Berlin-Heidelberg
  89.  *      </li>
  90.  *      <li>
  91.  *          Wikipedia (2019): Fokker-Planck Equation
  92.  *              https://en.wikipedia.org/wiki/Fokker%E2%80%93Planck_equation
  93.  *      </li>
  94.  *  </ul>
  95.  *
  96.  *  <br><br>
  97.  *  <ul>
  98.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  99.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  100.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  101.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/ckls/README.md">Analysis of CKLS Process Variants</a></li>
  102.  *  </ul>
  103.  *
  104.  * @author Lakshmi Krishnamurthy
  105.  */

  106. public class CIRFutureValueDistribution
  107. {

  108.     private static final void NonCentralChiSquareParameters (
  109.         final double r0,
  110.         final double meanReversionSpeed,
  111.         final double meanReversionLevel,
  112.         final double[] tArray)
  113.         throws Exception
  114.     {
  115.         R1CIRStochasticEvolver r1CIRStochasticEvolver = R1CIRStochasticEvolver.Wiener (
  116.             meanReversionSpeed,
  117.             meanReversionLevel,
  118.             0.05,
  119.             0.01
  120.         );

  121.         java.lang.String dump = "\t| [" +
  122.             FormatUtil.FormatDouble (meanReversionSpeed, 1, 1, 1.) + "," +
  123.             FormatUtil.FormatDouble (meanReversionLevel, 1, 1, 1.) + "] =>";

  124.         for (double t : tArray)
  125.         {
  126.             R1NonCentral r1NonCentral = r1CIRStochasticEvolver.futureValueDistribution (
  127.                 r0,
  128.                 t
  129.             );

  130.             dump = dump + " {" + FormatUtil.FormatDouble (
  131.                 r1NonCentral.parameters().degreesOfFreedom(), 1, 4, 1.
  132.             ) + " |" + FormatUtil.FormatDouble (
  133.                 r1NonCentral.parameters().nonCentralityParameter(), 1, 4, 1.
  134.             ) + "}";
  135.         }

  136.         System.out.println (dump);
  137.     }

  138.     private static final void NonCentralChiSquareStatistics (
  139.         final double r0,
  140.         final double meanReversionSpeed,
  141.         final double meanReversionLevel,
  142.         final double[] tArray)
  143.         throws Exception
  144.     {
  145.         R1CIRStochasticEvolver r1CIRStochasticEvolver = R1CIRStochasticEvolver.Wiener (
  146.             meanReversionSpeed,
  147.             meanReversionLevel,
  148.             0.05,
  149.             0.01
  150.         );

  151.         java.lang.String dump = "\t| [" +
  152.             FormatUtil.FormatDouble (meanReversionSpeed, 1, 1, 1.) + "," +
  153.             FormatUtil.FormatDouble (meanReversionLevel, 1, 1, 1.) + "] =>";

  154.         for (double t : tArray)
  155.         {
  156.             R1NonCentral r1NonCentral = r1CIRStochasticEvolver.futureValueDistribution (
  157.                 r0,
  158.                 t
  159.             );

  160.             dump = dump + " {" + FormatUtil.FormatDouble (
  161.                 r1NonCentral.mean(), 1, 4, 1.
  162.             ) + " |" + FormatUtil.FormatDouble (
  163.                 r1NonCentral.variance(), 1, 4, 1.
  164.             ) + "}";
  165.         }

  166.         System.out.println (dump);
  167.     }

  168.     public static final void main (
  169.         final String[] argumentArray)
  170.         throws Exception
  171.     {
  172.         EnvManager.InitEnv (
  173.             ""
  174.         );

  175.         double r0 = 3.0;
  176.         double[] tArray =
  177.         {
  178.              1.0,
  179.              2.0,
  180.              3.0,
  181.              4.0,
  182.              5.0,
  183.              6.0,
  184.              7.0,
  185.         };
  186.         double[] meanReversionLevelArray =
  187.         {
  188.              2.0,
  189.              3.0,
  190.              4.0,
  191.         };
  192.         double[] meanReversionSpeedArray =
  193.         {
  194.              0.5,
  195.              1.0,
  196.              1.5,
  197.              2.0,
  198.              2.5,
  199.         };

  200.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------------------------||");

  201.         System.out.println ("\t|                                                          CIR FUTURE VALUE DISTRIBUTION                                                            ||");

  202.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------------------------||");

  203.         System.out.println ("\t|        L -> R:                                                                                                                                    ||");

  204.         System.out.println ("\t|                - Mean Reversion Speed                                                                                                             ||");

  205.         System.out.println ("\t|                - Mean Reversion Level                                                                                                             ||");

  206.         System.out.println ("\t|                - Volatility                                                                                                                       ||");

  207.         System.out.println ("\t|                - Chi-Square Degrees of Freedom and Non-Centrality Parameter over t                                                                ||");

  208.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------------------------||");

  209.         for (double meanReversionSpeed : meanReversionSpeedArray)
  210.         {
  211.             for (double meanReversionLevel : meanReversionLevelArray)
  212.             {
  213.                 NonCentralChiSquareParameters (
  214.                     r0,
  215.                     meanReversionSpeed,
  216.                     meanReversionLevel,
  217.                     tArray
  218.                 );
  219.             }
  220.         }

  221.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------------------------||");

  222.         System.out.println();

  223.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------------------------||");

  224.         System.out.println ("\t|                                                          CIR FUTURE VALUE DISTRIBUTION                                                            ||");

  225.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------------------------||");

  226.         System.out.println ("\t|        L -> R:                                                                                                                                    ||");

  227.         System.out.println ("\t|                - Mean Reversion Speed                                                                                                             ||");

  228.         System.out.println ("\t|                - Mean Reversion Level                                                                                                             ||");

  229.         System.out.println ("\t|                - Volatility                                                                                                                       ||");

  230.         System.out.println ("\t|                - Chi-Square Mean and Variance over t                                                                                              ||");

  231.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------------------------||");

  232.         for (double meanReversionSpeed : meanReversionSpeedArray)
  233.         {
  234.             for (double meanReversionLevel : meanReversionLevelArray)
  235.             {
  236.                 NonCentralChiSquareStatistics (
  237.                     r0,
  238.                     meanReversionSpeed,
  239.                     meanReversionLevel,
  240.                     tArray
  241.                 );
  242.             }
  243.         }

  244.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------------------------||");

  245.         System.out.println();

  246.         EnvManager.TerminateEnv();
  247.     }
  248. }