BookLatentStateMap.java

  1. package org.drip.sample.xvatopology;

  2. import java.util.Map;

  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.numerical.common.StringUtil;
  5. import org.drip.service.env.EnvManager;
  6. import org.drip.state.identifier.CSALabel;
  7. import org.drip.state.identifier.EntityFundingLabel;
  8. import org.drip.state.identifier.EntityHazardLabel;
  9. import org.drip.state.identifier.EntityRecoveryLabel;
  10. import org.drip.state.identifier.OvernightLabel;
  11. import org.drip.xva.proto.CollateralGroupSpecification;
  12. import org.drip.xva.proto.CreditDebtGroupSpecification;
  13. import org.drip.xva.proto.FundingGroupSpecification;
  14. import org.drip.xva.proto.PositionGroupSpecification;
  15. import org.drip.xva.settings.BrokenDateScheme;
  16. import org.drip.xva.settings.CloseOutScheme;
  17. import org.drip.xva.settings.PositionReplicationScheme;
  18. import org.drip.xva.topology.Adiabat;
  19. import org.drip.xva.topology.AdiabatMarketParams;
  20. import org.drip.xva.topology.CollateralGroup;
  21. import org.drip.xva.topology.CreditDebtGroup;
  22. import org.drip.xva.topology.FundingGroup;
  23. import org.drip.xva.topology.PositionGroup;

  24. /*
  25.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  26.  */

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

  67. /**
  68.  * BookLatentStateMap represents the Latent State Map across all the Book Groups. The References are:
  69.  *  
  70.  *  - Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party Risk
  71.  *      and Funding Costs, Journal of Credit Risk, 7 (3) 1-19.
  72.  *  
  73.  *  - Burgard, C., and M. Kjaer (2014): In the Balance, Risk, 24 (11) 72-75.
  74.  *  
  75.  *  - Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk, Risk 20 (2) 86-90.
  76.  *  
  77.  *  - Albanese, C., L. Andersen, and, S. Iabichino (2015): The FVA Puzzle: Accounting, Risk Management, and
  78.  *      Collateral Trading <b>https://papers.ssrn.com/sol3/paper.cfm?abstract_id_2517301</b><br><br>
  79.  *
  80.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  81.  *      21 (2) 97-102.
  82.  *
  83.  * @author Lakshmi Krishnamurthy
  84.  */

  85. public class BookLatentStateMap
  86. {

  87.     private static final String ThreeDigitRandom()
  88.     {
  89.         return FormatUtil.FormatDouble (Math.random(), 3, 0, 1000.);
  90.     }

  91.     private static final PositionGroup[] PositionGroupArray (
  92.         final PositionGroupSpecification positionGroupSpecification,
  93.         final int count)
  94.         throws Exception
  95.     {
  96.         PositionGroup[] positionGroupArray = new PositionGroup[count];

  97.         for (int i = 0; i < count; ++i)
  98.         {
  99.             positionGroupArray[i] = new PositionGroup (
  100.                 StringUtil.GUID(),
  101.                 "POSITIONGROUP" + ThreeDigitRandom(),
  102.                 positionGroupSpecification
  103.             );
  104.         }

  105.         return positionGroupArray;
  106.     }

  107.     private static final CollateralGroup[] CollateralGroupArray (
  108.         final PositionGroupSpecification positionGroupSpecification,
  109.         final CollateralGroupSpecification collateralGroupSpecification,
  110.         final String currency,
  111.         final int collateralGroupCount,
  112.         final int positionGroupPerCollateralGroup)
  113.         throws Exception
  114.     {
  115.         CollateralGroup[] collateralGroupArray = new CollateralGroup[collateralGroupCount];

  116.         for (int i = 0; i < collateralGroupCount; ++i)
  117.         {
  118.             collateralGroupArray[i] = new CollateralGroup (
  119.                 StringUtil.GUID(),
  120.                 "COLLATERALGROUP" + ThreeDigitRandom(),
  121.                 collateralGroupSpecification
  122.             );

  123.             PositionGroup[] positionGroupArray = PositionGroupArray (
  124.                 positionGroupSpecification,
  125.                 positionGroupPerCollateralGroup
  126.             );

  127.             for (PositionGroup positionGroup : positionGroupArray)
  128.                 collateralGroupArray[i].addPositionGroup (positionGroup);
  129.         }

  130.         return collateralGroupArray;
  131.     }

  132.     private static final CreditDebtGroup[] CreditDebtGroupArray (
  133.         final PositionGroupSpecification positionGroupSpecification,
  134.         final CollateralGroupSpecification collateralGroupSpecification,
  135.         final CreditDebtGroupSpecification creditDebtGroupSpecification,
  136.         final int creditDebtGroupCount,
  137.         final String currency,
  138.         final String bank,
  139.         final String counterParty,
  140.         final int collateralGroupPerCreditDebtGroup,
  141.         final int positionGroupPerCollateralGroup)
  142.         throws Exception
  143.     {
  144.         CreditDebtGroup[] creditDebtGroupArray = new CreditDebtGroup[creditDebtGroupCount];

  145.         for (int i = 0; i < creditDebtGroupCount; ++i)
  146.         {
  147.             creditDebtGroupArray[i] = new CreditDebtGroup (
  148.                 StringUtil.GUID(),
  149.                 "CREDITDEBTGROUP" + ThreeDigitRandom(),
  150.                 creditDebtGroupSpecification
  151.             );

  152.             CollateralGroup[] collateralGroupArray = CollateralGroupArray (
  153.                 positionGroupSpecification,
  154.                 collateralGroupSpecification,
  155.                 currency,
  156.                 collateralGroupPerCreditDebtGroup,
  157.                 positionGroupPerCollateralGroup
  158.             );

  159.             for (int j = 0; j < collateralGroupArray.length; ++j)
  160.             {
  161.                 creditDebtGroupArray[i].addCollateralGroup (collateralGroupArray[j]);
  162.             }
  163.         }

  164.         return creditDebtGroupArray;
  165.     }

  166.     private static final Adiabat BookTopology (
  167.         final PositionGroupSpecification positionGroupSpecification,
  168.         final CollateralGroupSpecification collateralGroupSpecification,
  169.         final CreditDebtGroupSpecification creditDebtGroupSpecification,
  170.         final FundingGroupSpecification fundingGroupSpecification,
  171.         final int fundingGroupCount,
  172.         final String currency,
  173.         final String bank,
  174.         final String counterParty,
  175.         final int creditDebtGroupPerFundingGroup,
  176.         final int collateralGroupPerCreditDebtGroup,
  177.         final int positionGroupPerCollateralGroup)
  178.         throws Exception
  179.     {
  180.         Adiabat bookGraph = new Adiabat (
  181.             StringUtil.GUID(),
  182.             "BOOKGRAPH"
  183.         );

  184.         FundingGroup[] fundingGroupArray = new FundingGroup[fundingGroupCount];

  185.         for (int i = 0; i < fundingGroupCount; ++i)
  186.         {
  187.             fundingGroupArray[i] = new FundingGroup (
  188.                 StringUtil.GUID(),
  189.                 "FUNDINGGROUP" + ThreeDigitRandom(),
  190.                 fundingGroupSpecification
  191.             );

  192.             CreditDebtGroup[] creditDebtGroupArray = CreditDebtGroupArray (
  193.                 positionGroupSpecification,
  194.                 collateralGroupSpecification,
  195.                 creditDebtGroupSpecification,
  196.                 creditDebtGroupPerFundingGroup,
  197.                 currency,
  198.                 bank,
  199.                 counterParty,
  200.                 collateralGroupPerCreditDebtGroup,
  201.                 positionGroupPerCollateralGroup
  202.             );

  203.             for (int j = 0; j < creditDebtGroupArray.length; ++j)
  204.             {
  205.                 fundingGroupArray[i].addCreditDebtGroup (creditDebtGroupArray[j]);
  206.             }
  207.         }

  208.         for (int k = 0; k < fundingGroupArray.length; ++k)
  209.         {
  210.             bookGraph.addFundingGroup (fundingGroupArray[k]);
  211.         }

  212.         return bookGraph;
  213.     }

  214.     public static final void main (
  215.         final String[] args)
  216.         throws Exception
  217.     {
  218.         EnvManager.InitEnv ("");

  219.         String bank = "NOM";
  220.         String currency = "USD";
  221.         String counterParty = "BAC";
  222.         int positionGroupPerCollateralGroup = 3;
  223.         int collateralGroupPerCreditDebtGroup = 3;
  224.         int creditDebtGroupCountPerFundingGroup = 3;
  225.         int fundingGroupCount = 3;

  226.         Adiabat bookGraph = BookTopology (
  227.             PositionGroupSpecification.ZeroThreshold (
  228.                 "POSITIONGROUPSPEC",
  229.                 PositionReplicationScheme.ALBANESE_ANDERSEN_VERTEX,
  230.                 BrokenDateScheme.SQUARE_ROOT_OF_TIME,
  231.                 0.,
  232.                 CloseOutScheme.ISDA_92
  233.             ),
  234.             new CollateralGroupSpecification (
  235.                 StringUtil.GUID(),
  236.                 "COLLATERALGROUPSPEC",
  237.                 OvernightLabel.Create (currency),
  238.                 CSALabel.ISDA (currency)
  239.             ),
  240.             new CreditDebtGroupSpecification (
  241.                 StringUtil.GUID(),
  242.                 "CREDITDEBTGROUPSPEC",
  243.                 EntityHazardLabel.Standard (
  244.                     bank,
  245.                     currency
  246.                 ),
  247.                 EntityHazardLabel.Standard (
  248.                     counterParty,
  249.                     currency
  250.                 ),
  251.                 EntityRecoveryLabel.Senior (
  252.                     bank,
  253.                     currency
  254.                 ),
  255.                 EntityRecoveryLabel.Senior (
  256.                     counterParty,
  257.                     currency
  258.                 ),
  259.                 EntityRecoveryLabel.Subordinate (
  260.                     bank,
  261.                     currency
  262.                 ),
  263.                 true,
  264.                 true
  265.             ),
  266.             new FundingGroupSpecification (
  267.                 StringUtil.GUID(),
  268.                 "FUNDINGGROUPSPEC",
  269.                 EntityFundingLabel.Senior (
  270.                     bank,
  271.                     currency
  272.                 ),
  273.                 EntityFundingLabel.Senior (
  274.                     counterParty,
  275.                     currency
  276.                 ),
  277.                 EntityFundingLabel.Subordinate (
  278.                     bank,
  279.                     currency
  280.                 )
  281.             ),
  282.             fundingGroupCount,
  283.             currency,
  284.             bank,
  285.             counterParty,
  286.             creditDebtGroupCountPerFundingGroup,
  287.             collateralGroupPerCreditDebtGroup,
  288.             positionGroupPerCollateralGroup
  289.         );

  290.         AdiabatMarketParams bookMarketParams = bookGraph.marketParams();

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

  292.         System.out.println ("\t||   OVERNIGHT    ||");

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

  294.         for (Map.Entry<String, OvernightLabel> overnightLabelEntry :
  295.             bookMarketParams.overnightLabelMap().entrySet())
  296.         {
  297.             System.out.println ("\t|| " + overnightLabelEntry.getValue().fullyQualifiedName() + " ||");
  298.         }

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

  300.         System.out.println();

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

  302.         System.out.println ("\t||    CSA    ||");

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

  304.         for (Map.Entry<String, CSALabel> csaLabelEntry : bookMarketParams.csaLabelMap().entrySet())
  305.         {
  306.             System.out.println ("\t|| " + csaLabelEntry.getValue().fullyQualifiedName() + " ||");
  307.         }

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

  309.         System.out.println();

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

  311.         System.out.println ("\t|| BANK HAZ ||");

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

  313.         for (Map.Entry<String, EntityHazardLabel> bankHazardLabelEntry :
  314.             bookMarketParams.dealerHazardLabelMap().entrySet())
  315.         {
  316.             System.out.println ("\t|| " + bankHazardLabelEntry.getValue().fullyQualifiedName() + " ||");
  317.         }

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

  319.         System.out.println();

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

  321.         System.out.println ("\t|| CPTY HAZ ||");

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

  323.         for (Map.Entry<String, EntityHazardLabel> counterPartyHazardLabelEntry :
  324.             bookMarketParams.clientHazardLabelMap().entrySet())
  325.         {
  326.             System.out.println ("\t|| " + counterPartyHazardLabelEntry.getValue().fullyQualifiedName() + " ||");
  327.         }

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

  329.         System.out.println();

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

  331.         System.out.println ("\t|| BANK SR RECOVERY ||");

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

  333.         for (Map.Entry<String, EntityRecoveryLabel> bankSeniorRecoveryLabelEntry :
  334.             bookMarketParams.dealerSeniorRecoveryLabelMap().entrySet())
  335.         {
  336.             System.out.println ("\t|| " + bankSeniorRecoveryLabelEntry.getValue().fullyQualifiedName() + " ||");
  337.         }

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

  339.         System.out.println();

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

  341.         System.out.println ("\t||  CPTY RECOVERY   ||");

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

  343.         for (Map.Entry<String, EntityRecoveryLabel> counterPartyRecoveryLabelEntry :
  344.             bookMarketParams.clientRecoveryLabelMap().entrySet())
  345.         {
  346.             System.out.println ("\t|| " + counterPartyRecoveryLabelEntry.getValue().fullyQualifiedName() + " ||");
  347.         }

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

  349.         System.out.println();

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

  351.         System.out.println ("\t|| BANK SUBORD RECOVERY  ||");

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

  353.         for (Map.Entry<String, EntityRecoveryLabel> bankSubordinateRecoveryLabelEntry :
  354.             bookMarketParams.dealerSubordinateRecoveryLabelMap().entrySet())
  355.         {
  356.             System.out.println ("\t|| " + bankSubordinateRecoveryLabelEntry.getValue().fullyQualifiedName() + " ||");
  357.         }

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

  359.         System.out.println();

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

  361.         System.out.println ("\t|| BANK SR FUNDING  ||");

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

  363.         for (Map.Entry<String, EntityFundingLabel> bankSeniorFundingLabelEntry :
  364.             bookMarketParams.dealerSeniorFundingLabelMap().entrySet())
  365.         {
  366.             System.out.println ("\t|| " + bankSeniorFundingLabelEntry.getValue().fullyQualifiedName() + " ||");
  367.         }

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

  369.         System.out.println();

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

  371.         System.out.println ("\t||   CPTY FUNDING   ||");

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

  373.         for (Map.Entry<String, EntityFundingLabel> counterPartyFundingLabelEntry :
  374.             bookMarketParams.clientFundingLabelMap().entrySet())
  375.         {
  376.             System.out.println ("\t|| " + counterPartyFundingLabelEntry.getValue().fullyQualifiedName() + " ||");
  377.         }

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

  379.         System.out.println();

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

  381.         System.out.println ("\t||  BANK SUBORD FUNDING  ||");

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

  383.         for (Map.Entry<String, EntityFundingLabel> bankSubordinateFundingLabelEntry :
  384.             bookMarketParams.dealerSubordinateFundingLabelMap().entrySet())
  385.         {
  386.             System.out.println ("\t|| " + bankSubordinateFundingLabelEntry.getValue().fullyQualifiedName() + " ||");
  387.         }

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

  389.         System.out.println();

  390.         EnvManager.TerminateEnv();
  391.     }
  392. }