TrinomialTreeTransitionMetrics.java

  1. package org.drip.dynamics.hullwhite;

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

  78. /**
  79.  * <i>TrinomialTreeTransitionMetrics</i> records the Transition Metrics associated with Node-to-Node
  80.  * Evolution of the Instantaneous Short Rate using the Hull-White Model Trinomial Tree.
  81.  *
  82.  *  <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  86.  *      <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>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/dynamics/hullwhite/README.md">Hull White Latent State Evolution</a></li>
  88.  *  </ul>
  89.  *
  90.  * @author Lakshmi Krishnamurthy
  91.  */

  92. public class TrinomialTreeTransitionMetrics {
  93.     private long _lTreeTimeIndex = -1L;
  94.     private long _lTreeStochasticBaseIndex = -1L;
  95.     private long _lTreeStochasticDisplacementIndex = -1L;
  96.     private double _dblXVariance = java.lang.Double.NaN;
  97.     private double _dblTerminalAlpha = java.lang.Double.NaN;
  98.     private double _dblProbabilityUp = java.lang.Double.NaN;
  99.     private int _iInitialDate = java.lang.Integer.MIN_VALUE;
  100.     private int _iTerminalDate = java.lang.Integer.MIN_VALUE;
  101.     private double _dblProbabilityDown = java.lang.Double.NaN;
  102.     private double _dblProbabilityStay = java.lang.Double.NaN;
  103.     private double _dblXStochasticShift = java.lang.Double.NaN;
  104.     private double _dblExpectedTerminalX = java.lang.Double.NaN;

  105.     /**
  106.      * TrinomialTreeTransitionMetrics Constructor
  107.      *
  108.      * @param iInitialDate The Initial Date
  109.      * @param iTerminalDate The Terminal/Final Date
  110.      * @param lTreeTimeIndex The Tree Time Index
  111.      * @param lTreeStochasticBaseIndex The Tree Stochastic Base Index
  112.      * @param dblExpectedTerminalX Expectation of the Final/Terminal Value for X
  113.      * @param dblXVariance Variance of X
  114.      * @param dblTerminalAlpha The Final/Terminal Alpha
  115.      *
  116.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  117.      */

  118.     public TrinomialTreeTransitionMetrics (
  119.         final int iInitialDate,
  120.         final int iTerminalDate,
  121.         final long lTreeTimeIndex,
  122.         final long lTreeStochasticBaseIndex,
  123.         final double dblExpectedTerminalX,
  124.         final double dblXVariance,
  125.         final double dblTerminalAlpha)
  126.         throws java.lang.Exception
  127.     {
  128.         if (0 > (_lTreeTimeIndex = lTreeTimeIndex) || !org.drip.numerical.common.NumberUtil.IsValid
  129.             (_dblExpectedTerminalX = dblExpectedTerminalX) || !org.drip.numerical.common.NumberUtil.IsValid
  130.                 (_dblXVariance = dblXVariance) || !org.drip.numerical.common.NumberUtil.IsValid
  131.                     (_dblTerminalAlpha = dblTerminalAlpha))
  132.             throw new java.lang.Exception ("TrinomialTreeTransitionMetrics ctr: Invalid Inputs");

  133.         _dblXStochasticShift = java.lang.Math.sqrt (_dblXVariance * 3.);

  134.         _lTreeStochasticDisplacementIndex = java.lang.Math.round (_dblExpectedTerminalX /
  135.             _dblXStochasticShift);

  136.         _iInitialDate = iInitialDate;
  137.         _iTerminalDate = iTerminalDate;
  138.         _lTreeStochasticBaseIndex = lTreeStochasticBaseIndex;
  139.         double dblEta = _dblExpectedTerminalX - _lTreeStochasticDisplacementIndex * _dblXStochasticShift;
  140.         _dblProbabilityStay = (2. / 3.) - (dblEta * dblEta / (3. * _dblXVariance));
  141.         _dblProbabilityDown = (1. / 6.) + (dblEta * dblEta / (6. * _dblXVariance)) - (0.5 * dblEta /
  142.             _dblXStochasticShift);
  143.         _dblProbabilityUp = (1. / 6.) + (dblEta * dblEta / (6. * _dblXVariance)) + (0.5 * dblEta /
  144.             _dblXStochasticShift);
  145.     }

  146.     /**
  147.      * Retrieve the Initial Date
  148.      *
  149.      * @return The Initial Date
  150.      */

  151.     public int initialDate()
  152.     {
  153.         return _iInitialDate;
  154.     }

  155.     /**
  156.      * Retrieve the Terminal Date
  157.      *
  158.      * @return The Terminal Date
  159.      */

  160.     public int terminalDate()
  161.     {
  162.         return _iTerminalDate;
  163.     }

  164.     /**
  165.      * Retrieve the Tree Time Index
  166.      *
  167.      * @return The Tree Time Index
  168.      */

  169.     public long treeTimeIndex()
  170.     {
  171.         return _lTreeTimeIndex;
  172.     }

  173.     /**
  174.      * Retrieve the Expected Final/Terminal Value for X
  175.      *
  176.      * @return The Expected Final/Terminal Value for X
  177.      */

  178.     public double expectedTerminalX()
  179.     {
  180.         return _dblExpectedTerminalX;
  181.     }

  182.     /**
  183.      * Retrieve the Variance in the Final Value of X
  184.      *
  185.      * @return The Variance in the Final Value of X
  186.      */

  187.     public double xVariance()
  188.     {
  189.         return _dblXVariance;
  190.     }

  191.     /**
  192.      * Retrieve the Stochastic Shift of X
  193.      *
  194.      * @return The Stochastic Shift of X
  195.      */

  196.     public double xStochasticShift()
  197.     {
  198.         return _dblXStochasticShift;
  199.     }

  200.     /**
  201.      * Retrieve the Tree Stochastic Displacement Index
  202.      *
  203.      * @return The Tree Stochastic Displacement Index
  204.      */

  205.     public long treeStochasticDisplacementIndex()
  206.     {
  207.         return _lTreeStochasticDisplacementIndex;
  208.     }

  209.     /**
  210.      * Retrieve the Probability of the Up Stochastic Shift
  211.      *
  212.      * @return Probability of the Up Stochastic Shift
  213.      */

  214.     public double probabilityUp()
  215.     {
  216.         return _dblProbabilityUp;
  217.     }

  218.     /**
  219.      * Retrieve the Probability of the Down Stochastic Shift
  220.      *
  221.      * @return Probability of the Down Stochastic Shift
  222.      */

  223.     public double probabilityDown()
  224.     {
  225.         return _dblProbabilityDown;
  226.     }

  227.     /**
  228.      * Retrieve the Probability of the No Shift
  229.      *
  230.      * @return Probability of the No Shift
  231.      */

  232.     public double probabilityStay()
  233.     {
  234.         return _dblProbabilityStay;
  235.     }

  236.     /**
  237.      * Retrieve the "Up" Value for X
  238.      *
  239.      * @return The "Up" Value for X
  240.      */

  241.     public double xUp()
  242.     {
  243.         return (_lTreeStochasticDisplacementIndex + 1) * _dblXStochasticShift;
  244.     }

  245.     /**
  246.      * Retrieve the "Down" Value for X
  247.      *
  248.      * @return The "Down" Value for X
  249.      */

  250.     public double xDown()
  251.     {
  252.         return (_lTreeStochasticDisplacementIndex - 1) * _dblXStochasticShift;
  253.     }

  254.     /**
  255.      * Retrieve the Final/Terminal Alpha
  256.      *
  257.      * @return The Final/Terminal Alpha
  258.      */

  259.     public double terminalAlpha()
  260.     {
  261.         return _dblTerminalAlpha;
  262.     }

  263.     /**
  264.      * Retrieve the "Up" Node Metrics
  265.      *
  266.      * @return The "Up" Node Metrics
  267.      */

  268.     public org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics upNodeMetrics()
  269.     {
  270.         try {
  271.             return new org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics (_lTreeTimeIndex,
  272.                 _lTreeStochasticBaseIndex + 1, (_lTreeStochasticDisplacementIndex + 1) *
  273.                     _dblXStochasticShift, _dblTerminalAlpha);
  274.         } catch (java.lang.Exception e) {
  275.             e.printStackTrace();
  276.         }

  277.         return null;
  278.     }

  279.     /**
  280.      * Retrieve the "Down" Node Metrics
  281.      *
  282.      * @return The "Down" Node Metrics
  283.      */

  284.     public org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics downNodeMetrics()
  285.     {
  286.         try {
  287.             return new org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics (_lTreeTimeIndex,
  288.                 _lTreeStochasticBaseIndex - 1, (_lTreeStochasticDisplacementIndex - 1) *
  289.                     _dblXStochasticShift, _dblTerminalAlpha);
  290.         } catch (java.lang.Exception e) {
  291.             e.printStackTrace();
  292.         }

  293.         return null;
  294.     }

  295.     /**
  296.      * Retrieve the "Stay" Node Metrics
  297.      *
  298.      * @return The "Stay" Node Metrics
  299.      */

  300.     public org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics stayNodeMetrics()
  301.     {
  302.         try {
  303.             return new org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics (_lTreeTimeIndex,
  304.                 _lTreeStochasticBaseIndex, _lTreeStochasticDisplacementIndex * _dblXStochasticShift,
  305.                     _dblTerminalAlpha);
  306.         } catch (java.lang.Exception e) {
  307.             e.printStackTrace();
  308.         }

  309.         return null;
  310.     }
  311. }