CallVolSplineSurface.java

  1. package org.drip.sample.stochasticvolatility;

  2. import org.drip.analytics.date.*;
  3. import org.drip.analytics.definition.MarketSurface;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.numerical.fourier.PhaseAdjuster;
  6. import org.drip.param.pricer.HestonOptionPricerParams;
  7. import org.drip.pricer.option.HestonStochasticVolatilityAlgorithm;
  8. import org.drip.spline.basis.*;
  9. import org.drip.spline.params.*;
  10. import org.drip.spline.stretch.MultiSegmentSequenceBuilder;
  11. import org.drip.state.creator.ScenarioMarketSurfaceBuilder;

  12. /*
  13.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  14.  */

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

  59. /**
  60.  * CallVolSplineSurface demonstrates the spline volatility surface generator by a stochastic volatility
  61.  *  algorithm, i.e., in this case the Heston 1993 algorithm.
  62.  *
  63.  * @author Lakshmi Krishnamurthy
  64.  */

  65. public class CallVolSplineSurface {
  66.     private static final SegmentCustomBuilderControl CubicPolySCBC()
  67.         throws Exception
  68.     {
  69.         return new SegmentCustomBuilderControl (
  70.             MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  71.             new PolynomialFunctionSetParams (4),
  72.             SegmentInelasticDesignControl.Create (
  73.                 2,
  74.                 2
  75.             ),
  76.             null,
  77.             null
  78.         );
  79.     }

  80.     private static final SegmentCustomBuilderControl KLKHyperbolicSCBC (
  81.         final double dblTension)
  82.         throws Exception
  83.     {
  84.         return new SegmentCustomBuilderControl (
  85.             MultiSegmentSequenceBuilder.BASIS_SPLINE_KLK_HYPERBOLIC_TENSION,
  86.             new ExponentialTensionSetParams (dblTension),
  87.             SegmentInelasticDesignControl.Create (
  88.                 2,
  89.                 2
  90.             ),
  91.             null,
  92.             null
  93.         );
  94.     }

  95.     private static final void EvaluateSplineSurface (
  96.         final MarketSurface volSurface,
  97.         final double[] adblStrikeATMFactor,
  98.         final String[] astrMaturityTenor)
  99.         throws Exception
  100.     {
  101.         System.out.println ("\t|------------------------------------------------------------|");

  102.         System.out.print ("\t|------------------------------------------------------------|\n\t|  ATM/TTE  =>");

  103.         for (String strMaturity : astrMaturityTenor)
  104.             System.out.print ("    " + strMaturity + "  ");

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

  106.         for (double dblStrike : adblStrikeATMFactor) {
  107.             System.out.print ("\t|  " + FormatUtil.FormatDouble (dblStrike, 1, 2, 1.) + "    =>");

  108.             for (String strMaturity : astrMaturityTenor)
  109.                 System.out.print ("  " + FormatUtil.FormatDouble (volSurface.node (dblStrike, strMaturity), 2, 2, 100.) + "%");

  110.             System.out.print ("  |\n");
  111.         }

  112.         System.out.println ("\t|------------------------------------------------------------|");
  113.     }

  114.     public static final void main (
  115.         final String[] astrArgs)
  116.         throws Exception
  117.     {
  118.         JulianDate dtStart = DateUtil.Today();

  119.         double[] adblStrikeATMFactorCalib = new double[] {
  120.             0.8, 0.9, 1.0, 1.1, 1.2
  121.         };
  122.         String[] astrMaturityTenorCalib = new String[] {
  123.             "12M", "24M", "36M", "48M", "60M"
  124.         };

  125.         double dblRho = 0.3;
  126.         double dblKappa = 1.;
  127.         double dblSigma = 0.5;
  128.         double dblTheta = 0.2;
  129.         double dblLambda = 0.;

  130.         HestonOptionPricerParams hopp = new HestonOptionPricerParams (
  131.             HestonStochasticVolatilityAlgorithm.PAYOFF_TRANSFORM_SCHEME_AMST_2007,
  132.             dblRho,
  133.             dblKappa,
  134.             dblSigma,
  135.             dblTheta,
  136.             dblLambda,
  137.             PhaseAdjuster.MULTI_VALUE_BRANCH_POWER_PHASE_TRACKER_KAHL_JACKEL
  138.         );

  139.         MarketSurface priceSurfCubicPoly = ScenarioMarketSurfaceBuilder.HestonRunMarketSurface (
  140.             "HESTON1993_CUBICPOLY_VOLATILITY_SURFACE",
  141.             dtStart,
  142.             "USD",
  143.             0.01,
  144.             1.,
  145.             false,
  146.             0.20,
  147.             adblStrikeATMFactorCalib,
  148.             astrMaturityTenorCalib,
  149.             hopp,
  150.             false,
  151.             CubicPolySCBC(),
  152.             CubicPolySCBC()
  153.         );

  154.         EvaluateSplineSurface (
  155.             priceSurfCubicPoly,
  156.             adblStrikeATMFactorCalib,
  157.             astrMaturityTenorCalib
  158.         );

  159.         EvaluateSplineSurface (
  160.             priceSurfCubicPoly,
  161.             new double[] {
  162.                 0.500, 0.700, 0.850, 1.000, 1.150, 1.300, 1.500
  163.             },
  164.             new String[] {
  165.                 "06M", "21M", "36M", "51M", "66M"
  166.             }
  167.         );

  168.         MarketSurface priceSurfKLKHyper = ScenarioMarketSurfaceBuilder.HestonRunMarketSurface (
  169.             "HESTON1993_KLKHYPER_VOLATILITY_SURFACE",
  170.             dtStart,
  171.             "USD",
  172.             0.01,
  173.             1.,
  174.             false,
  175.             0.20,
  176.             adblStrikeATMFactorCalib,
  177.             astrMaturityTenorCalib,
  178.             hopp,
  179.             false,
  180.             KLKHyperbolicSCBC (4.),
  181.             KLKHyperbolicSCBC (2.)
  182.         );

  183.         EvaluateSplineSurface (
  184.             priceSurfKLKHyper,
  185.             adblStrikeATMFactorCalib,
  186.             astrMaturityTenorCalib
  187.         );

  188.         EvaluateSplineSurface (
  189.             priceSurfKLKHyper,
  190.             new double[] {
  191.                 0.500, 0.700, 0.850, 1.000, 1.150, 1.300, 1.500
  192.             },
  193.             new String[] {
  194.                 "06M", "21M", "36M", "51M", "66M"
  195.             }
  196.         );
  197.     }
  198. }