DomesticCollateralForeignForexAnalysis.java

  1. package org.drip.sample.piterbarg2012;

  2. import org.drip.analytics.date.*;
  3. import org.drip.analytics.support.CaseInsensitiveTreeMap;
  4. import org.drip.function.r1tor1.*;
  5. import org.drip.param.creator.*;
  6. import org.drip.param.market.CurveSurfaceQuoteContainer;
  7. import org.drip.param.valuation.*;
  8. import org.drip.product.fx.DomesticCollateralizedForeignForward;
  9. import org.drip.product.params.CurrencyPair;
  10. import org.drip.service.env.EnvManager;
  11. import org.drip.state.creator.*;
  12. import org.drip.state.curve.ForeignCollateralizedDiscountCurve;
  13. import org.drip.state.discount.MergedDiscountForwardCurve;
  14. import org.drip.state.fx.FXCurve;
  15. import org.drip.state.identifier.*;
  16. import org.drip.state.nonlinear.*;
  17. import org.drip.state.volatility.VolatilityCurve;

  18. /*
  19.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  20.  */

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

  65. /**
  66.  * DomesticCollateralForeignForexAnalysis contains an analysis of the correlation and volatility impact on the
  67.  *  price of a Domestic Collateralized ForeignPay-out Forex Contract.
  68.  *
  69.  * @author Lakshmi Krishnamurthy
  70.  */

  71. public class DomesticCollateralForeignForexAnalysis {
  72.     public static final void main (
  73.         final String[] astrArgs)
  74.         throws Exception
  75.     {
  76.         /*
  77.          * Initialize the Credit Analytics Library
  78.          */

  79.         EnvManager.InitEnv ("");

  80.         JulianDate dtToday = DateUtil.Today();

  81.         String strDomesticCurrency = "USD";
  82.         String strForeignCurrency = "EUR";
  83.         String strMaturity = "1Y";
  84.         double dblFXFwdStrike = 0.984;
  85.         double dblDomesticCollateralRate = 0.02;
  86.         double dblCollateralizedFXRate = 1.10;

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

  88.         MergedDiscountForwardCurve dcCcyDomesticCollatDomestic = ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  89.             dtToday,
  90.             strDomesticCurrency,
  91.             dblDomesticCollateralRate
  92.         );

  93.         ValuationParams valParams = new ValuationParams (
  94.             dtToday,
  95.             dtToday,
  96.             strDomesticCurrency
  97.         );

  98.         FXCurve fxCurve = new FlatForwardFXCurve (
  99.             dtToday.julian(),
  100.             cp,
  101.             dblCollateralizedFXRate,
  102.             new int[] {dtToday.julian()},
  103.             new double[] {dblCollateralizedFXRate}
  104.         );

  105.         VolatilityCurve vcForeignFunding = new FlatForwardVolatilityCurve (
  106.             dtToday.julian(),
  107.             VolatilityLabel.Standard (CollateralLabel.Standard (strForeignCurrency)),
  108.             strDomesticCurrency,
  109.             new int[] {dtToday.julian()},
  110.             new double[] {0.}
  111.         );

  112.         VolatilityCurve vcFX = new FlatForwardVolatilityCurve (
  113.             dtToday.julian(),
  114.             VolatilityLabel.Standard (FXLabel.Standard (cp)),
  115.             strDomesticCurrency,
  116.             new int[] {dtToday.julian()},
  117.             new double[] {0.}
  118.         );

  119.         MergedDiscountForwardCurve dcCcyForeignCollatDomestic = new ForeignCollateralizedDiscountCurve (
  120.             strForeignCurrency,
  121.             dcCcyDomesticCollatDomestic,
  122.             fxCurve,
  123.             vcForeignFunding,
  124.             vcFX,
  125.             new FlatUnivariate (0.)
  126.         );

  127.         CurveSurfaceQuoteContainer mktParams = MarketParamsBuilder.Create (
  128.             null,
  129.             null,
  130.             null,
  131.             null,
  132.             null,
  133.             null,
  134.             null
  135.         );

  136.         mktParams.setPayCurrencyCollateralCurrencyCurve (
  137.             strForeignCurrency,
  138.             strDomesticCurrency,
  139.             dcCcyForeignCollatDomestic
  140.         );

  141.         mktParams.setPayCurrencyCollateralCurrencyCurve (
  142.             strDomesticCurrency,
  143.             strDomesticCurrency,
  144.             dcCcyDomesticCollatDomestic
  145.         );

  146.         mktParams.setFXState (
  147.             ScenarioFXCurveBuilder.CubicPolynomialCurve (
  148.                 "FX::" + cp.code(),
  149.                 dtToday,
  150.                 cp,
  151.                 new String[] {"10Y"},
  152.                 new double[] {dblCollateralizedFXRate},
  153.                 dblCollateralizedFXRate
  154.             )
  155.         );

  156.         DomesticCollateralizedForeignForward dcff = new DomesticCollateralizedForeignForward (
  157.             cp,
  158.             dblFXFwdStrike,
  159.             dtToday.addTenor (strMaturity)
  160.         );

  161.         CaseInsensitiveTreeMap<Double> mapBaseValue = dcff.value (
  162.             new ValuationParams (
  163.                 dtToday,
  164.                 dtToday,
  165.                 strDomesticCurrency
  166.             ),
  167.             null,
  168.             mktParams,
  169.             null
  170.         );

  171.         double dblBaselinePrice = mapBaseValue.get ("Price");

  172.         double dblBaselineParForward = mapBaseValue.get ("ParForward");

  173.         double[] adblForeignRatesVolatility = new double[] {
  174.             0.1, 0.2, 0.3, 0.4, 0.5
  175.         };
  176.         double[] adblFXVolatility = new double[] {
  177.             0.10, 0.15, 0.20, 0.25, 0.30
  178.         };
  179.         double[] adblFXForeignRatesCorrelation = new double[] {
  180.             -0.99, -0.50, 0.00, 0.50, 0.99
  181.         };

  182.         System.out.println ("\tPrinting the Domestic Collateralized Foreign Forex Output in Order (Left -> Right):");

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

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

  185.         System.out.println ("\t\tPar Forward (abs)");

  186.         System.out.println ("\t\tPar Forward Difference (abs)");

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

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

  189.         for (double dblForeignRatesVolatility : adblForeignRatesVolatility) {
  190.             for (double dblFXVolatility : adblFXVolatility) {
  191.                 for (double dblFXForeignRatesCorrelation : adblFXForeignRatesCorrelation) {
  192.                     dcCcyForeignCollatDomestic = new ForeignCollateralizedDiscountCurve (
  193.                         strForeignCurrency,
  194.                         dcCcyDomesticCollatDomestic,
  195.                         fxCurve,
  196.                         new FlatForwardVolatilityCurve (
  197.                             dtToday.julian(),
  198.                             VolatilityLabel.Standard (CollateralLabel.Standard (strForeignCurrency)),
  199.                             strDomesticCurrency,
  200.                             new int[] {dtToday.julian()},
  201.                             new double[] {dblForeignRatesVolatility}
  202.                         ),
  203.                         new FlatForwardVolatilityCurve (
  204.                             dtToday.julian(),
  205.                             VolatilityLabel.Standard (FXLabel.Standard (cp)),
  206.                             strDomesticCurrency,
  207.                             new int[] {dtToday.julian()},
  208.                             new double[] {dblFXVolatility}
  209.                         ),
  210.                         new FlatUnivariate (dblFXForeignRatesCorrelation)
  211.                     );

  212.                     mktParams.setPayCurrencyCollateralCurrencyCurve (
  213.                         strForeignCurrency,
  214.                         strDomesticCurrency,
  215.                         dcCcyForeignCollatDomestic
  216.                     );

  217.                     CaseInsensitiveTreeMap<Double> mapDCFF = dcff.value (
  218.                         valParams,
  219.                         null,
  220.                         mktParams,
  221.                         null
  222.                     );

  223.                     double dblPrice = mapDCFF.get ("Price");

  224.                     double dblParForward = mapDCFF.get ("ParForward");

  225.                     System.out.println ("\t[" +
  226.                         org.drip.numerical.common.FormatUtil.FormatDouble (dblForeignRatesVolatility, 2, 0, 100.) + "%," +
  227.                         org.drip.numerical.common.FormatUtil.FormatDouble (dblFXVolatility, 2, 0, 100.) + "%," +
  228.                         org.drip.numerical.common.FormatUtil.FormatDouble (dblFXForeignRatesCorrelation, 2, 0, 100.) + "%] = " +
  229.                         org.drip.numerical.common.FormatUtil.FormatDouble (dblPrice, 2, 2, 100.) + " | " +
  230.                         org.drip.numerical.common.FormatUtil.FormatDouble (dblPrice - dblBaselinePrice, 2, 2, 100.) + " | " +
  231.                         org.drip.numerical.common.FormatUtil.FormatDouble (dblParForward, 1, 4, 1.) + " | " +
  232.                         org.drip.numerical.common.FormatUtil.FormatDouble (dblParForward - dblBaselineParForward, 1, 4, 1.)
  233.                     );
  234.                 }
  235.             }
  236.         }
  237.     }
  238. }