ShortRateDynamics.java

  1. package org.drip.sample.hullwhite;

  2. import org.drip.analytics.date.*;
  3. import org.drip.dynamics.hullwhite.SingleFactorStateEvolver;
  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>ShortRateDynamics</i> demonstrates the Construction and Usage of the Hull-White 1F Model Dynamics for
  76.  * 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 ShortRateDynamics {

  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 ShortRateEvolution (
  109.         final SingleFactorStateEvolver hw,
  110.         final JulianDate dtSpot,
  111.         final String strCurrency,
  112.         final String strViewTenor,
  113.         final double dblStartingShortRate)
  114.         throws Exception
  115.     {
  116.         int iDayStep = 2;
  117.         JulianDate dtView = dtSpot;
  118.         double dblShortRate = dblStartingShortRate;

  119.         int iSpotDate = dtSpot.julian();

  120.         int iEndDate = dtSpot.addTenor (strViewTenor).julian();

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

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

  123.         System.out.println ("\t|    Hull-White Evolution Run                          ||");

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

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

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

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

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

  129.         System.out.println ("\t|        Short Rate - Change (%)                       ||");

  130.         System.out.println ("\t|        Alpha (%)                                     ||");

  131.         System.out.println ("\t|        Theta (%)                                     ||");

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

  133.         while (dtView.julian() < iEndDate) {
  134.             int iViewDate = dtView.julian();

  135.             double dblAlpha = hw.alpha (
  136.                 iSpotDate,
  137.                 iViewDate
  138.             );

  139.             double dblTheta = hw.theta (
  140.                 iSpotDate,
  141.                 iViewDate
  142.             );

  143.             double dblShortRateIncrement = hw.shortRateIncrement (
  144.                 iSpotDate,
  145.                 iViewDate,
  146.                 dblShortRate,
  147.                 iDayStep
  148.             );

  149.             dblShortRate += dblShortRateIncrement;

  150.             System.out.println ("\t| [" + dtView + "] = " +
  151.                 FormatUtil.FormatDouble (dblShortRate, 1, 2, 100.) + "% | " +
  152.                 FormatUtil.FormatDouble (dblShortRateIncrement, 1, 2, 100.) + "% | " +
  153.                 FormatUtil.FormatDouble (dblAlpha, 1, 4, 100.) + "% | " +
  154.                 FormatUtil.FormatDouble (dblTheta, 1, 4, 100.) + "% || "
  155.             );

  156.             dtView = dtView.addBusDays (
  157.                 iDayStep,
  158.                 strCurrency
  159.             );
  160.         }

  161.         System.out.println ("\t|------------------------------------------------------||");
  162.     }

  163.     public static final void main (
  164.         final String[] astrArgs)
  165.         throws Exception
  166.     {
  167.         EnvManager.InitEnv ("");

  168.         JulianDate dtSpot = DateUtil.Today();

  169.         String strCurrency = "USD";
  170.         double dblStartingShortRate = 0.05;
  171.         double dblSigma = 0.05;
  172.         double dblA = 1.;

  173.         SingleFactorStateEvolver hw = HullWhiteEvolver (
  174.             strCurrency,
  175.             dblSigma,
  176.             dblA,
  177.             dblStartingShortRate
  178.         );

  179.         ShortRateEvolution (
  180.             hw,
  181.             dtSpot,
  182.             strCurrency,
  183.             "4M",
  184.             dblStartingShortRate
  185.         );

  186.         EnvManager.TerminateEnv();
  187.     }
  188. }