Yamabe2016.java

  1. package org.drip.sample.blacklitterman;

  2. import org.drip.measure.bayesian.R1MultivariateConvolutionMetrics;
  3. import org.drip.measure.continuous.MultivariateMeta;
  4. import org.drip.measure.gaussian.*;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.portfolioconstruction.allocator.ForwardReverseHoldingsAllocation;
  7. import org.drip.portfolioconstruction.asset.Portfolio;
  8. import org.drip.portfolioconstruction.bayesian.*;
  9. import org.drip.service.env.EnvManager;

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

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

  85. /**
  86.  * <i>Yamabe2016</i> reconciles the Outputs of the Black-Litterman Model Process. The Reference is:
  87.  *  
  88.  * <br><br>
  89.  *  <ul>
  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 Yamabe2016 {

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

  116.         double dblTau = 1.0000;
  117.         double dblRiskAversion = 2.6;
  118.         double dblRiskFreeRate = 0.00;

  119.         String[] astrAssetID = new String[] {
  120.             "ASSET A ",
  121.             "ASSET B ",
  122.             "ASSET C ",
  123.             "ASSET D ",
  124.             "ASSET E ",
  125.             "ASSET F "
  126.         };

  127.         double[] adblAssetEquilibriumWeight = new double[] {
  128.             0.2535,
  129.             0.1343,
  130.             0.1265,
  131.             0.1375,
  132.             0.0733,
  133.             0.2749
  134.         };

  135.         double[][] aadblAssetExcessReturnsCovariance = new double[][] {
  136.             {0.00273, 0.00208, 0.00159, 0.00049, 0.00117, 0.00071},
  137.             {0.00208, 0.00277, 0.00130, 0.00046, 0.00111, 0.00056},
  138.             {0.00159, 0.00130, 0.00146, 0.00064, 0.00105, 0.00052},
  139.             {0.00049, 0.00046, 0.00064, 0.00061, 0.00066, 0.00037},
  140.             {0.00117, 0.00111, 0.00105, 0.00066, 0.00139, 0.00066},
  141.             {0.00071, 0.00056, 0.00052, 0.00037, 0.00066, 0.00070}
  142.         };

  143.         double[][] aadblAssetSpaceViewProjection = new double[][] {
  144.             {  0.00,  0.00, -1.00,  0.00,  1.00,  0.00},
  145.             {  0.00,  1.00,  0.00,  0.00, -1.00,  0.00},
  146.             { -1.00,  1.00,  1.00,  0.00,  0.00, -1.00}
  147.         };

  148.         double[] adblProjectionExpectedExcessReturns = new double[] {
  149.             0.0002,
  150.             0.0003,
  151.             0.0001
  152.         };

  153.         double[][] aadblProjectionExcessReturnsCovariance = new double[][] {
  154.             { 0.00075, -0.00053, -0.00033},
  155.             {-0.00053,  0.00195,  0.00110},
  156.             {-0.00033,  0.00110,  0.00217}
  157.         };

  158.         R1MultivariateNormal viewDistribution = R1MultivariateNormal.Standard (
  159.             new MultivariateMeta (
  160.                 new String[] {
  161.                     "PROJECTION #1",
  162.                     "PROJECTION #2",
  163.                     "PROJECTION #3"
  164.                 }
  165.             ),
  166.             adblProjectionExpectedExcessReturns,
  167.             aadblProjectionExcessReturnsCovariance
  168.         );

  169.         double[] adblAssetSpacePriorReturnsReconciler = new double[] {
  170.             0.003954,
  171.             0.003540,
  172.             0.002782,
  173.             0.001299,
  174.             0.002476,
  175.             0.001594
  176.         };

  177.         double[] adblAssetSpaceJointReturnsReconciler = new double[] {
  178.             0.003755,
  179.             0.003241,
  180.             0.002612,
  181.             0.001305,
  182.             0.002559,
  183.             0.001662
  184.         };

  185.         double[] adblExpectedHistoricalReturns = new double[] {
  186.             0.003559,
  187.             0.000469,
  188.             0.004053,
  189.             0.004527,
  190.             0.000904,
  191.             0.001581
  192.         };

  193.         BlackLittermanCombinationEngine blce = new BlackLittermanCombinationEngine (
  194.             ForwardReverseHoldingsAllocation.Reverse (
  195.                 Portfolio.Standard (
  196.                     astrAssetID,
  197.                     adblAssetEquilibriumWeight
  198.                 ),
  199.                 aadblAssetExcessReturnsCovariance,
  200.                 dblRiskAversion
  201.             ),
  202.             new PriorControlSpecification (
  203.                 false,
  204.                 dblRiskFreeRate,
  205.                 dblTau
  206.             ),
  207.             new ProjectionSpecification (
  208.                 viewDistribution,
  209.                 aadblAssetSpaceViewProjection
  210.             )
  211.         );

  212.         R1MultivariateConvolutionMetrics jpm = blce.customConfidenceRun().jointPosteriorMetrics();

  213.         R1MultivariateNormal priorDistribution = (R1MultivariateNormal) jpm.prior();

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

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

  216.         double[] adblAssetSpacePriorReturns = priorDistribution.mean();

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

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

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

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

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

  222.         System.out.println ("\t| RISK AVERSION  =>" + FormatUtil.FormatDouble (dblRiskAversion, 1, 2, 1.) + "  ||");

  223.         System.out.println ("\t| RISK FREE RATE =>" + FormatUtil.FormatDouble (dblRiskFreeRate, 1, 2, 1.) + "% ||");

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

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

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

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

  228.         String strHeader = "\t|    ID    |";

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

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

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

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

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

  237.             System.out.println (strDump + "||");
  238.         }

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

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

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

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

  243.         strHeader = "\t|     |";

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

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

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

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

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

  252.             System.out.println (strDump + "||");
  253.         }

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

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

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

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

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

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

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

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

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

  266.         strHeader = "\t|    ID    |";

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

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

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

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

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

  275.             System.out.println (strDump + "||");
  276.         }

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

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

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

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

  281.         strHeader = "\t|    ID    |";

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

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

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

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

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

  290.             System.out.println (strDump + "||");
  291.         }

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

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

  294.         System.out.println ("\t|      IMPLIED/PRIOR RETURN       ||");

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

  296.         System.out.println ("\t|     ID     =>  RIOCEE |  YAMABE ||");

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

  298.         for (int i = 0; i < adblAssetSpacePriorReturns.length; ++i) {
  299.             System.out.println (
  300.                 "\t| [" + astrAssetID[i] + "] =>" +
  301.                 FormatUtil.FormatDouble (adblAssetSpacePriorReturns[i], 1, 4, 100.) + "% |" +
  302.                 FormatUtil.FormatDouble (adblAssetSpacePriorReturnsReconciler[i], 1, 4, 100.) + "% ||"
  303.             );
  304.         }

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

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

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

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

  309.         System.out.println ("\t|     ID     =>  RIOCEE |  YAMABE ||");

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

  311.         for (int i = 0; i < adblAssetSpaceJointReturnsReconciler.length; ++i) {
  312.             System.out.println (
  313.                 "\t| [" + astrAssetID[i] + "] =>" +
  314.                 FormatUtil.FormatDouble (adblAssetSpaceJointReturns[i], 1, 4, 100.) + "% |" +
  315.                 FormatUtil.FormatDouble (adblAssetSpaceJointReturnsReconciler[i], 1, 4, 100.) + "% ||"
  316.             );
  317.         }

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

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

  320.         System.out.println ("\t|     PRIOR/POSTERIOR/HISTORICAL RETURN     ||");

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

  322.         System.out.println ("\t|     ID     =>  PRIOR  |   POST  |   HIST  ||");

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

  324.         for (int i = 0; i < adblAssetSpaceJointReturnsReconciler.length; ++i) {
  325.             System.out.println (
  326.                 "\t| [" + astrAssetID[i] + "] =>" +
  327.                 FormatUtil.FormatDouble (adblAssetSpacePriorReturns[i], 1, 4, 100.) + "% |" +
  328.                 FormatUtil.FormatDouble (adblAssetSpaceJointReturns[i], 1, 4, 100.) + "% |" +
  329.                 FormatUtil.FormatDouble (adblExpectedHistoricalReturns[i], 1, 4, 100.) + "% ||"
  330.             );
  331.         }

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

  333.         EnvManager.TerminateEnv();
  334.     }
  335. }