ConstantPaymentBond.java

  1. package org.drip.sample.assetbacked;

  2. import org.drip.analytics.cashflow.CompositePeriod;
  3. import org.drip.analytics.date.*;
  4. import org.drip.analytics.daycount.Convention;
  5. import org.drip.analytics.support.Helper;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.param.market.CurveSurfaceQuoteContainer;
  8. import org.drip.param.valuation.ValuationParams;
  9. import org.drip.product.creator.ConstantPaymentBondBuilder;
  10. import org.drip.product.definition.Bond;
  11. import org.drip.service.env.EnvManager;

  12. /*
  13.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  14.  */

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

  88. /**
  89.  * <i>ConstantPaymentBond</i> demonstrates the Construction and Valuation of a Custom Constant Payment
  90.  * Mortgage Bond.
  91.  *  
  92.  * <br><br>
  93.  *  <ul>
  94.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  95.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  96.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  97.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/assetbacked/README.md">ABS Custom Cash Flow Bonds</a></li>
  98.  *  </ul>
  99.  * <br><br>
  100.  *
  101.  * @author Lakshmi Krishnamurthy
  102.  */

  103. public class ConstantPaymentBond {

  104.     private static final void BondMetrics (
  105.         final Bond bond,
  106.         final double dblInitialNotional,
  107.         final JulianDate dtSettle,
  108.         final CurveSurfaceQuoteContainer mktParams,
  109.         final double dblCleanPrice)
  110.         throws Exception
  111.     {
  112.         double dblAccrued = bond.accrued (
  113.             dtSettle.julian(),
  114.             null
  115.         );

  116.         ValuationParams valParams = ValuationParams.Spot (dtSettle.julian());

  117.         double dblYield = bond.yieldFromPrice (
  118.             valParams,
  119.             mktParams,
  120.             null,
  121.             dblCleanPrice
  122.         );

  123.         double dblModifiedDuration = bond.modifiedDurationFromPrice (
  124.             valParams,
  125.             mktParams,
  126.             null,
  127.             dblCleanPrice
  128.         );

  129.         double dblRisk = bond.yield01FromPrice (
  130.             valParams,
  131.             mktParams,
  132.             null,
  133.             dblCleanPrice
  134.         );

  135.         double dblConvexity = bond.convexityFromPrice (
  136.             valParams,
  137.             mktParams,
  138.             null,
  139.             dblCleanPrice
  140.         );

  141.         JulianDate dtPreviousCouponDate = bond.previousCouponDate (dtSettle);

  142.         double dblCurrentPrincipal = bond.notional (dtPreviousCouponDate.julian()) * dblInitialNotional;

  143.         double dblAccruedAmount = dblAccrued * dblInitialNotional;

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

  145.         System.out.println ("\tAnalytics Metrics for " + bond.name());

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

  147.         System.out.println ("\tPrice             : " + FormatUtil.FormatDouble (dblCleanPrice, 1, 4, 100.));

  148.         System.out.println ("\tYield             : " + FormatUtil.FormatDouble (dblYield, 1, 2, 100.) + "%");

  149.         System.out.println ("\tSettle            :  " + dtSettle);

  150.         System.out.println();

  151.         System.out.println ("\tModified Duration : " + FormatUtil.FormatDouble (dblModifiedDuration, 1, 4, 10000.));

  152.         System.out.println ("\tRisk              : " + FormatUtil.FormatDouble (dblRisk, 1, 4, 10000.));

  153.         System.out.println ("\tConvexity         : " + FormatUtil.FormatDouble (dblConvexity * dblInitialNotional, 1, 4, 1.));

  154.         System.out.println ("\tDV01              : " + FormatUtil.FormatDouble (dblRisk * dblInitialNotional, 1, 2, 1.));

  155.         System.out.println();

  156.         System.out.println ("\tPrevious Coupon Date : " + dtPreviousCouponDate);

  157.         System.out.println ("\tFace                 : " + FormatUtil.FormatDouble (dblInitialNotional, 1, 2, 1.));

  158.         System.out.println ("\tNotional             : " + FormatUtil.FormatDouble (dblInitialNotional, 1, 2, 1.));

  159.         System.out.println ("\tCurrent Principal    : " + FormatUtil.FormatDouble (dblCurrentPrincipal, 1, 2, 1.));

  160.         System.out.println ("\tAccrued              : " + FormatUtil.FormatDouble (dblAccruedAmount, 1, 6, 1.));

  161.         System.out.println ("\tTotal                : " + FormatUtil.FormatDouble (dblCleanPrice * dblCurrentPrincipal + dblAccruedAmount, 1, 2, 1.));

  162.         System.out.println ("\tNPV                  : " + FormatUtil.FormatDouble (dblCleanPrice * dblCurrentPrincipal + dblAccruedAmount, 1, 2, 1.));

  163.         System.out.println ("\tAccrual Days         : " + FormatUtil.FormatDouble (dtSettle.julian() - dtPreviousCouponDate.julian(), 1, 0, 1.));
  164.     }

  165.     public static void main (
  166.         final String[] astrArgs)
  167.         throws Exception
  168.     {
  169.         EnvManager.InitEnv (
  170.             "",
  171.             true
  172.         );

  173.         double dblBeginPrincipalFactor = 1.;
  174.         double dblCouponRate = 0.1189;
  175.         double dblServiceFeeRate = 0.00;
  176.         double dblBondNotional = 147544.28;
  177.         String strDayCount = "Act/365";
  178.         String strCurrency = "USD";
  179.         int iNumPayment = 48;
  180.         int iPayFrequency = 12;
  181.         double dblConstantPaymentAmount = 3941.98;

  182.         double dblFixedPaymentAmount = ConstantPaymentBondBuilder.ConstantUniformPaymentAmount (
  183.             dblBondNotional,
  184.             dblCouponRate,
  185.             iNumPayment / iPayFrequency
  186.         );

  187.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  188.             2015,
  189.             DateUtil.OCTOBER,
  190.             22
  191.         );

  192.         Bond bond = ConstantPaymentBondBuilder.Standard (
  193.             "FPMA 11.89 2019",
  194.             dtEffective,
  195.             strCurrency,
  196.             iNumPayment,
  197.             strDayCount,
  198.             iPayFrequency,
  199.             dblCouponRate,
  200.             0.,
  201.             dblConstantPaymentAmount,
  202.             dblBondNotional
  203.         );

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

  205.         System.out.println ("\t|                                         FIXED CASH-FLOW MORTGAGE BOND ANALYTICS                                                                            ||");

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

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

  208.         System.out.println ("\t|            - Start Date                                                                                                                                    ||");

  209.         System.out.println ("\t|            - End Date                                                                                                                                      ||");

  210.         System.out.println ("\t|            - Pay Date                                                                                                                                      ||");

  211.         System.out.println ("\t|            - Discount Factor                                                                                                                               ||");

  212.         System.out.println ("\t|            - Survival Factor                                                                                                                               ||");

  213.         System.out.println ("\t|            - Principal Factor                                                                                                                              ||");

  214.         System.out.println ("\t|            - Accrual Days                                                                                                                                  ||");

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

  216.         System.out.println ("\t|            - Coupon Rate (%)                                                                                                                               ||");

  217.         System.out.println ("\t|            - Coupon Amount                                                                                                                                 ||");

  218.         System.out.println ("\t|            - Fee Rate (%)                                                                                                                                  ||");

  219.         System.out.println ("\t|            - Fee Amount                                                                                                                                    ||");

  220.         System.out.println ("\t|            - Principal Amount                                                                                                                              ||");

  221.         System.out.println ("\t|            - Total Amount                                                                                                                                  ||");

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

  223.         for (CompositePeriod p : bond.couponPeriods()) {
  224.             double dblPeriodCouponRate = p.couponMetrics (
  225.                 dtEffective.julian(),
  226.                 null
  227.             ).rate();

  228.             double dblCouponDCF = p.couponDCF();

  229.             double dblEndPrincipalFactor = bond.notional (p.endDate());

  230.             double dblPrincipalAmount = (dblBeginPrincipalFactor - dblEndPrincipalFactor) * dblBondNotional;

  231.             double dblCouponAmount = dblBeginPrincipalFactor * dblPeriodCouponRate * dblCouponDCF * dblBondNotional;

  232.             double dblYieldDF = Helper.Yield2DF (
  233.                 iPayFrequency,
  234.                 dblCouponRate,
  235.                 Convention.YearFraction (
  236.                     dtEffective.julian(),
  237.                     p.endDate(),
  238.                     "30/360",
  239.                     false,
  240.                     null,
  241.                     strCurrency
  242.                 )
  243.             );

  244.             System.out.println ("\t| [" +
  245.                 DateUtil.YYYYMMDD (p.startDate()) + " -> " +
  246.                 DateUtil.YYYYMMDD (p.endDate()) + "] => " +
  247.                 DateUtil.YYYYMMDD (p.payDate()) + " | " +
  248.                 FormatUtil.FormatDouble (dblYieldDF, 1, 4, 1.) + " | " +
  249.                 FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  250.                 FormatUtil.FormatDouble (dblBeginPrincipalFactor, 1, 4, 1.) + " | " +
  251.                 FormatUtil.FormatDouble (dblCouponDCF * 365, 1, 0, 1.) + " | " +
  252.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 10, 1.) + " | " +
  253.                 FormatUtil.FormatDouble (dblPeriodCouponRate, 2, 2, 100.) + "% | " +
  254.                 FormatUtil.FormatDouble (dblCouponAmount, 2, 2, 1.) + " | " +
  255.                 FormatUtil.FormatDouble (dblServiceFeeRate, 2, 2, 100.) + "% | " +
  256.                 FormatUtil.FormatDouble (dblCouponAmount * dblServiceFeeRate / dblPeriodCouponRate, 2, 2, 1.) + " | " +
  257.                 FormatUtil.FormatDouble (dblPrincipalAmount, 3, 2, 1.) + " | " +
  258.                 FormatUtil.FormatDouble (dblPrincipalAmount + dblCouponAmount, 3, 2, 1.) + " ||"
  259.             );

  260.             dblBeginPrincipalFactor = dblEndPrincipalFactor;
  261.         }

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

  263.         JulianDate dtSettle = DateUtil.CreateFromYMD (
  264.             2015,
  265.             DateUtil.DECEMBER,
  266.             1
  267.         );

  268.         double dblCleanPrice = 1.00; // PAR

  269.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  270.         BondMetrics (
  271.             bond,
  272.             dblBondNotional,
  273.             dtSettle,
  274.             mktParams,
  275.             dblCleanPrice
  276.         );

  277.         Bond bondFeeAdjusted = ConstantPaymentBondBuilder.Standard (
  278.             "FPMA  9.24 2016",
  279.             dtEffective,
  280.             strCurrency,
  281.             iNumPayment,
  282.             strDayCount,
  283.             iPayFrequency,
  284.             dblCouponRate,
  285.             dblServiceFeeRate,
  286.             dblConstantPaymentAmount,
  287.             dblBondNotional
  288.         );

  289.         ValuationParams valParams = ValuationParams.Spot (dtSettle.julian());

  290.         double dblYieldFeeAdjusted = bondFeeAdjusted.yieldFromPrice (
  291.             valParams,
  292.             mktParams,
  293.             null,
  294.             dblCleanPrice
  295.         );

  296.         System.out.println ("\tFee Adjusted Yield   : " + FormatUtil.FormatDouble (dblYieldFeeAdjusted, 1, 2, 100.) + "%");

  297.         System.out.println ("\n\tUniform Constant Mortgage Amount       => " + FormatUtil.FormatDouble (dblFixedPaymentAmount, 1, 2, 1.));

  298.         System.out.println (
  299.             "\tFee Unadjusted Price From Coupon Yield => " + FormatUtil.FormatDouble (
  300.                 bond.priceFromYield (
  301.                     valParams,
  302.                     mktParams,
  303.                     null,
  304.                     dblCouponRate
  305.                 ),
  306.             1, 2, 100.)
  307.         );

  308.         System.out.println (
  309.             "\tFee Adjusted Price From Coupon Yield   => " + FormatUtil.FormatDouble (
  310.                 bondFeeAdjusted.priceFromYield (
  311.                     valParams,
  312.                     mktParams,
  313.                     null,
  314.                     dblCouponRate
  315.                 ),
  316.             1, 2, 100.)
  317.         );

  318.         EnvManager.TerminateEnv();
  319.     }
  320. }