DaJagannathan2005a.java

  1. package org.drip.sample.blacklitterman;

  2. import org.drip.measure.bayesian.*;
  3. import org.drip.measure.continuous.MultivariateMeta;
  4. import org.drip.measure.gaussian.*;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.service.env.EnvManager;

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

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

  82. /**
  83.  * <i>DaJagannathan2005a</i> reconciles the Outputs of the Black-Litterman Model Process. The References are:
  84.  *  
  85.  * <br><br>
  86.  *  <ul>
  87.  *      <li>
  88.  *          Da, Z., and R. Jagannathan (2005): https://www3.nd.edu/~zda/TeachingNote_Black-Litterman.pdf
  89.  *      </li>
  90.  *      <li>
  91.  *          He. G., and R. Litterman (1999): <i>The Intuition behind the Black-Litterman Model Portfolios</i>
  92.  *              <b>Goldman Sachs Asset Management</b>
  93.  *      </li>
  94.  *  </ul>
  95.  *  
  96.  * <br><br>
  97.  *  <ul>
  98.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  99.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/TransactionCostAnalyticsLibrary.md">Transaction Cost Analytics</a></li>
  100.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  101.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/blacklitterman/README.md">Canonical Black Litterman and Extensions</a></li>
  102.  *  </ul>
  103.  * <br><br>
  104.  *
  105.  * @author Lakshmi Krishnamurthy
  106.  */

  107. public class DaJagannathan2005a {

  108.     public static final void main (
  109.         final String[] astArgs)
  110.         throws Exception
  111.     {
  112.         EnvManager.InitEnv (
  113.             "",
  114.             true
  115.         );

  116.         String[] astrAssetID = new String[] {
  117.             "A",
  118.             "B",
  119.             "C"
  120.         };

  121.         double[] adblAssetExcessReturns = new double[] {
  122.             0.01,
  123.             0.01,
  124.             0.01
  125.         };

  126.         double[][] aadblAssetExcessReturnsCovariance = new double[][] {
  127.             {0.00091, 0.00030, 0.00060},
  128.             {0.00030, 0.00011, 0.00020},
  129.             {0.00060, 0.00020, 0.00041}
  130.         };

  131.         double[][] aadblAssetSpaceViewProjection = new double[][] {
  132.             {1.000, -1.000,  0.000}
  133.         };

  134.         double dblTau = 1.00;
  135.         double dblDelta = 1.00;

  136.         double[] adblProjectionExpectedExcessReturns = new double[] {
  137.             0.02
  138.         };

  139.         double[][] aadblProjectionExcessReturnsCovariance = new double[][] {
  140.             {0.0001}
  141.         };

  142.         double[] adblAssetSpaceJointReturnsReconciler = new double[] {
  143.             0.0335,
  144.             0.0173,
  145.             0.0254
  146.         };

  147.         R1MultivariateNormal viewDistribution = R1MultivariateNormal.Standard (
  148.             new MultivariateMeta (new String[] {"DJVIEW"}),
  149.             adblProjectionExpectedExcessReturns,
  150.             aadblProjectionExcessReturnsCovariance
  151.         );

  152.         R1MultivariateNormal r1mnScoping = R1MultivariateNormal.Standard (
  153.             new MultivariateMeta (astrAssetID),
  154.             adblAssetExcessReturns,
  155.             aadblAssetExcessReturnsCovariance
  156.         );

  157.         ScopingProjectionVariateDistribution spvd = new ScopingProjectionVariateDistribution (r1mnScoping);

  158.         spvd.addProjectionDistributionLoading (
  159.             "VIEW",
  160.             new ProjectionDistributionLoading (
  161.                 viewDistribution,
  162.                 aadblAssetSpaceViewProjection
  163.             )
  164.         );

  165.         R1MultivariateConvolutionMetrics jpm = TheilMixedEstimationModel.GenerateComposite (
  166.             spvd,
  167.             "VIEW",
  168.             r1mnScoping
  169.         );

  170.         R1MultivariateNormal jointDistribution = (R1MultivariateNormal) jpm.joint();

  171.         R1MultivariateNormal posteriorDistribution = (R1MultivariateNormal) jpm.posterior();

  172.         double[] adblAssetSpaceJointReturns = jointDistribution.mean();

  173.         double[][] aadblAssetSpaceJointCovariance = jointDistribution.covariance().covarianceMatrix();

  174.         double[][] aadblAssetSpacePosteriorCovariance = posteriorDistribution.covariance().covarianceMatrix();

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

  176.         System.out.println ("\t| TAU   => " + FormatUtil.FormatDouble (dblTau, 1, 8, 1.) + "   ||");

  177.         System.out.println ("\t| DELTA => " + FormatUtil.FormatDouble (dblDelta, 1, 8, 1.) + "   ||");

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

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

  180.         System.out.println ("\t|                           PRIOR CROSS ASSET COVARIANCE MATRIX                                  ||");

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

  182.         String strHeader = "\t|     |";

  183.         for (int i = 0; i < astrAssetID.length; ++i)
  184.             strHeader += "    " + astrAssetID[i] + "     |";

  185.         System.out.println (strHeader + "|");

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

  187.         for (int i = 0; i < astrAssetID.length; ++i) {
  188.             String strDump = "\t| " + astrAssetID[i] + " ";

  189.             for (int j = 0; j < astrAssetID.length; ++j)
  190.                 strDump += "|" + FormatUtil.FormatDouble (aadblAssetExcessReturnsCovariance[i][j], 1, 8, 1.) + " ";

  191.             System.out.println (strDump + "||");
  192.         }

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

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

  195.         System.out.println ("\t|                          VIEW SCOPING ASSET PROJECTION LOADING                                 ||");

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

  197.         strHeader = "\t|     |";

  198.         for (int i = 0; i < astrAssetID.length; ++i)
  199.             strHeader += "    " + astrAssetID[i] + "     |";

  200.         System.out.println (strHeader + "|");

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

  202.         for (int i = 0; i < aadblAssetSpaceViewProjection.length; ++i) {
  203.             String strDump = "\t|  #" + i + " ";

  204.             for (int j = 0; j < astrAssetID.length; ++j)
  205.                 strDump += "|" + FormatUtil.FormatDouble (aadblAssetSpaceViewProjection[i][j], 1, 8, 1.) + " ";

  206.             System.out.println (strDump + "||");
  207.         }

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

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

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

  211.         for (int i = 0; i < aadblAssetSpaceViewProjection.length; ++i) {
  212.             String strDump = "\t|  #" + i + " ";

  213.             for (int j = 0; j < aadblAssetSpaceViewProjection.length; ++j)
  214.                 strDump += "|" + FormatUtil.FormatDouble (aadblProjectionExcessReturnsCovariance[i][j], 1, 8, 1.) + " ";

  215.             System.out.println (strDump + "|" + FormatUtil.FormatDouble (adblProjectionExpectedExcessReturns[i], 1, 2, 100.) + "%");
  216.         }

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

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

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

  220.         System.out.println ("\t|                           JOINT CROSS ASSET COVARIANCE MATRIX                                  ||");

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

  222.         strHeader = "\t|     |";

  223.         for (int i = 0; i < astrAssetID.length; ++i)
  224.             strHeader += "    " + astrAssetID[i] + "     |";

  225.         System.out.println (strHeader + "|");

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

  227.         for (int i = 0; i < astrAssetID.length; ++i) {
  228.             String strDump = "\t| " + astrAssetID[i] + " ";

  229.             for (int j = 0; j < astrAssetID.length; ++j)
  230.                 strDump += "|" + FormatUtil.FormatDouble (aadblAssetSpaceJointCovariance[i][j], 1, 8, 1.) + " ";

  231.             System.out.println (strDump + "||");
  232.         }

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

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

  235.         System.out.println ("\t|                         POSTERIOR CROSS ASSET COVARIANCE MATRIX                                ||");

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

  237.         strHeader = "\t|     |";

  238.         for (int i = 0; i < astrAssetID.length; ++i)
  239.             strHeader += "    " + astrAssetID[i] + "     |";

  240.         System.out.println (strHeader + "|");

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

  242.         for (int i = 0; i < astrAssetID.length; ++i) {
  243.             String strDump = "\t| " + astrAssetID[i] + " ";

  244.             for (int j = 0; j < astrAssetID.length; ++j)
  245.                 strDump += "|" + FormatUtil.FormatDouble (aadblAssetSpacePosteriorCovariance[i][j], 1, 8, 1.) + " ";

  246.             System.out.println (strDump + "||");
  247.         }

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

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

  250.         System.out.println ("\t| JOINT/POSTERIOR RETURN ||");

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

  252.         System.out.println ("\t|  ID  =>  RIOC  | DJ05  ||");

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

  254.         for (int i = 0; i < adblAssetSpaceJointReturnsReconciler.length; ++i) {
  255.             System.out.println (
  256.                 "\t|  [" + astrAssetID[i] + "] => " +
  257.                 FormatUtil.FormatDouble (adblAssetSpaceJointReturns[i], 1, 2, 100.) + "% |" +
  258.                 FormatUtil.FormatDouble (adblAssetSpaceJointReturnsReconciler[i], 1, 2, 100.) + "% ||"
  259.             );
  260.         }

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

  262.         EnvManager.TerminateEnv();
  263.     }
  264. }