PathVertexRd.java

  1. package org.drip.state.sequence;

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

  76. /**
  77.  * <i>PathVertexRd</i> exposes the Functionality to generate a Sequence of the Path Vertex Latent State
  78.  * R<sup>d</sup> Realizations across Multiple Paths.
  79.  *
  80.  *  <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  84.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/README.md">Latent State Inference and Creation Utilities</a></li>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/sequence/README.md">Monte Carlo Path State Realizations</a></li>
  86.  *  </ul>
  87.  * <br><br>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class PathVertexRd {
  92.     private org.drip.measure.process.DiffusionEvolver[] _aDE = null;
  93.     private org.drip.measure.discrete.CorrelatedPathVertexDimension _cpvd = null;

  94.     /**
  95.      * Generate a Standard Instance of PathVertexRd
  96.      *
  97.      * @param cpvd Latent State Evolver CPVD Instance
  98.      * @param de The Latent State Diffusion Evolver
  99.      *
  100.      * @return Standard Instance of PathVertexRd
  101.      */

  102.     public static final PathVertexRd Standard (
  103.         final org.drip.measure.discrete.CorrelatedPathVertexDimension cpvd,
  104.         final org.drip.measure.process.DiffusionEvolver de)
  105.     {
  106.         if (null == cpvd || null == de) return null;

  107.         int iNumDimension = cpvd.numDimension();

  108.         org.drip.measure.process.DiffusionEvolver[] aDE = new
  109.             org.drip.measure.process.DiffusionEvolver[iNumDimension];

  110.         for (int iDimension = 0; iDimension < iNumDimension; ++iDimension)
  111.             aDE[iDimension] = de;

  112.         try {
  113.             return new PathVertexRd (cpvd, aDE);
  114.         } catch (java.lang.Exception e) {
  115.             e.printStackTrace();
  116.         }

  117.         return null;
  118.     }

  119.     /**
  120.      * PathVertexRd Constructor
  121.      *
  122.      * @param cpvd Latent State Evolver CPVD Instance
  123.      * @param aDE Array of the Latent State Diffusion Evolvers
  124.      *
  125.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  126.      */

  127.     public PathVertexRd (
  128.         final org.drip.measure.discrete.CorrelatedPathVertexDimension cpvd,
  129.         final org.drip.measure.process.DiffusionEvolver[] aDE)
  130.         throws java.lang.Exception
  131.     {
  132.         if (null == (_cpvd = cpvd) || null == (_aDE = aDE))
  133.             throw new java.lang.Exception ("PathVertexRd Constructor => Invalid Inputs");

  134.         int iNumDimension = _aDE.length;

  135.         if (iNumDimension != _cpvd.numDimension())
  136.             throw new java.lang.Exception ("PathVertexRd Constructor => Invalid Inputs");

  137.         for (int iDimension = 0; iDimension < iNumDimension; ++iDimension) {
  138.             if (null == _aDE[iDimension])
  139.                 throw new java.lang.Exception ("PathVertexRd Constructor => Invalid Inputs");
  140.         }
  141.     }

  142.     /**
  143.      * Retrieve the Latent State Dimension
  144.      *
  145.      * @return The Latent State Dimension
  146.      */

  147.     public int dimension()
  148.     {
  149.         return _aDE.length;
  150.     }

  151.     /**
  152.      * Retrieve the Latent State Evolver CPVD Instance
  153.      *
  154.      * @return The Latent State Evolver CPVD Instance
  155.      */

  156.     public org.drip.measure.discrete.CorrelatedPathVertexDimension cpvd()
  157.     {
  158.         return _cpvd;
  159.     }

  160.     /**
  161.      * Retrieve the Array of the Latent State Diffusion Evolvers
  162.      *
  163.      * @return The Array of the Latent State Diffusion Evolvers
  164.      */

  165.     public org.drip.measure.process.DiffusionEvolver[] evolver()
  166.     {
  167.         return _aDE;
  168.     }

  169.     /**
  170.      * Generate the R^d Path Vertex Realizations using the Initial R^d and the Evolution Time Width
  171.      *
  172.      * @param adblPathInitial The Initial Path R^d
  173.      * @param adblTimeIncrement The Array of Evolution Time Width Increments
  174.      *
  175.      * @return The R^d Path Vertex Realizations
  176.      */

  177.     public double[][][] pathVertex (
  178.         final double[] adblPathInitial,
  179.         final double[] adblTimeIncrement)
  180.     {
  181.         if (null == adblPathInitial || null == adblTimeIncrement) return null;

  182.         int iNumPath = _cpvd.numPath();

  183.         int iNumDimension = dimension();

  184.         int iNumVertex = _cpvd.numVertex();

  185.         if (iNumDimension != adblPathInitial.length || iNumVertex != adblTimeIncrement.length) return null;

  186.         double[][][] aaadblPathForward = new double[iNumPath][iNumVertex][iNumDimension];

  187.         org.drip.measure.discrete.VertexRd[] aVertexRd = _cpvd.multiPathVertexRd();

  188.         if (null == aVertexRd || iNumPath != aVertexRd.length) return null;

  189.         for (int iPath = 0; iPath < iNumPath; ++iPath) {
  190.             if (null == aVertexRd[iPath]) return null;

  191.             java.util.List<double[]> lsVertexRd = aVertexRd[iPath].vertexList();

  192.             org.drip.measure.realization.JumpDiffusionEdgeUnit[][] aaJDEU = new
  193.                 org.drip.measure.realization.JumpDiffusionEdgeUnit[iNumDimension][iNumVertex];
  194.             org.drip.measure.realization.JumpDiffusionVertex[][] aaJDV = new
  195.                 org.drip.measure.realization.JumpDiffusionVertex[iNumDimension][iNumVertex + 1];

  196.             for (int iTimeVertex = 0; iTimeVertex < iNumVertex; ++iTimeVertex) {
  197.                 double[] adblRd = lsVertexRd.get (iTimeVertex);

  198.                 if (null == adblRd || iNumDimension != adblRd.length) return null;

  199.                 for (int iDimension = 0; iDimension < iNumDimension; ++iDimension) {
  200.                     try {
  201.                         aaJDEU[iDimension][iTimeVertex] = new
  202.                             org.drip.measure.realization.JumpDiffusionEdgeUnit
  203.                                 (adblTimeIncrement[iTimeVertex], adblRd[iDimension], 0.);
  204.                     } catch (java.lang.Exception e) {
  205.                         e.printStackTrace();

  206.                         return null;
  207.                     }
  208.                 }
  209.             }

  210.             for (int iDimension = 0; iDimension < iNumDimension; ++iDimension) {
  211.                 try {
  212.                     aaJDV[iDimension] = _aDE[iDimension].vertexSequence (new
  213.                         org.drip.measure.realization.JumpDiffusionVertex (0., adblPathInitial[iDimension],
  214.                             0., false), aaJDEU[iDimension], adblTimeIncrement);
  215.                 } catch (java.lang.Exception e) {
  216.                     e.printStackTrace();

  217.                     return null;
  218.                 }
  219.             }

  220.             for (int iTimeVertex = 0; iTimeVertex < iNumVertex; ++iTimeVertex) {
  221.                 for (int iDimension = 0; iDimension < iNumDimension; ++iDimension)
  222.                     aaadblPathForward[iPath][iTimeVertex][iDimension] =
  223.                         aaJDV[iDimension][iTimeVertex].value();
  224.             }
  225.         }

  226.         return aaadblPathForward;
  227.     }

  228.     /**
  229.      * Generate the R^d Path Vertex Realizations using the Initial R^d and the Evolution Time Width
  230.      *
  231.      * @param adblPathInitial The Initial Path R^d
  232.      * @param dblTimeIncrement The Evolution Time Width
  233.      *
  234.      * @return The R^d Path Vertex Realizations
  235.      */

  236.     public double[][][] pathVertex (
  237.         final double[] adblPathInitial,
  238.         final double dblTimeIncrement)
  239.     {
  240.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblTimeIncrement)) return null;

  241.         int iNumVertex = _cpvd.numVertex();

  242.         double[] adblTimeIncrement = new double[iNumVertex];

  243.         for (int iTimeVertex = 0; iTimeVertex < iNumVertex; ++iTimeVertex)
  244.             adblTimeIncrement[iTimeVertex] = dblTimeIncrement;

  245.         return pathVertex (adblPathInitial, adblTimeIncrement);
  246.     }

  247.     /**
  248.      * Generate the R^d Path Vertex Realizations using the Initial R^d and the Array of Event Tenors
  249.      *
  250.      * @param adblPathInitial The Initial Path R^d
  251.      * @param astrEventTenor The Array of Event Tenors
  252.      *
  253.      * @return The R^d Path Vertex Realizations
  254.      */

  255.     public double[][][] pathVertex (
  256.         final double[] adblPathInitial,
  257.         final java.lang.String[] astrEventTenor)
  258.     {
  259.         if (null == astrEventTenor) return null;

  260.         int iNumVertex = _cpvd.numVertex();

  261.         if (iNumVertex != astrEventTenor.length) return null;

  262.         double[] adblTimeIncrement = new double[iNumVertex];

  263.         for (int iTimeVertex = 0; iTimeVertex < iNumVertex; ++iTimeVertex) {
  264.             try {
  265.                 adblTimeIncrement[iTimeVertex] = org.drip.analytics.support.Helper.TenorToYearFraction
  266.                     (astrEventTenor[iTimeVertex]);
  267.             } catch (java.lang.Exception e) {
  268.                 e.printStackTrace();

  269.                 return null;
  270.             }
  271.         }

  272.         return pathVertex (adblPathInitial, adblTimeIncrement);
  273.     }

  274.     /**
  275.      * Generate the R^d Path Vertex Realizations using the Initial R^d and the Array of Event Tenors
  276.      *
  277.      * @param adblPathInitial The Initial Path R^d
  278.      * @param iSpotDate The Spot Date
  279.      * @param aiEventDate The Array of Event Dates
  280.      *
  281.      * @return The R^d Path Vertex Realizations
  282.      */

  283.     public double[][][] pathVertex (
  284.         final double[] adblPathInitial,
  285.         final int iSpotDate,
  286.         final int[] aiEventDate)
  287.     {
  288.         if (null == aiEventDate) return null;

  289.         int iNumVertex = _cpvd.numVertex();

  290.         if (iNumVertex != aiEventDate.length) return null;

  291.         double[] adblTimeIncrement = new double[iNumVertex];

  292.         org.drip.analytics.daycount.ActActDCParams aap =
  293.             org.drip.analytics.daycount.ActActDCParams.FromFrequency (1);

  294.         for (int iTimeVertex = 0; iTimeVertex < iNumVertex; ++iTimeVertex) {
  295.             try {
  296.                 adblTimeIncrement[iTimeVertex] = org.drip.analytics.daycount.Convention.YearFraction
  297.                     (iSpotDate, aiEventDate[iTimeVertex], "Act/Act ISDA", false, aap, "");
  298.             } catch (java.lang.Exception e) {
  299.                 e.printStackTrace();

  300.                 return null;
  301.             }
  302.         }

  303.         return pathVertex (adblPathInitial, adblTimeIncrement);
  304.     }
  305. }