FixedBondAPI.java

  1. package org.drip.service.product;

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

  77. /**
  78.  * <i>FixedBondAPI</i> demonstrates the Details behind the Pricing and the Scenario Runs behind a Fixed Bond.
  79.  *
  80.  * <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  84.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/README.md">Environment, Product/Definition Containers, and Scenario/State Manipulation APIs</a></li>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/product/README.md">Product Horizon PnL Attribution Decomposition</a></li>
  86.  *  </ul>
  87.  * <br><br>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class FixedBondAPI {

  92.     /**
  93.      * Generate a Full Map Invocation of the Bond Valuation Run
  94.      *
  95.      * @param strIssuerName Bond Issuer Name
  96.      * @param iBondEffectiveDate Bond Effective Date
  97.      * @param iBondMaturityDate Bond Maturity Date
  98.      * @param dblBondCoupon Bond Coupon
  99.      * @param iBondCouponFrequency Bond Coupon Frequency
  100.      * @param strBondCouponDayCount Bond Coupon Day Count
  101.      * @param strBondCouponCurrency Bond Coupon Currency
  102.      * @param iSpotDate Spot Date
  103.      * @param astrFundingCurveDepositTenor Deposit Instruments Tenor (for Funding Curve)
  104.      * @param adblFundingCurveDepositQuote Deposit Instruments Quote (for Funding Curve)
  105.      * @param strFundingCurveDepositMeasure Deposit Instruments Measure (for Funding Curve)
  106.      * @param adblFundingCurveFuturesQuote Futures Instruments Tenor (for Funding Curve)
  107.      * @param strFundingCurveFuturesMeasure Futures Instruments Measure (for Funding Curve)
  108.      * @param astrFundingCurveFixFloatTenor Fix-Float Instruments Tenor (for Funding Curve)
  109.      * @param adblFundingCurveFixFloatQuote Fix-Float Instruments Quote (for Funding Curve)
  110.      * @param strFundingFixFloatMeasure Fix-Float Instruments Tenor (for Funding Curve)
  111.      * @param strGovvieCode Govvie Bond Code (for Treasury Curve)
  112.      * @param aiGovvieCurveTreasuryEffectiveDate Array of the Treasury Instrument Effective Date (for Treasury
  113.      *      Curve)
  114.      * @param aiGovvieCurveTreasuryMaturityDate Array of the Treasury Instrument Maturity Date (for Treasury
  115.      *      Curve)
  116.      * @param adblGovvieCurveTreasuryCoupon Array of the Treasury Instrument Coupon (for Treasury Curve)
  117.      * @param adblGovvieCurveTreasuryYield Array of the Treasury Instrument Yield (for Treasury Curve)
  118.      * @param strGovvieCurveTreasuryMeasure Treasury Instrument Measure (for Treasury Curve)
  119.      * @param strCreditCurveName Credit Curve Name (for Credit Curve)
  120.      * @param astrCreditCurveCDSTenor CDS Maturity Tenor (for Credit Curve)
  121.      * @param adblCreditCurveCDSCoupon Array of CDS Fixed Coupon (for Credit Curve)
  122.      * @param adblCreditCurveCDSQuote Array of CDS Market Quotes (for Credit Curve)
  123.      * @param strCreditCurveCDSMeasure CDS Calibration Measure (for Credit Curve)
  124.      * @param strBondMarketQuoteName Name of the Bond Market Quote
  125.      * @param dblBondMarketQuote Bond Market Quote Value
  126.      *
  127.      * @return The Output Measure Map
  128.      */

  129.     public static final java.util.Map<java.lang.String, java.lang.Double> ValuationMetrics (
  130.         final java.lang.String strIssuerName,
  131.         final int iBondEffectiveDate,
  132.         final int iBondMaturityDate,
  133.         final double dblBondCoupon,
  134.         final int iBondCouponFrequency,
  135.         final java.lang.String strBondCouponDayCount,
  136.         final java.lang.String strBondCouponCurrency,
  137.         final int iSpotDate,
  138.         final java.lang.String[] astrFundingCurveDepositTenor,
  139.         final double[] adblFundingCurveDepositQuote,
  140.         final java.lang.String strFundingCurveDepositMeasure,
  141.         final double[] adblFundingCurveFuturesQuote,
  142.         final java.lang.String strFundingCurveFuturesMeasure,
  143.         final java.lang.String[] astrFundingCurveFixFloatTenor,
  144.         final double[] adblFundingCurveFixFloatQuote,
  145.         final java.lang.String strFundingFixFloatMeasure,
  146.         final java.lang.String strGovvieCode,
  147.         final int[] aiGovvieCurveTreasuryEffectiveDate,
  148.         final int[] aiGovvieCurveTreasuryMaturityDate,
  149.         final double[] adblGovvieCurveTreasuryCoupon,
  150.         final double[] adblGovvieCurveTreasuryYield,
  151.         final java.lang.String strGovvieCurveTreasuryMeasure,
  152.         final java.lang.String strCreditCurveName,
  153.         final java.lang.String[] astrCreditCurveCDSTenor,
  154.         final double[] adblCreditCurveCDSCoupon,
  155.         final double[] adblCreditCurveCDSQuote,
  156.         final java.lang.String strCreditCurveCDSMeasure,
  157.         final java.lang.String strBondMarketQuoteName,
  158.         final double dblBondMarketQuote)
  159.     {
  160.         org.drip.analytics.date.JulianDate dtSpot = null;
  161.         org.drip.analytics.date.JulianDate dtMaturity = null;
  162.         org.drip.analytics.date.JulianDate dtEffective = null;
  163.         org.drip.analytics.date.JulianDate[] adtGovvieCurveTreasuryMaturity = null;
  164.         org.drip.analytics.date.JulianDate[] adtGovvieCurveTreasuryEffective = null;
  165.         int iNumGovvieCurveMaturity = null == aiGovvieCurveTreasuryMaturityDate ? 0 :
  166.             aiGovvieCurveTreasuryMaturityDate.length;
  167.         int iNumGovvieCurveEffective = null == aiGovvieCurveTreasuryEffectiveDate ? 0 :
  168.             aiGovvieCurveTreasuryEffectiveDate.length;
  169.         java.lang.String[] astrTreasuryBenchmarkCode = new java.lang.String[] {"01YON", "02YON", "03YON",
  170.             "05YON", "07YON", "10YON", "30YON"};
  171.         int iNumTreasuryBenchmark = astrTreasuryBenchmarkCode.length;

  172.         if (0 != iNumGovvieCurveMaturity)
  173.             adtGovvieCurveTreasuryMaturity = new org.drip.analytics.date.JulianDate[iNumGovvieCurveMaturity];

  174.         if (0 != iNumGovvieCurveEffective)
  175.             adtGovvieCurveTreasuryEffective = new
  176.                 org.drip.analytics.date.JulianDate[iNumGovvieCurveEffective];

  177.         org.drip.param.market.CurveSurfaceQuoteContainer csqc = new
  178.             org.drip.param.market.CurveSurfaceQuoteContainer();

  179.         try {
  180.             dtSpot = new org.drip.analytics.date.JulianDate (iSpotDate);

  181.             dtMaturity = new org.drip.analytics.date.JulianDate (iBondMaturityDate);

  182.             dtEffective = new org.drip.analytics.date.JulianDate (iBondEffectiveDate);

  183.             for (int i = 0; i < iNumGovvieCurveMaturity; ++i)
  184.                 adtGovvieCurveTreasuryMaturity[i] = new org.drip.analytics.date.JulianDate
  185.                     (aiGovvieCurveTreasuryMaturityDate[i]);

  186.             for (int i = 0; i < iNumGovvieCurveEffective; ++i)
  187.                 adtGovvieCurveTreasuryEffective[i] = new org.drip.analytics.date.JulianDate
  188.                     (aiGovvieCurveTreasuryEffectiveDate[i]);

  189.             if (null != adblGovvieCurveTreasuryYield && adblGovvieCurveTreasuryYield.length ==
  190.                 iNumTreasuryBenchmark) {
  191.                 for (int i = 0; i < iNumTreasuryBenchmark; ++i) {
  192.                     org.drip.param.quote.ProductMultiMeasure pmm = new
  193.                         org.drip.param.quote.ProductMultiMeasure();

  194.                     pmm.addQuote ("Yield", new org.drip.param.quote.MultiSided ("mid",
  195.                         adblGovvieCurveTreasuryYield[i]), true);

  196.                     if (!csqc.setProductQuote (astrTreasuryBenchmarkCode[i], pmm)) return null;
  197.                 }
  198.             }
  199.         } catch (java.lang.Exception e) {
  200.             e.printStackTrace();

  201.             return null;
  202.         }

  203.         org.drip.product.credit.BondComponent bond = org.drip.product.creator.BondBuilder.CreateSimpleFixed
  204.             (strIssuerName + " " + org.drip.numerical.common.FormatUtil.FormatDouble (dblBondCoupon, 1, 4, 100.)
  205.                 + " " + dtMaturity, strBondCouponCurrency, strIssuerName, dblBondCoupon,
  206.                     iBondCouponFrequency, strBondCouponDayCount, dtEffective, dtMaturity, null, null);

  207.         if (null == bond) return null;

  208.         org.drip.param.quote.ProductMultiMeasure pmm = new org.drip.param.quote.ProductMultiMeasure();

  209.         try {
  210.             pmm.addQuote (strBondMarketQuoteName, new org.drip.param.quote.MultiSided ("mid",
  211.                 dblBondMarketQuote), true);
  212.         } catch (java.lang.Exception e) {
  213.         }

  214.         csqc.setProductQuote (bond.name(), pmm);

  215.         org.drip.state.discount.MergedDiscountForwardCurve dcFunding =
  216.             org.drip.service.template.LatentMarketStateBuilder.SmoothFundingCurve (dtSpot,
  217.                 strBondCouponCurrency, astrFundingCurveDepositTenor, adblFundingCurveDepositQuote,
  218.                     strFundingCurveDepositMeasure, adblFundingCurveFuturesQuote,
  219.                         strFundingCurveFuturesMeasure, astrFundingCurveFixFloatTenor,
  220.                             adblFundingCurveFixFloatQuote, strFundingFixFloatMeasure);

  221.         csqc.setFundingState (dcFunding);

  222.         csqc.setGovvieState (org.drip.service.template.LatentMarketStateBuilder.ShapePreservingGovvieCurve
  223.             (strGovvieCode, dtSpot, adtGovvieCurveTreasuryEffective, adtGovvieCurveTreasuryMaturity,
  224.                 adblGovvieCurveTreasuryCoupon, adblGovvieCurveTreasuryYield, strGovvieCurveTreasuryMeasure));

  225.         csqc.setCreditState (org.drip.service.template.LatentMarketStateBuilder.CreditCurve (dtSpot,
  226.             strCreditCurveName, astrCreditCurveCDSTenor, adblCreditCurveCDSCoupon, adblCreditCurveCDSQuote,
  227.                 strCreditCurveCDSMeasure, dcFunding));

  228.         return bond.value (org.drip.param.valuation.ValuationParams.Spot (iSpotDate), null, csqc, null);
  229.     }

  230.     /**
  231.      * Generate the Treasury Curve Tenor Key Rate Sensitivity/Duration
  232.      *
  233.      * @param strIssuerName Bond Issuer Name
  234.      * @param iBondEffectiveDate Bond Effective Date
  235.      * @param iBondMaturityDate Bond Maturity Date
  236.      * @param dblBondCoupon Bond Coupon
  237.      * @param iBondCouponFrequency Bond Coupon Frequency
  238.      * @param strBondCouponDayCount Bond Coupon Day Count
  239.      * @param strBondCouponCurrency Bond Coupon Currency
  240.      * @param iSpotDate Spot Date
  241.      * @param strGovvieCode Govvie Bond Code (for Treasury Curve)
  242.      * @param aiGovvieCurveTreasuryEffectiveDate Array of the Treasury Instrument Effective Date (for Treasury
  243.      *      Curve)
  244.      * @param aiGovvieCurveTreasuryMaturityDate Array of the Treasury Instrument Maturity Date (for Treasury
  245.      *      Curve)
  246.      * @param adblGovvieCurveTreasuryCoupon Array of the Treasury Instrument Coupon (for Treasury Curve)
  247.      * @param adblGovvieCurveTreasuryYield Array of the Treasury Instrument Yield (for Treasury Curve)
  248.      * @param strGovvieCurveTreasuryMeasure Treasury Instrument Measure (for Govvie Curve)
  249.      * @param dblBondMarketCleanPrice Bond Market Clean Price
  250.      *
  251.      * @return The Treasury Curve Tenor Sensitivity/Duration
  252.      */

  253.     public static final java.util.Map<java.lang.String, java.lang.Double> KeyRateDuration (
  254.         final java.lang.String strIssuerName,
  255.         final int iBondEffectiveDate,
  256.         final int iBondMaturityDate,
  257.         final double dblBondCoupon,
  258.         final int iBondCouponFrequency,
  259.         final java.lang.String strBondCouponDayCount,
  260.         final java.lang.String strBondCouponCurrency,
  261.         final int iSpotDate,
  262.         final java.lang.String strGovvieCode,
  263.         final int[] aiGovvieCurveTreasuryEffectiveDate,
  264.         final int[] aiGovvieCurveTreasuryMaturityDate,
  265.         final double[] adblGovvieCurveTreasuryCoupon,
  266.         final double[] adblGovvieCurveTreasuryYield,
  267.         final java.lang.String strGovvieCurveTreasuryMeasure,
  268.         final double dblBondMarketCleanPrice)
  269.     {
  270.         double dblBaselineOAS = java.lang.Double.NaN;
  271.         org.drip.analytics.date.JulianDate dtSpot = null;
  272.         org.drip.analytics.date.JulianDate dtMaturity = null;
  273.         org.drip.analytics.date.JulianDate dtEffective = null;
  274.         org.drip.analytics.date.JulianDate[] adtGovvieCurveTreasuryMaturity = null;
  275.         org.drip.analytics.date.JulianDate[] adtGovvieCurveTreasuryEffective = null;
  276.         int iNumGovvieCurveMaturity = null == aiGovvieCurveTreasuryMaturityDate ? 0 :
  277.             aiGovvieCurveTreasuryMaturityDate.length;
  278.         int iNumGovvieCurveEffective = null == aiGovvieCurveTreasuryEffectiveDate ? 0 :
  279.             aiGovvieCurveTreasuryEffectiveDate.length;
  280.         java.lang.String[] astrTreasuryBenchmarkCode = new java.lang.String[] {"01YON", "02YON", "03YON",
  281.             "05YON", "07YON", "10YON", "30YON"};
  282.         int iNumTreasuryBenchmark = astrTreasuryBenchmarkCode.length;

  283.         if (0 != iNumGovvieCurveMaturity)
  284.             adtGovvieCurveTreasuryMaturity = new org.drip.analytics.date.JulianDate[iNumGovvieCurveMaturity];

  285.         if (0 != iNumGovvieCurveEffective)
  286.             adtGovvieCurveTreasuryEffective = new
  287.                 org.drip.analytics.date.JulianDate[iNumGovvieCurveEffective];

  288.         org.drip.param.market.CurveSurfaceQuoteContainer csqc = new
  289.             org.drip.param.market.CurveSurfaceQuoteContainer();

  290.         try {
  291.             dtSpot = new org.drip.analytics.date.JulianDate (iSpotDate);

  292.             dtMaturity = new org.drip.analytics.date.JulianDate (iBondMaturityDate);

  293.             dtEffective = new org.drip.analytics.date.JulianDate (iBondEffectiveDate);

  294.             for (int i = 0; i < iNumGovvieCurveMaturity; ++i)
  295.                 adtGovvieCurveTreasuryMaturity[i] = new org.drip.analytics.date.JulianDate
  296.                     (aiGovvieCurveTreasuryMaturityDate[i]);

  297.             for (int i = 0; i < iNumGovvieCurveEffective; ++i)
  298.                 adtGovvieCurveTreasuryEffective[i] = new org.drip.analytics.date.JulianDate
  299.                     (aiGovvieCurveTreasuryEffectiveDate[i]);

  300.             if (null != adblGovvieCurveTreasuryYield && adblGovvieCurveTreasuryYield.length ==
  301.                 iNumTreasuryBenchmark) {
  302.                 for (int i = 0; i < iNumTreasuryBenchmark; ++i) {
  303.                     org.drip.param.quote.ProductMultiMeasure pmm = new
  304.                         org.drip.param.quote.ProductMultiMeasure();

  305.                     pmm.addQuote ("Yield", new org.drip.param.quote.MultiSided ("mid",
  306.                         adblGovvieCurveTreasuryYield[i]), true);

  307.                     if (!csqc.setProductQuote (astrTreasuryBenchmarkCode[i], pmm)) return null;
  308.                 }
  309.             }
  310.         } catch (java.lang.Exception e) {
  311.             e.printStackTrace();

  312.             return null;
  313.         }

  314.         org.drip.product.credit.BondComponent bond = org.drip.product.creator.BondBuilder.CreateSimpleFixed
  315.             (strIssuerName + " " + org.drip.numerical.common.FormatUtil.FormatDouble (dblBondCoupon, 1, 4, 100.)
  316.                 + " " + dtMaturity, strBondCouponCurrency, strIssuerName, dblBondCoupon,
  317.                     iBondCouponFrequency, strBondCouponDayCount, dtEffective, dtMaturity, null, null);

  318.         if (null == bond) return null;

  319.         org.drip.param.quote.ProductMultiMeasure pmm = new org.drip.param.quote.ProductMultiMeasure();

  320.         try {
  321.             pmm.addQuote ("Price", new org.drip.param.quote.MultiSided ("mid", dblBondMarketCleanPrice),
  322.                 true);
  323.         } catch (java.lang.Exception e) {
  324.             e.printStackTrace();

  325.             return null;
  326.         }

  327.         csqc.setProductQuote (bond.name(), pmm);

  328.         org.drip.state.govvie.GovvieCurve gc =
  329.             org.drip.service.template.LatentMarketStateBuilder.ShapePreservingGovvieCurve (strGovvieCode,
  330.                 dtSpot, adtGovvieCurveTreasuryEffective, adtGovvieCurveTreasuryMaturity,
  331.                     adblGovvieCurveTreasuryCoupon, adblGovvieCurveTreasuryYield,
  332.                         strGovvieCurveTreasuryMeasure);

  333.         csqc.setGovvieState (gc);

  334.         org.drip.param.valuation.ValuationParams valParams = org.drip.param.valuation.ValuationParams.Spot
  335.             (iSpotDate);

  336.         try {
  337.             if (!org.drip.numerical.common.NumberUtil.IsValid (dblBaselineOAS = bond.oasFromPrice (valParams,
  338.                 csqc, null, dblBondMarketCleanPrice)))
  339.                 return null;
  340.         } catch (java.lang.Exception e) {
  341.             e.printStackTrace();

  342.             return null;
  343.         }

  344.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.govvie.GovvieCurve>
  345.             mapTenorGovvieCurve = org.drip.service.template.LatentMarketStateBuilder.BumpedGovvieCurve
  346.                 (strGovvieCode, dtSpot, adtGovvieCurveTreasuryEffective, adtGovvieCurveTreasuryMaturity,
  347.                     adblGovvieCurveTreasuryCoupon, adblGovvieCurveTreasuryYield,
  348.                         strGovvieCurveTreasuryMeasure,
  349.                             org.drip.service.template.LatentMarketStateBuilder.SHAPE_PRESERVING, 0.0001,
  350.                                 false);

  351.         if (null == mapTenorGovvieCurve || iNumTreasuryBenchmark > mapTenorGovvieCurve.size()) return null;

  352.         org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> mapKeyRateDuration = new
  353.             org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double>();

  354.         for (java.util.Map.Entry<java.lang.String, org.drip.state.govvie.GovvieCurve> me :
  355.             mapTenorGovvieCurve.entrySet()) {
  356.             java.lang.String strKey = me.getKey();

  357.             if (!strKey.contains ("tsy")) continue;

  358.             if (!csqc.setGovvieState (me.getValue())) return null;

  359.             try {
  360.                 mapKeyRateDuration.put (strKey, 10000. * (bond.priceFromOAS (valParams, csqc, null,
  361.                     dblBaselineOAS) - dblBondMarketCleanPrice) / dblBondMarketCleanPrice);
  362.             } catch (java.lang.Exception e) {
  363.                 e.printStackTrace();

  364.                 return null;
  365.             }
  366.         }

  367.         return mapKeyRateDuration;
  368.     }

  369.     /**
  370.      * Returns Attribution for the Specified Bond Instance
  371.      *
  372.      * @param strIssuerName Bond Issuer Name
  373.      * @param iBondEffectiveDate Bond Effective Date
  374.      * @param iBondMaturityDate Bond Maturity Date
  375.      * @param dblBondCoupon Bond Coupon
  376.      * @param iBondCouponFrequency Bond Coupon Frequency
  377.      * @param strBondCouponDayCount Bond Coupon Day Count
  378.      * @param strBondCouponCurrency Bond Coupon Currency
  379.      * @param adtSpot Array of Spot Dates
  380.      * @param adblCleanPrice Array of Closing Clean Prices
  381.      *
  382.      * @return List of the Position Change Components
  383.      */

  384.     public static final java.util.List<org.drip.historical.attribution.PositionChangeComponents>
  385.         HorizonChangeAttribution (
  386.             final java.lang.String strIssuerName,
  387.             final int iBondEffectiveDate,
  388.             final int iBondMaturityDate,
  389.             final double dblBondCoupon,
  390.             final int iBondCouponFrequency,
  391.             final java.lang.String strBondCouponDayCount,
  392.             final java.lang.String strBondCouponCurrency,
  393.             final org.drip.analytics.date.JulianDate[] adtSpot,
  394.             final double[] adblCleanPrice)
  395.     {
  396.         org.drip.analytics.date.JulianDate dtMaturity = null;
  397.         org.drip.analytics.date.JulianDate dtEffective = null;

  398.         try {
  399.             dtMaturity = new org.drip.analytics.date.JulianDate (iBondMaturityDate);

  400.             dtEffective = new org.drip.analytics.date.JulianDate (iBondEffectiveDate);
  401.         } catch (java.lang.Exception e) {
  402.             e.printStackTrace();

  403.             return null;
  404.         }

  405.         org.drip.product.credit.BondComponent bond = org.drip.product.creator.BondBuilder.CreateSimpleFixed
  406.             (strIssuerName + " " + org.drip.numerical.common.FormatUtil.FormatDouble (dblBondCoupon, 1, 4, 100.)
  407.                 + " " + dtMaturity, strBondCouponCurrency, strIssuerName, dblBondCoupon,
  408.                     iBondCouponFrequency, strBondCouponDayCount, dtEffective, dtMaturity, null, null);

  409.         if (null == bond || null == adtSpot || null == adblCleanPrice) return null;

  410.         int iNumCloses = adtSpot.length;
  411.         int[] aiSpotDate = new int[iNumCloses];
  412.         double[] adblYield = new double[iNumCloses];
  413.         double[] adblDirtyPrice = new double[iNumCloses];
  414.         double[] adblModifiedDuration = new double[iNumCloses];

  415.         if (1 >= iNumCloses || iNumCloses != adblCleanPrice.length) return null;

  416.         for (int i = 0; i < iNumCloses; ++i) {
  417.             org.drip.param.valuation.ValuationParams valParamsSpot =
  418.                 org.drip.param.valuation.ValuationParams.Spot (aiSpotDate[i] = adtSpot[i].julian());

  419.             try {
  420.                 if (!org.drip.numerical.common.NumberUtil.IsValid (adblYield[i] = bond.yieldFromPrice
  421.                     (valParamsSpot, null, null, adblCleanPrice[i])))
  422.                     return null;

  423.                 if (!org.drip.numerical.common.NumberUtil.IsValid (adblModifiedDuration[i] =
  424.                     bond.modifiedDurationFromPrice (valParamsSpot, null, null, adblCleanPrice[i])))
  425.                     return null;

  426.                 if (!org.drip.numerical.common.NumberUtil.IsValid (adblDirtyPrice[i] = adblCleanPrice[i] +
  427.                     bond.accrued (aiSpotDate[i], null)))
  428.                     return null;
  429.             } catch (java.lang.Exception e) {
  430.                 e.printStackTrace();

  431.                 return null;
  432.             }
  433.         }

  434.         java.util.List<org.drip.historical.attribution.PositionChangeComponents> lsPCC = new
  435.             java.util.ArrayList<org.drip.historical.attribution.PositionChangeComponents>();

  436.         for (int i = 1; i < iNumCloses; ++i) {
  437.             try {
  438.                 org.drip.historical.attribution.BondMarketSnap bpms1 = new
  439.                     org.drip.historical.attribution.BondMarketSnap (adtSpot[i - 1],
  440.                         adblCleanPrice[i - 1]);

  441.                 if (!bpms1.setYieldMarketFactor (adblYield[i - 1], -1. * adblDirtyPrice[i - 1] *
  442.                     adblModifiedDuration[i - 1], 0.))
  443.                     return null;

  444.                 org.drip.historical.attribution.BondMarketSnap bpms2 = new
  445.                     org.drip.historical.attribution.BondMarketSnap (adtSpot[i], adblCleanPrice[i]);

  446.                 if (!bpms2.setYieldMarketFactor (adblYield[i], -1. * adblDirtyPrice[i] *
  447.                     adblModifiedDuration[i], 0.))
  448.                     return null;

  449.                 lsPCC.add (new org.drip.historical.attribution.PositionChangeComponents (false, bpms1, bpms2,
  450.                     org.drip.analytics.daycount.Convention.YearFraction (aiSpotDate[i - 1],
  451.                         aiSpotDate[i], strBondCouponDayCount, false, null, strBondCouponCurrency), null));
  452.             } catch (java.lang.Exception e) {
  453.                 e.printStackTrace();

  454.                 return null;
  455.             }
  456.         }

  457.         return lsPCC;
  458.     }

  459.     /**
  460.      * Generate the Relative Value Metrics for the Specified Bond
  461.      *
  462.      * @param strIssuerName Bond Issuer Name
  463.      * @param iBondEffectiveDate Bond Effective Date
  464.      * @param iBondMaturityDate Bond Maturity Date
  465.      * @param dblBondCoupon Bond Coupon
  466.      * @param iBondCouponFrequency Bond Coupon Frequency
  467.      * @param strBondCouponDayCount Bond Coupon Day Count
  468.      * @param strBondCouponCurrency Bond Coupon Currency
  469.      * @param iSpotDate Spot Date
  470.      * @param astrFundingCurveDepositTenor Deposit Instruments Tenor (for Funding Curve)
  471.      * @param adblFundingCurveDepositQuote Deposit Instruments Quote (for Funding Curve)
  472.      * @param strFundingCurveDepositMeasure Deposit Instruments Measure (for Funding Curve)
  473.      * @param adblFundingCurveFuturesQuote Futures Instruments Tenor (for Funding Curve)
  474.      * @param strFundingCurveFuturesMeasure Futures Instruments Measure (for Funding Curve)
  475.      * @param astrFundingCurveFixFloatTenor Fix-Float Instruments Tenor (for Funding Curve)
  476.      * @param adblFundingCurveFixFloatQuote Fix-Float Instruments Quote (for Funding Curve)
  477.      * @param strFundingFixFloatMeasure Fix-Float Instruments Tenor (for Funding Curve)
  478.      * @param strGovvieCode Govvie Bond Code (for Treasury Curve)
  479.      * @param aiGovvieCurveTreasuryEffectiveDate Array of the Treasury Instrument Effective Date (for Treasury
  480.      *      Curve)
  481.      * @param aiGovvieCurveTreasuryMaturityDate Array of the Treasury Instrument Maturity Date (for Treasury
  482.      *      Curve)
  483.      * @param adblGovvieCurveTreasuryCoupon Array of the Treasury Instrument Coupon (for Treasury Curve)
  484.      * @param adblGovvieCurveTreasuryYield Array of the Treasury Instrument Yield (for Treasury Curve)
  485.      * @param strGovvieCurveTreasuryMeasure Treasury Instrument Measure (for Treasury Curve)
  486.      * @param strCreditCurveName Credit Curve Name (for Credit Curve)
  487.      * @param astrCreditCurveCDSTenor CDS Maturity Tenor (for Credit Curve)
  488.      * @param adblCreditCurveCDSCoupon Array of CDS Fixed Coupon (for Credit Curve)
  489.      * @param adblCreditCurveCDSQuote Array of CDS Market Quotes (for Credit Curve)
  490.      * @param strCreditCurveCDSMeasure CDS Calibration Measure (for Credit Curve)
  491.      * @param dblBondMarketCleanPrice Bond Market Clean Price
  492.      *
  493.      * @return The Relative Value Metrics
  494.      */

  495.     public static final org.drip.analytics.output.BondRVMeasures RelativeValueMetrics (
  496.         final java.lang.String strIssuerName,
  497.         final int iBondEffectiveDate,
  498.         final int iBondMaturityDate,
  499.         final double dblBondCoupon,
  500.         final int iBondCouponFrequency,
  501.         final java.lang.String strBondCouponDayCount,
  502.         final java.lang.String strBondCouponCurrency,
  503.         final int iSpotDate,
  504.         final java.lang.String[] astrFundingCurveDepositTenor,
  505.         final double[] adblFundingCurveDepositQuote,
  506.         final java.lang.String strFundingCurveDepositMeasure,
  507.         final double[] adblFundingCurveFuturesQuote,
  508.         final java.lang.String strFundingCurveFuturesMeasure,
  509.         final java.lang.String[] astrFundingCurveFixFloatTenor,
  510.         final double[] adblFundingCurveFixFloatQuote,
  511.         final java.lang.String strFundingFixFloatMeasure,
  512.         final java.lang.String strGovvieCode,
  513.         final int[] aiGovvieCurveTreasuryEffectiveDate,
  514.         final int[] aiGovvieCurveTreasuryMaturityDate,
  515.         final double[] adblGovvieCurveTreasuryCoupon,
  516.         final double[] adblGovvieCurveTreasuryYield,
  517.         final java.lang.String strGovvieCurveTreasuryMeasure,
  518.         final java.lang.String strCreditCurveName,
  519.         final java.lang.String[] astrCreditCurveCDSTenor,
  520.         final double[] adblCreditCurveCDSCoupon,
  521.         final double[] adblCreditCurveCDSQuote,
  522.         final java.lang.String strCreditCurveCDSMeasure,
  523.         final double dblBondMarketCleanPrice)
  524.     {
  525.         org.drip.analytics.date.JulianDate dtSpot = null;
  526.         org.drip.analytics.date.JulianDate dtMaturity = null;
  527.         org.drip.analytics.date.JulianDate dtEffective = null;
  528.         org.drip.analytics.date.JulianDate[] adtGovvieCurveTreasuryMaturity = null;
  529.         org.drip.analytics.date.JulianDate[] adtGovvieCurveTreasuryEffective = null;
  530.         int iNumGovvieCurveMaturity = null == aiGovvieCurveTreasuryMaturityDate ? 0 :
  531.             aiGovvieCurveTreasuryMaturityDate.length;
  532.         int iNumGovvieCurveEffective = null == aiGovvieCurveTreasuryEffectiveDate ? 0 :
  533.             aiGovvieCurveTreasuryEffectiveDate.length;
  534.         java.lang.String[] astrTreasuryBenchmarkCode = new java.lang.String[] {"01YON", "02YON", "03YON",
  535.             "05YON", "07YON", "10YON", "30YON"};
  536.         int iNumTreasuryBenchmark = astrTreasuryBenchmarkCode.length;

  537.         if (0 != iNumGovvieCurveMaturity)
  538.             adtGovvieCurveTreasuryMaturity = new org.drip.analytics.date.JulianDate[iNumGovvieCurveMaturity];

  539.         if (0 != iNumGovvieCurveEffective)
  540.             adtGovvieCurveTreasuryEffective = new
  541.                 org.drip.analytics.date.JulianDate[iNumGovvieCurveEffective];

  542.         org.drip.param.market.CurveSurfaceQuoteContainer csqc = new
  543.             org.drip.param.market.CurveSurfaceQuoteContainer();

  544.         try {
  545.             dtSpot = new org.drip.analytics.date.JulianDate (iSpotDate);

  546.             dtMaturity = new org.drip.analytics.date.JulianDate (iBondMaturityDate);

  547.             dtEffective = new org.drip.analytics.date.JulianDate (iBondEffectiveDate);

  548.             for (int i = 0; i < iNumGovvieCurveMaturity; ++i)
  549.                 adtGovvieCurveTreasuryMaturity[i] = new org.drip.analytics.date.JulianDate
  550.                     (aiGovvieCurveTreasuryMaturityDate[i]);

  551.             for (int i = 0; i < iNumGovvieCurveEffective; ++i)
  552.                 adtGovvieCurveTreasuryEffective[i] = new org.drip.analytics.date.JulianDate
  553.                     (aiGovvieCurveTreasuryEffectiveDate[i]);

  554.             if (null != adblGovvieCurveTreasuryYield && adblGovvieCurveTreasuryYield.length ==
  555.                 iNumTreasuryBenchmark) {
  556.                 for (int i = 0; i < iNumTreasuryBenchmark; ++i) {
  557.                     org.drip.param.quote.ProductMultiMeasure pmm = new
  558.                         org.drip.param.quote.ProductMultiMeasure();

  559.                     pmm.addQuote ("Yield", new org.drip.param.quote.MultiSided ("mid",
  560.                         adblGovvieCurveTreasuryYield[i]), true);

  561.                     if (!csqc.setProductQuote (astrTreasuryBenchmarkCode[i], pmm)) return null;
  562.                 }
  563.             }
  564.         } catch (java.lang.Exception e) {
  565.             e.printStackTrace();

  566.             return null;
  567.         }

  568.         org.drip.product.credit.BondComponent bond = org.drip.product.creator.BondBuilder.CreateSimpleFixed
  569.             (strIssuerName + " " + org.drip.numerical.common.FormatUtil.FormatDouble (dblBondCoupon, 1, 4, 100.)
  570.                 + " " + dtMaturity, strBondCouponCurrency, strIssuerName, dblBondCoupon,
  571.                     iBondCouponFrequency, strBondCouponDayCount, dtEffective, dtMaturity, null, null);

  572.         if (null == bond) return null;

  573.         org.drip.param.quote.ProductMultiMeasure pmm = new org.drip.param.quote.ProductMultiMeasure();

  574.         try {
  575.             pmm.addQuote ("Price", new org.drip.param.quote.MultiSided ("mid", dblBondMarketCleanPrice),
  576.                 true);
  577.         } catch (java.lang.Exception e) {
  578.         }

  579.         csqc.setProductQuote (bond.name(), pmm);

  580.         org.drip.state.discount.MergedDiscountForwardCurve dcFunding =
  581.             org.drip.service.template.LatentMarketStateBuilder.SmoothFundingCurve (dtSpot,
  582.                 strBondCouponCurrency, astrFundingCurveDepositTenor, adblFundingCurveDepositQuote,
  583.                     strFundingCurveDepositMeasure, adblFundingCurveFuturesQuote,
  584.                         strFundingCurveFuturesMeasure, astrFundingCurveFixFloatTenor,
  585.                             adblFundingCurveFixFloatQuote, strFundingFixFloatMeasure);

  586.         csqc.setFundingState (dcFunding);

  587.         csqc.setGovvieState (org.drip.service.template.LatentMarketStateBuilder.ShapePreservingGovvieCurve
  588.             (strGovvieCode, dtSpot, adtGovvieCurveTreasuryEffective, adtGovvieCurveTreasuryMaturity,
  589.                 adblGovvieCurveTreasuryCoupon, adblGovvieCurveTreasuryYield, strGovvieCurveTreasuryMeasure));

  590.         csqc.setCreditState (org.drip.service.template.LatentMarketStateBuilder.CreditCurve (dtSpot,
  591.             strCreditCurveName, astrCreditCurveCDSTenor, adblCreditCurveCDSCoupon, adblCreditCurveCDSQuote,
  592.                 strCreditCurveCDSMeasure, dcFunding));

  593.         org.drip.param.valuation.ValuationParams valParams = org.drip.param.valuation.ValuationParams.Spot
  594.             (iSpotDate);

  595.         return bond.standardMeasures (valParams, null, csqc, null, bond.exerciseYieldFromPrice (valParams,
  596.             csqc, null, dblBondMarketCleanPrice), dblBondMarketCleanPrice);
  597.     }
  598. }