EvolutionMetrics.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>EvolutionMetrics</i> demonstrates the Construction and Usage of the Hull-White Metrics Using Hull-White
  76.  * 1F Model Dynamics for the Evolution of the Short Rate.
  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 EvolutionMetrics {

  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 ShortRateUpdate hwem)
  110.         throws Exception
  111.     {
  112.         System.out.println ("\t| [" + new JulianDate (hwem.evolutionStartDate()) + " -> " +
  113.             new JulianDate (hwem.evolutionFinishDate()) + "] => " +
  114.             FormatUtil.FormatDouble (hwem.initialShortRate(), 1, 2, 100.) + "% | " +
  115.             FormatUtil.FormatDouble (hwem.realizedFinalShortRate(), 1, 2, 100.) + "% | " +
  116.             FormatUtil.FormatDouble (hwem.expectedFinalShortRate(), 1, 2, 100.) + "% | " +
  117.             FormatUtil.FormatDouble (hwem.zeroCouponBondPrice (0.975), 1, 2, 100.) + " | " +
  118.             FormatUtil.FormatDouble (Math.sqrt (hwem.finalShortRateVariance()), 1, 2, 100.) + "% || "
  119.         );
  120.     }

  121.     public static final void main (
  122.         final String[] astrArgs)
  123.         throws Exception
  124.     {
  125.         EnvManager.InitEnv ("");

  126.         JulianDate dtSpot = DateUtil.Today();

  127.         String strCurrency = "USD";
  128.         double dblStartingShortRate = 0.05;
  129.         double dblSigma = 0.03;
  130.         double dblA = 1.;
  131.         int iNumRun = 50;

  132.         SingleFactorStateEvolver hw = HullWhiteEvolver (
  133.             strCurrency,
  134.             dblSigma,
  135.             dblA,
  136.             dblStartingShortRate
  137.         );

  138.         int iSpotDate = dtSpot.julian();

  139.         int iInitialDate = dtSpot.addMonths (1).julian();

  140.         int iFinalDate = dtSpot.addMonths (7).julian();

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

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

  143.         System.out.println ("\t|    Hull-White Scenario Evolution Metrics                                 ||");

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

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

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

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

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

  149.         System.out.println ("\t|        Initial Short Rate (%)                                            ||");

  150.         System.out.println ("\t|        Realized Final Short Rate (%)                                     ||");

  151.         System.out.println ("\t|        Expected Final Short Rate (%)                                     ||");

  152.         System.out.println ("\t|        Zero Coupon Bond Price                                            ||");

  153.         System.out.println ("\t|        Final Short Rate Variance (%)                                     ||");

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

  155.         ShortRateUpdate sruInitial = ShortRateUpdate.Create (
  156.             FundingLabel.Standard (strCurrency),
  157.             iInitialDate,
  158.             iInitialDate,
  159.             iFinalDate,
  160.             dblStartingShortRate,
  161.             dblStartingShortRate,
  162.             dblStartingShortRate,
  163.             0.,
  164.             1.
  165.         );

  166.         for (int i = 0; i < iNumRun; ++i)
  167.             DumpMetrics (
  168.                 (ShortRateUpdate) hw.evolve (
  169.                     iSpotDate,
  170.                     iInitialDate,
  171.                     iFinalDate - iInitialDate,
  172.                     sruInitial
  173.                 )
  174.             );

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

  176.         EnvManager.TerminateEnv();
  177.     }
  178. }