FundingBasisEvolver.java

  1. package org.drip.exposure.csadynamics;

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

  76. /**
  77.  * <i>FundingBasisEvolver</i> implements a Two Factor Stochastic Funding Model Evolver with a Log Normal
  78.  * Forward Process and a Mean Reverting Diffusion Process for the Funding Spread. The References are:
  79.  *  
  80.  * <br><br>
  81.  *      <ul>
  82.  *          <li>
  83.  *              Antonov, A., and M. Arneguy (2009): Analytical Formulas for Pricing CMS Products in the LIBOR
  84.  *                  Market Model with Stochastic Volatility
  85.  *                  https://papers.ssrn.com/sol3/Papers.cfm?abstract_id=1352606 <b>eSSRN</b>
  86.  *          </li>
  87.  *          <li>
  88.  *              Burgard, C., and M. Kjaer (2009): Modeling and successful Management of Credit Counter-party
  89.  *                  Risk of Derivative Portfolios <i>ICBI Conference</i> <b>Rome</b>
  90.  *          </li>
  91.  *          <li>
  92.  *              Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk <i>Risk</i> <b>20 (2)</b>
  93.  *                  86-90
  94.  *          </li>
  95.  *          <li>
  96.  *              Johannes, M., and S. Sundaresan (2007): Pricing Collateralized Swaps <i>Journal of
  97.  *                  Finance</i> <b>62</b> 383-410
  98.  *          </li>
  99.  *          <li>
  100.  *              Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives
  101.  *                  Pricing <i>Risk</i> <b>21 (2)</b> 97-102
  102.  *          </li>
  103.  *      </ul>
  104.  *
  105.  *  <br><br>
  106.  *  <ul>
  107.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  108.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ExposureAnalyticsLibrary.md">Exposure Analytics</a></li>
  109.  *      <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>
  110.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/csadynamics/README.md">CSA Numeraire Basis/Measure Dynamics</a></li>
  111.  *  </ul>
  112.  *
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class FundingBasisEvolver
  116. {
  117.     private double _correlation = java.lang.Double.NaN;
  118.     private org.drip.measure.dynamics.DiffusionEvaluatorLogarithmic _underlyingEvolver = null;
  119.     private org.drip.measure.dynamics.DiffusionEvaluatorMeanReversion _fundingSpreadEvolver = null;

  120.     /**
  121.      * FundingBasisEvolver Constructor
  122.      *
  123.      * @param underlyingEvolver The Underlying Diffusion Evolver
  124.      * @param fundingSpreadEvolver The Funding Spread Diffusion Evolver
  125.      * @param correlation Correlation between the Underlying and the Funding Spread Processes
  126.      *
  127.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  128.      */

  129.     public FundingBasisEvolver (
  130.         final org.drip.measure.dynamics.DiffusionEvaluatorLogarithmic underlyingEvolver,
  131.         final org.drip.measure.dynamics.DiffusionEvaluatorMeanReversion fundingSpreadEvolver,
  132.         final double correlation)
  133.         throws java.lang.Exception
  134.     {
  135.         if (null == (_underlyingEvolver = underlyingEvolver) ||
  136.             null == (_fundingSpreadEvolver = fundingSpreadEvolver) ||
  137.             !org.drip.numerical.common.NumberUtil.IsValid (_correlation = correlation) ||
  138.             1. < _correlation || -1. > _correlation)
  139.         {
  140.             throw new java.lang.Exception ("FundingBasisEvolver Constructor => Invalid Inputs");
  141.         }
  142.     }

  143.     /**
  144.      * Retrieve the Underlying Diffusion Evolver
  145.      *
  146.      * @return The Underlying Diffusion Evolver
  147.      */

  148.     public org.drip.measure.dynamics.DiffusionEvaluatorLogarithmic underlyingEvolver()
  149.     {
  150.         return _underlyingEvolver;
  151.     }

  152.     /**
  153.      * Retrieve the Funding Spread Diffusion Evolver
  154.      *
  155.      * @return The Funding Spread Diffusion Evolver
  156.      */

  157.     public org.drip.measure.dynamics.DiffusionEvaluatorMeanReversion fundingSpreadEvolver()
  158.     {
  159.         return _fundingSpreadEvolver;
  160.     }

  161.     /**
  162.      * Retrieve the Correlation between the Underlying and the Funding Spread Processes
  163.      *
  164.      * @return The Correlation between the Underlying and the Funding Spread Processes
  165.      */

  166.     public double underlyingFundingSpreadCorrelation()
  167.     {
  168.         return _correlation;
  169.     }

  170.     /**
  171.      * Generate the CSA Forward Diffusion Process
  172.      *
  173.      * @return The CSA Forward Diffusion Process
  174.      */

  175.     public org.drip.measure.process.DiffusionEvolver csaForwardProcess()
  176.     {
  177.         try
  178.         {
  179.             org.drip.measure.dynamics.LocalEvaluator driftEvolver = new
  180.                 org.drip.measure.dynamics.LocalEvaluator()
  181.             {
  182.                 @Override public double value (
  183.                     final org.drip.measure.realization.JumpDiffusionVertex vertex)
  184.                     throws java.lang.Exception
  185.                 {
  186.                     return 0.;
  187.                 }
  188.             };

  189.             org.drip.measure.dynamics.LocalEvaluator volatilityEvolver = new
  190.                 org.drip.measure.dynamics.LocalEvaluator()
  191.             {
  192.                 @Override public double value (
  193.                     final org.drip.measure.realization.JumpDiffusionVertex vertex)
  194.                     throws java.lang.Exception
  195.                 {
  196.                     if (null == vertex)
  197.                         throw new java.lang.Exception
  198.                             ("FundingBasisEvolver::CSAForwardVolatility::Evaluator::value => Invalid Inputs");

  199.                     return vertex.value() * _underlyingEvolver.volatilityValue();
  200.                 }
  201.             };

  202.             return new org.drip.measure.process.DiffusionEvolver (
  203.                 new org.drip.measure.dynamics.DiffusionEvaluator (
  204.                     driftEvolver,
  205.                     volatilityEvolver
  206.                 )
  207.             );
  208.         }
  209.         catch (java.lang.Exception e)
  210.         {
  211.             e.printStackTrace();
  212.         }

  213.         return null;
  214.     }

  215.     /**
  216.      * Generate the Funding Numeraire Diffusion Process
  217.      *
  218.      * @param tenor The Tenor of the Underlying Forward
  219.      *
  220.      * @return The Funding Numeraire Diffusion Process
  221.      */

  222.     public org.drip.measure.process.DiffusionEvolver fundingNumeraireProcess (
  223.         final java.lang.String tenor)
  224.     {
  225.         try {
  226.             double meanReversionSpeed = _fundingSpreadEvolver.meanReversionRate();

  227.             double b = org.drip.analytics.support.Helper.TenorToYearFraction (tenor);

  228.             if (0. != meanReversionSpeed)
  229.             {
  230.                 b = (1. - java.lang.Math.exp (-1. * meanReversionSpeed * b)) / meanReversionSpeed;
  231.             }

  232.             final double piterbarg2010BFactor = b;

  233.             org.drip.measure.dynamics.LocalEvaluator driftEvaluator = new
  234.                 org.drip.measure.dynamics.LocalEvaluator()
  235.             {
  236.                 @Override public double value (
  237.                     final org.drip.measure.realization.JumpDiffusionVertex jumpDiffusionVertex)
  238.                     throws java.lang.Exception
  239.                 {
  240.                     return 0.;
  241.                 }
  242.             };

  243.             org.drip.measure.dynamics.LocalEvaluator volatilityEvaluator = new
  244.                 org.drip.measure.dynamics.LocalEvaluator()
  245.             {
  246.                 @Override public double value (
  247.                     final org.drip.measure.realization.JumpDiffusionVertex jumpDiffusionVertex)
  248.                     throws java.lang.Exception
  249.                 {
  250.                     if (null == jumpDiffusionVertex)
  251.                     {
  252.                         throw new java.lang.Exception
  253.                             ("FundingBasisEvolver::CSAFundingNumeraireVolatility::Evaluator::value => Invalid Inputs");
  254.                     }

  255.                     return -1. * jumpDiffusionVertex.value() * piterbarg2010BFactor *
  256.                         _fundingSpreadEvolver.volatilityValue();
  257.                 }
  258.             };

  259.             return new org.drip.measure.process.DiffusionEvolver (
  260.                 new org.drip.measure.dynamics.DiffusionEvaluator (
  261.                     driftEvaluator,
  262.                     volatilityEvaluator
  263.                 )
  264.             );
  265.         }
  266.         catch (java.lang.Exception e)
  267.         {
  268.             e.printStackTrace();
  269.         }

  270.         return null;
  271.     }

  272.     /**
  273.      * Generate the Funding Spread Numeraire Diffusion Process
  274.      *
  275.      * @param tenor The Tenor of the Underlying Forward
  276.      *
  277.      * @return The Funding Spread Numeraire Diffusion Process
  278.      */

  279.     public org.drip.measure.process.DiffusionEvolver fundingSpreadNumeraireProcess (
  280.         final java.lang.String tenor)
  281.     {
  282.         try
  283.         {
  284.             double meanReversionSpeed = _fundingSpreadEvolver.meanReversionRate();

  285.             double b = org.drip.analytics.support.Helper.TenorToYearFraction (tenor);

  286.             if (0. != meanReversionSpeed)
  287.             {
  288.                 b = (1. - java.lang.Math.exp (-1. * meanReversionSpeed * b)) / meanReversionSpeed;
  289.             }

  290.             final double piterbarg2010BFactor = b;

  291.             org.drip.measure.dynamics.LocalEvaluator driftEvaluator = new
  292.                 org.drip.measure.dynamics.LocalEvaluator()
  293.             {
  294.                 @Override public double value (
  295.                     final org.drip.measure.realization.JumpDiffusionVertex jumpDiffusionVertex)
  296.                     throws java.lang.Exception
  297.                 {
  298.                     return 0.;
  299.                 }
  300.             };

  301.             org.drip.measure.dynamics.LocalEvaluator volatilityEvaluator = new
  302.                 org.drip.measure.dynamics.LocalEvaluator()
  303.             {
  304.                 @Override public double value (
  305.                     final org.drip.measure.realization.JumpDiffusionVertex jumpDiffusionVertex)
  306.                     throws java.lang.Exception
  307.                 {
  308.                     if (null == jumpDiffusionVertex)
  309.                     {
  310.                         throw new java.lang.Exception
  311.                             ("FundingBasisEvolver::CSAFundingSpreadNumeraireVolatility::Evaluator::value => Invalid Inputs");
  312.                     }

  313.                     return -1. * jumpDiffusionVertex.value() * piterbarg2010BFactor *
  314.                         _fundingSpreadEvolver.volatilityValue();
  315.                 }
  316.             };

  317.             return new org.drip.measure.process.DiffusionEvolver (
  318.                 new org.drip.measure.dynamics.DiffusionEvaluator (
  319.                     driftEvaluator,
  320.                     volatilityEvaluator
  321.                 )
  322.             );
  323.         }
  324.         catch (java.lang.Exception e)
  325.         {
  326.             e.printStackTrace();
  327.         }

  328.         return null;
  329.     }

  330.     /**
  331.      * Compute the CSA vs. No CSA Forward Ratio
  332.      *
  333.      * @param tenor The Tenor of the Underlying Forward
  334.      *
  335.      * @return The CSA vs. No CSA Forward Ratio
  336.      *
  337.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  338.      */

  339.     public double CSANoCSARatio (
  340.         final java.lang.String tenor)
  341.         throws java.lang.Exception
  342.     {
  343.         double underlyingVolatility = _underlyingEvolver.volatilityValue();

  344.         double meanReversionSpeed = _fundingSpreadEvolver.meanReversionRate();

  345.         double fundingSpreadVolatility = _fundingSpreadEvolver.volatilityValue();

  346.         double maturity = org.drip.analytics.support.Helper.TenorToYearFraction (tenor);

  347.         if (0. == meanReversionSpeed)
  348.         {
  349.             return java.lang.Math.exp (-0.5 * _correlation * underlyingVolatility *
  350.                 fundingSpreadVolatility * maturity * maturity);
  351.         }

  352.         double b = (1. - java.lang.Math.exp (-1. * meanReversionSpeed * maturity)) /
  353.             meanReversionSpeed;

  354.         return java.lang.Math.exp (-1. * _correlation * underlyingVolatility *
  355.             fundingSpreadVolatility * (maturity - b) / meanReversionSpeed);
  356.     }
  357. }