ATMTTESurface2D.java

  1. package org.drip.sample.stretch;

  2. import java.util.TreeMap;

  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.spline.basis.PolynomialFunctionSetParams;
  5. import org.drip.spline.grid.*;
  6. import org.drip.spline.multidimensional.WireSurfaceStretch;
  7. import org.drip.spline.params.*;
  8. import org.drip.spline.stretch.*;

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

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

  56. /**
  57.  * ATMTTESurface2D demonstrates the Surface 2D ATM/TTE (X/Y) Stretch Construction and usage API.
  58.  *
  59.  * @author Lakshmi Krishnamurthy
  60.  */

  61. public class ATMTTESurface2D {

  62.     public static final void main (
  63.         final String[] astrArgs)
  64.         throws Exception
  65.     {
  66.         double[] adblATMFactor = new double[] {
  67.             0.8, 0.9, 1.0, 1.1, 1.2
  68.         };
  69.         double[] adblTTE = new double[] {
  70.             1., 2., 3., 4., 5.
  71.         };

  72.         double[][] aadblImpliedVolatility = new double[][] {
  73.             {0.44, 0.38, 0.33, 0.27, 0.25},
  74.             {0.41, 0.34, 0.30, 0.22, 0.27},
  75.             {0.36, 0.31, 0.28, 0.30, 0.37},
  76.             {0.38, 0.31, 0.34, 0.40, 0.47},
  77.             {0.43, 0.46, 0.48, 0.52, 0.57}
  78.         };

  79.         SegmentCustomBuilderControl scbcSpan = new SegmentCustomBuilderControl (
  80.             MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  81.             new PolynomialFunctionSetParams (4),
  82.             SegmentInelasticDesignControl.Create (
  83.                 2,
  84.                 2
  85.             ),
  86.             null,
  87.             null
  88.         );

  89.         TreeMap<Double, Span> mapSpan = new TreeMap<Double, Span>();

  90.         SegmentCustomBuilderControl[] aSCBCSpan = new SegmentCustomBuilderControl[adblATMFactor.length - 1];

  91.         for (int i = 0; i < aSCBCSpan.length; ++i)
  92.             aSCBCSpan[i] = scbcSpan;

  93.         for (int i = 0; i < adblATMFactor.length; ++i)
  94.             mapSpan.put (adblATMFactor[i], new OverlappingStretchSpan (
  95.                 MultiSegmentSequenceBuilder.CreateCalibratedStretchEstimator (
  96.                     "Stretch@" + adblTTE + "@" + org.drip.numerical.common.StringUtil.GUID(),
  97.                     adblTTE,
  98.                     aadblImpliedVolatility[i],
  99.                     aSCBCSpan,
  100.                     null,
  101.                     BoundarySettings.NaturalStandard(),
  102.                     MultiSegmentSequence.CALIBRATE
  103.                 )
  104.             )
  105.         );

  106.         SegmentCustomBuilderControl scbcSurface = new SegmentCustomBuilderControl (
  107.             MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  108.             new PolynomialFunctionSetParams (4),
  109.             SegmentInelasticDesignControl.Create (
  110.                 2,
  111.                 2
  112.             ),
  113.             null,
  114.             null
  115.         );

  116.         WireSurfaceStretch ss = new WireSurfaceStretch (
  117.             "SurfaceStretch@" + org.drip.numerical.common.StringUtil.GUID(),
  118.             scbcSurface,
  119.             mapSpan
  120.         );

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

  122.         System.out.println ("\t|----------------- INPUT  SURFACE  RECOVERY -----------------|");

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

  124.         for (double dblTTE : adblTTE)
  125.             System.out.print ("   " + FormatUtil.FormatDouble (dblTTE, 1, 2, 1.) + " ");

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

  127.         for (double dblATMFactor : adblATMFactor) {
  128.             System.out.print ("\t|  " + FormatUtil.FormatDouble (dblATMFactor, 1, 2, 1.) + "    =>");

  129.             for (double dblTTE : adblTTE)
  130.                 System.out.print ("  " +
  131.                     FormatUtil.FormatDouble (ss.responseValue (
  132.                         dblATMFactor,
  133.                         dblTTE
  134.                     ), 2, 2, 100.) + "%");

  135.             System.out.print ("  |\n");
  136.         }

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

  138.         adblATMFactor = new double[] {
  139.             0.850, 0.925, 1.000, 1.075, 1.15
  140.         };
  141.         adblTTE = new double[] {
  142.             1.50, 2.25, 3., 3.75, 4.50
  143.         };

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

  145.         System.out.println ("\t|------------- IN-SURFACE RESPONSE CALCULATION --------------|");

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

  147.         for (double dblTTE : adblTTE)
  148.             System.out.print ("   " + FormatUtil.FormatDouble (dblTTE, 1, 2, 1.) + " ");

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

  150.         for (double dblATMFactor : adblATMFactor) {
  151.             System.out.print ("\t|  " + FormatUtil.FormatDouble (dblATMFactor, 1, 2, 1.) + "    =>");

  152.             for (double dblTTE : adblTTE)
  153.                 System.out.print ("  " +
  154.                     FormatUtil.FormatDouble (
  155.                         ss.responseValue (
  156.                             dblATMFactor,
  157.                             dblTTE
  158.                         ), 2, 2, 100.) + "%");

  159.             System.out.print ("  |\n");
  160.         }

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

  162.         adblATMFactor = new double[] {
  163.             0.70, 0.85, 1.00, 1.15, 1.30
  164.         };
  165.         adblTTE = new double[] {
  166.             0.50, 1.75, 3.00, 4.25, 5.50
  167.         };

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

  169.         System.out.println ("\t|------------- OFF-SURFACE RESPONSE CALCULATION -------------|");

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

  171.         for (double dblTTE : adblTTE)
  172.             System.out.print ("   " + FormatUtil.FormatDouble (dblTTE, 1, 2, 1.) + " ");

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

  174.         for (double dblATMFactor : adblATMFactor) {
  175.             System.out.print ("\t|  " + FormatUtil.FormatDouble (dblATMFactor, 1, 2, 1.) + "    =>");

  176.             for (double dblTTE : adblTTE)
  177.                 System.out.print ("  " + FormatUtil.FormatDouble (
  178.                     ss.responseValue (
  179.                         dblATMFactor,
  180.                         dblTTE
  181.                     ), 2, 2, 100.) + "%");

  182.             System.out.print ("  |\n");
  183.         }

  184.         System.out.println ("\t|------------------------------------------------------------|");
  185.     }
  186. }