R1JointDiffusion.java

  1. package org.drip.sample.numeraire;

  2. import org.drip.measure.discrete.SequenceGenerator;
  3. import org.drip.measure.dynamics.DiffusionEvaluatorLogarithmic;
  4. import org.drip.measure.process.DiffusionEvolver;
  5. import org.drip.measure.realization.*;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.numerical.linearalgebra.Matrix;
  8. import org.drip.service.env.EnvManager;

  9. /*
  10.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  11.  */

  12. /*!
  13.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  15.  *
  16.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  17.  *      libraries targeting analysts and developers
  18.  *      https://lakshmidrip.github.io/DRIP/
  19.  *  
  20.  *  DRIP is composed of four main libraries:
  21.  *  
  22.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  23.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  24.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  25.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  26.  *
  27.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  28.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  29.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  30.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  31.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  32.  *
  33.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  34.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  35.  *
  36.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  37.  *
  38.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  39.  *
  40.  *  Licensed under the Apache License, Version 2.0 (the "License");
  41.  *      you may not use this file except in compliance with the License.
  42.  *  
  43.  *  You may obtain a copy of the License at
  44.  *      http://www.apache.org/licenses/LICENSE-2.0
  45.  *  
  46.  *  Unless required by applicable law or agreed to in writing, software
  47.  *      distributed under the License is distributed on an "AS IS" BASIS,
  48.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  49.  *  
  50.  *  See the License for the specific language governing permissions and
  51.  *      limitations under the License.
  52.  */

  53. /**
  54.  * R1JointDiffusion demonstrates the Joint Evolution of R^1 Diffusion Variates - the Continuous Asset, the
  55.  *  Collateral, the Bank, and the Counter-Party Numeraires involved in the Dynamic XVA Replication Portfolio
  56.  *  of the Burgard and Kjaer (2011) Methodology. The References are:
  57.  *  
  58.  *  - Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party Risk
  59.  *      and Funding Costs, Journal of Credit Risk, 7 (3) 1-19.
  60.  *  
  61.  *  - Cesari, G., J. Aquilina, N. Charpillon, X. Filipovic, G. Lee, and L. Manda (2009): Modeling, Pricing,
  62.  *      and Hedging Counter-party Credit Exposure - A Technical Guide, Springer Finance, New York.
  63.  *  
  64.  *  - Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk, Risk 20 (2) 86-90.
  65.  *  
  66.  *  - Li, B., and Y. Tang (2007): Quantitative Analysis, Derivatives Modeling, and Trading Strategies in the
  67.  *      Presence of Counter-party Credit Risk for the Fixed Income Market, World Scientific Publishing,
  68.  *      Singapore.
  69.  *
  70.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  71.  *      21 (2) 97-102.
  72.  *
  73.  * @author Lakshmi Krishnamurthy
  74.  */

  75. public class R1JointDiffusion {

  76.     private static final double[][] NumeraireSequence (
  77.         final int iCount,
  78.         final double[][] aadblCorrelation,
  79.         final String strHeader)
  80.         throws Exception
  81.     {
  82.         double[][] aadblGaussianJoint = SequenceGenerator.GaussianJoint (
  83.             iCount,
  84.             aadblCorrelation
  85.         );

  86.         System.out.println();

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

  88.         System.out.println (strHeader);

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

  90.         for (int i = 0; i < iCount; ++i) {
  91.             String strDump = "\t||" + FormatUtil.FormatDouble (i, 2, 0, 1.) + " |";

  92.             for (int j = 0; j < aadblCorrelation.length; ++j)
  93.                 strDump = strDump + " " + FormatUtil.FormatDouble (aadblGaussianJoint[i][j], 1, 6, 1.) + " |";

  94.             System.out.println (strDump + "|");
  95.         }

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

  97.         System.out.println();

  98.         return Matrix.Transpose (aadblGaussianJoint);
  99.     }

  100.     public static final void main (
  101.         final String[] astrArgs)
  102.         throws Exception
  103.     {
  104.         EnvManager.InitEnv ("");

  105.         double dblTimeWidth = 1. / 24.;
  106.         double dblTime = 1.;
  107.         double[][] aadblCorrelation = new double[][] {
  108.             {1.00, 0.20, 0.15, 0.05}, // #0 ASSET
  109.             {0.20, 1.00, 0.13, 0.25}, // #1 COLLATERAL
  110.             {0.15, 0.13, 1.00, 0.00}, // #2 BANK
  111.             {0.05, 0.25, 0.00, 1.00}  // #3 COUNTER PARTY
  112.         };
  113.         double dblAssetDrift = 0.06;
  114.         double dblAssetVolatility = 0.15;
  115.         double dblTerminalBankNumeraire = 1.;
  116.         double dblTerminalAssetNumeraire = 1.;
  117.         double dblTerminalCollateralNumeraire = 1.;

  118.         int iNumTimeStep = (int) (dblTime / dblTimeWidth);
  119.         double[] adblTimeWidth = new double[iNumTimeStep];

  120.         for (int i = 0; i < iNumTimeStep; ++i)
  121.             adblTimeWidth[i] = dblTimeWidth;

  122.         DiffusionEvolver meAsset = new DiffusionEvolver (
  123.             DiffusionEvaluatorLogarithmic.Standard (
  124.                 dblAssetDrift,
  125.                 dblAssetVolatility
  126.             )
  127.         );

  128.         double[][] aadblNumeraireTimeSeries = NumeraireSequence (
  129.             iNumTimeStep,
  130.             aadblCorrelation,
  131.             "\t|| ASSET, COLLATERAL, BANK, COUNTER PARTY REALIZATION ||"
  132.         );

  133.         JumpDiffusionEdge[] aR1AssetLR = meAsset.incrementSequence (
  134.             new JumpDiffusionVertex (
  135.                 dblTime,
  136.                 dblTerminalAssetNumeraire,
  137.                 0.,
  138.                 false
  139.             ),
  140.             JumpDiffusionEdgeUnit.Diffusion (
  141.                 adblTimeWidth,
  142.                 aadblNumeraireTimeSeries[0]
  143.             ),
  144.             -1. * dblTimeWidth
  145.         );

  146.         JumpDiffusionEdge[] aR1CollateralLR = meAsset.incrementSequence (
  147.             new JumpDiffusionVertex (
  148.                 dblTime,
  149.                 dblTerminalCollateralNumeraire,
  150.                 0.,
  151.                 false
  152.             ),
  153.             JumpDiffusionEdgeUnit.Diffusion (
  154.                 adblTimeWidth,
  155.                 aadblNumeraireTimeSeries[1]
  156.             ),
  157.             -1. * dblTimeWidth
  158.         );

  159.         JumpDiffusionEdge[] aR1BankLR = meAsset.incrementSequence (
  160.             new JumpDiffusionVertex (
  161.                 dblTime,
  162.                 dblTerminalBankNumeraire,
  163.                 0.,
  164.                 false
  165.             ),
  166.             JumpDiffusionEdgeUnit.Diffusion (
  167.                 adblTimeWidth,
  168.                 aadblNumeraireTimeSeries[2]
  169.             ),
  170.             -1. * dblTimeWidth
  171.         );

  172.         JumpDiffusionEdge[] aR1CounterPartyLR = meAsset.incrementSequence (
  173.             new JumpDiffusionVertex (
  174.                 dblTime,
  175.                 dblTerminalBankNumeraire,
  176.                 0.,
  177.                 false
  178.             ),
  179.             JumpDiffusionEdgeUnit.Diffusion (
  180.                 adblTimeWidth,
  181.                 aadblNumeraireTimeSeries[3]
  182.             ),
  183.             -1. * dblTimeWidth
  184.         );

  185.         System.out.println();

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

  187.         System.out.println("\t||                  BURGARD & KJAER (2011) CORRELATED JOINT ASSET/COLLATERAL/BANK/COUNTER-PARTY NUMERAIRE EVOLUTION                 ||");

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

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

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

  191.         System.out.println("\t||          - Asset Numeraire Finish Value                                                                                          ||");

  192.         System.out.println("\t||          - Asset Numeraire Start Value                                                                                           ||");

  193.         System.out.println("\t||          - Asset Numeraire Continuous Wander Realization                                                                         ||");

  194.         System.out.println("\t||          - Collateral Numeraire Finish Value                                                                                     ||");

  195.         System.out.println("\t||          - Collateral Numeraire Start Value                                                                                      ||");

  196.         System.out.println("\t||          - Collateral Numeraire Continuous Wander Realization                                                                    ||");

  197.         System.out.println("\t||          - Bank Numeraire Finish Value                                                                                           ||");

  198.         System.out.println("\t||          - Bank Numeraire Start Value                                                                                            ||");

  199.         System.out.println("\t||          - Bank Numeraire Continuous Wander Realization                                                                          ||");

  200.         System.out.println("\t||          - Counter-Party Numeraire Finish Value                                                                                  ||");

  201.         System.out.println("\t||          - Counter-Party Numeraire Start Value                                                                                   ||");

  202.         System.out.println("\t||          - Counter-Party Numeraire Continuous Wander Realization                                                                 ||");

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

  204.         for (int i = 0; i < iNumTimeStep; ++i) {
  205.             dblTime = dblTime - dblTimeWidth;

  206.             System.out.println (
  207.                 "\t|| " +
  208.                 FormatUtil.FormatDouble (dblTime, 1, 4, 1.) + " => " +
  209.                 FormatUtil.FormatDouble (aR1AssetLR[i].start(), 1, 4, 1.) + " | " +
  210.                 FormatUtil.FormatDouble (aR1AssetLR[i].finish(), 1, 4, 1.) + " | " +
  211.                 FormatUtil.FormatDouble (aR1AssetLR[i].diffusionWander(), 1, 4, 1.) + " ||" +
  212.                 FormatUtil.FormatDouble (aR1CollateralLR[i].start(), 1, 4, 1.) + " | " +
  213.                 FormatUtil.FormatDouble (aR1CollateralLR[i].finish(), 1, 4, 1.) + " | " +
  214.                 FormatUtil.FormatDouble (aR1CollateralLR[i].diffusionWander(), 1, 4, 1.) + " ||" +
  215.                 FormatUtil.FormatDouble (aR1BankLR[i].start(), 1, 4, 1.) + " | " +
  216.                 FormatUtil.FormatDouble (aR1BankLR[i].finish(), 1, 4, 1.) + " | " +
  217.                 FormatUtil.FormatDouble (aR1BankLR[i].diffusionWander(), 1, 4, 1.) + " ||" +
  218.                 FormatUtil.FormatDouble (aR1CounterPartyLR[i].start(), 1, 4, 1.) + " | " +
  219.                 FormatUtil.FormatDouble (aR1CounterPartyLR[i].finish(), 1, 4, 1.) + " | " +
  220.                 FormatUtil.FormatDouble (aR1CounterPartyLR[i].diffusionWander(), 1, 4, 1.) + " ||"
  221.             );
  222.         }

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

  224.         System.out.println();
  225.     }
  226. }