RdFokkerPlanck.java

  1. package org.drip.dynamics.kolmogorov;

  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>RdFokkerPlanck</i> exposes the R<sup>d</sup> Fokker-Planck Probability Density Function Evolution
  76.  *  Equation. The References are:
  77.  *  
  78.  *  <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Bogoliubov, N. N., and D. P. Sankevich (1994): N. N. Bogoliubov and Statistical Mechanics
  82.  *              <i>Russian Mathematical Surveys</i> <b>49 (5)</b> 19-49
  83.  *      </li>
  84.  *      <li>
  85.  *          Holubec, V., K. Kroy, and S. Steffenoni (2019): Physically Consistent Numerical Solver for
  86.  *              Time-dependent Fokker-Planck Equations <i>Physical Review E</i> <b>99 (4)</b> 032117
  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.  *          Ottinger, H. C. (1996): <i>Stochastic Processes in Polymeric Fluids</i> <b>Springer-Verlag</b>
  94.  *              Berlin-Heidelberg
  95.  *      </li>
  96.  *      <li>
  97.  *          Wikipedia (2019): Fokker-Planck Equation
  98.  *              https://en.wikipedia.org/wiki/Fokker%E2%80%93Planck_equation
  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/kolmogorov/README.md">Fokker Planck Kolmogorov Forward/Backward</a></li>
  108.  *  </ul>
  109.  *
  110.  * @author Lakshmi Krishnamurthy
  111.  */

  112. public class RdFokkerPlanck
  113. {
  114.     private org.drip.dynamics.ito.DiffusionTensor _diffusionTensor = null;
  115.     private org.drip.dynamics.ito.RdToR1Drift[] _driftFunctionArray = null;
  116.     private org.drip.dynamics.kolmogorov.RiskenOmegaEstimator _riskenOmegaEstimator = null;

  117.     /**
  118.      * RdFokkerPlanck Constructor
  119.      *
  120.      * @param driftFunctionArray Drift Function Array
  121.      * @param diffusionTensor Diffusion Tensor
  122.      * @param riskenOmegaEstimator Risken Omega Estimator
  123.      *
  124.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  125.      */

  126.     public RdFokkerPlanck (
  127.         final org.drip.dynamics.ito.RdToR1Drift[] driftFunctionArray,
  128.         final org.drip.dynamics.ito.DiffusionTensor diffusionTensor,
  129.         final org.drip.dynamics.kolmogorov.RiskenOmegaEstimator riskenOmegaEstimator)
  130.         throws java.lang.Exception
  131.     {
  132.         if (null == (_driftFunctionArray = driftFunctionArray) ||
  133.             null == (_diffusionTensor = diffusionTensor) ||
  134.             null == (_riskenOmegaEstimator = riskenOmegaEstimator)
  135.         )
  136.         {
  137.             throw new java.lang.Exception (
  138.                 "RdFokkerPlanck Constructor => Invalid Inputs"
  139.             );
  140.         }

  141.         int dimension = _driftFunctionArray.length;

  142.         if (dimension != _diffusionTensor.dimension())
  143.         {
  144.             throw new java.lang.Exception (
  145.                 "RdFokkerPlanck Constructor => Invalid Inputs"
  146.             );
  147.         }

  148.         for (int dimensionIndex = 0;
  149.             dimensionIndex < dimension;
  150.             ++dimensionIndex)
  151.         {
  152.             if (null == _driftFunctionArray[dimensionIndex])
  153.             {
  154.                 throw new java.lang.Exception (
  155.                     "RdFokkerPlanck Constructor => Invalid Inputs"
  156.                 );
  157.             }
  158.         }
  159.     }

  160.     /**
  161.      * Retrieve the Drift Function Array
  162.      *
  163.      * @return The Drift Function Array
  164.      */

  165.     public org.drip.dynamics.ito.RdToR1Drift[] driftFunctionArray()
  166.     {
  167.         return _driftFunctionArray;
  168.     }

  169.     /**
  170.      * Retrieve the Diffusion Tensor
  171.      *
  172.      * @return The Diffusion Tensor
  173.      */

  174.     public org.drip.dynamics.ito.DiffusionTensor diffusionTensor()
  175.     {
  176.         return _diffusionTensor;
  177.     }

  178.     /**
  179.      * Retrieve the Risken Omega Estimator
  180.      *
  181.      * @return The Risken Omega Estimator
  182.      */

  183.     public org.drip.dynamics.kolmogorov.RiskenOmegaEstimator riskenOmegaEstimator()
  184.     {
  185.         return _riskenOmegaEstimator;
  186.     }

  187.     /**
  188.      * Compute the Next Incremental Time Derivative of the PDF
  189.      *
  190.      * @param probabilityDensityFunction The PDF
  191.      * @param timeRdVertex The R<sup>d</sup> Time Vertex
  192.      *
  193.      * @return Next Incremental Time Derivative of the PDF
  194.      *
  195.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  196.      */

  197.     public double pdfDot (
  198.         final org.drip.dynamics.process.RdProbabilityDensityFunction probabilityDensityFunction,
  199.         final org.drip.dynamics.ito.TimeRdVertex timeRdVertex)
  200.         throws java.lang.Exception
  201.     {
  202.         if (null == probabilityDensityFunction ||
  203.             null == timeRdVertex)
  204.         {
  205.             throw new java.lang.Exception (
  206.                 "RdFokkerPlanck::pdfDot => Invalid Inputs"
  207.             );
  208.         }

  209.         final int dimension = _diffusionTensor.dimension();

  210.         final double time = timeRdVertex.t();

  211.         double pdfDot = 0.;

  212.         for (int dimensionIndex = 0;
  213.             dimensionIndex < dimension;
  214.             ++dimensionIndex)
  215.         {
  216.             final int index = dimensionIndex;

  217.             pdfDot = pdfDot - new org.drip.function.definition.RdToR1 (
  218.                 null
  219.             )
  220.             {
  221.                 @Override public int dimension()
  222.                 {
  223.                     return dimension;
  224.                 }

  225.                 @Override public double evaluate (
  226.                     final double[] xArray)
  227.                     throws java.lang.Exception
  228.                 {
  229.                     org.drip.dynamics.ito.TimeRdVertex localTimeRdVertex =
  230.                         new org.drip.dynamics.ito.TimeRdVertex (
  231.                             time,
  232.                             xArray
  233.                         );

  234.                     return _driftFunctionArray[index].drift (
  235.                         localTimeRdVertex
  236.                     ) * probabilityDensityFunction.density (
  237.                         localTimeRdVertex
  238.                     );
  239.                 }
  240.             }.derivative (
  241.                 timeRdVertex.xArray(),
  242.                 dimensionIndex,
  243.                 1
  244.             );
  245.         }

  246.         for (int dimensionIndexI = 0;
  247.             dimensionIndexI < dimension;
  248.             ++dimensionIndexI)
  249.         {
  250.             final int indexI = dimensionIndexI;

  251.             for (int dimensionIndexJ = 0;
  252.                 dimensionIndexJ < dimension;
  253.                 ++dimensionIndexJ)
  254.             {
  255.                 final int indexJ = dimensionIndexJ;

  256.                 pdfDot = pdfDot + new org.drip.function.definition.RdToR1 (
  257.                     null
  258.                 )
  259.                 {
  260.                     @Override public int dimension()
  261.                     {
  262.                         return dimension;
  263.                     }

  264.                     @Override public double evaluate (
  265.                         final double[] xArray)
  266.                         throws java.lang.Exception
  267.                     {
  268.                         org.drip.dynamics.ito.TimeRdVertex localTimeRdVertex =
  269.                             new org.drip.dynamics.ito.TimeRdVertex (
  270.                                 time,
  271.                                 xArray
  272.                             );

  273.                         return _diffusionTensor.diffusionCoefficient (
  274.                             localTimeRdVertex,
  275.                             indexI,
  276.                             indexJ
  277.                         ) * probabilityDensityFunction.density (
  278.                             localTimeRdVertex
  279.                         );
  280.                     }
  281.                 }.hessian (
  282.                     timeRdVertex.xArray()
  283.                 )[indexI][indexJ];
  284.             };
  285.         }

  286.         return pdfDot;
  287.     }

  288.     /**
  289.      * Compute the Temporal Probability Distribution Function, if any
  290.      *
  291.      * @param intialProbabilityDensityFunction The Initial Probability Density Function
  292.      *
  293.      * @return The Temporal Probability Distribution Function
  294.      */

  295.     public org.drip.dynamics.process.RdProbabilityDensityFunction temporalPDF (
  296.         final org.drip.function.definition.RdToR1 intialProbabilityDensityFunction)
  297.     {
  298.         return null;
  299.     }

  300.     /**
  301.      * Compute the Steady-State Probability Distribution Function, if any
  302.      *
  303.      * @return The Steady-State Probability Distribution Function
  304.      */

  305.     public org.drip.function.definition.RdToR1 steadyStatePDF()
  306.     {
  307.         double[][] omega = _riskenOmegaEstimator.estimateOmega (
  308.             _diffusionTensor,
  309.             _driftFunctionArray
  310.         );

  311.         final double[][] omegaInverse =
  312.             org.drip.numerical.linearalgebra.Matrix.InvertUsingGaussianElimination (
  313.                 omega
  314.             );

  315.         if (null == omegaInverse)
  316.         {
  317.             return null;
  318.         }

  319.         final int dimension = _diffusionTensor.dimension();

  320.         double rdNormalizer = java.lang.Double.NaN;

  321.         try
  322.         {
  323.             rdNormalizer = java.lang.Math.sqrt (
  324.                 java.lang.Math.pow (
  325.                     2. * java.lang.Math.PI,
  326.                     -1. * dimension
  327.                 ) / new org.drip.function.matrix.Square (
  328.                     omega
  329.                 ).determinant()
  330.             );
  331.         }
  332.         catch (java.lang.Exception e)
  333.         {
  334.             e.printStackTrace();
  335.         }

  336.         final double rdNormalizerFinal = rdNormalizer;

  337.         final org.drip.function.definition.R1ToR1 r1ToR1Exponential =
  338.             new org.drip.function.definition.R1ToR1 (
  339.                 null
  340.             )
  341.         {
  342.             @Override public double evaluate (
  343.                 final double x)
  344.                 throws java.lang.Exception
  345.             {
  346.                 return java.lang.Math.exp (
  347.                     -0.5 * x
  348.                 );
  349.             }
  350.         };

  351.         return new org.drip.function.definition.RdToR1 (
  352.             null
  353.         )
  354.         {
  355.             @Override public int dimension()
  356.             {
  357.                 return dimension;
  358.             }

  359.             @Override public double evaluate (
  360.                 final double[] xArray)
  361.                 throws java.lang.Exception
  362.             {
  363.                 return rdNormalizerFinal * r1ToR1Exponential.evaluate (
  364.                     org.drip.numerical.linearalgebra.Matrix.DotProduct (
  365.                         xArray,
  366.                         org.drip.numerical.linearalgebra.Matrix.Product (
  367.                             omegaInverse,
  368.                             xArray
  369.                         )
  370.                     )
  371.                 );
  372.             }
  373.         };
  374.     }
  375. }