TrinomialTreeEvolution.java

  1. package org.drip.sample.hullwhite;

  2. import org.drip.analytics.date.*;
  3. import org.drip.dynamics.hullwhite.*;
  4. import org.drip.function.r1tor1.FlatUnivariate;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.sequence.random.BoxMullerGaussian;
  7. import org.drip.service.env.EnvManager;
  8. import org.drip.state.identifier.FundingLabel;

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

  12. /*!
  13.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  16.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  17.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  18.  *
  19.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  20.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  21.  *      credit, commodity, equity, FX, and structured products.
  22.  *  
  23.  *      https://lakshmidrip.github.io/DROP/
  24.  *  
  25.  *  DROP is composed of three modules:
  26.  *  
  27.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  28.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  29.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  30.  *
  31.  *  DROP Analytics Core implements libraries for the following:
  32.  *  - Fixed Income Analytics
  33.  *  - Asset Backed Analytics
  34.  *  - XVA Analytics
  35.  *  - Exposure and Margin Analytics
  36.  *
  37.  *  DROP Portfolio Core implements libraries for the following:
  38.  *  - Asset Allocation Analytics
  39.  *  - Transaction Cost Analytics
  40.  *
  41.  *  DROP Numerical Core implements libraries for the following:
  42.  *  - Statistical Learning
  43.  *  - Numerical Optimizer
  44.  *  - Spline Builder
  45.  *  - Algorithm Support
  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>TrinomialTreeEvolution</i> demonstrates the Construction and Usage of the Hull-White Trinomial Tree and
  76.  * the Eventual Evolution of the Short Rate on it.
  77.  *  
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  81.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  82.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  83.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/hullwhite/README.md">Hull-White 1F Dynamics</a></li>
  84.  *  </ul>
  85.  * <br><br>
  86.  *
  87.  * @author Lakshmi Krishnamurthy
  88.  */

  89. public class TrinomialTreeEvolution {

  90.     private static final SingleFactorStateEvolver HullWhiteEvolver (
  91.         final String strCurrency,
  92.         final double dblSigma,
  93.         final double dblA,
  94.         final double dblStartingForwardRate)
  95.         throws Exception
  96.     {
  97.         return new SingleFactorStateEvolver (
  98.             FundingLabel.Standard (strCurrency),
  99.             dblSigma,
  100.             dblA,
  101.             new FlatUnivariate (dblStartingForwardRate),
  102.             new BoxMullerGaussian (
  103.                 0.,
  104.                 1.
  105.             )
  106.         );
  107.     }

  108.     private static final void DumpMetrics (
  109.         final TrinomialTreeTransitionMetrics hwtm)
  110.         throws Exception
  111.     {
  112.         System.out.println ("\t| [" + new JulianDate (hwtm.initialDate()) + " -> " +
  113.             new JulianDate (hwtm.terminalDate()) + "] => " +
  114.             FormatUtil.FormatDouble (hwtm.expectedTerminalX(), 1, 4, 1.) + " | " +
  115.             FormatUtil.FormatDouble (Math.sqrt (hwtm.xVariance()), 1, 2, 100.) + "% | " +
  116.             FormatUtil.FormatDouble (hwtm.xStochasticShift(), 1, 4, 1.) + " || " +
  117.             FormatUtil.FormatDouble (hwtm.probabilityUp(), 1, 4, 1.) + " | " +
  118.             FormatUtil.FormatDouble (hwtm.upNodeMetrics().x(), 1, 4, 1.) + " | " +
  119.             FormatUtil.FormatDouble (hwtm.upNodeMetrics().shortRate(), 1, 2, 100.) + "% || " +
  120.             FormatUtil.FormatDouble (hwtm.probabilityDown(), 1, 4, 1.) + " | " +
  121.             FormatUtil.FormatDouble (hwtm.downNodeMetrics().x(), 1, 4, 1.) + " | " +
  122.             FormatUtil.FormatDouble (hwtm.downNodeMetrics().shortRate(), 1, 2, 100.) + "% || " +
  123.             FormatUtil.FormatDouble (hwtm.probabilityStay(), 1, 4, 1.) + " | " +
  124.             FormatUtil.FormatDouble (hwtm.stayNodeMetrics().x(), 1, 4, 1.) + " | " +
  125.             FormatUtil.FormatDouble (hwtm.stayNodeMetrics().shortRate(), 1, 2, 100.) + "% ||"
  126.         );
  127.     }

  128.     private static final void TreeHeader (
  129.         final String strEvolutionComment)
  130.         throws Exception
  131.     {
  132.         System.out.println ("\n\n\t|----------------------------------------------------------------------------------------------------------------------------------------------------||");

  133.         System.out.println ("\t|                                                                                                                                                    ||");

  134.         System.out.println (strEvolutionComment);

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

  136.         System.out.println ("\t|                                                                                                                                                    ||");

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

  138.         System.out.println ("\t|                                                                                                                                                    ||");

  139.         System.out.println ("\t|        Initial Date                                                                                                                                ||");

  140.         System.out.println ("\t|        Final Date                                                                                                                                  ||");

  141.         System.out.println ("\t|        Expected Final X                                                                                                                            ||");

  142.         System.out.println ("\t|        X Volatility (%)                                                                                                                            ||");

  143.         System.out.println ("\t|        X Stochastic Shift                                                                                                                          ||");

  144.         System.out.println ("\t|        Move-Up Probability                                                                                                                         ||");

  145.         System.out.println ("\t|        Move-Up X Node Value                                                                                                                        ||");

  146.         System.out.println ("\t|        Move-Up Short Rate                                                                                                                          ||");

  147.         System.out.println ("\t|        Move-Down Probability                                                                                                                       ||");

  148.         System.out.println ("\t|        Move-Down X Node Value                                                                                                                      ||");

  149.         System.out.println ("\t|        Move-Down Short Rate                                                                                                                        ||");

  150.         System.out.println ("\t|        Stay Probability                                                                                                                            ||");

  151.         System.out.println ("\t|        Stay X Node Value                                                                                                                           ||");

  152.         System.out.println ("\t|        Stay Short Rate                                                                                                                             ||");

  153.         System.out.println ("\t|                                                                                                                                                    ||");

  154.         System.out.println ("\t|----------------------------------------------------------------------------------------------------------------------------------------------------||");
  155.     }

  156.     public static final void main (
  157.         final String[] astrArgs)
  158.         throws Exception
  159.     {
  160.         EnvManager.InitEnv ("");

  161.         JulianDate dtSpot = DateUtil.Today();

  162.         String strCurrency = "USD";
  163.         double dblStartingShortRate = 0.05;
  164.         double dblSigma = 0.01;
  165.         double dblA = 1.;

  166.         SingleFactorStateEvolver hw = HullWhiteEvolver (
  167.             strCurrency,
  168.             dblSigma,
  169.             dblA,
  170.             dblStartingShortRate
  171.         );

  172.         int iSpotDate = dtSpot.julian();

  173.         int iFinalDate = dtSpot.addMonths (6).julian();

  174.         int iInitialDate = iSpotDate;
  175.         TrinomialTreeTransitionMetrics hwtm = null;

  176.         TreeHeader ("\t|    Hull-White Trinomial Tree Upwards Evolution Metrics                                                                                             ||");

  177.         while (iInitialDate < iFinalDate) {
  178.             DumpMetrics (hwtm =
  179.                 hw.evolveTrinomialTree (
  180.                     iSpotDate,
  181.                     iInitialDate,
  182.                     iFinalDate,
  183.                     null == hwtm ? null : hwtm.upNodeMetrics()
  184.                 )
  185.             );

  186.             iInitialDate += 10;
  187.         }

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

  189.         hwtm = null;
  190.         iInitialDate = iSpotDate;

  191.         TreeHeader ("\t|    Hull-White Trinomial Tree Downwards Evolution Metrics                                                                                           ||");

  192.         while (iInitialDate < iFinalDate) {
  193.             DumpMetrics (hwtm =
  194.                 hw.evolveTrinomialTree (
  195.                     iSpotDate,
  196.                     iInitialDate,
  197.                     iFinalDate,
  198.                     null == hwtm ? null : hwtm.downNodeMetrics()
  199.                 )
  200.             );

  201.             iInitialDate += 10;
  202.         }

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

  204.         hwtm = null;
  205.         iInitialDate = iSpotDate;

  206.         TreeHeader ("\t|    Hull-White Trinomial Tree Stay-Put Evolution Metrics                                                                                            ||");

  207.         while (iInitialDate < iFinalDate) {
  208.             DumpMetrics (hwtm =
  209.                 hw.evolveTrinomialTree (
  210.                     iSpotDate,
  211.                     iInitialDate,
  212.                     iFinalDate,
  213.                     null == hwtm ? null : hwtm.stayNodeMetrics()
  214.                 )
  215.             );

  216.             iInitialDate += 10;
  217.         }

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

  219.         EnvManager.TerminateEnv();
  220.     }
  221. }