MultiFactorDynamics.java

  1. package org.drip.sample.hjm;

  2. import org.drip.analytics.date.*;
  3. import org.drip.analytics.definition.MarketSurface;
  4. import org.drip.dynamics.hjm.*;
  5. import org.drip.function.definition.R1ToR1;
  6. import org.drip.function.r1tor1.FlatUnivariate;
  7. import org.drip.numerical.common.FormatUtil;
  8. import org.drip.sequence.random.*;
  9. import org.drip.service.env.EnvManager;
  10. import org.drip.spline.basis.PolynomialFunctionSetParams;
  11. import org.drip.spline.params.*;
  12. import org.drip.spline.stretch.MultiSegmentSequenceBuilder;
  13. import org.drip.state.creator.ScenarioMarketSurfaceBuilder;
  14. import org.drip.state.identifier.*;

  15. /*
  16.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  17.  */

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

  80. /**
  81.  * <i>MultiFactorDynamics</i> demonstrates the Construction and Usage of the Multi-Factor Gaussian Model
  82.  * Dynamics for the Evolution of the Instantaneous Forward Rate, the Price, and the Short Rate.
  83.  *  
  84.  * <br><br>
  85.  *  <ul>
  86.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  87.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  88.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  89.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/hjm/README.md">Generic HJM Evolution</a></li>
  90.  *  </ul>
  91.  * <br><br>
  92.  *
  93.  * @author Lakshmi Krishnamurthy
  94.  */

  95. public class MultiFactorDynamics {

  96.     private static final MarketSurface FlatVolatilitySurface (
  97.         final JulianDate dtStart,
  98.         final String strCurrency,
  99.         final double dblFlatVol)
  100.         throws Exception
  101.     {
  102.         return ScenarioMarketSurfaceBuilder.CustomSplineWireSurface (
  103.             "VIEW_TARGET_VOLATILITY_SURFACE",
  104.             dtStart,
  105.             strCurrency,
  106.             new double[] {
  107.                 dtStart.julian(),
  108.                 dtStart.addYears (2).julian(),
  109.                 dtStart.addYears (4).julian(),
  110.                 dtStart.addYears (6).julian(),
  111.                 dtStart.addYears (8).julian(),
  112.                 dtStart.addYears (10).julian()
  113.             },
  114.             new double[] {
  115.                 dtStart.julian(),
  116.                 dtStart.addYears (2).julian(),
  117.                 dtStart.addYears (4).julian(),
  118.                 dtStart.addYears (6).julian(),
  119.                 dtStart.addYears (8).julian(),
  120.                 dtStart.addYears (10).julian()
  121.             },
  122.             new double[][] {
  123.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  124.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  125.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  126.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  127.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  128.                 {dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol, dblFlatVol},
  129.             },
  130.             new SegmentCustomBuilderControl (
  131.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  132.                 new PolynomialFunctionSetParams (4),
  133.                 SegmentInelasticDesignControl.Create (
  134.                     2,
  135.                     2
  136.                 ),
  137.                 null,
  138.                 null
  139.             ),
  140.             new SegmentCustomBuilderControl (
  141.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  142.                 new PolynomialFunctionSetParams (4),
  143.                 SegmentInelasticDesignControl.Create (
  144.                     2,
  145.                     2
  146.                 ),
  147.                 null,
  148.                 null
  149.             )
  150.         );
  151.     }

  152.     private static final MultiFactorStateEvolver HJMInstance (
  153.         final JulianDate dtStart,
  154.         final String strCurrency,
  155.         final MarketSurface mktSurfFlatVol1,
  156.         final MarketSurface mktSurfFlatVol2,
  157.         final MarketSurface mktSurfFlatVol3,
  158.         final R1ToR1 auForwardRate)
  159.         throws Exception
  160.     {
  161.         MultiFactorVolatility mfv = new MultiFactorVolatility (
  162.             new MarketSurface[] {
  163.                 mktSurfFlatVol1,
  164.                 mktSurfFlatVol2,
  165.                 mktSurfFlatVol3
  166.             },
  167.             new PrincipalFactorSequenceGenerator (
  168.                 new UnivariateSequenceGenerator[] {
  169.                     new BoxMullerGaussian (
  170.                         0.,
  171.                         1.
  172.                     ),
  173.                     new BoxMullerGaussian (
  174.                         0.,
  175.                         1.
  176.                     ),
  177.                     new BoxMullerGaussian (
  178.                         0.,
  179.                         1.
  180.                     )
  181.                 },
  182.                 new double[][] {
  183.                     {1.0, 0.1, 0.2},
  184.                     {0.1, 1.0, 0.2},
  185.                     {0.2, 0.1, 1.0}
  186.                 },
  187.                 3
  188.             )
  189.         );

  190.         return new MultiFactorStateEvolver (
  191.             FundingLabel.Standard (strCurrency),
  192.             ForwardLabel.Create (
  193.                 strCurrency,
  194.                 "6M"
  195.             ),
  196.             mfv,
  197.             auForwardRate
  198.         );
  199.     }

  200.     private static final void Evolve (
  201.         final MultiFactorStateEvolver hjm,
  202.         final JulianDate dtStart,
  203.         final String strCurrency,
  204.         final String strViewTenor,
  205.         final String strTargetTenor,
  206.         final double dblStartingForwardRate,
  207.         final double dblStartingPrice)
  208.         throws Exception
  209.     {
  210.         int iViewDate = dtStart.addTenor (strViewTenor).julian();

  211.         int iTargetDate = dtStart.addTenor (strTargetTenor).julian();

  212.         int iDayStep = 2;
  213.         JulianDate dtSpot = dtStart;
  214.         double dblPrice = dblStartingPrice;
  215.         double dblShortRate = dblStartingForwardRate;
  216.         double dblLIBORForwardRate = dblStartingForwardRate;
  217.         double dblInstantaneousForwardRate = dblStartingForwardRate;
  218.         double dblContinuouslyCompoundedShortRate = dblStartingForwardRate;
  219.         double dblShiftedLIBORForwardRate = dblStartingForwardRate + (365.25 / (iTargetDate - iViewDate));

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

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

  222.         System.out.println ("\t|    Heath-Jarrow-Morton Gaussian Run                                                                                           ||");

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

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

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

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

  227.         System.out.println ("\t|            Instantaneous Forward Rate (%)                                                                                     ||");

  228.         System.out.println ("\t|            Instantaneous Forward Rate - Change (%)                                                                            ||");

  229.         System.out.println ("\t|            LIBOR Forward Rate (%)                                                                                             ||");

  230.         System.out.println ("\t|            LIBOR Forward Rate - Change (%)                                                                                    ||");

  231.         System.out.println ("\t|            Shifted LIBOR Forward Rate (%)                                                                                     ||");

  232.         System.out.println ("\t|            Shifted LIBOR Forward Rate - Change (%)                                                                            ||");

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

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

  235.         System.out.println ("\t|            Continuously Compounded Short Rate (%)                                                                             ||");

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

  237.         System.out.println ("\t|            Price                                                                                                              ||");

  238.         System.out.println ("\t|            Price - Change                                                                                                     ||");

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

  240.         while (dtSpot.julian() < iViewDate) {
  241.             int iSpotDate = dtSpot.julian();

  242.             double dblIFRIncrement = hjm.instantaneousForwardRateIncrement (
  243.                 iViewDate,
  244.                 iTargetDate,
  245.                 iDayStep
  246.             );

  247.             dblInstantaneousForwardRate += dblIFRIncrement;

  248.             double dblLIBORForwardRateIncrement = hjm.liborForwardRateIncrement (
  249.                 iSpotDate,
  250.                 iViewDate,
  251.                 iTargetDate,
  252.                 dblLIBORForwardRate,
  253.                 iDayStep
  254.             );

  255.             dblLIBORForwardRate += dblLIBORForwardRateIncrement;

  256.             double dblShiftedLIBORForwardRateIncrement = hjm.shiftedLIBORForwardIncrement (
  257.                 iSpotDate,
  258.                 iViewDate,
  259.                 iTargetDate,
  260.                 dblShiftedLIBORForwardRate,
  261.                 iDayStep
  262.             );

  263.             dblShiftedLIBORForwardRate += dblShiftedLIBORForwardRateIncrement;

  264.             double dblShortRateIncrement = hjm.shortRateIncrement (
  265.                 iSpotDate,
  266.                 iViewDate,
  267.                 iDayStep
  268.             );

  269.             dblShortRate += dblShortRateIncrement;

  270.             double dblProportionalPriceIncrement = hjm.proportionalPriceIncrement (
  271.                 iViewDate,
  272.                 iTargetDate,
  273.                 dblShortRate,
  274.                 iDayStep
  275.             );

  276.             dblPrice *= (1. + dblProportionalPriceIncrement);

  277.             double dblContinuouslyCompoundedShortRateIncrement = hjm.compoundedShortRateIncrement (
  278.                 iSpotDate,
  279.                 iViewDate,
  280.                 iTargetDate,
  281.                 dblContinuouslyCompoundedShortRate,
  282.                 dblShortRate,
  283.                 iDayStep
  284.             );

  285.             dblContinuouslyCompoundedShortRate += dblContinuouslyCompoundedShortRateIncrement;

  286.             System.out.println ("\t| [" + dtSpot + "] = " +
  287.                 FormatUtil.FormatDouble (dblInstantaneousForwardRate, 1, 2, 100.) + "% | " +
  288.                 FormatUtil.FormatDouble (dblIFRIncrement, 1, 2, 100.) + "% || " +
  289.                 FormatUtil.FormatDouble (dblLIBORForwardRate, 1, 2, 100.) + "% | " +
  290.                 FormatUtil.FormatDouble (dblLIBORForwardRateIncrement, 1, 2, 100.) + "% || " +
  291.                 FormatUtil.FormatDouble (dblShiftedLIBORForwardRate, 1, 4, 1.) + " | " +
  292.                 FormatUtil.FormatDouble (dblShiftedLIBORForwardRateIncrement, 1, 2, 100.) + "% || " +
  293.                 FormatUtil.FormatDouble (dblShortRate, 1, 2, 100.) + "% | " +
  294.                 FormatUtil.FormatDouble (dblShortRateIncrement, 1, 2, 100.) + "% || " +
  295.                 FormatUtil.FormatDouble (dblContinuouslyCompoundedShortRate, 1, 2, 100.) + "% | " +
  296.                 FormatUtil.FormatDouble (dblContinuouslyCompoundedShortRateIncrement, 1, 2, 100.) + "% || " +
  297.                 FormatUtil.FormatDouble (dblPrice, 2, 2, 100.) + " | " +
  298.                 FormatUtil.FormatDouble (dblProportionalPriceIncrement, 1, 2, 100.) + " || "
  299.             );

  300.             dtSpot = dtSpot.addBusDays (
  301.                 iDayStep,
  302.                 strCurrency
  303.             );
  304.         }

  305.         System.out.println ("\t|-------------------------------------------------------------------------------------------------------------------------------||");
  306.     }

  307.     public static final void main (
  308.         final String[] astrArgs)
  309.         throws Exception
  310.     {
  311.         EnvManager.InitEnv ("");

  312.         String strCurrency = "USD";

  313.         JulianDate dtSpot = DateUtil.Today();

  314.         double dblFlatVol1 = 0.01;
  315.         double dblFlatVol2 = 0.02;
  316.         double dblFlatVol3 = 0.03;
  317.         double dblFlatForwardRate = 0.05;
  318.         double dblStartingPrice = 0.9875;

  319.         MarketSurface mktSurfFlatVol1 = FlatVolatilitySurface (
  320.             dtSpot,
  321.             strCurrency,
  322.             dblFlatVol1
  323.         );

  324.         MarketSurface mktSurfFlatVol2 = FlatVolatilitySurface (
  325.             dtSpot,
  326.             strCurrency,
  327.             dblFlatVol2
  328.         );

  329.         MarketSurface mktSurfFlatVol3 = FlatVolatilitySurface (
  330.             dtSpot,
  331.             strCurrency,
  332.             dblFlatVol3
  333.         );

  334.         MultiFactorStateEvolver hjm = HJMInstance (
  335.             dtSpot,
  336.             strCurrency,
  337.             mktSurfFlatVol1,
  338.             mktSurfFlatVol2,
  339.             mktSurfFlatVol3,
  340.             new FlatUnivariate (dblFlatForwardRate)
  341.         );

  342.         Evolve (
  343.             hjm,
  344.             dtSpot,
  345.             strCurrency,
  346.             "3M",
  347.             "6M",
  348.             dblFlatForwardRate,
  349.             dblStartingPrice
  350.         );

  351.         EnvManager.TerminateEnv();
  352.     }
  353. }