ForwardRateEvolution.java

  1. package org.drip.sample.sabr;

  2. import org.drip.analytics.date.*;
  3. import org.drip.dynamics.sabr.*;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.sequence.random.BoxMullerGaussian;
  6. import org.drip.service.env.EnvManager;
  7. import org.drip.state.identifier.ForwardLabel;

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

  11. /*!
  12.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  16.  *
  17.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  18.  *      libraries targeting analysts and developers
  19.  *      https://lakshmidrip.github.io/DRIP/
  20.  *  
  21.  *  DRIP is composed of four main libraries:
  22.  *  
  23.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  24.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  25.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  26.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  27.  *
  28.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  29.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  30.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  31.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  32.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  33.  *
  34.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  35.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  36.  *
  37.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  38.  *
  39.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  40.  *
  41.  *  Licensed under the Apache License, Version 2.0 (the "License");
  42.  *      you may not use this file except in compliance with the License.
  43.  *  
  44.  *  You may obtain a copy of the License at
  45.  *      http://www.apache.org/licenses/LICENSE-2.0
  46.  *  
  47.  *  Unless required by applicable law or agreed to in writing, software
  48.  *      distributed under the License is distributed on an "AS IS" BASIS,
  49.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  50.  *  
  51.  *  See the License for the specific language governing permissions and
  52.  *      limitations under the License.
  53.  */

  54. /**
  55.  * ForwardRateEvolution demonstrates the Construction and Usage of the SABR Model Dynamics for the Evolution
  56.  *  of Forward Rate.
  57.  *
  58.  * @author Lakshmi Krishnamurthy
  59.  */

  60. public class ForwardRateEvolution {

  61.     private static StochasticVolatilityStateEvolver SABREvolver (
  62.         final double dblBeta,
  63.         final double dblRho,
  64.         final double dblVolatilityOfVolatility)
  65.         throws Exception
  66.     {
  67.         return new StochasticVolatilityStateEvolver (
  68.             ForwardLabel.Create (
  69.                 "USD",
  70.                 "6M"
  71.             ),
  72.             dblBeta,
  73.             dblRho,
  74.             dblVolatilityOfVolatility,
  75.             new BoxMullerGaussian (
  76.                 0.,
  77.                 1.
  78.             ),
  79.             new BoxMullerGaussian (
  80.                 0.,
  81.                 1.
  82.             )
  83.         );
  84.     }

  85.     private static void SABREvolution (
  86.         final StochasticVolatilityStateEvolver seSABR1,
  87.         final StochasticVolatilityStateEvolver seSABR2,
  88.         final StochasticVolatilityStateEvolver seSABR3,
  89.         final int iSpotDate,
  90.         final int iTerminalDate,
  91.         final ForwardRateUpdate lsqmInitial1,
  92.         final ForwardRateUpdate lsqmInitial2,
  93.         final ForwardRateUpdate lsqmInitial3)
  94.         throws Exception
  95.     {
  96.         int iDayStep = 2;
  97.         int iDate = iSpotDate;
  98.         ForwardRateUpdate lsqm1 = lsqmInitial1;
  99.         ForwardRateUpdate lsqm2 = lsqmInitial2;
  100.         ForwardRateUpdate lsqm3 = lsqmInitial3;

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

  102.         System.out.println ("\t||     SABR  EVOLUTION  DYNAMICS                                                   ||");

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

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

  105.         System.out.println ("\t||        Forward Rate (%)  - Gaussian (beta = 0.0)                                ||");

  106.         System.out.println ("\t||        Forward Rate Vol (%)  - Gaussian (beta = 0.0)                            ||");

  107.         System.out.println ("\t||        Forward Rate (%)  - beta = 0.5                                           ||");

  108.         System.out.println ("\t||        Forward Rate Vol (%)  - beta = 0.5                                       ||");

  109.         System.out.println ("\t||        Forward Rate (%)  - Lognormal (beta = 1.0)                               ||");

  110.         System.out.println ("\t||        Forward Rate Vol (%)  - Lognormal (beta = 1.0)                           ||");

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

  112.         while (iDate < iTerminalDate) {
  113.             lsqm1 = (ForwardRateUpdate) seSABR1.evolve (
  114.                 iSpotDate,
  115.                 iDate,
  116.                 iDayStep,
  117.                 lsqm1
  118.             );

  119.             lsqm2 = (ForwardRateUpdate) seSABR2.evolve (
  120.                 iSpotDate,
  121.                 iDate,
  122.                 iDayStep,
  123.                 lsqm2
  124.             );

  125.             lsqm3 = (ForwardRateUpdate) seSABR3.evolve (
  126.                 iSpotDate,
  127.                 iDate,
  128.                 iDayStep,
  129.                 lsqm3
  130.             );

  131.             System.out.println (
  132.                 "\t|| " + new JulianDate (iDate) + " => " +
  133.                 FormatUtil.FormatDouble (lsqm1.forwardRate(), 1, 4, 100.) + " % | " +
  134.                 FormatUtil.FormatDouble (lsqm1.forwardRateVolatility(), 1, 2, 100.) + " % || " +
  135.                 FormatUtil.FormatDouble (lsqm2.forwardRate(), 1, 4, 100.) + " % | " +
  136.                 FormatUtil.FormatDouble (lsqm2.forwardRateVolatility(), 1, 1, 100.) + " % || " +
  137.                 FormatUtil.FormatDouble (lsqm3.forwardRate(), 1, 4, 100.) + " % | " +
  138.                 FormatUtil.FormatDouble (lsqm3.forwardRateVolatility(), 1, 1, 100.) + " % ||"
  139.             );

  140.             iDate += iDayStep;
  141.         }

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

  144.     public static void main (
  145.         final String[] astrArgs)
  146.         throws Exception
  147.     {
  148.         EnvManager.InitEnv ("");

  149.         JulianDate dtSpot = DateUtil.Today();

  150.         double dblRho = 0.1;
  151.         double dblForwardRate = 0.04;
  152.         double dblVolatilityOfVolatility = 0.59;
  153.         String strViewTenor = "3M";
  154.         double[] adblBeta = {
  155.             0.00, 0.50, 1.00
  156.         };
  157.         double[] adblForwardRateVolatility = {
  158.             0.03, 0.26, 0.51
  159.         };

  160.         int iViewDate = dtSpot.addTenor (strViewTenor).julian();

  161.         StochasticVolatilityStateEvolver seSABR1 = SABREvolver (
  162.             adblBeta[0],
  163.             dblRho,
  164.             dblVolatilityOfVolatility
  165.         );

  166.         StochasticVolatilityStateEvolver seSABR2 = SABREvolver (
  167.             adblBeta[1],
  168.             dblRho,
  169.             dblVolatilityOfVolatility
  170.         );

  171.         StochasticVolatilityStateEvolver seSABR3 = SABREvolver (
  172.             adblBeta[2],
  173.             dblRho,
  174.             dblVolatilityOfVolatility
  175.         );

  176.         ForwardRateUpdate lsqmInitial1 = ForwardRateUpdate.Create (
  177.             ForwardLabel.Create (
  178.                 "USD",
  179.                 "6M"
  180.             ),
  181.             dtSpot.julian(),
  182.             dtSpot.julian(),
  183.             iViewDate,
  184.             dblForwardRate,
  185.             0.,
  186.             adblForwardRateVolatility[0],
  187.             0.
  188.         );

  189.         ForwardRateUpdate lsqmInitial2 = ForwardRateUpdate.Create (
  190.             ForwardLabel.Create (
  191.                 "USD",
  192.                 "6M"
  193.             ),
  194.             dtSpot.julian(),
  195.             dtSpot.julian(),
  196.             iViewDate,
  197.             dblForwardRate,
  198.             0.,
  199.             adblForwardRateVolatility[1],
  200.             0.
  201.         );

  202.         ForwardRateUpdate lsqmInitial3 = ForwardRateUpdate.Create (
  203.             ForwardLabel.Create (
  204.                 "USD",
  205.                 "6M"
  206.             ),
  207.             dtSpot.julian(),
  208.             dtSpot.julian(),
  209.             iViewDate,
  210.             dblForwardRate,
  211.             0.,
  212.             adblForwardRateVolatility[2],
  213.             0.
  214.         );

  215.         SABREvolution (
  216.             seSABR1,
  217.             seSABR2,
  218.             seSABR3,
  219.             dtSpot.julian(),
  220.             iViewDate,
  221.             lsqmInitial1,
  222.             lsqmInitial2,
  223.             lsqmInitial3
  224.         );
  225.     }
  226. }