StandardHestonPricingMeasures.java

  1. package org.drip.sample.stochasticvolatility;

  2. import org.drip.analytics.date.*;
  3. import org.drip.function.r1tor1.FlatUnivariate;
  4. import org.drip.market.otc.*;
  5. import org.drip.numerical.fourier.PhaseAdjuster;
  6. import org.drip.param.pricer.HestonOptionPricerParams;
  7. import org.drip.param.valuation.*;
  8. import org.drip.pricer.option.*;
  9. import org.drip.product.creator.SingleStreamComponentBuilder;
  10. import org.drip.product.definition.CalibratableComponent;
  11. import org.drip.product.option.EuropeanCallPut;
  12. import org.drip.product.rates.*;
  13. import org.drip.service.env.EnvManager;
  14. import org.drip.state.creator.ScenarioDiscountCurveBuilder;
  15. import org.drip.state.discount.MergedDiscountForwardCurve;
  16. import org.drip.state.identifier.ForwardLabel;

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

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

  64. /**
  65.  * StandardHestonPricingMeasures contains an illustration of the Stochastic Volatility based Pricing
  66.  *  Algorithm of an European Call Using the Heston Algorithm.
  67.  *
  68.  * @author Lakshmi Krishnamurthy
  69.  */

  70. public class StandardHestonPricingMeasures {

  71.     private static final FixFloatComponent OTCIRS (
  72.         final JulianDate dtSpot,
  73.         final String strCurrency,
  74.         final String strMaturityTenor,
  75.         final double dblCoupon)
  76.     {
  77.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  78.             strCurrency,
  79.             "ALL",
  80.             strMaturityTenor,
  81.             "MAIN"
  82.         );

  83.         return ffConv.createFixFloatComponent (
  84.             dtSpot,
  85.             strMaturityTenor,
  86.             dblCoupon,
  87.             0.,
  88.             1.
  89.         );
  90.     }

  91.     /*
  92.      * Construct the Array of Deposit Instruments from the given set of parameters
  93.      *
  94.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  95.      */

  96.     private static final CalibratableComponent[] DepositInstrumentsFromMaturityDays (
  97.         final JulianDate dtEffective,
  98.         final int[] aiDay,
  99.         final int iNumFutures,
  100.         final String strCurrency)
  101.         throws Exception
  102.     {
  103.         CalibratableComponent[] aCalibComp = new CalibratableComponent[aiDay.length + iNumFutures];

  104.         for (int i = 0; i < aiDay.length; ++i)
  105.             aCalibComp[i] = SingleStreamComponentBuilder.Deposit (
  106.                 dtEffective,
  107.                 dtEffective.addBusDays (
  108.                     aiDay[i],
  109.                     strCurrency
  110.                 ),
  111.                 ForwardLabel.Create (
  112.                     strCurrency,
  113.                     "3M"
  114.                 )
  115.             );

  116.         CalibratableComponent[] aEDF = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  117.             dtEffective,
  118.             iNumFutures,
  119.             strCurrency
  120.         );

  121.         for (int i = aiDay.length; i < aiDay.length + iNumFutures; ++i)
  122.             aCalibComp[i] = aEDF[i - aiDay.length];

  123.         return aCalibComp;
  124.     }

  125.     /*
  126.      * Construct the Array of Swap Instruments from the given set of parameters
  127.      *
  128.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  129.      */

  130.     private static final FixFloatComponent[] SwapInstrumentsFromMaturityTenor (
  131.         final JulianDate dtSpot,
  132.         final String strCurrency,
  133.         final String[] astrMaturityTenor,
  134.         final double[] adblCoupon)
  135.         throws Exception
  136.     {
  137.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  138.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  139.             FixFloatComponent irs = OTCIRS (
  140.                 dtSpot,
  141.                 strCurrency,
  142.                 astrMaturityTenor[i],
  143.                 adblCoupon[i]
  144.             );

  145.             irs.setPrimaryCode ("IRS." + astrMaturityTenor[i] + "." + strCurrency);

  146.             aIRS[i] = irs;
  147.         }

  148.         return aIRS;
  149.     }

  150.     /*
  151.      * Construct the discount curve using the following steps:
  152.      *  - Construct the array of cash instruments and their quotes.
  153.      *  - Construct the array of swap instruments and their quotes.
  154.      *  - Construct a shape preserving and smoothing KLK Hyperbolic Spline from the cash/swap instruments.
  155.      *
  156.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  157.      */

  158.     private static final MergedDiscountForwardCurve MakeDC (
  159.         final JulianDate dtSpot,
  160.         final String strCurrency)
  161.         throws Exception
  162.     {
  163.         /*
  164.          * Construct the array of Deposit instruments and their quotes.
  165.          */

  166.         CalibratableComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  167.             dtSpot,
  168.             new int[] {
  169.                 1, 2, 3, 7, 14, 21, 30, 60
  170.             },
  171.             0,
  172.             strCurrency
  173.         );

  174.         double[] adblDepositQuote = new double[] {
  175.             0.01200, 0.01200, 0.01200, 0.01450, 0.01550, 0.01600, 0.01660, 0.01850
  176.         };

  177.         String[] astrDepositManifestMeasure = new String[] {
  178.             "ForwardRate", "ForwardRate", "ForwardRate", "ForwardRate", "ForwardRate", "ForwardRate", "ForwardRate", "ForwardRate"
  179.         };

  180.         /*
  181.          * Construct the array of Swap instruments and their quotes.
  182.          */

  183.         double[] adblSwapQuote = new double[] {
  184.             0.02604,    //  4Y
  185.             0.02808,    //  5Y
  186.             0.02983,    //  6Y
  187.             0.03136,    //  7Y
  188.             0.03268,    //  8Y
  189.             0.03383,    //  9Y
  190.             0.03488,    // 10Y
  191.             0.03583,    // 11Y
  192.             0.03668,    // 12Y
  193.             0.03833,    // 15Y
  194.             0.03854,    // 20Y
  195.             0.03672,    // 25Y
  196.             0.03510,    // 30Y
  197.             0.03266,    // 40Y
  198.             0.03145     // 50Y
  199.         };

  200.         String[] astrSwapManifestMeasure = new String[] {
  201.             "SwapRate",    //  4Y
  202.             "SwapRate",    //  5Y
  203.             "SwapRate",    //  6Y
  204.             "SwapRate",    //  7Y
  205.             "SwapRate",    //  8Y
  206.             "SwapRate",    //  9Y
  207.             "SwapRate",    // 10Y
  208.             "SwapRate",    // 11Y
  209.             "SwapRate",    // 12Y
  210.             "SwapRate",    // 15Y
  211.             "SwapRate",    // 20Y
  212.             "SwapRate",    // 25Y
  213.             "SwapRate",    // 30Y
  214.             "SwapRate",    // 40Y
  215.             "SwapRate"     // 50Y
  216.         };

  217.         CalibratableComponent[] aSwapComp = SwapInstrumentsFromMaturityTenor (
  218.             dtSpot,
  219.             strCurrency,
  220.             new java.lang.String[] {
  221.                 "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y", "40Y", "50Y"
  222.             },
  223.             adblSwapQuote
  224.         );

  225.         /*
  226.          * Construct a shape preserving and smoothing KLK Hyperbolic Spline from the cash/swap instruments.
  227.          */

  228.         return ScenarioDiscountCurveBuilder.CubicKLKHyperbolicDFRateShapePreserver (
  229.             "KLK_HYPERBOLIC_SHAPE_TEMPLATE",
  230.             new ValuationParams (
  231.                 dtSpot,
  232.                 dtSpot,
  233.                 strCurrency
  234.             ),
  235.             aDepositComp,
  236.             adblDepositQuote,
  237.             astrDepositManifestMeasure,
  238.             aSwapComp,
  239.             adblSwapQuote,
  240.             astrSwapManifestMeasure,
  241.             true
  242.         );
  243.     }

  244.     public static final void main (
  245.         final String[] astrArgs)
  246.         throws Exception
  247.     {
  248.         /*
  249.          * Initialize the Credit Analytics Library
  250.          */

  251.         EnvManager.InitEnv ("");

  252.         JulianDate dtToday = DateUtil.Today();

  253.         ValuationParams valParams = new ValuationParams (
  254.             dtToday,
  255.             dtToday,
  256.             "USD"
  257.         );

  258.         /*
  259.          * Construct the Discount Curve using its instruments and quotes
  260.          */

  261.         MergedDiscountForwardCurve dc = MakeDC (
  262.             dtToday,
  263.             "USD"
  264.         );

  265.         JulianDate dtMaturity = dtToday.addTenor ("6M");

  266.         double dblStrike = 1.;

  267.         EuropeanCallPut option = new EuropeanCallPut (
  268.             dtMaturity,
  269.             dblStrike
  270.         );

  271.         double dblSpot = 1.;

  272.         double dblRho = 0.3;
  273.         double dblKappa = 1.;
  274.         double dblSigma = 0.5;
  275.         double dblTheta = 0.2;
  276.         double dblLambda = 0.;
  277.         double dblSpotVolatility = 0.2;

  278.         HestonOptionPricerParams fphp = new HestonOptionPricerParams (
  279.             HestonStochasticVolatilityAlgorithm.PAYOFF_TRANSFORM_SCHEME_HESTON_1993,
  280.             dblRho,             // Rho
  281.             dblKappa,           // Kappa
  282.             dblSigma,           // Sigma
  283.             dblTheta,           // Theta
  284.             dblLambda,          // Lambda
  285.             PhaseAdjuster.MULTI_VALUE_BRANCH_POWER_PHASE_TRACKER_KAHL_JACKEL // Indicates Apply Phase Tracking Adjustment for Log + Power
  286.         );

  287.         FokkerPlanckGenerator fpg = new HestonStochasticVolatilityAlgorithm (
  288.             fphp                // FP Heston Parameters
  289.         );

  290.         System.out.println (
  291.             option.value (
  292.                 valParams,
  293.                 dblSpot,
  294.                 false,
  295.                 dc,
  296.                 new FlatUnivariate (dblSpotVolatility),
  297.                 fpg
  298.             )
  299.         );
  300.     }
  301. }