BlackLittermanProcessor.java

  1. package org.drip.json.assetallocation;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

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

  77. /**
  78.  * <i>BlackLittermanProcessor</i> Sets Up and Executes a JSON Based In/Out Processing Service for the Black
  79.  * Litterman Bayesian View Incorporation/Parameter Estimation.
  80.  *
  81.  *  <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/json">RFC-4627 Compliant JSON Encoder/Decoder (Parser)</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/json/assetallocation">JSON Based In/Out Service</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class BlackLittermanProcessor {

  92.     /**
  93.      * JSON Based in/out Bayesian Co-variance/Returns Estimation Thunker
  94.      *
  95.      * @param jsonParameter Bayesian Co-variance/Returns Estimation Parameters
  96.      *
  97.      * @return JSON Bayesian Co-variance/Returns Estimation Response
  98.      */

  99.     @SuppressWarnings ("unchecked") public static final org.drip.json.simple.JSONObject Estimate (
  100.         final org.drip.json.simple.JSONObject jsonParameter)
  101.     {
  102.         java.lang.String[] astrAssetID = org.drip.json.parser.Converter.StringArrayEntry (jsonParameter,
  103.             "AssetSet");

  104.         double[][] aadblAssetSpaceViewProjection = org.drip.json.parser.Converter.DualDoubleArrayEntry
  105.             (jsonParameter, "AssetSpaceViewProjection");

  106.         double dblTau = java.lang.Double.NaN;
  107.         double dblRiskAversion = java.lang.Double.NaN;
  108.         double dblRiskFreeRate = java.lang.Double.NaN;
  109.         org.drip.measure.gaussian.R1MultivariateNormal viewDistribution = null;
  110.         org.drip.portfolioconstruction.bayesian.BlackLittermanCombinationEngine blce = null;
  111.         int iNumView = null == aadblAssetSpaceViewProjection ? 0 : aadblAssetSpaceViewProjection.length;
  112.         java.lang.String[] astrProjectionName = 0 == iNumView? null : new java.lang.String[iNumView];

  113.         double[] adblAssetEquilibriumWeight = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter,
  114.             "AssetEquilibriumWeight");

  115.         double[][] aadblAssetExcessReturnsCovariance = org.drip.json.parser.Converter.DualDoubleArrayEntry
  116.             (jsonParameter, "AssetExcessReturnsCovariance");

  117.         double[] adblProjectionExpectedExcessReturns = org.drip.json.parser.Converter.DoubleArrayEntry
  118.             (jsonParameter, "ProjectionExpectedExcessReturns");

  119.         double[][] aadblProjectionExcessReturnsCovariance =
  120.             org.drip.json.parser.Converter.DualDoubleArrayEntry (jsonParameter,
  121.                 "ProjectionExcessReturnsCovariance");

  122.         for (int i = 0; i < iNumView ; ++i)
  123.             astrProjectionName[i] = "PROJECTION #" + i;

  124.         try {
  125.             dblTau = org.drip.json.parser.Converter.DoubleEntry (jsonParameter, "Tau");

  126.             dblRiskAversion = org.drip.json.parser.Converter.DoubleEntry (jsonParameter, "Delta");

  127.             dblRiskFreeRate = org.drip.json.parser.Converter.DoubleEntry (jsonParameter, "RiskFreeRate");

  128.             viewDistribution = org.drip.measure.gaussian.R1MultivariateNormal.Standard (new
  129.                 org.drip.measure.continuous.MultivariateMeta (astrProjectionName),
  130.                     adblProjectionExpectedExcessReturns, aadblProjectionExcessReturnsCovariance);

  131.             blce = new org.drip.portfolioconstruction.bayesian.BlackLittermanCombinationEngine
  132.                 (org.drip.portfolioconstruction.allocator.ForwardReverseHoldingsAllocation.Reverse
  133.                     (org.drip.portfolioconstruction.asset.Portfolio.Standard (astrAssetID,
  134.                         adblAssetEquilibriumWeight), aadblAssetExcessReturnsCovariance, dblRiskAversion), new
  135.                             org.drip.portfolioconstruction.bayesian.PriorControlSpecification (false,
  136.                                 dblRiskFreeRate, dblTau), new
  137.                                     org.drip.portfolioconstruction.bayesian.ProjectionSpecification
  138.                                         (viewDistribution, aadblAssetSpaceViewProjection));
  139.         } catch (java.lang.Exception e) {
  140.             e.printStackTrace();

  141.             return null;
  142.         }

  143.         org.drip.portfolioconstruction.bayesian.BlackLittermanCustomConfidenceOutput blo = blce.customConfidenceRun();

  144.         if (null == blo) return null;

  145.         org.drip.measure.bayesian.R1MultivariateConvolutionMetrics jpm = blo.jointPosteriorMetrics();

  146.         org.drip.measure.continuous.R1Multivariate r1mPrior = jpm.prior();

  147.         org.drip.measure.continuous.R1Multivariate r1mJoint = jpm.joint();

  148.         org.drip.measure.continuous.R1Multivariate r1mPosterior = jpm.posterior();

  149.         if (null == r1mPrior || !(r1mPrior instanceof org.drip.measure.gaussian.R1MultivariateNormal) || null
  150.             == r1mJoint || !(r1mJoint instanceof org.drip.measure.gaussian.R1MultivariateNormal) || null ==
  151.                 r1mPosterior || !(r1mPosterior instanceof org.drip.measure.gaussian.R1MultivariateNormal))
  152.             return null;

  153.         org.drip.measure.gaussian.R1MultivariateNormal r1mnPrior =
  154.             (org.drip.measure.gaussian.R1MultivariateNormal) r1mPrior;
  155.         org.drip.measure.gaussian.R1MultivariateNormal r1mnJoint =
  156.             (org.drip.measure.gaussian.R1MultivariateNormal) r1mJoint;
  157.         org.drip.measure.gaussian.R1MultivariateNormal r1mnPosterior =
  158.             (org.drip.measure.gaussian.R1MultivariateNormal) r1mPosterior;

  159.         org.drip.json.simple.JSONObject jsonResponse = new org.drip.json.simple.JSONObject();

  160.         jsonResponse.put ("Tau", dblTau);

  161.         jsonResponse.put ("Delta", dblRiskAversion);

  162.         jsonResponse.put ("RiskFreeRate", dblRiskFreeRate);

  163.         jsonResponse.put ("ScopingSet", org.drip.json.parser.Converter.Array (astrAssetID));

  164.         jsonResponse.put ("ScopingExpectedExcessReturns", org.drip.json.parser.Converter.Array
  165.             (r1mnPrior.mean()));

  166.         jsonResponse.put ("ScopingExcessReturnsCovariance", org.drip.json.parser.Converter.Array
  167.             (r1mnPrior.covariance().covarianceMatrix()));

  168.         jsonResponse.put ("ProjectionExpectedExcessReturns", org.drip.json.parser.Converter.Array
  169.             (adblProjectionExpectedExcessReturns));

  170.         jsonResponse.put ("ViewScopingProjectionLoading", org.drip.json.parser.Converter.Array
  171.             (aadblAssetSpaceViewProjection));

  172.         jsonResponse.put ("JointExcessReturnsCovariance", org.drip.json.parser.Converter.Array
  173.             (r1mnJoint.covariance().covarianceMatrix()));

  174.         jsonResponse.put ("PosteriorExcessReturnsCovariance", org.drip.json.parser.Converter.Array
  175.             (r1mnPosterior.covariance().covarianceMatrix()));

  176.         jsonResponse.put ("PriorExpectedExcessReturn", org.drip.json.parser.Converter.Array
  177.             (r1mPrior.mean()));

  178.         jsonResponse.put ("PosteriorExpectedExcessReturn", org.drip.json.parser.Converter.Array
  179.             (r1mPosterior.mean()));

  180.         return jsonResponse;
  181.     }
  182. }