BookGroupLayout.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.CollateralGroup;
  20. import org.drip.xva.topology.CreditDebtGroup;
  21. import org.drip.xva.topology.FundingGroup;
  22. import org.drip.xva.topology.PositionGroup;

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

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

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

  84. public class BookGroupLayout
  85. {

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

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

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

  104.         return positionGroupArray;
  105.     }

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

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

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

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

  129.         return collateralGroupArray;
  130.     }

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

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

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

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

  163.         return creditDebtGroupArray;
  164.     }

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

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

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

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

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

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

  211.         return bookGraph;
  212.     }

  213.     private static final void DisplayBookGraph (
  214.         final Adiabat bookGraph,
  215.         final String displayPrefix)
  216.     {
  217.         System.out.println (
  218.             displayPrefix +
  219.             "-------------------------------------------------||"
  220.         );

  221.         System.out.println (
  222.             displayPrefix +
  223.             bookGraph.name() + " | " +
  224.             bookGraph.id() + " ||"
  225.         );

  226.         System.out.println (
  227.             displayPrefix +
  228.             "-------------------------------------------------||"
  229.         );

  230.         System.out.println();

  231.         System.out.println (
  232.             displayPrefix +
  233.             "----------------------------------------------------------------------------- ||"
  234.         );

  235.         for (Map.Entry<String, FundingGroup> fundingGroupEntry : bookGraph.fundingGroupMap().entrySet())
  236.         {
  237.             FundingGroup fundingGroup = fundingGroupEntry.getValue();

  238.             System.out.println (
  239.                 displayPrefix + "\t" +
  240.                 fundingGroup.name() + " | " +
  241.                 fundingGroup.id() + " ||"
  242.             );

  243.             System.out.println (
  244.                 displayPrefix +
  245.                 "----------------------------------------------------------------------------- ||"
  246.             );

  247.             for (Map.Entry<String, CreditDebtGroup> creditDebtGroupEntry :
  248.                 fundingGroup.creditDebtGroupMap().entrySet())
  249.             {
  250.                 CreditDebtGroup creditDebtGroup = creditDebtGroupEntry.getValue();

  251.                 System.out.println (
  252.                     displayPrefix + "\t\t" +
  253.                     creditDebtGroup.name() + " | " +
  254.                     creditDebtGroup.id() + " ||"
  255.                 );

  256.                 for (Map.Entry<String, CollateralGroup> collateralGroupEntry :
  257.                     creditDebtGroup.collateralGroupMap().entrySet())
  258.                 {
  259.                     CollateralGroup collateralGroup = collateralGroupEntry.getValue();
  260.    
  261.                     System.out.println (
  262.                         displayPrefix + "\t\t\t" +
  263.                         collateralGroup.name() + " | " +
  264.                         collateralGroup.id() + " ||"
  265.                     );

  266.                     for (Map.Entry<String, PositionGroup> positionGroupEntry :
  267.                         collateralGroup.positionGroupMap().entrySet())
  268.                     {
  269.                         PositionGroup positionGroup = positionGroupEntry.getValue();

  270.                         System.out.println (
  271.                             displayPrefix + "\t\t\t\t" +
  272.                             positionGroup.name() + " | " +
  273.                             positionGroup.id() + " ||"
  274.                         );
  275.                     }
  276.                 }

  277.                 System.out.println (
  278.                     displayPrefix +
  279.                     "----------------------------------------------------------------------------- ||"
  280.                 );
  281.             }

  282.             System.out.println (
  283.                 displayPrefix +
  284.                 "----------------------------------------------------------------------------- ||"
  285.             );
  286.         }
  287.     }

  288.     public static final void main (
  289.         final String[] args)
  290.         throws Exception
  291.     {
  292.         EnvManager.InitEnv ("");

  293.         String bank = "NOM";
  294.         String currency = "USD";
  295.         String counterParty = "BAC";
  296.         int positionGroupPerCollateralGroup = 3;
  297.         int collateralGroupPerCreditDebtGroup = 3;
  298.         int creditDebtGroupCountPerFundingGroup = 3;
  299.         int fundingGroupCount = 3;
  300.         String displayPrefix = "\t|| ";

  301.         Adiabat bookGraph = BookTopology (
  302.             PositionGroupSpecification.ZeroThreshold (
  303.                 "POSITIONGROUPSPEC",
  304.                 PositionReplicationScheme.ALBANESE_ANDERSEN_VERTEX,
  305.                 BrokenDateScheme.SQUARE_ROOT_OF_TIME,
  306.                 0.,
  307.                 CloseOutScheme.ISDA_92
  308.             ),
  309.             new CollateralGroupSpecification (
  310.                 StringUtil.GUID(),
  311.                 "COLLATERALGROUPSPEC",
  312.                 OvernightLabel.Create (currency),
  313.                 CSALabel.ISDA (currency)
  314.             ),
  315.             new CreditDebtGroupSpecification (
  316.                 StringUtil.GUID(),
  317.                 "CREDITDEBTGROUPSPEC",
  318.                 EntityHazardLabel.Standard (
  319.                     bank,
  320.                     currency
  321.                 ),
  322.                 EntityHazardLabel.Standard (
  323.                     counterParty,
  324.                     currency
  325.                 ),
  326.                 EntityRecoveryLabel.Senior (
  327.                     bank,
  328.                     currency
  329.                 ),
  330.                 EntityRecoveryLabel.Senior (
  331.                     counterParty,
  332.                     currency
  333.                 ),
  334.                 EntityRecoveryLabel.Subordinate (
  335.                     bank,
  336.                     currency
  337.                 ),
  338.                 true,
  339.                 true
  340.             ),
  341.             new FundingGroupSpecification (
  342.                 StringUtil.GUID(),
  343.                 "FUNDINGGROUPSPEC",
  344.                 EntityFundingLabel.Senior (
  345.                     bank,
  346.                     currency
  347.                 ),
  348.                 EntityFundingLabel.Senior (
  349.                     counterParty,
  350.                     currency
  351.                 ),
  352.                 EntityFundingLabel.Subordinate (
  353.                     bank,
  354.                     currency
  355.                 )
  356.             ),
  357.             fundingGroupCount,
  358.             currency,
  359.             bank,
  360.             counterParty,
  361.             creditDebtGroupCountPerFundingGroup,
  362.             collateralGroupPerCreditDebtGroup,
  363.             positionGroupPerCollateralGroup
  364.         );

  365.         DisplayBookGraph (
  366.             bookGraph,
  367.             displayPrefix
  368.         );

  369.         System.out.println();

  370.         EnvManager.TerminateEnv();
  371.     }
  372. }