TrinomialTreeSequenceMetrics.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>TrinomialTreeSequenceMetrics</i> records the Evolution Metrics of the Hull-White Model Trinomial Tree
  80.  * Sequence.
  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 TrinomialTreeSequenceMetrics {
  93.     private java.util.Map<java.lang.String, java.lang.Double> _mapSourceTargetTransitionProbability = new
  94.         java.util.HashMap<java.lang.String, java.lang.Double>();

  95.     private java.util.Map<java.lang.String, java.lang.Double> _mapTargetSourceTransitionProbability = new
  96.         java.util.HashMap<java.lang.String, java.lang.Double>();

  97.     private java.util.Map<java.lang.String, org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics> _mapTTNM =
  98.         new java.util.HashMap<java.lang.String, org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics>();

  99.     private java.util.Map<java.lang.Long, org.drip.dynamics.hullwhite.TrinomialTreeTransitionMetrics>
  100.         _mapTTTM = new java.util.HashMap<java.lang.Long,
  101.             org.drip.dynamics.hullwhite.TrinomialTreeTransitionMetrics>();

  102.     private static final java.lang.String NodeMetricsKey (
  103.         final org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics hwnm)
  104.     {
  105.         return hwnm.timeIndex() + "," + hwnm.xStochasticIndex();
  106.     }

  107.     /**
  108.      * Empty TrinomialTreeSequenceMetrics Constructor
  109.      */

  110.     public TrinomialTreeSequenceMetrics()
  111.     {
  112.     }

  113.     /**
  114.      * Add a Path Transition Metrics Instance
  115.      *
  116.      * @param hwtm The Path Transition Metrics Instance
  117.      *
  118.      * @return TRUE - The Path Transition Metrics Instance successfully added
  119.      */

  120.     public boolean addTransitionMetrics (
  121.         final org.drip.dynamics.hullwhite.TrinomialTreeTransitionMetrics hwtm)
  122.     {
  123.         if (null == hwtm) return false;

  124.         _mapTTTM.put (hwtm.treeTimeIndex(), hwtm);

  125.         return true;
  126.     }

  127.     /**
  128.      * Retrieve the Transition Metrics associated with the specified Tree Time Index
  129.      *
  130.      * @param lTreeTimeIndex The Tree Time Index
  131.      *
  132.      * @return The Transition Metrics associated with the specified Tree Time Index
  133.      */

  134.     public org.drip.dynamics.hullwhite.TrinomialTreeTransitionMetrics transitionMetrics (
  135.         final long lTreeTimeIndex)
  136.     {
  137.         return _mapTTTM.containsKey (lTreeTimeIndex) ? _mapTTTM.get (lTreeTimeIndex) : null;
  138.     }

  139.     /**
  140.      * Retrieve the Transition Metrics Map
  141.      *
  142.      * @return The Transition Metrics Map
  143.      */

  144.     public java.util.Map<java.lang.Long, org.drip.dynamics.hullwhite.TrinomialTreeTransitionMetrics>
  145.         transitionMetrics()
  146.     {
  147.         return _mapTTTM;
  148.     }

  149.     /**
  150.      * Add the Hull-White Node Metrics Instance
  151.      *
  152.      * @param hwnm The Hull-White Node Metrics Instance
  153.      *
  154.      * @return The Node Met5rics Instance successfully added
  155.      */

  156.     public boolean addNodeMetrics (
  157.         final org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics hwnm)
  158.     {
  159.         if (null == hwnm) return false;

  160.         _mapTTNM.put (NodeMetricsKey (hwnm), hwnm);

  161.         return true;
  162.     }

  163.     /**
  164.      * Retrieve the Node Metrics from the corresponding Tree Time/Space Indexes
  165.      *
  166.      * @param lTreeTimeIndex The Tree Time Index
  167.      * @param lTreeStochasticIndex The Tree Space Index
  168.      *
  169.      * @return The Node Metrics
  170.      */

  171.     public org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics nodeMetrics (
  172.         final long lTreeTimeIndex,
  173.         final long lTreeStochasticIndex)
  174.     {
  175.         java.lang.String strKey = lTreeTimeIndex + "," + lTreeStochasticIndex;

  176.         return _mapTTNM.containsKey (strKey) ? _mapTTNM.get (strKey) : null;
  177.     }

  178.     /**
  179.      * Retrieve the Node Metrics Map
  180.      *
  181.      * @return The Node Metrics Map
  182.      */

  183.     public java.util.Map<java.lang.String, org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics>
  184.         nodeMetrics()
  185.     {
  186.         return _mapTTNM;
  187.     }

  188.     /**
  189.      * Set the Transition Probability for the specified Pair of Nodes
  190.      *
  191.      * @param hwnmSource Source Node
  192.      * @param hwnmTarget Target Node
  193.      * @param dblTransitionProbability The Transition Probability
  194.      *
  195.      * @return TRUE - The Transition Probability Successfully set
  196.      */

  197.     public boolean setTransitionProbability (
  198.         final org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics hwnmSource,
  199.         final org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics hwnmTarget,
  200.         final double dblTransitionProbability)
  201.     {
  202.         if (null == hwnmSource || null == hwnmTarget || !org.drip.numerical.common.NumberUtil.IsValid
  203.             (dblTransitionProbability) || 0. >= dblTransitionProbability || 1. < dblTransitionProbability)
  204.             return false;

  205.         java.lang.String strSourceNodeKey = NodeMetricsKey (hwnmSource);

  206.         java.lang.String strTargetNodeKey = NodeMetricsKey (hwnmTarget);

  207.         _mapSourceTargetTransitionProbability.put (strSourceNodeKey + "#" + strTargetNodeKey,
  208.             dblTransitionProbability);

  209.         _mapTargetSourceTransitionProbability.put (strTargetNodeKey + "#" + strSourceNodeKey,
  210.             dblTransitionProbability);

  211.         return true;
  212.     }

  213.     /**
  214.      * Retrieve the Source-To-Target Transition Probability
  215.      *
  216.      * @param hwnmSource Source Node
  217.      * @param hwnmTarget Target Node
  218.      *
  219.      * @return The Source-To-Target Transition Probability
  220.      *
  221.      * @throws java.lang.Exception Thrown if the Source-To-Target Transition Probability cannot be computed
  222.      */

  223.     public double sourceTargetTransitionProbability (
  224.         final org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics hwnmSource,
  225.         final org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics hwnmTarget)
  226.         throws java.lang.Exception
  227.     {
  228.         if (null == hwnmSource || null == hwnmTarget)
  229.             throw new java.lang.Exception
  230.                 ("TrinomialTreeSequenceMetrics::sourceTargetTransitionProbability => Invalid Inputs!");

  231.         java.lang.String strKey = NodeMetricsKey (hwnmSource) + "#" + NodeMetricsKey (hwnmTarget);

  232.         if (!_mapSourceTargetTransitionProbability.containsKey (strKey))
  233.             throw new java.lang.Exception
  234.                 ("TrinomialTreeSequenceMetrics::sourceTargetTransitionProbability => No Transition Entry!");

  235.         return _mapSourceTargetTransitionProbability.get (strKey);
  236.     }

  237.     /**
  238.      * Retrieve the FULL Source-Target Transition Probability Map
  239.      *
  240.      * @return The Source-Target Transition Probability Map
  241.      */

  242.     public java.util.Map<java.lang.String, java.lang.Double> sourceTargetTransitionProbability()
  243.     {
  244.         return _mapSourceTargetTransitionProbability;
  245.     }

  246.     /**
  247.      * Retrieve the Target-From-Source Transition Probability
  248.      *
  249.      * @param hwnmTarget Target Node
  250.      * @param hwnmSource Source Node
  251.      *
  252.      * @return The Target-From-Source Transition Probability
  253.      *
  254.      * @throws java.lang.Exception Thrown if the Target-From-Source Transition Probability cannot be computed
  255.      */

  256.     public double targetSourceTransitionProbability (
  257.         final org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics hwnmTarget,
  258.         final org.drip.dynamics.hullwhite.TrinomialTreeNodeMetrics hwnmSource)
  259.         throws java.lang.Exception
  260.     {
  261.         if (null == hwnmSource || null == hwnmTarget)
  262.             throw new java.lang.Exception
  263.                 ("TrinomialTreeSequenceMetrics::targetSourceTransitionProbability => Invalid Inputs!");

  264.         java.lang.String strKey = NodeMetricsKey (hwnmTarget) + "#" + NodeMetricsKey (hwnmSource);

  265.         if (!_mapTargetSourceTransitionProbability.containsKey (strKey))
  266.             throw new java.lang.Exception
  267.                 ("TrinomialTreeSequenceMetrics::targetSourceTransitionProbability => No Transition Entry!");

  268.         return _mapTargetSourceTransitionProbability.get (strKey);
  269.     }

  270.     /**
  271.      * Retrieve the FULL Target-Source Transition Probability Map
  272.      *
  273.      * @return The Target-Source Transition Probability Map
  274.      */

  275.     public java.util.Map<java.lang.String, java.lang.Double> targetSourceTransitionProbability()
  276.     {
  277.         return _mapTargetSourceTransitionProbability;
  278.     }
  279. }