R1JointJumpDiffusion.java

  1. package org.drip.sample.numeraire;

  2. import org.drip.measure.discrete.SequenceGenerator;
  3. import org.drip.measure.dynamics.*;
  4. import org.drip.measure.process.*;
  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.  * R1JointJumpDiffusion demonstrates the Joint Evolution of R^1 Jump Diffusion Variates - the Continuous
  55.  *  Asset, the Collateral, the Bank, and the Counter-Party Numeraires involved in the Dynamic XVA Replication
  56.  *  Portfolio 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 R1JointJumpDiffusion {

  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 = 0.;
  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 dblInitialAssetNumeraire = 1.;

  116.         double dblZeroCouponBankBondDrift = 0.03;
  117.         double dblZeroCouponBankBondVolatility = 0.10;
  118.         double dblBankHazardRate = 0.03;
  119.         double dblBankRecoveryRate = 0.45;
  120.         double dblInitialBankNumeraire = 1.;

  121.         double dblZeroCouponCollateralBondDrift = 0.01;
  122.         double dblZeroCouponCollateralBondVolatility = 0.05;
  123.         double dblInitialCollateralNumeraire = 1.;

  124.         double dblZeroCouponCounterPartyBondDrift = 0.03;
  125.         double dblZeroCouponCounterPartyBondVolatility = 0.10;
  126.         double dblCounterPartyHazardRate = 0.05;
  127.         double dblCounterPartyRecoveryRate = 0.30;
  128.         double dblInitialCounterPartyNumeraire = 1.;

  129.         int iNumTimeStep = (int) (1. / dblTimeWidth);

  130.         DiffusionEvolver meAsset = new DiffusionEvolver (
  131.             DiffusionEvaluatorLogarithmic.Standard (
  132.                 dblAssetDrift,
  133.                 dblAssetVolatility
  134.             )
  135.         );

  136.         DiffusionEvolver meZeroCouponCollateralBond = new DiffusionEvolver (
  137.             DiffusionEvaluatorLogarithmic.Standard (
  138.                 dblZeroCouponCollateralBondDrift,
  139.                 dblZeroCouponCollateralBondVolatility
  140.             )
  141.         );

  142.         JumpDiffusionEvolver meZeroCouponBankBond = new JumpDiffusionEvolver (
  143.             DiffusionEvaluatorLogarithmic.Standard (
  144.                 dblZeroCouponBankBondDrift,
  145.                 dblZeroCouponBankBondVolatility
  146.             ),
  147.             HazardJumpEvaluator.Standard (
  148.                 dblBankHazardRate,
  149.                 dblBankRecoveryRate
  150.             )
  151.         );

  152.         JumpDiffusionEvolver meZeroCouponCounterPartyBond = new JumpDiffusionEvolver (
  153.             DiffusionEvaluatorLogarithmic.Standard (
  154.                 dblZeroCouponCounterPartyBondDrift,
  155.                 dblZeroCouponCounterPartyBondVolatility
  156.             ),
  157.             HazardJumpEvaluator.Standard (
  158.                 dblCounterPartyHazardRate,
  159.                 dblCounterPartyRecoveryRate
  160.             )
  161.         );

  162.         double[][] aadblNumeraireTimeSeries = NumeraireSequence (
  163.             iNumTimeStep,
  164.             aadblCorrelation,
  165.             "\t|| ASSET, COLLATERAL, BANK, COUNTER PARTY REALIZATION ||"
  166.         );

  167.         double[] adblBankDefaultIndicator = SequenceGenerator.Uniform (iNumTimeStep);

  168.         double[] adblCounterPartyDefaultIndicator = SequenceGenerator.Uniform (iNumTimeStep);

  169.         double[] adblTimeWidth = new double[iNumTimeStep];

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

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

  185.         JumpDiffusionEdge[] aR1CollateralLR = meZeroCouponCollateralBond.incrementSequence (
  186.             new JumpDiffusionVertex (
  187.                 dblTime,
  188.                 dblInitialCollateralNumeraire,
  189.                 0.,
  190.                 false
  191.             ),
  192.             JumpDiffusionEdgeUnit.Diffusion (
  193.                 adblTimeWidth,
  194.                 aadblNumeraireTimeSeries[1]
  195.             ),
  196.             dblTimeWidth
  197.         );

  198.         JumpDiffusionEdge[] aR1BankLR = meZeroCouponBankBond.incrementSequence (
  199.             new JumpDiffusionVertex (
  200.                 dblTime,
  201.                 dblInitialBankNumeraire,
  202.                 0.,
  203.                 false
  204.             ),
  205.             JumpDiffusionEdgeUnit.JumpDiffusion (
  206.                 adblTimeWidth,
  207.                 aadblNumeraireTimeSeries[2],
  208.                 adblBankDefaultIndicator
  209.             ),
  210.             dblTimeWidth
  211.         );

  212.         JumpDiffusionEdge[] aR1CounterPartyLR = meZeroCouponCounterPartyBond.incrementSequence (
  213.             new JumpDiffusionVertex (
  214.                 dblTime,
  215.                 dblInitialCounterPartyNumeraire,
  216.                 0.,
  217.                 false
  218.             ),
  219.             JumpDiffusionEdgeUnit.JumpDiffusion (
  220.                 adblTimeWidth,
  221.                 aadblNumeraireTimeSeries[3],
  222.                 adblCounterPartyDefaultIndicator
  223.             ),
  224.             dblTimeWidth
  225.         );

  226.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  245.         for (int i = 0; i < iNumTimeStep; ++i) {
  246.             dblTime = dblTime + dblTimeWidth;

  247.             System.out.println (
  248.                 "\t|| " +
  249.                 FormatUtil.FormatDouble (dblTime, 1, 4, 1.) + " => " +
  250.                 FormatUtil.FormatDouble (aR1AssetLR[i].start(), 1, 4, 1.) + " | " +
  251.                 FormatUtil.FormatDouble (aR1AssetLR[i].finish(), 1, 4, 1.) + " | " +
  252.                 FormatUtil.FormatDouble (aR1AssetLR[i].diffusionWander(), 1, 4, 1.) + " ||" +
  253.                 FormatUtil.FormatDouble (aR1CollateralLR[i].start(), 1, 4, 1.) + " | " +
  254.                 FormatUtil.FormatDouble (aR1CollateralLR[i].finish(), 1, 4, 1.) + " | " +
  255.                 FormatUtil.FormatDouble (aR1CollateralLR[i].diffusionWander(), 1, 4, 1.) + " ||" +
  256.                 FormatUtil.FormatDouble (aR1BankLR[i].start(), 1, 4, 1.) + " | " +
  257.                 FormatUtil.FormatDouble (aR1BankLR[i].finish(), 1, 4, 1.) + " | " +
  258.                 FormatUtil.FormatDouble (aR1BankLR[i].diffusionWander(), 1, 4, 1.) + " ||" +
  259.                 FormatUtil.FormatDouble (aR1CounterPartyLR[i].start(), 1, 4, 1.) + " | " +
  260.                 FormatUtil.FormatDouble (aR1CounterPartyLR[i].finish(), 1, 4, 1.) + " | " +
  261.                 FormatUtil.FormatDouble (aR1CounterPartyLR[i].diffusionWander(), 1, 4, 1.) + " ||"
  262.             );
  263.         }

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

  265.         System.out.println();
  266.     }
  267. }