CIRTemporalPDF.java

  1. package org.drip.sample.kolmogorov;

  2. import org.drip.dynamics.ito.TimeR1Vertex;
  3. import org.drip.dynamics.meanreverting.R1CIRStochasticEvolver;
  4. import org.drip.dynamics.process.R1ProbabilityDensityFunction;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.service.env.EnvManager;

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

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

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

  107. public class CIRTemporalPDF
  108. {

  109.     public static final void main (
  110.         final String[] argumentArray)
  111.         throws Exception
  112.     {
  113.         EnvManager.InitEnv (
  114.             ""
  115.         );

  116.         double r0 = 3.0;
  117.         double[] timeArray =
  118.         {
  119.             0.5,
  120.             1.0,
  121.             1.5,
  122.             2.0,
  123.             2.5,
  124.             3.0,
  125.             3.5,
  126.             4.0,
  127.             4.5,
  128.             5.0,
  129.             5.5,
  130.             6.0,
  131.             6.5,
  132.             7.0,
  133.             7.5,
  134.             8.0,
  135.             8.5,
  136.             9.0,
  137.             9.5
  138.         };
  139.         double[] xArray =
  140.         {
  141.              0.01,
  142.              0.25,
  143.              0.50,
  144.              0.75,
  145.              1.00,
  146.              1.25,
  147.              1.50,
  148.              1.75,
  149.              2.00,
  150.              2.25,
  151.              2.50,
  152.         };
  153.         double[] meanReversionLevelArray =
  154.         {
  155.              2.0,
  156.              3.0,
  157.              4.0,
  158.         };
  159.         double[] meanReversionSpeedArray =
  160.         {
  161.              0.5,
  162.              1.0,
  163.              1.5,
  164.              2.0,
  165.              2.5,
  166.         };
  167.         double[] volatilityArray =
  168.         {
  169.             0.5,
  170.             1.0,
  171.             1.5,
  172.             2.0,
  173.             2.5,
  174.             3.0,
  175.         };

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

  177.         System.out.println ("\t|                                                  CIR DISTRIBUTION REALIZATIONS OVER TIME                                                 ||");

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

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

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

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

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

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

  184.         System.out.println ("\t|                - Row of PDF Values over x                                                                                                ||");

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

  186.         for (double meanReversionSpeed : meanReversionSpeedArray)
  187.         {
  188.             for (double meanReversionLevel : meanReversionLevelArray)
  189.             {
  190.                 for (double volatility : volatilityArray)
  191.                 {
  192.                     R1ProbabilityDensityFunction r1ProbabilityDensityFunction =
  193.                         R1CIRStochasticEvolver.Wiener (
  194.                             meanReversionSpeed,
  195.                             meanReversionLevel,
  196.                             volatility,
  197.                             0.01
  198.                         ).fokkerPlanckGenerator().deltaStartTemporalPDF (
  199.                             r0
  200.                         );

  201.                     for (double t : timeArray)
  202.                     {
  203.                         java.lang.String dump = "\t| [" +
  204.                             FormatUtil.FormatDouble (meanReversionSpeed, 1, 1, 1.) + "," +
  205.                             FormatUtil.FormatDouble (meanReversionLevel, 1, 1, 1.) + "," +
  206.                             FormatUtil.FormatDouble (volatility, 1, 1, 1.) + "]; t =" +
  207.                             FormatUtil.FormatDouble (t, 1, 1, 1.) + " =>";

  208.                         for (double x : xArray)
  209.                         {
  210.                             dump = dump + " " + FormatUtil.FormatDouble (
  211.                                 r1ProbabilityDensityFunction.density (
  212.                                     new TimeR1Vertex (
  213.                                         t,
  214.                                         x
  215.                                     )
  216.                                 ), 1, 4, 1.
  217.                             ) + " |";
  218.                         }

  219.                         System.out.println (dump + "|");
  220.                     }

  221.                     System.out.println ("\t|------------------------------------------------------------------------------------------------------------------------------------------||");
  222.                 }
  223.             }
  224.         }

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

  226.         EnvManager.TerminateEnv();
  227.     }
  228. }