FRAStandardComponent.java

  1. package org.drip.product.fra;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  13.  *
  14.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  15.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  16.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  17.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  18.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  19.  *      and computational support.
  20.  *  
  21.  *      https://lakshmidrip.github.io/DROP/
  22.  *  
  23.  *  DROP is composed of three modules:
  24.  *  
  25.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  26.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  27.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  28.  *
  29.  *  DROP Product Core implements libraries for the following:
  30.  *  - Fixed Income Analytics
  31.  *  - Loan Analytics
  32.  *  - Transaction Cost Analytics
  33.  *
  34.  *  DROP Portfolio Core implements libraries for the following:
  35.  *  - Asset Allocation Analytics
  36.  *  - Asset Liability Management Analytics
  37.  *  - Capital Estimation Analytics
  38.  *  - Exposure Analytics
  39.  *  - Margin Analytics
  40.  *  - XVA Analytics
  41.  *
  42.  *  DROP Computational Core implements libraries for the following:
  43.  *  - Algorithm Support
  44.  *  - Computation Support
  45.  *  - Function Analysis
  46.  *  - Model Validation
  47.  *  - Numerical Analysis
  48.  *  - Numerical Optimizer
  49.  *  - Spline Builder
  50.  *  - Statistical Learning
  51.  *
  52.  *  Documentation for DROP is Spread Over:
  53.  *
  54.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  55.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  56.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  57.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  58.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  59.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  60.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  61.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  62.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  63.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  64.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  65.  *
  66.  *  Licensed under the Apache License, Version 2.0 (the "License");
  67.  *      you may not use this file except in compliance with the License.
  68.  *  
  69.  *  You may obtain a copy of the License at
  70.  *      http://www.apache.org/licenses/LICENSE-2.0
  71.  *  
  72.  *  Unless required by applicable law or agreed to in writing, software
  73.  *      distributed under the License is distributed on an "AS IS" BASIS,
  74.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  75.  *  
  76.  *  See the License for the specific language governing permissions and
  77.  *      limitations under the License.
  78.  */

  79. /**
  80.  * <i>FRAStandardComponent</i> contains the implementation of the Standard Multi-Curve FRA Component.
  81.  *
  82.  *  <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  86.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/README.md">Product Components/Baskets for Credit, FRA, FX, Govvie, Rates, and Option AssetClasses</a></li>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/fra/README.md">Standard/Market FRAs - Caps/Floors</a></li>
  88.  *  </ul>
  89.  * <br><br>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class FRAStandardComponent extends org.drip.product.rates.SingleStreamComponent {
  94.     private double _dblStrike = java.lang.Double.NaN;

  95.     /**
  96.      * FRAStandardComponent constructor
  97.      *
  98.      * @param strName Futures Component Name
  99.      * @param stream Futures Stream
  100.      * @param dblStrike Futures Strike
  101.      * @param csp Cash Settle Parameters Instance
  102.      *
  103.      * @throws java.lang.Exception Thrown if Inputs are Invalid
  104.      */

  105.     public FRAStandardComponent (
  106.         final java.lang.String strName,
  107.         final org.drip.product.rates.Stream stream,
  108.         final double dblStrike,
  109.         final org.drip.param.valuation.CashSettleParams csp)
  110.         throws java.lang.Exception
  111.     {
  112.         super (strName, stream, csp);

  113.         if (!org.drip.numerical.common.NumberUtil.IsValid (_dblStrike = dblStrike))
  114.             throw new java.lang.Exception ("FRAStandardComponent ctr => Invalid Inputs!");
  115.     }

  116.     @Override public org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> value (
  117.         final org.drip.param.valuation.ValuationParams valParams,
  118.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  119.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  120.         final org.drip.param.valuation.ValuationCustomizationParams vcp)
  121.     {
  122.         if (null == valParams || null == csqs) return null;

  123.         org.drip.state.identifier.FundingLabel fundingLabel =
  124.             org.drip.state.identifier.FundingLabel.Standard (payCurrency());

  125.         org.drip.state.discount.MergedDiscountForwardCurve dcFunding = csqs.fundingState (fundingLabel);

  126.         if (null == dcFunding) return null;

  127.         long lStart = System.nanoTime();

  128.         double dblParForward = java.lang.Double.NaN;

  129.         int iValueDate = valParams.valueDate();

  130.         int iEffectiveDate = effectiveDate().julian();

  131.         if (iValueDate > iEffectiveDate) return null;

  132.         org.drip.analytics.date.JulianDate dtMaturity = maturityDate();

  133.         int iMaturityDate = dtMaturity.julian();

  134.         org.drip.state.identifier.ForwardLabel forwardLabel = forwardLabel().get ("DERIVED");

  135.         org.drip.state.forward.ForwardRateEstimator fc = csqs.forwardState (forwardLabel);

  136.         if (null == fc || !forwardLabel.match (fc.index())) return null;

  137.         org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> mapResult = new
  138.             org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double>();

  139.         org.drip.param.valuation.CashSettleParams settleParams = cashSettleParams();

  140.         try {
  141.             int dblCashSettle = null == settleParams ? valParams.cashPayDate() : settleParams.cashSettleDate
  142.                 (iValueDate);

  143.             dblParForward = csqs.available (dtMaturity, forwardLabel) ? csqs.fixing (dtMaturity,
  144.                 forwardLabel) : fc.forward (dtMaturity);

  145.             double dblMultiplicativeQuantoAdjustment = java.lang.Math.exp
  146.                 (org.drip.analytics.support.OptionHelper.IntegratedCrossVolQuanto (csqs.forwardVolatility
  147.                     (forwardLabel), csqs.fundingVolatility (fundingLabel), csqs.forwardFundingCorrelation
  148.                         (forwardLabel, fundingLabel), iValueDate, iEffectiveDate));

  149.             double dblDCF = org.drip.analytics.daycount.Convention.YearFraction (iEffectiveDate,
  150.                 iMaturityDate, stream().couponDC(), false, null, stream().calendar());

  151.             double dblQuantoAdjustedParForward = dblParForward * dblMultiplicativeQuantoAdjustment;

  152.             double dblDV01 = 0.0001 * dblDCF * dcFunding.df (iMaturityDate) / dcFunding.df (dblCashSettle)
  153.                 * notional (iValueDate);

  154.             double dblLevelLift = dblQuantoAdjustedParForward - _dblStrike;
  155.             double dblPV = dblDV01 * dblLevelLift;
  156.             double dblLevelCapLift = dblLevelLift < 0. ? -1. * dblLevelLift : 0.;
  157.             double dblLevelFloorLift = dblLevelLift > 0. ? dblLevelLift : 0.;

  158.             double dblDCParForward = dcFunding.libor (iEffectiveDate, iMaturityDate);

  159.             mapResult.put ("additivequantoadjustment", dblQuantoAdjustedParForward - dblParForward);

  160.             mapResult.put ("caplift", dblLevelCapLift);

  161.             mapResult.put ("discountcurveadditivebasis", dblQuantoAdjustedParForward - dblDCParForward);

  162.             mapResult.put ("discountcurvemultiplicativebasis", dblQuantoAdjustedParForward /
  163.                 dblDCParForward);

  164.             mapResult.put ("discountcurveparforward", dblDCParForward);

  165.             mapResult.put ("dv01", dblDV01);

  166.             mapResult.put ("floorlift", dblLevelFloorLift);

  167.             mapResult.put ("forward", dblParForward);

  168.             mapResult.put ("forwardrate", dblParForward);

  169.             mapResult.put ("mercuriorfactor", (dblDCF * dblDCParForward + 1.) / (dblDCF *
  170.                 dblQuantoAdjustedParForward + 1.));

  171.             mapResult.put ("multiplicativequantoadjustment", dblMultiplicativeQuantoAdjustment);

  172.             mapResult.put ("parforward", dblParForward);

  173.             mapResult.put ("parforwardrate", dblParForward);

  174.             mapResult.put ("price", dblPV);

  175.             mapResult.put ("pv", dblPV);

  176.             mapResult.put ("quantoadjustedparforward", dblQuantoAdjustedParForward);

  177.             mapResult.put ("upfront", dblPV);
  178.         } catch (java.lang.Exception e) {
  179.             e.printStackTrace();

  180.             return null;
  181.         }

  182.         mapResult.put ("calctime", (System.nanoTime() - lStart) * 1.e-09);

  183.         return mapResult;
  184.     }

  185.     @Override public java.util.Set<java.lang.String> measureNames()
  186.     {
  187.         java.util.Set<java.lang.String> setstrMeasureNames = new java.util.TreeSet<java.lang.String>();

  188.         setstrMeasureNames.add ("AdditiveQuantoAdjustment");

  189.         setstrMeasureNames.add ("CalcTime");

  190.         setstrMeasureNames.add ("CapLift");

  191.         setstrMeasureNames.add ("DiscountCurveAdditiveBasis");

  192.         setstrMeasureNames.add ("DiscountCurveMultiplicativeBasis");

  193.         setstrMeasureNames.add ("DiscountCurveParForward");

  194.         setstrMeasureNames.add ("DV01");

  195.         setstrMeasureNames.add ("FloorLift");

  196.         setstrMeasureNames.add ("Forward");

  197.         setstrMeasureNames.add ("ForwardRate");

  198.         setstrMeasureNames.add ("MercurioRFactor");

  199.         setstrMeasureNames.add ("MultiplicativeQuantoAdjustment");

  200.         setstrMeasureNames.add ("ParForward");

  201.         setstrMeasureNames.add ("ParForwardRate");

  202.         setstrMeasureNames.add ("Price");

  203.         setstrMeasureNames.add ("PV");

  204.         setstrMeasureNames.add ("QuantoAdjustedParForward");

  205.         setstrMeasureNames.add ("Upfront");

  206.         return setstrMeasureNames;
  207.     }

  208.     @Override public org.drip.numerical.differentiation.WengertJacobian jackDDirtyPVDManifestMeasure (
  209.         final org.drip.param.valuation.ValuationParams valParams,
  210.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  211.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  212.         final org.drip.param.valuation.ValuationCustomizationParams vcp)
  213.     {
  214.         return null;
  215.     }

  216.     @Override public org.drip.numerical.differentiation.WengertJacobian manifestMeasureDFMicroJack (
  217.         final java.lang.String strManifestMeasure,
  218.         final org.drip.param.valuation.ValuationParams valParams,
  219.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  220.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  221.         final org.drip.param.valuation.ValuationCustomizationParams vcp)
  222.     {
  223.         return null;
  224.     }

  225.     @Override public org.drip.product.calib.ProductQuoteSet calibQuoteSet (
  226.         final org.drip.state.representation.LatentStateSpecification[] aLSS)
  227.     {
  228.         try {
  229.             return new org.drip.product.calib.FRAComponentQuoteSet (aLSS);
  230.         } catch (java.lang.Exception e) {
  231.             e.printStackTrace();
  232.         }

  233.         return null;
  234.     }

  235.     @Override public org.drip.state.estimator.PredictorResponseWeightConstraint fundingPRWC (
  236.         final org.drip.param.valuation.ValuationParams valParams,
  237.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  238.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  239.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  240.         final org.drip.product.calib.ProductQuoteSet pqs)
  241.     {
  242.         return null;
  243.     }

  244.     @Override public org.drip.state.estimator.PredictorResponseWeightConstraint forwardPRWC (
  245.         final org.drip.param.valuation.ValuationParams valParams,
  246.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  247.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  248.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  249.         final org.drip.product.calib.ProductQuoteSet pqs)
  250.     {
  251.         if (null == valParams || null == pqs || !(pqs instanceof
  252.             org.drip.product.calib.FRAComponentQuoteSet))
  253.             return null;

  254.         if (valParams.valueDate() > effectiveDate().julian()) return null;

  255.         org.drip.product.calib.FRAComponentQuoteSet fcqs = (org.drip.product.calib.FRAComponentQuoteSet) pqs;

  256.         if (!fcqs.containsFRARate() && !fcqs.containsParForwardRate()) return null;

  257.         double dblForwardRate = java.lang.Double.NaN;

  258.         try {
  259.             if (fcqs.containsParForwardRate())
  260.                 dblForwardRate = fcqs.parForwardRate();
  261.             else if (fcqs.containsFRARate())
  262.                 dblForwardRate = fcqs.fraRate();
  263.         } catch (java.lang.Exception e) {
  264.             e.printStackTrace();

  265.             return null;
  266.         }

  267.         org.drip.state.estimator.PredictorResponseWeightConstraint prwc = new
  268.             org.drip.state.estimator.PredictorResponseWeightConstraint();

  269.         double dblMaturity = maturityDate().julian();

  270.         if (!prwc.addPredictorResponseWeight (dblMaturity, 1.)) return null;

  271.         if (!prwc.addDResponseWeightDManifestMeasure ("Rate", dblMaturity, 1.)) return null;

  272.         if (!prwc.updateValue (dblForwardRate)) return null;

  273.         if (!prwc.updateDValueDManifestMeasure ("Rate", 1.)) return null;

  274.         return prwc;
  275.     }

  276.     /**
  277.      * Retrieve the FRA Strike
  278.      *
  279.      * @return The FRA Strike
  280.      */

  281.     public double strike()
  282.     {
  283.         return _dblStrike;
  284.     }
  285. }