TrinomialTreeCalibration.java

  1. package org.drip.sample.hullwhite;

  2. import java.util.Map;

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

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

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

  75. /**
  76.  * <i>TrinomialTreeCalibration</i> demonstrates the Construction and Calibration of the Hull-White Trinomial
  77.  * Tree and the Eventual Evolution of the Short Rate on it.
  78.  *  
  79.  * <br><br>
  80.  *  <ul>
  81.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  82.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  83.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  84.  *      <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>
  85.  *  </ul>
  86.  * <br><br>
  87.  *
  88.  * @author Lakshmi Krishnamurthy
  89.  */

  90. public class TrinomialTreeCalibration {

  91.     private static final String SourceToTarget (
  92.         final String strKey)
  93.     {
  94.         String[] astrNode = strKey.split ("#");

  95.         String[] astrSourceNode = astrNode[0].split (",");

  96.         String[] astrTargetNode = astrNode[1].split (",");

  97.         return "[" +
  98.             astrSourceNode[0] + "," +
  99.             FormatUtil.FormatDouble (Double.parseDouble (astrSourceNode[1]), 1, 0, 1.) + "] => [" +
  100.             astrTargetNode[0] + "," +
  101.             FormatUtil.FormatDouble (Double.parseDouble (astrTargetNode[1]), 1, 0, 1.) + "]";
  102.     }

  103.     private static final SingleFactorStateEvolver HullWhiteEvolver (
  104.         final String strCurrency,
  105.         final double dblSigma,
  106.         final double dblA,
  107.         final double dblStartingForwardRate)
  108.         throws Exception
  109.     {
  110.         return new SingleFactorStateEvolver (
  111.             FundingLabel.Standard (strCurrency),
  112.             dblSigma,
  113.             dblA,
  114.             new FlatUnivariate (dblStartingForwardRate),
  115.             new BoxMullerGaussian (
  116.                 0.,
  117.                 1.
  118.             )
  119.         );
  120.     }

  121.     private static void EmitNodeDetails (
  122.         final TrinomialTreeTransitionMetrics hwtm,
  123.         final TrinomialTreeNodeMetrics hwnm)
  124.         throws Exception
  125.     {
  126.         System.out.println ("\n\n\t|----------------------------------------------------------|");

  127.         System.out.println ("\t|    NODE [" + hwnm.timeIndex() + ", " + hwnm.xStochasticIndex() + "]                                           |");

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

  129.         System.out.println ("\t|        Expected Terminal X                  :  " + FormatUtil.FormatDouble (hwtm.expectedTerminalX(), 1, 6, 1.) + " |");

  130.         System.out.println ("\t|        X Variance                           :  " + FormatUtil.FormatDouble (hwtm.xVariance(), 1, 6, 1.) + " |");

  131.         System.out.println ("\t|        X Stochastic Volatility Shift        :  " + FormatUtil.FormatDouble (hwtm.xStochasticShift(), 1, 6, 1.) + " |");

  132.         System.out.println ("\t|        X Tree Stochastic Displacement Index :   " + hwtm.treeStochasticDisplacementIndex() + "        |");

  133.         System.out.println ("\t|        Probability Up                       :  " + FormatUtil.FormatDouble (hwtm.probabilityUp(), 1, 6, 1.) + " |");

  134.         System.out.println ("\t|        Probability Stay                     :  " + FormatUtil.FormatDouble (hwtm.probabilityStay(), 1, 6, 1.) + " |");

  135.         System.out.println ("\t|        Probability Down                     :  " + FormatUtil.FormatDouble (hwtm.probabilityDown(), 1, 6, 1.) + " |");

  136.         System.out.println ("\t|        Node X Value                         :  " + FormatUtil.FormatDouble (hwnm.x(), 1, 6, 1.) + " |");

  137.         System.out.println ("\t|        Node Alpha                           :  " + FormatUtil.FormatDouble (hwnm.alpha(), 1, 6, 1.) + " |");

  138.         System.out.println ("\t|        Node Short Rate                      :  " + FormatUtil.FormatDouble (hwnm.shortRate(), 1, 6, 1.) + " |");

  139.         System.out.println ("\t|----------------------------------------------------------|");
  140.     }

  141.     public static final void main (
  142.         final String[] astrArgs)
  143.         throws Exception
  144.     {
  145.         EnvManager.InitEnv ("");

  146.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  147.             2011,
  148.             DateUtil.MAY,
  149.             18
  150.         );

  151.         double dblA = 0.1;
  152.         double dblSigma = 0.01;
  153.         String strCurrency = "USD";
  154.         String[] astrTenor = {
  155.             "3M", "6M", "9M"
  156.         };
  157.         double[] adblQuote = {
  158.             0.0026, 0.00412, 0.00572
  159.         };

  160.         SingleFactorStateEvolver hw = HullWhiteEvolver (
  161.             strCurrency,
  162.             dblSigma,
  163.             dblA,
  164.             adblQuote[0]
  165.         );

  166.         TrinomialTreeTransitionMetrics hwtmp0p0 = hw.evolveTrinomialTree (
  167.             dtSpot.julian(),
  168.             dtSpot.julian(),
  169.             dtSpot.addTenor (astrTenor[0]).julian(),
  170.             null
  171.         );

  172.         EmitNodeDetails (
  173.             hwtmp0p0,
  174.             hwtmp0p0.downNodeMetrics()
  175.         );

  176.         EmitNodeDetails (
  177.             hwtmp0p0,
  178.             hwtmp0p0.stayNodeMetrics()
  179.         );

  180.         EmitNodeDetails (
  181.             hwtmp0p0,
  182.             hwtmp0p0.upNodeMetrics()
  183.         );

  184.         TrinomialTreeTransitionMetrics hwtmp1n1 = hw.evolveTrinomialTree (
  185.             dtSpot.julian(),
  186.             dtSpot.addTenor (astrTenor[0]).julian(),
  187.             dtSpot.addTenor (astrTenor[1]).julian(),
  188.             hwtmp0p0.downNodeMetrics()
  189.         );

  190.         EmitNodeDetails (
  191.             hwtmp1n1,
  192.             hwtmp1n1.downNodeMetrics()
  193.         );

  194.         EmitNodeDetails (
  195.             hwtmp1n1,
  196.             hwtmp1n1.stayNodeMetrics()
  197.         );

  198.         EmitNodeDetails (
  199.             hwtmp1n1,
  200.             hwtmp1n1.upNodeMetrics()
  201.         );

  202.         TrinomialTreeTransitionMetrics hwtmp1n0 = hw.evolveTrinomialTree (
  203.             dtSpot.julian(),
  204.             dtSpot.addTenor (astrTenor[0]).julian(),
  205.             dtSpot.addTenor (astrTenor[1]).julian(),
  206.             hwtmp0p0.stayNodeMetrics()
  207.         );

  208.         EmitNodeDetails (
  209.             hwtmp1n0,
  210.             hwtmp1n0.downNodeMetrics()
  211.         );

  212.         EmitNodeDetails (
  213.             hwtmp1n0,
  214.             hwtmp1n0.stayNodeMetrics()
  215.         );

  216.         EmitNodeDetails (
  217.             hwtmp1n0,
  218.             hwtmp1n0.upNodeMetrics()
  219.         );

  220.         TrinomialTreeTransitionMetrics hwtmp1p1 = hw.evolveTrinomialTree (
  221.             dtSpot.julian(),
  222.             dtSpot.addTenor (astrTenor[0]).julian(),
  223.             dtSpot.addTenor (astrTenor[1]).julian(),
  224.             hwtmp0p0.upNodeMetrics()
  225.         );

  226.         EmitNodeDetails (
  227.             hwtmp1p1,
  228.             hwtmp1p1.downNodeMetrics()
  229.         );

  230.         EmitNodeDetails (
  231.             hwtmp1p1,
  232.             hwtmp1p1.stayNodeMetrics()
  233.         );

  234.         EmitNodeDetails (
  235.             hwtmp1p1,
  236.             hwtmp1p1.upNodeMetrics()
  237.         );

  238.         TrinomialTreeSequenceMetrics hwsm = hw.evolveTrinomialTreeSequence (
  239.             dtSpot.julian(),
  240.             30,
  241.             2
  242.         );

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

  244.         System.out.println ("\t| SOURCE TARGET PROBABILITY METRICS |");

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

  246.         Map<String, Double> mapProbSourceTarget = hwsm.sourceTargetTransitionProbability();

  247.         for (Map.Entry<String, Double> me : mapProbSourceTarget.entrySet())
  248.             System.out.println ("\t|    " + SourceToTarget (me.getKey()) + ": " + FormatUtil.FormatDouble (me.getValue(), 1, 6, 1.) + "    |");

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

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

  251.         System.out.println ("\t| TARGET SOURCE PROBABILITY METRICS |");

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

  253.         Map<String, Double> mapProbTargetSource = hwsm.targetSourceTransitionProbability();

  254.         for (Map.Entry<String, Double> me : mapProbTargetSource.entrySet())
  255.             System.out.println ("\t|    " + SourceToTarget (me.getKey()) + ": " + FormatUtil.FormatDouble (me.getValue(), 1, 6, 1.) + "    |");

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

  257.         EnvManager.TerminateEnv();
  258.     }
  259. }