MarketSurfaceTermStructure.java

  1. package org.drip.sample.option;

  2. import java.util.*;

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

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

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

  60. /**
  61.  * MarketSurfaceTermStructure contains an illustration of the Creation and Usage of the Strike Anchored and
  62.  *  Maturity Anchored Term Structures extracted from the given Market Surface.
  63.  *
  64.  * @author Lakshmi Krishnamurthy
  65.  */

  66. public class MarketSurfaceTermStructure {

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

  81.     private static final void EvaluateSplineSurface (
  82.         final MarketSurface mktSurf,
  83.         final double[] adblStrikeATMFactor,
  84.         final String[] astrMaturityTenor)
  85.         throws Exception
  86.     {
  87.         System.out.println ("\t|------------------------------------------------------------|");

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

  89.         for (String strMaturity : astrMaturityTenor)
  90.             System.out.print ("    " + strMaturity + "  ");

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

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

  94.             for (String strMaturity : astrMaturityTenor)
  95.                 System.out.print ("  " + FormatUtil.FormatDouble (mktSurf.node (dblStrike, strMaturity), 2, 2, 100.) + "%");

  96.             System.out.print ("  |\n");
  97.         }

  98.         System.out.println ("\t|------------------------------------------------------------|");
  99.     }

  100.     private static final void EvaluateStrikeTermStructure (
  101.         final MarketSurface mktSurf,
  102.         final double[] adblStrikeATMFactor,
  103.         final String[] astrMaturityTenor)
  104.         throws Exception
  105.     {
  106.         System.out.println ("\n\t|--------- STRIKE TERM STRUCTURE FROM MARKET SURFACE --------|");

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

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

  109.         for (String strMaturity : astrMaturityTenor)
  110.             System.out.print ("    " + strMaturity + "  ");

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

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

  114.             NodeStructure tsStrike = mktSurf.xAnchorTermStructure (dblStrike);

  115.             for (String strMaturity : astrMaturityTenor)
  116.                 System.out.print ("  " + FormatUtil.FormatDouble (tsStrike.node (strMaturity), 2, 2, 100.) + "%");

  117.             System.out.print ("  |\n");
  118.         }

  119.         System.out.println ("\t|------------------------------------------------------------|");
  120.     }

  121.     private static final void EvaluateMaturityTermStructure (
  122.         final MarketSurface mktSurf,
  123.         final double[] adblStrikeATMFactor,
  124.         final String[] astrMaturityTenor)
  125.         throws Exception
  126.     {
  127.         System.out.println ("\n\t|-------- MATURITY TERM STRUCTURE FROM MARKET SURFACE -------|");

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

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

  130.         Map<String, NodeStructure> mapMaturityTS = new TreeMap<String, NodeStructure>();

  131.         for (String strMaturity : astrMaturityTenor) {
  132.             System.out.print ("    " + strMaturity + "  ");

  133.             mapMaturityTS.put (strMaturity, mktSurf.maturityAnchorTermStructure (strMaturity));
  134.         }

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

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

  138.             for (String strMaturity : astrMaturityTenor) {
  139.                 NodeStructure tsMaturity = mapMaturityTS.get (strMaturity);

  140.                 System.out.print ("  " + FormatUtil.FormatDouble (tsMaturity.node ((int) dblStrike), 2, 2, 100.) + "%");
  141.             }

  142.             System.out.print ("  |\n");
  143.         }

  144.         System.out.println ("\t|------------------------------------------------------------|");
  145.     }

  146.     public static final void main (
  147.         final String[] astrArgs)
  148.         throws Exception
  149.     {
  150.         JulianDate dtStart = DateUtil.Today();

  151.         double[] adblStrikeATMFactorCalib = new double[] {
  152.             0.8, 0.9, 1.0, 1.1, 1.2
  153.         };
  154.         String[] astrMaturityTenorCalib = new String[] {
  155.             "12M", "24M", "36M", "48M", "60M"
  156.         };

  157.         double dblRho = 0.3;
  158.         double dblKappa = 1.;
  159.         double dblSigma = 0.5;
  160.         double dblTheta = 0.2;
  161.         double dblLambda = 0.;

  162.         HestonOptionPricerParams hopp = new HestonOptionPricerParams (
  163.             HestonStochasticVolatilityAlgorithm.PAYOFF_TRANSFORM_SCHEME_AMST_2007,
  164.             dblRho,
  165.             dblKappa,
  166.             dblSigma,
  167.             dblTheta,
  168.             dblLambda,
  169.             PhaseAdjuster.MULTI_VALUE_BRANCH_POWER_PHASE_TRACKER_KAHL_JACKEL
  170.         );

  171.         MarketSurface priceSurfCubicPoly = ScenarioMarketSurfaceBuilder.HestonRunMarketSurface (
  172.             "HESTON1993_CUBICPOLY_CALLPRICE_SURFACE",
  173.             dtStart,
  174.             "USD",
  175.             0.01,
  176.             1.,
  177.             false,
  178.             0.20,
  179.             adblStrikeATMFactorCalib,
  180.             astrMaturityTenorCalib,
  181.             hopp,
  182.             true,
  183.             CubicPolySCBC(),
  184.             CubicPolySCBC()
  185.         );

  186.         EvaluateSplineSurface (
  187.             priceSurfCubicPoly,
  188.             adblStrikeATMFactorCalib,
  189.             astrMaturityTenorCalib
  190.         );

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

  196.         EvaluateStrikeTermStructure (
  197.             priceSurfCubicPoly,
  198.             new double[] {0.500, 0.700, 0.850, 1.000, 1.150, 1.300, 1.500},
  199.             new String[] {"06M", "21M", "36M", "51M", "66M"}
  200.         );

  201.         EvaluateMaturityTermStructure (
  202.             priceSurfCubicPoly,
  203.             new double[] {0.500, 0.700, 0.850, 1.000, 1.150, 1.300, 1.500},
  204.             new String[] {"06M", "21M", "36M", "51M", "66M"}
  205.         );
  206.     }
  207. }