ForeignCollateralizedZeroCoupon.java

  1. package org.drip.sample.piterbarg2012;

  2. import org.drip.analytics.date.*;
  3. import org.drip.function.r1tor1.*;
  4. import org.drip.product.params.CurrencyPair;
  5. import org.drip.service.env.EnvManager;
  6. import org.drip.state.creator.ScenarioDiscountCurveBuilder;
  7. import org.drip.state.curve.ForeignCollateralizedDiscountCurve;
  8. import org.drip.state.discount.MergedDiscountForwardCurve;
  9. import org.drip.state.fx.FXCurve;
  10. import org.drip.state.identifier.*;
  11. import org.drip.state.nonlinear.*;

  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.  * ForeignCollateralizedZeroCoupon contains an analysis of the correlation and volatility impact on the
  61.  *  single cash flow discount factor of a Foreign Collateralized Zero Coupon.
  62.  *
  63.  * @author Lakshmi Krishnamurthy
  64.  */

  65. public class ForeignCollateralizedZeroCoupon {
  66.     private static final double ZeroCouponVolCorr (
  67.         final JulianDate dtSpot,
  68.         final CurrencyPair cp,
  69.         final MergedDiscountForwardCurve dcCcyForeignCollatForeign,
  70.         final FXCurve fxCurve,
  71.         final double dblForeignRatesVolatility,
  72.         final double dblFXVolatility,
  73.         final double dblFXForeignRatesCorrelation,
  74.         final JulianDate dtMaturity,
  75.         final double dblBaselinePrice)
  76.         throws Exception
  77.     {
  78.         MergedDiscountForwardCurve dcCcyDomesticCollatForeign = new ForeignCollateralizedDiscountCurve (
  79.             cp.denomCcy(),
  80.             dcCcyForeignCollatForeign,
  81.             fxCurve,
  82.             new FlatForwardVolatilityCurve (
  83.                 dtSpot.julian(),
  84.                 VolatilityLabel.Standard (CollateralLabel.Standard (cp.numCcy())),
  85.                 cp.denomCcy(),
  86.                 new int[] {dtSpot.julian()},
  87.                 new double[] {dblForeignRatesVolatility}
  88.             ),
  89.             new FlatForwardVolatilityCurve (
  90.                 dtSpot.julian(),
  91.                 VolatilityLabel.Standard (FXLabel.Standard (cp)),
  92.                 cp.denomCcy(),
  93.                 new int[] {dtSpot.julian()},
  94.                 new double[] {dblFXVolatility}
  95.             ),
  96.             new FlatUnivariate (dblFXForeignRatesCorrelation)
  97.         );

  98.         double dblPrice = dcCcyDomesticCollatForeign.df (dtMaturity);

  99.         System.out.println ("\t[" +
  100.             org.drip.numerical.common.FormatUtil.FormatDouble (dblForeignRatesVolatility, 2, 0, 100.) + "%," +
  101.             org.drip.numerical.common.FormatUtil.FormatDouble (dblFXVolatility, 2, 0, 100.) + "%," +
  102.             org.drip.numerical.common.FormatUtil.FormatDouble (dblFXForeignRatesCorrelation, 2, 0, 100.) + "%] =" +
  103.             org.drip.numerical.common.FormatUtil.FormatDouble (dblPrice, 1, 2, 100.) + " | " +
  104.             org.drip.numerical.common.FormatUtil.FormatDouble (dblPrice - dblBaselinePrice, 1, 0, 10000.)
  105.         );

  106.         return dblPrice;
  107.     }

  108.     public static final void main (
  109.         final String[] astrArgs)
  110.         throws Exception
  111.     {
  112.         /*
  113.          * Initialize the Credit Analytics Library
  114.          */

  115.         EnvManager.InitEnv ("");

  116.         JulianDate dtToday = DateUtil.Today();

  117.         String strMaturityTenor = "5Y";
  118.         String strDomesticCurrency = "USD";
  119.         String strForeignCurrency = "JPY";
  120.         double dblForeignCollateralRate = 0.02;
  121.         double dblCollateralizedFXRate = 0.01;

  122.         JulianDate dtZeroCouponMaturity = dtToday.addTenor (strMaturityTenor);

  123.         MergedDiscountForwardCurve dcCcyForeignCollatForeign = ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  124.             dtToday,
  125.             strForeignCurrency,
  126.             dblForeignCollateralRate
  127.         );

  128.         CurrencyPair cp = CurrencyPair.FromCode (strForeignCurrency + "/" + strDomesticCurrency);

  129.         FXCurve fxCurve = new FlatForwardFXCurve (
  130.             dtToday.julian(),
  131.             cp,
  132.             dblCollateralizedFXRate,
  133.             new int[] {dtToday.julian()},
  134.             new double[] {dblCollateralizedFXRate}
  135.         );

  136.         double dblBaselinePrice = ZeroCouponVolCorr (
  137.             dtToday,
  138.             cp,
  139.             dcCcyForeignCollatForeign,
  140.             fxCurve,
  141.             0.,
  142.             0.,
  143.             0.,
  144.             dtZeroCouponMaturity,
  145.             0.
  146.         );

  147.         double[] adblForeignRatesVol = new double[] {
  148.             0.1, 0.2, 0.3, 0.4, 0.5
  149.         };
  150.         double[] adblFXVol = new double[] {
  151.             0.10, 0.15, 0.20, 0.25, 0.30
  152.         };
  153.         double[] adblForeignRatesFXCorr = new double[] {
  154.             -0.99, -0.50, 0.00, 0.50, 0.99
  155.         };

  156.         System.out.println ("\tPrinting the Zero Coupon Bond Price in Order (Left -> Right):");

  157.         System.out.println ("\t\tPrice (%)");

  158.         System.out.println ("\t\tDifference from Baseline (pt)");

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

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

  161.         for (double dblForeignRatesVol : adblForeignRatesVol) {
  162.             for (double dblFXVol : adblFXVol) {
  163.                 for (double dblForeignRatesFXCorr : adblForeignRatesFXCorr)
  164.                     ZeroCouponVolCorr (
  165.                         dtToday,
  166.                         cp,
  167.                         dcCcyForeignCollatForeign,
  168.                         fxCurve,
  169.                         dblForeignRatesVol,
  170.                         dblFXVol,
  171.                         dblForeignRatesFXCorr,
  172.                         dtZeroCouponMaturity,
  173.                         dblBaselinePrice
  174.                     );
  175.             }
  176.         }
  177.     }
  178. }