ForwardContract.java

  1. package org.drip.sample.piterbarg2010;

  2. import org.drip.analytics.date.*;
  3. import org.drip.analytics.support.Helper;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.service.env.EnvManager;
  6. import org.drip.service.template.LatentMarketStateBuilder;
  7. import org.drip.state.discount.*;

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

  52. /**
  53.  * ForwardContract examines the Valuation of Forward Contract under CSA and non-CSA Settle Agreements. CSA is
  54.  *  proxied using the OIS Curve, and non-CSA using the Issuer Hedge Funding Curve. The corresponding
  55.  *  Convexity Adjustments using Spread/CSA Covariance are also calculated. The References are:
  56.  *  
  57.  *  - Barden, P. (2009): Equity Forward Prices in the Presence of Funding Spreads, ICBI Conference, Rome.
  58.  *  
  59.  *  - Burgard, C., and M. Kjaer (2009): Modeling and successful Management of Credit Counter-party Risk of
  60.  *      Derivative Portfolios, ICBI Conference, Rome.
  61.  *  
  62.  *  - Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk, Risk 20 (2) 86-90.
  63.  *  
  64.  *  - Johannes, M., and S. Sundaresan (2007): Pricing Collateralized Swaps, Journal of Finance 62 383-410.
  65.  *
  66.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  67.  *      21 (2) 97-102.
  68.  *
  69.  * @author Lakshmi Krishnamurthy
  70.  */

  71. public class ForwardContract {

  72.     private static final DiscountCurve CSACurve (
  73.         final String strCurrency,
  74.         final JulianDate dtSpot)
  75.         throws Exception
  76.     {
  77.         String[] astrDepositMaturityTenor = new String[] {
  78.             "1D",
  79.             // "2D",
  80.             // "3D"
  81.         };

  82.         double[] adblDepositQuote = new double[] {
  83.             0.0004,     // 1D
  84.             // 0.0004,      // 2D
  85.             // 0.0004       // 3D
  86.         };

  87.         String[] astrShortEndOISMaturityTenor = new String[] {
  88.             "1W",
  89.             "2W",
  90.             "3W",
  91.             "1M"
  92.         };

  93.         double[] adblShortEndOISQuote = new double[] {
  94.             0.00070,    //   1W
  95.             0.00069,    //   2W
  96.             0.00078,    //   3W
  97.             0.00074     //   1M
  98.         };

  99.         String[] astrOISFuturesEffectiveTenor = new String[] {
  100.             "1M",
  101.             "2M",
  102.             "3M",
  103.             "4M",
  104.             "5M"
  105.         };

  106.         String[] astrOISFuturesMaturityTenor = new String[] {
  107.             "1M",
  108.             "1M",
  109.             "1M",
  110.             "1M",
  111.             "1M"
  112.         };

  113.         double[] adblOISFuturesQuote = new double[] {
  114.              0.00046,    //   1M x 1M
  115.              0.00016,    //   2M x 1M
  116.             -0.00007,    //   3M x 1M
  117.             -0.00013,    //   4M x 1M
  118.             -0.00014     //   5M x 1M
  119.         };

  120.         String[] astrLongEndOISMaturityTenor = new String[] {
  121.             "15M",
  122.             "18M",
  123.             "21M",
  124.             "02Y",
  125.             "03Y",
  126.             "04Y",
  127.             "05Y",
  128.             "06Y",
  129.             "07Y",
  130.             "08Y",
  131.             "09Y",
  132.             "10Y",
  133.             "11Y",
  134.             "12Y",
  135.             "15Y",
  136.             "20Y",
  137.             "25Y",
  138.             "30Y"
  139.         };

  140.         double[] adblLongEndOISQuote = new double[] {
  141.             0.00002,    //  15M
  142.             0.00008,    //  18M
  143.             0.00021,    //  21M
  144.             0.00036,    //   2Y
  145.             0.00127,    //   3Y
  146.             0.00274,    //   4Y
  147.             0.00456,    //   5Y
  148.             0.00647,    //   6Y
  149.             0.00827,    //   7Y
  150.             0.00996,    //   8Y
  151.             0.01147,    //   9Y
  152.             0.01280,    //  10Y
  153.             0.01404,    //  11Y
  154.             0.01516,    //  12Y
  155.             0.01764,    //  15Y
  156.             0.01939,    //  20Y
  157.             0.02003,    //  25Y
  158.             0.02038     //  30Y
  159.         };

  160.         return LatentMarketStateBuilder.SmoothOvernightCurve (
  161.             dtSpot,
  162.             strCurrency,
  163.             astrDepositMaturityTenor,
  164.             adblDepositQuote,
  165.             "Rate",
  166.             astrShortEndOISMaturityTenor,
  167.             adblShortEndOISQuote,
  168.             "SwapRate",
  169.             astrOISFuturesEffectiveTenor,
  170.             astrOISFuturesMaturityTenor,
  171.             adblOISFuturesQuote,
  172.             "SwapRate",
  173.             astrLongEndOISMaturityTenor,
  174.             adblLongEndOISQuote,
  175.             "SwapRate"
  176.         );
  177.     }

  178.     private static final DiscountCurve NonCSACurve (
  179.         final String strCurrency,
  180.         final JulianDate dtSpot)
  181.         throws Exception
  182.     {
  183.         String[] astrDepositMaturityTenor = new String[] {
  184.             "01D",
  185.             "04D",
  186.             "07D",
  187.             "14D",
  188.             "30D",
  189.             "60D"
  190.         };

  191.         double[] adblDepositQuote = new double[] {
  192.             0.0013,     //  1D
  193.             0.0017,     //  2D
  194.             0.0017,     //  7D
  195.             0.0018,     // 14D
  196.             0.0020,     // 30D
  197.             0.0023      // 60D
  198.         };

  199.         double[] adblFuturesQuote = new double[] {
  200.             0.0027,
  201.             0.0032,
  202.             0.0041,
  203.             0.0054,
  204.             0.0077,
  205.             0.0104,
  206.             0.0134,
  207.             0.0160
  208.         };

  209.         String[] astrFixFloatMaturityTenor = new String[] {
  210.             "04Y",
  211.             "05Y",
  212.             "06Y",
  213.             "07Y",
  214.             "08Y",
  215.             "09Y",
  216.             "10Y",
  217.             "11Y",
  218.             "12Y",
  219.             "15Y",
  220.             "20Y",
  221.             "25Y",
  222.             "30Y",
  223.             "40Y",
  224.             "50Y"
  225.         };

  226.         double[] adblFixFloatQuote = new double[] {
  227.             0.0166,     //   4Y
  228.             0.0206,     //   5Y
  229.             0.0241,     //   6Y
  230.             0.0269,     //   7Y
  231.             0.0292,     //   8Y
  232.             0.0311,     //   9Y
  233.             0.0326,     //  10Y
  234.             0.0340,     //  11Y
  235.             0.0351,     //  12Y
  236.             0.0375,     //  15Y
  237.             0.0393,     //  20Y
  238.             0.0402,     //  25Y
  239.             0.0407,     //  30Y
  240.             0.0409,     //  40Y
  241.             0.0409      //  50Y
  242.         };

  243.         return LatentMarketStateBuilder.SmoothFundingCurve (
  244.             dtSpot,
  245.             strCurrency,
  246.             astrDepositMaturityTenor,
  247.             adblDepositQuote,
  248.             "ForwardRate",
  249.             adblFuturesQuote,
  250.             "ForwardRate",
  251.             astrFixFloatMaturityTenor,
  252.             adblFixFloatQuote,
  253.             "SwapRate"
  254.         );
  255.     }

  256.     public static final void main (
  257.         final String[] astrArgs)
  258.         throws Exception
  259.     {
  260.         EnvManager.InitEnv ("");

  261.         String strCurrency = "USD";
  262.         double dblATMForward = 50.;
  263.         double dblCSAVolatility = 0.1;
  264.         double dblCSANonCSASpreadVolatility = 0.2;
  265.         double dblCSANonCSASpreadCorrelation = 0.2;

  266.         JulianDate dtSpot = DateUtil.Today().addBusDays (
  267.             0,
  268.             strCurrency
  269.         );

  270.         String[] astrTenor = new String[] {
  271.              "1W",
  272.              "2W",
  273.              "3W",
  274.              "1M",
  275.              "2M",
  276.              "3M",
  277.              "6M",
  278.              "9M",
  279.             "12M",
  280.             "18M",
  281.              "2Y",
  282.              "3Y",
  283.              "4Y",
  284.              "5Y",
  285.              "7Y",
  286.             "10Y",
  287.             "12Y",
  288.             "15Y",
  289.             "20Y",
  290.             "25Y",
  291.             "30Y",
  292.             "40Y",
  293.             "50Y"
  294.         };

  295.         DiscountCurve dcOvernight = CSACurve (
  296.             strCurrency,
  297.             dtSpot
  298.         );

  299.         DiscountCurve dcFunding = NonCSACurve (
  300.             strCurrency,
  301.             dtSpot
  302.         );

  303.         System.out.println();

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

  305.         System.out.println ("\t||     FORWARD CONTRACT CONVEXITY ADJUSTMENT     ||");

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

  307.         System.out.println ("\t|| L -> R:                                       ||");

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

  309.         System.out.println ("\t||        - Spread Numeraire                     ||");

  310.         System.out.println ("\t||        - Convexity Adjustment                 ||");

  311.         System.out.println ("\t||        - CSA-Funding Convexity Adjustment     ||");

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

  313.         for (String strTenor : astrTenor) {
  314.             JulianDate dt = dtSpot.addTenor (strTenor);

  315.             double dblTenorToYF = Helper.TenorToYearFraction (strTenor);

  316.             double dblSpreadNumeraire = dcFunding.df (dt) / dcOvernight.df (dt);

  317.             double dblConvexityAdjustment = dblCSANonCSASpreadCorrelation * dblCSANonCSASpreadVolatility * dblCSAVolatility;

  318.             System.out.println ("\t|| " + dt + " | " +
  319.                 FormatUtil.FormatDouble (dblSpreadNumeraire, 1, 4, 1.) + " | " +
  320.                 FormatUtil.FormatDouble (dblConvexityAdjustment * dblTenorToYF, 1, 6, 1.) + " | " +
  321.                 FormatUtil.FormatDouble (dblConvexityAdjustment * dblATMForward * dblSpreadNumeraire * dblTenorToYF, 1, 6, 1.) + " ||"
  322.             );
  323.         }

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

  325.         System.out.println();
  326.     }
  327. }