CustomBasisCurveBuilder.java

  1. package org.drip.sample.multicurve;

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

  55. /**
  56.  * CustomBasisCurveBuilder contains the sample demonstrating the full functionality behind creating highly
  57.  *  customized spline based Basis curves.
  58.  *
  59.  * @author Lakshmi Krishnamurthy
  60.  */

  61. public class CustomBasisCurveBuilder {
  62.     public static final void main (
  63.         final String[] astrArgs)
  64.         throws Exception
  65.     {
  66.         /*
  67.          * Initialize the Credit Analytics Library
  68.          */

  69.         EnvManager.InitEnv ("");

  70.         JulianDate dtToday = DateUtil.Today();

  71.         String[] astrTenor = new String[] {
  72.             "1Y", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y"
  73.         };

  74.         double[] adblBasis = new double[] {
  75.             0.00186,    //  1Y
  76.             0.00127,    //  2Y
  77.             0.00097,    //  3Y
  78.             0.00080,    //  4Y
  79.             0.00067,    //  5Y
  80.             0.00058,    //  6Y
  81.             0.00051,    //  7Y
  82.             0.00046,    //  8Y
  83.             0.00042,    //  9Y
  84.             0.00038,    // 10Y
  85.             0.00035,    // 11Y
  86.             0.00033,    // 12Y
  87.             0.00028,    // 15Y
  88.             0.00022,    // 20Y
  89.             0.00020,    // 25Y
  90.             0.00018     // 30Y
  91.         };

  92.         BasisCurve bcCubicPolynomial = ScenarioBasisCurveBuilder.CubicPolynomialBasisCurve (
  93.             "USD3M6MBasis_CubicPolynomial",
  94.             dtToday,
  95.             ForwardLabel.Create (
  96.                 "USD",
  97.                 "6M"
  98.             ),
  99.             ForwardLabel.Create (
  100.                 "USD",
  101.                 "3M"
  102.             ),
  103.             false,
  104.             astrTenor,
  105.             adblBasis
  106.         );

  107.         BasisCurve bcQuinticPolynomial = ScenarioBasisCurveBuilder.QuarticPolynomialBasisCurve (
  108.             "USD3M6MBasis_QuinticPolynomial",
  109.             dtToday,
  110.             ForwardLabel.Create (
  111.                 "USD",
  112.                 "6M"
  113.             ),
  114.             ForwardLabel.Create (
  115.                 "USD",
  116.                 "3M"
  117.             ),
  118.             false,
  119.             astrTenor,
  120.             adblBasis
  121.         );

  122.         BasisCurve bcKaklisPandelis = ScenarioBasisCurveBuilder.KaklisPandelisBasisCurve (
  123.             "USD3M6MBasis_KaklisPandelis",
  124.             dtToday,
  125.             ForwardLabel.Create (
  126.                 "USD",
  127.                 "6M"
  128.             ),
  129.             ForwardLabel.Create (
  130.                 "USD",
  131.                 "3M"
  132.             ),
  133.             false,
  134.             astrTenor,
  135.             adblBasis
  136.         );

  137.         BasisCurve bcKLKHyperbolic = ScenarioBasisCurveBuilder.KLKHyperbolicBasisCurve (
  138.             "USD3M6MBasis_KLKHyperbolic",
  139.             dtToday,
  140.             ForwardLabel.Create (
  141.                 "USD",
  142.                 "6M"
  143.             ),
  144.             ForwardLabel.Create (
  145.                 "USD",
  146.                 "3M"
  147.             ),
  148.             false,
  149.             astrTenor,
  150.             adblBasis,
  151.             1.
  152.         );

  153.         BasisCurve bcKLKRationalLinear = ScenarioBasisCurveBuilder.KLKRationalLinearBasisCurve (
  154.             "USD3M6MBasis_KLKRationalLinear",
  155.             dtToday,
  156.             ForwardLabel.Create (
  157.                 "USD",
  158.                 "6M"
  159.             ),
  160.             ForwardLabel.Create (
  161.                 "USD",
  162.                 "3M"
  163.             ),
  164.             false,
  165.             astrTenor,
  166.             adblBasis,
  167.             0.1
  168.         );

  169.         BasisCurve bcKLKRationalQuadratic = ScenarioBasisCurveBuilder.KLKRationalLinearBasisCurve (
  170.             "USD3M6MBasis_KLKRationalQuadratic",
  171.             dtToday,
  172.             ForwardLabel.Create (
  173.                 "USD",
  174.                 "6M"
  175.             ),
  176.             ForwardLabel.Create (
  177.                 "USD",
  178.                 "3M"
  179.             ),
  180.             false,
  181.             astrTenor,
  182.             adblBasis,
  183.             2.
  184.         );

  185.         System.out.println ("\tPrinting the Basis Node Values in Order (Left -> Right):");

  186.         System.out.println ("\t\tCalculated Cubic Polynomial Basis (%)");

  187.         System.out.println ("\t\tCalculated Quintic Polynomial Basis (%)");

  188.         System.out.println ("\t\tCalculated Kaklis Pandelis Basis (%)");

  189.         System.out.println ("\t\tCalculated KLK Hyperbolic Basis (%)");

  190.         System.out.println ("\t\tCalculated KLK Rational Linear Basis (%)");

  191.         System.out.println ("\t\tCalculated KLK Rational Quadratic Basis (%)");

  192.         System.out.println ("\t\tInput Quote (bp)");

  193.         System.out.println ("\t-------------------------------------------------------------");

  194.         System.out.println ("\t-------------------------------------------------------------");

  195.         for (int i = 0; i < adblBasis.length; ++i)
  196.             System.out.println ("\t" + astrTenor[i] + " => " +
  197.                 FormatUtil.FormatDouble (bcCubicPolynomial.basis (astrTenor[i]), 1, 2, 10000.) + " | " +
  198.                 FormatUtil.FormatDouble (bcQuinticPolynomial.basis (astrTenor[i]), 1, 2, 10000.) + " | " +
  199.                 FormatUtil.FormatDouble (bcKaklisPandelis.basis (astrTenor[i]), 1, 2, 10000.) + " | " +
  200.                 FormatUtil.FormatDouble (bcKLKHyperbolic.basis (astrTenor[i]), 1, 2, 10000.) + " | " +
  201.                 FormatUtil.FormatDouble (bcKLKRationalLinear.basis (astrTenor[i]), 1, 2, 10000.) + " | " +
  202.                 FormatUtil.FormatDouble (bcKLKRationalQuadratic.basis (astrTenor[i]), 1, 2, 10000.) + " | " +
  203.                 FormatUtil.FormatDouble (adblBasis[i], 1, 2, 10000.)
  204.             );

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

  206.         System.out.println ("\t|  DATE    =>  CUBIC | QUINTIC  | KAKPAND | KLKHYPER | KLKRATLNR | KLKRATQUA |");

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

  208.         for (int i = 3; i < 30; ++i) {
  209.             JulianDate dt = dtToday.addTenor (i + "Y");

  210.             System.out.println ("\t" + dt + " => " +
  211.                 FormatUtil.FormatDouble (bcCubicPolynomial.basis (dt), 1, 2, 10000.) + "  |  " +
  212.                 FormatUtil.FormatDouble (bcQuinticPolynomial.basis (dt), 1, 2, 10000.) + "   |  " +
  213.                 FormatUtil.FormatDouble (bcKaklisPandelis.basis (dt), 1, 2, 10000.) + "  |  " +
  214.                 FormatUtil.FormatDouble (bcKLKHyperbolic.basis (dt), 1, 2, 10000.) + "   |  " +
  215.                 FormatUtil.FormatDouble (bcKLKRationalLinear.basis (dt), 1, 2, 10000.) + "    |  " +
  216.                 FormatUtil.FormatDouble (bcKLKRationalQuadratic.basis (dt), 1, 2, 10000.) + "    |  "
  217.             );
  218.         }

  219.         System.out.println ("\n\t|----------------------------------------------------------------------------|");
  220.     }
  221. }