PathVariationMarginTrajectoryEstimator.java

  1. package org.drip.exposure.mpor;

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

  75. /**
  76.  * <i>PathVariationMarginTrajectoryEstimator</i> computes the Variation Margin Estimate/Posting from the
  77.  * specified Dense Uncollateralized Exposures and Trade Payments along the specified Path Trajectory. The
  78.  * References are:
  79.  *  
  80.  * <br><br>
  81.  *      <ul>
  82.  *          <li>
  83.  *              Burgard, C., and M. Kjaer (2013): Funding Costs, Funding Strategies <i>Risk</i> <b>23
  84.  *                  (12)</b> 82-87
  85.  *          </li>
  86.  *          <li>
  87.  *              Burgard, C., and M. Kjaer (2014): In the Balance <i>Risk</i> <b>24 (11)</b> 72-75
  88.  *          </li>
  89.  *          <li>
  90.  *              Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-
  91.  *                  party Risk and Funding Costs <i>Journal of Credit Risk</i> <b>7 (3)</b> 1-19
  92.  *          </li>
  93.  *          <li>
  94.  *              Burgard, C., and M. Kjaer (2017): Derivatives Funding, Netting, and Accounting
  95.  *                  https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2534011 <b>eSSRN</b>
  96.  *          </li>
  97.  *          <li>
  98.  *              Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives
  99.  *                  Pricing <i>Risk</i> <b>21 (2)</b> 97-102
  100.  *          </li>
  101.  *          <li>
  102.  *      </ul>
  103.  *
  104.  *  <br><br>
  105.  *  <ul>
  106.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  107.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ExposureAnalyticsLibrary.md">Exposure Analytics</a></li>
  108.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/README.md">Exposure Group Level Collateralized/Uncollateralized Exposure</a></li>
  109.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/mpor/README.md">Margin Period Collateral Amount Estimation</a></li>
  110.  *  </ul>
  111.  *
  112.  * @author Lakshmi Krishnamurthy
  113.  */

  114. public class PathVariationMarginTrajectoryEstimator
  115. {
  116.     private int[] _exposureDateArray = null;
  117.     private java.lang.String _calendar = "";
  118.     private org.drip.exposure.mpor.TradePayment[] _tradePaymentTrajectory = null;
  119.     private org.drip.exposure.csatimeline.AndersenPykhtinSokolLag _csaTimelineLag = null;
  120.     private java.util.Map<java.lang.Integer, java.lang.Double> _variationMarginEstimateTrajectory = null;

  121.     private static final double ClientTradePayment (
  122.         final java.util.Map<java.lang.Integer, java.lang.Double> clientTradePaymentTrajectory,
  123.         final int startDate,
  124.         final int endDate)
  125.     {
  126.         double clientTradePayment = 0.;

  127.         for (java.util.Map.Entry<java.lang.Integer, java.lang.Double> clientTradePaymentEntry :
  128.             clientTradePaymentTrajectory.entrySet())
  129.         {
  130.             int clientTradePaymentDate = clientTradePaymentEntry.getKey();

  131.             if (clientTradePaymentDate > startDate && clientTradePaymentDate <= endDate)
  132.             {
  133.                 clientTradePayment += clientTradePaymentEntry.getValue();
  134.             }
  135.         }

  136.         return clientTradePayment;
  137.     }

  138.     private static final double NetTradePayment (
  139.         final java.util.Map<java.lang.Integer, java.lang.Double> clientTradePaymentTrajectory,
  140.         final java.util.Map<java.lang.Integer, java.lang.Double> dealerTradePaymentTrajectory,
  141.         final int startDate,
  142.         final int endDate)
  143.     {
  144.         double netTradePayment = 0.;

  145.         for (java.util.Map.Entry<java.lang.Integer, java.lang.Double> clientTradePaymentEntry :
  146.             clientTradePaymentTrajectory.entrySet())
  147.         {
  148.             int clientTradePaymentDate = clientTradePaymentEntry.getKey();

  149.             if (clientTradePaymentDate > startDate && clientTradePaymentDate <= endDate)
  150.             {
  151.                 netTradePayment += clientTradePaymentEntry.getValue();
  152.             }
  153.         }

  154.         for (java.util.Map.Entry<java.lang.Integer, java.lang.Double> dealerTradePaymentEntry :
  155.             dealerTradePaymentTrajectory.entrySet())
  156.         {
  157.             int dealerTradePaymentDate = dealerTradePaymentEntry.getKey();

  158.             if (dealerTradePaymentDate > startDate && dealerTradePaymentDate <= endDate)
  159.             {
  160.                 netTradePayment += dealerTradePaymentEntry.getValue();
  161.             }
  162.         }

  163.         return netTradePayment;
  164.     }

  165.     private static final double VariationMarginPosting (
  166.         final java.util.Map<java.lang.Integer, java.lang.Double> variationMarginEstimateTrajectory,
  167.         final int variationMarginPostingStartDate,
  168.         final int variationMarginPostingEndDate)
  169.     {
  170.         double variationMarginPosting = java.lang.Double.NaN;

  171.         for (java.util.Map.Entry<java.lang.Integer, java.lang.Double> variationMarginEstimateTrajectoryEntry
  172.             : variationMarginEstimateTrajectory.entrySet())
  173.         {
  174.             int variationMarginEstimateDate = variationMarginEstimateTrajectoryEntry.getKey();

  175.             double variationMarginEstimate = variationMarginEstimateTrajectoryEntry.getValue();

  176.             if (variationMarginEstimateDate >= variationMarginPostingStartDate &&
  177.                 variationMarginEstimateDate <= variationMarginPostingEndDate)
  178.             {
  179.                 if (java.lang.Double.isNaN (variationMarginPosting))
  180.                 {
  181.                     variationMarginPosting = variationMarginEstimate;
  182.                 }
  183.                 else
  184.                 {
  185.                     if (variationMarginEstimate < variationMarginPosting)
  186.                     {
  187.                         variationMarginPosting = variationMarginEstimate;
  188.                     }
  189.                 }
  190.             }
  191.         }

  192.         return java.lang.Double.isNaN (variationMarginPosting) ? 0. : variationMarginPosting;
  193.     }

  194.     /**
  195.      * Generate a Standard Instance of PathVariationMarginTrajectoryEstimator
  196.      *
  197.      * @param exposureDateArray Array of Exposure Dates
  198.      * @param calendar The Date Adjustment Calendar
  199.      * @param variationMarginTradePaymentVertex The Variation Margin Trade Payment Trajectory Generator
  200.      * @param marketPath The Market Path
  201.      * @param csaTimelineLag The CSA Time-line Lag Parameters
  202.      *
  203.      * @return The Standard Instance of PathVariationMarginTrajectoryEstimator
  204.      */

  205.     public static final PathVariationMarginTrajectoryEstimator Standard (
  206.         final int[] exposureDateArray,
  207.         final java.lang.String calendar,
  208.         final org.drip.exposure.mpor.VariationMarginTradePaymentVertex variationMarginTradePaymentVertex,
  209.         final org.drip.exposure.universe.MarketPath marketPath,
  210.         final org.drip.exposure.csatimeline.AndersenPykhtinSokolLag csaTimelineLag)
  211.     {
  212.         if (null == exposureDateArray)
  213.         {
  214.             return null;
  215.         }

  216.         int exposureDateCount = exposureDateArray.length;
  217.         org.drip.exposure.mpor.TradePayment[] tradePaymentTrajectory = 0 == exposureDateCount ? null : new
  218.             org.drip.exposure.mpor.TradePayment[exposureDateCount];

  219.         if (0 == exposureDateCount)
  220.         {
  221.             return null;
  222.         }

  223.         for (int exposureDateIndex = 0; exposureDateIndex < exposureDateCount; ++exposureDateIndex)
  224.         {
  225.             tradePaymentTrajectory[exposureDateIndex] = variationMarginTradePaymentVertex.tradePayment (
  226.                 exposureDateArray[exposureDateIndex],
  227.                 marketPath
  228.             );
  229.         }

  230.         try
  231.         {
  232.             return new PathVariationMarginTrajectoryEstimator (
  233.                 exposureDateArray,
  234.                 calendar,
  235.                 org.drip.exposure.mpor.VariationMarginTrajectoryBuilder.Grid (
  236.                     exposureDateArray,
  237.                     variationMarginTradePaymentVertex,
  238.                     marketPath
  239.                 ),
  240.                 tradePaymentTrajectory,
  241.                 csaTimelineLag
  242.             );
  243.         }
  244.         catch (java.lang.Exception e)
  245.         {
  246.             e.printStackTrace();
  247.         }

  248.         return null;
  249.     }

  250.     /**
  251.      * PathVariationMarginTrajectoryEstimator Constructor
  252.      *
  253.      * @param exposureDateArray Array of Exposure Dates
  254.      * @param calendar The Date Adjustment Calendar
  255.      * @param variationMarginEstimateTrajectory The Variation Margin Estimate Trajectory
  256.      * @param tradePaymentTrajectory The Trade Payment Trajectory
  257.      * @param csaTimelineLag The CSA Time-line Lag Parameters
  258.      *
  259.      * @throws java.lang.Exception Throws if the Inputs are Invalid
  260.      */

  261.     public PathVariationMarginTrajectoryEstimator (
  262.         final int[] exposureDateArray,
  263.         final java.lang.String calendar,
  264.         final java.util.Map<java.lang.Integer, java.lang.Double> variationMarginEstimateTrajectory,
  265.         final org.drip.exposure.mpor.TradePayment[] tradePaymentTrajectory,
  266.         final org.drip.exposure.csatimeline.AndersenPykhtinSokolLag csaTimelineLag)
  267.         throws java.lang.Exception
  268.     {
  269.         if (null == (_exposureDateArray = exposureDateArray) ||
  270.             null == (_calendar = calendar) || _calendar.isEmpty() ||
  271.             null == (_variationMarginEstimateTrajectory = variationMarginEstimateTrajectory) ||
  272.             null == (_tradePaymentTrajectory = tradePaymentTrajectory) ||
  273.             null == (_csaTimelineLag = csaTimelineLag))
  274.         {
  275.             throw new java.lang.Exception
  276.                 ("PathVariationMarginTrajectoryEstimator Constructor => Invalid Inputs");
  277.         }

  278.         int exposureDateCount = _exposureDateArray.length;

  279.         if (0 == exposureDateCount || exposureDateCount != _tradePaymentTrajectory.length)
  280.         {
  281.             throw new java.lang.Exception
  282.                 ("PathVariationMarginTrajectoryEstimator Constructor => Invalid Inputs");
  283.         }

  284.         for (int exposureDateIndex = 0; exposureDateIndex < exposureDateCount; ++exposureDateIndex)
  285.         {
  286.             if (null == _tradePaymentTrajectory[exposureDateIndex])
  287.             {
  288.                 throw new java.lang.Exception
  289.                     ("PathVariationMarginTrajectoryEstimator Constructor => Invalid Inputs");
  290.             }
  291.         }
  292.     }

  293.     /**
  294.      * Retrieve the Array of Exposure Dates
  295.      *
  296.      * @return The Array of Exposure Dates
  297.      */

  298.     public int[] exposureDateArray()
  299.     {
  300.         return _exposureDateArray;
  301.     }

  302.     /**
  303.      * Retrieve the Date Adjustment Calendar
  304.      *
  305.      * @return The Date Adjustment Calendar
  306.      */

  307.     public java.lang.String calendar()
  308.     {
  309.         return _calendar;
  310.     }

  311.     /**
  312.      * Retrieve the CSA Events Timeline Lag
  313.      *
  314.      * @return The CSA Events Timeline Lag
  315.      */

  316.     final org.drip.exposure.csatimeline.AndersenPykhtinSokolLag csaTimelineLag()
  317.     {
  318.         return _csaTimelineLag;
  319.     }

  320.     /**
  321.      * Retrieve the Variation Margin Estimate Trajectory
  322.      *
  323.      * @return The Variation Margin Estimate Trajectory
  324.      */

  325.     public java.util.Map<java.lang.Integer, java.lang.Double> variationMarginEstimateTrajectory()
  326.     {
  327.         return _variationMarginEstimateTrajectory;
  328.     }

  329.     /**
  330.      * Retrieve the Trade Payment Trajectory
  331.      *
  332.      * @return The Trade Payment Trajectory
  333.      */

  334.     public org.drip.exposure.mpor.TradePayment[] tradePaymentTrajectory()
  335.     {
  336.         return _tradePaymentTrajectory;
  337.     }

  338.     /**
  339.      * Generate the Client and the Dealer Trade Payment Trajectories
  340.      *
  341.      * @param clientTradePaymentTrajectory The Client Trade Payment Trajectory
  342.      * @param dealerTradePaymentTrajectory The Dealer Trade Payment Trajectory
  343.      *
  344.      * @return TRUE - The Client and the Dealer Trade Payment Trajectories successfully generated
  345.      */

  346.     public boolean tradePaymentTrajectory (
  347.         final java.util.Map<java.lang.Integer, java.lang.Double> clientTradePaymentTrajectory,
  348.         final java.util.Map<java.lang.Integer, java.lang.Double> dealerTradePaymentTrajectory)
  349.     {
  350.         if (null == clientTradePaymentTrajectory || null == dealerTradePaymentTrajectory)
  351.         {
  352.             return false;
  353.         }

  354.         int exposureDateCount = _exposureDateArray.length;

  355.         for (int exposureDateIndex = 0; exposureDateIndex < exposureDateCount; ++exposureDateIndex)
  356.         {
  357.             org.drip.exposure.mpor.TradePayment tradePayment = _tradePaymentTrajectory[exposureDateIndex];

  358.             clientTradePaymentTrajectory.put (
  359.                 _exposureDateArray[exposureDateIndex],
  360.                 tradePayment.client()
  361.             );

  362.             dealerTradePaymentTrajectory.put (
  363.                 _exposureDateArray[exposureDateIndex],
  364.                 tradePayment.dealer()
  365.             );
  366.         }

  367.         return true;
  368.     }

  369.     /**
  370.      * Generate the Array of CSA Event Dates
  371.      *
  372.      * @return Array of CSA Event Dates
  373.      */

  374.     public org.drip.exposure.csatimeline.LastFlowDates[] csaEventDates()
  375.     {
  376.         int exposureDateCount = _exposureDateArray.length;
  377.         org.drip.exposure.csatimeline.LastFlowDates[] csaEventDateArray = new
  378.             org.drip.exposure.csatimeline.LastFlowDates[exposureDateCount];

  379.         for (int exposureDateIndex = 0; exposureDateIndex < exposureDateCount; ++exposureDateIndex)
  380.         {
  381.             if (null == (csaEventDateArray[exposureDateIndex] =
  382.                 org.drip.exposure.csatimeline.LastFlowDates.SpotStandard (
  383.                     new org.drip.analytics.date.JulianDate (_exposureDateArray[exposureDateIndex]),
  384.                     _csaTimelineLag,
  385.                     _calendar
  386.                 )))
  387.             {
  388.                 return null;
  389.             }
  390.         }

  391.         return csaEventDateArray;
  392.     }

  393.     /**
  394.      * Retrieve the Variation Margin Trade Payment Exposure Trajectory
  395.      *
  396.      * @return The Variation Margin Trade Payment Exposure Trajectory
  397.      */

  398.     public java.util.Map<java.lang.Integer, org.drip.exposure.mpor.VariationMarginTradeVertexExposure>
  399.         trajectory()
  400.     {
  401.         java.util.Map<java.lang.Integer, java.lang.Double> variationMarginEstimateTrajectory =
  402.             variationMarginEstimateTrajectory();

  403.         java.util.Map<java.lang.Integer, java.lang.Double> clientTradePaymentTrajectory = new
  404.             java.util.TreeMap<java.lang.Integer, java.lang.Double>();

  405.         java.util.Map<java.lang.Integer, java.lang.Double> dealerTradePaymentTrajectory = new
  406.             java.util.TreeMap<java.lang.Integer, java.lang.Double>();

  407.         if (!tradePaymentTrajectory (
  408.             clientTradePaymentTrajectory,
  409.             dealerTradePaymentTrajectory
  410.         ))
  411.         {
  412.             return null;
  413.         }

  414.         java.util.Map<java.lang.Integer, org.drip.exposure.mpor.VariationMarginTradeVertexExposure>
  415.             variationMarginTradeExposureTrajectory = new java.util.TreeMap<java.lang.Integer,
  416.                 org.drip.exposure.mpor.VariationMarginTradeVertexExposure>();

  417.         org.drip.exposure.csatimeline.LastFlowDates[] csaEventDateArray = csaEventDates();

  418.         int exposureDateCount = _exposureDateArray.length;

  419.         try
  420.         {
  421.             for (int exposureDateIndex = 0; exposureDateIndex < exposureDateCount; ++exposureDateIndex)
  422.             {
  423.                 variationMarginTradeExposureTrajectory.put (
  424.                     _exposureDateArray[exposureDateIndex],
  425.                     new org.drip.exposure.mpor.VariationMarginTradeVertexExposure (
  426.                         variationMarginEstimateTrajectory.get (_exposureDateArray[exposureDateIndex]),
  427.                         VariationMarginPosting (
  428.                             variationMarginEstimateTrajectory,
  429.                             csaEventDateArray[exposureDateIndex].clientVariationMarginPosting().julian(),
  430.                             csaEventDateArray[exposureDateIndex].dealerVariationMarginPosting().julian()
  431.                         ),
  432.                         ClientTradePayment (
  433.                             clientTradePaymentTrajectory,
  434.                             csaEventDateArray[exposureDateIndex].clientTradePayment().julian(),
  435.                             csaEventDateArray[exposureDateIndex].dealerTradePayment().julian()
  436.                         ),
  437.                         NetTradePayment (
  438.                             clientTradePaymentTrajectory,
  439.                             dealerTradePaymentTrajectory,
  440.                             csaEventDateArray[exposureDateIndex].dealerTradePayment().julian(),
  441.                             csaEventDateArray[exposureDateIndex].variationMarginPeriodEnd().julian()
  442.                         ),
  443.                         csaEventDateArray[exposureDateIndex]
  444.                     )
  445.                 );
  446.             }

  447.             return variationMarginTradeExposureTrajectory;
  448.         }
  449.         catch (java.lang.Exception e)
  450.         {
  451.             e.printStackTrace();
  452.         }

  453.         return null;
  454.     }
  455. }