CustomFXCurveBuilder.java

  1. package org.drip.sample.fx;

  2. import java.util.Map;

  3. import org.drip.analytics.date.*;
  4. import org.drip.function.r1tor1.QuadraticRationalShapeControl;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.param.market.CurveSurfaceQuoteContainer;
  7. import org.drip.param.valuation.ValuationParams;
  8. import org.drip.product.fx.FXForwardComponent;
  9. import org.drip.product.params.CurrencyPair;
  10. import org.drip.service.env.EnvManager;
  11. import org.drip.spline.basis.PolynomialFunctionSetParams;
  12. import org.drip.spline.params.*;
  13. import org.drip.spline.stretch.*;
  14. import org.drip.state.creator.*;
  15. import org.drip.state.discount.MergedDiscountForwardCurve;
  16. import org.drip.state.estimator.LatentStateStretchBuilder;
  17. import org.drip.state.fx.FXCurve;
  18. import org.drip.state.inference.*;

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

  22. /*!
  23.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  24.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  25.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  26.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  27.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  28.  *
  29.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  30.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  31.  *      credit, commodity, equity, FX, and structured products.
  32.  *  
  33.  *      https://lakshmidrip.github.io/DROP/
  34.  *  
  35.  *  DROP is composed of three modules:
  36.  *  
  37.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  38.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  39.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  40.  *
  41.  *  DROP Analytics Core implements libraries for the following:
  42.  *  - Fixed Income Analytics
  43.  *  - Asset Backed Analytics
  44.  *  - XVA Analytics
  45.  *  - Exposure and Margin Analytics
  46.  *
  47.  *  DROP Portfolio Core implements libraries for the following:
  48.  *  - Asset Allocation Analytics
  49.  *  - Transaction Cost Analytics
  50.  *
  51.  *  DROP Numerical Core implements libraries for the following:
  52.  *  - Statistical Learning
  53.  *  - Numerical Optimizer
  54.  *  - Spline Builder
  55.  *  - Algorithm Support
  56.  *
  57.  *  Documentation for DROP is Spread Over:
  58.  *
  59.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  60.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  61.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  62.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  63.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  64.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  65.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  66.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  67.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  68.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  69.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  70.  *
  71.  *  Licensed under the Apache License, Version 2.0 (the "License");
  72.  *      you may not use this file except in compliance with the License.
  73.  *  
  74.  *  You may obtain a copy of the License at
  75.  *      http://www.apache.org/licenses/LICENSE-2.0
  76.  *  
  77.  *  Unless required by applicable law or agreed to in writing, software
  78.  *      distributed under the License is distributed on an "AS IS" BASIS,
  79.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  80.  *  
  81.  *  See the License for the specific language governing permissions and
  82.  *      limitations under the License.
  83.  */

  84. /**
  85.  * <i>CustomFXCurveBuilder</i> illustrates the Construction and Usage of the FX Forward Curve.
  86.  *  
  87.  * <br><br>
  88.  *  <ul>
  89.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  90.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  91.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  92.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/fx/README.md">FX Curve Builder</a></li>
  93.  *  </ul>
  94.  * <br><br>
  95.  *
  96.  * @author Lakshmi Krishnamurthy
  97.  */

  98. public class CustomFXCurveBuilder {

  99.     private static FXForwardComponent[] FXForwardCalibComponent (
  100.         final CurrencyPair cp,
  101.         final JulianDate dtSpot,
  102.         final String[] astrMaturityTenor)
  103.         throws Exception
  104.     {
  105.         FXForwardComponent[] aFXForward = new FXForwardComponent[astrMaturityTenor.length];

  106.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  107.             aFXForward[i] = new FXForwardComponent (
  108.                 cp.code() + "::FXFWD::" + astrMaturityTenor[i],
  109.                 cp,
  110.                 dtSpot.julian(),
  111.                 dtSpot.addTenor (astrMaturityTenor[i]).julian(),
  112.                 1.,
  113.                 null
  114.             );

  115.         return aFXForward;
  116.     }

  117.     public static final void main (
  118.         final String[] astrArgs)
  119.         throws Exception
  120.     {
  121.         EnvManager.InitEnv ("");

  122.         JulianDate dtToday = DateUtil.Today().addTenor ("0D");

  123.         CurrencyPair cp = CurrencyPair.FromCode ("USD/EUR");

  124.         double dblSpot = 1.0993;

  125.         String[] astrMaturityTenor = new String[] {
  126.             "1W",
  127.             "1M",
  128.             "3M",
  129.             "6M",
  130.             "1Y",
  131.             "2Y",
  132.             "3Y"
  133.         };

  134.         FXForwardComponent[] aFXForward = FXForwardCalibComponent (
  135.             cp,
  136.             dtToday,
  137.             astrMaturityTenor
  138.         );

  139.         double[] adblFXForward = new double[] {
  140.             1.1000, //  "1W",
  141.             1.1012, //  "1M",
  142.             1.1039, //  "3M",
  143.             1.1148, //  "6M",
  144.             1.1232, //  "1Y",
  145.             1.1497, //  "2Y",
  146.             1.1865, //  "3Y"
  147.         };

  148.         LatentStateStretchSpec fxForwardStretch = LatentStateStretchBuilder.FXStretchSpec (
  149.             "FXFORWARD",
  150.             aFXForward,
  151.             "Outright",
  152.             adblFXForward
  153.         );

  154.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {
  155.             fxForwardStretch
  156.         };

  157.         LinearLatentStateCalibrator llsc = new LinearLatentStateCalibrator (
  158.             new SegmentCustomBuilderControl (
  159.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  160.                 new PolynomialFunctionSetParams (4),
  161.                 SegmentInelasticDesignControl.Create (
  162.                     2,
  163.                     2
  164.                 ),
  165.                 new ResponseScalingShapeControl (
  166.                     true,
  167.                     new QuadraticRationalShapeControl (0.)
  168.                 ),
  169.                 null
  170.             ),
  171.             BoundarySettings.NaturalStandard(),
  172.             MultiSegmentSequence.CALIBRATE,
  173.             null,
  174.             null
  175.         );

  176.         ValuationParams valParams = new ValuationParams (
  177.             dtToday,
  178.             dtToday,
  179.             cp.denomCcy()
  180.         );

  181.         FXCurve fxCurve = ScenarioFXCurveBuilder.ShapePreservingFXCurve (
  182.             llsc,
  183.             aStretchSpec,
  184.             cp,
  185.             valParams,
  186.             null,
  187.             null,
  188.             null,
  189.             dblSpot
  190.         );

  191.         MergedDiscountForwardCurve dcUSD = ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  192.             dtToday,
  193.             "USD",
  194.             0.02
  195.         );

  196.         MergedDiscountForwardCurve dcEUR = ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  197.             dtToday,
  198.             "EUR",
  199.             0.01
  200.         );

  201.         CurveSurfaceQuoteContainer csqs = new CurveSurfaceQuoteContainer();

  202.         csqs.setFXState (fxCurve);

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

  204.         System.out.println ("\t|                                                                   ||");

  205.         System.out.println ("\t|    Custom FX Curve Builder Metrics #1                             ||");

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

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

  208.         System.out.println ("\t|        FX Forward Tenor                                           ||");

  209.         System.out.println ("\t|        Input FX Forward Outright                                  ||");

  210.         System.out.println ("\t|        Curve FX Forward Outright                                  ||");

  211.         System.out.println ("\t|        Product FX Forward Outright                                ||");

  212.         System.out.println ("\t|        Product FX Forward PIP                                     ||");

  213.         System.out.println ("\t|        FX Forward Discount Curve Basis EUR Curve                  ||");

  214.         System.out.println ("\t|        FX Forward Discount Curve Basis USD Curve                  ||");

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

  216.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  217.             Map<String, Double> mapMeasures = aFXForward[i].value (
  218.                 valParams,
  219.                 null,
  220.                 csqs,
  221.                 null
  222.             );

  223.             System.out.println (
  224.                 "\t| [" + astrMaturityTenor[i] + "] => " +
  225.                 FormatUtil.FormatDouble (adblFXForward[i], 1, 4, 1.) + " | " +
  226.                 FormatUtil.FormatDouble (fxCurve.fx (astrMaturityTenor[i]), 1, 4, 1.) + " | " +
  227.                 FormatUtil.FormatDouble (mapMeasures.get ("Outright"), 1, 4, 1.) + " | " +
  228.                 FormatUtil.FormatDouble (mapMeasures.get ("PIP"), 1, 4, 1.) + " | " +
  229.                 FormatUtil.FormatDouble (
  230.                     aFXForward[i].discountCurveBasis (
  231.                         valParams,
  232.                         dcEUR,
  233.                         dcUSD,
  234.                         dblSpot,
  235.                         adblFXForward[i],
  236.                         false
  237.                     ), 1, 4, 1.) + " | " +
  238.                 FormatUtil.FormatDouble (
  239.                     aFXForward[i].discountCurveBasis (
  240.                         valParams,
  241.                         dcEUR,
  242.                         dcUSD,
  243.                         dblSpot,
  244.                         adblFXForward[i],
  245.                         true
  246.                     ), 1, 4, 1.) + " || "
  247.             );
  248.         }

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

  250.         int[] aiDateNode = new int[astrMaturityTenor.length];

  251.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  252.             aiDateNode[i] = dtToday.addTenor (astrMaturityTenor[i]).julian();

  253.         double[] adblZeroUSDBasis = fxCurve.zeroBasis (
  254.             aiDateNode,
  255.             valParams,
  256.             dcEUR,
  257.             dcUSD,
  258.             true
  259.         );

  260.         double[] adblZeroEURBasis = fxCurve.zeroBasis (
  261.             aiDateNode,
  262.             valParams,
  263.             dcEUR,
  264.             dcUSD,
  265.             false
  266.         );

  267.         double[] adblBootstrappedUSDBasis = fxCurve.bootstrapBasis (
  268.             aiDateNode,
  269.             valParams,
  270.             dcEUR,
  271.             dcUSD,
  272.             true
  273.         );

  274.         double[] adblBootstrappedEURBasis = fxCurve.bootstrapBasis (
  275.             aiDateNode,
  276.             valParams,
  277.             dcEUR,
  278.             dcUSD,
  279.             false
  280.         );

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

  282.         System.out.println ("\t|                                                                   ||");

  283.         System.out.println ("\t|    Custom FX Curve Builder Metrics #2                             ||");

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

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

  286.         System.out.println ("\t|        FX Forward Tenor                                           ||");

  287.         System.out.println ("\t|        FX Forward Discount Curve Zero Basis USD Curve             ||");

  288.         System.out.println ("\t|        FX Forward Discount Curve Zero Basis EUR Curve             ||");

  289.         System.out.println ("\t|        FX Forward Discount Curve Bootstrapped USD Curve Basis     ||");

  290.         System.out.println ("\t|        FX Forward Discount Curve Bootstrapped EUR Curve Basis     ||");

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

  292.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  293.             System.out.println (
  294.                 "\t| [" + astrMaturityTenor[i] + "] => " +
  295.                 FormatUtil.FormatDouble (adblZeroUSDBasis[i], 1, 4, 100.) + " | " +
  296.                 FormatUtil.FormatDouble (adblZeroEURBasis[i], 1, 4, 100.) + " | " +
  297.                 FormatUtil.FormatDouble (adblBootstrappedUSDBasis[i], 1, 4, 100.) + " | " +
  298.                 FormatUtil.FormatDouble (adblBootstrappedEURBasis[i], 1, 4, 100.) + " ||"
  299.             );

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

  301.         EnvManager.TerminateEnv();
  302.     }
  303. }