PortfolioConstructionProcessor.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>PortfolioConstructionProcessor</i> Sets Up and Executes a JSON Based In/Out Processing Service for
  79.  * Constrained and Unconstrained Portfolio Construction.
  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 PortfolioConstructionProcessor {

  92.     /**
  93.      * JSON Based in/out Budget Constrained Mean Variance Allocation Thunker
  94.      *
  95.      * @param jsonParameter Budget Constrained Mean Variance Allocation Parameters
  96.      *
  97.      * @return JSON Budget Constrained Mean Variance Allocation Response
  98.      */

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

  105.         org.drip.portfolioconstruction.allocator.BoundedHoldingsAllocationControl pdp = null;
  106.         int iNumAsset = null == astrAssetID ? 0 : astrAssetID.length;
  107.         double[] adblAssetLowerBound = new double[iNumAsset];
  108.         double[] adblAssetUpperBound = new double[iNumAsset];

  109.         if (0 == iNumAsset) return null;

  110.         double[] adblAssetExpectedReturns = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter,
  111.             "AssetExpectedReturns");

  112.         double[][] aadblAssetReturnsCovariance = org.drip.json.parser.Converter.DualDoubleArrayEntry
  113.             (jsonParameter, "AssetReturnsCovariance");

  114.         for (int i = 0; i < iNumAsset; ++i) {
  115.             try {
  116.                 adblAssetLowerBound[i] = org.drip.json.parser.Converter.DoubleEntry (jsonParameter,
  117.                     astrAssetID[i] + "::LowerBound");

  118.                 adblAssetUpperBound[i] = org.drip.json.parser.Converter.DoubleEntry (jsonParameter,
  119.                     astrAssetID[i] + "::UpperBound");
  120.             } catch (java.lang.Exception e) {
  121.                 e.printStackTrace();

  122.                 return null;
  123.             }
  124.         }

  125.         org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties ausp =
  126.             org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties.FromMultivariateMetrics
  127.                 (org.drip.measure.statistics.MultivariateMoments.Standard (astrAssetID,
  128.                     adblAssetExpectedReturns, aadblAssetReturnsCovariance));

  129.         if (null == ausp) return null;

  130.         org.drip.portfolioconstruction.allocator.ConstrainedMeanVarianceOptimizer cmva = new
  131.             org.drip.portfolioconstruction.allocator.ConstrainedMeanVarianceOptimizer (null,
  132.                 org.drip.function.rdtor1descent.LineStepEvolutionControl.NocedalWrightStrongWolfe (false));

  133.         try {
  134.             pdp = new org.drip.portfolioconstruction.allocator.BoundedHoldingsAllocationControl
  135.                 (astrAssetID, new org.drip.portfolioconstruction.allocator.CustomRiskUtilitySettings (1.,
  136.                     0.), new org.drip.portfolioconstruction.allocator.EqualityConstraintSettings
  137.                         (org.drip.portfolioconstruction.allocator.EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT,
  138.                         java.lang.Double.NaN));
  139.         } catch (java.lang.Exception e) {
  140.             e.printStackTrace();

  141.             return null;
  142.         }

  143.         for (int i = 0; i < astrAssetID.length; ++i) {
  144.             if (!pdp.addBound (astrAssetID[i], adblAssetLowerBound[i], adblAssetUpperBound[i])) return null;
  145.         }

  146.         org.drip.portfolioconstruction.allocator.HoldingsAllocation oo = cmva.allocate (pdp, ausp);

  147.         if (null == oo) return null;

  148.         org.drip.portfolioconstruction.asset.Portfolio pf = oo.optimalPortfolio();

  149.         org.drip.portfolioconstruction.asset.AssetComponent[] aAC = pf.assetComponentArray();

  150.         if (null == aAC || aAC.length != iNumAsset) return null;

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

  152.         jsonResponse.put ("AssetSet", org.drip.json.parser.Converter.Array (astrAssetID));

  153.         jsonResponse.put ("AssetExpectedReturns", org.drip.json.parser.Converter.Array
  154.             (adblAssetExpectedReturns));

  155.         jsonResponse.put ("AssetReturnsCovariance", org.drip.json.parser.Converter.Array
  156.             (aadblAssetReturnsCovariance));

  157.         for (int i = 0; i < adblAssetExpectedReturns.length; ++i) {
  158.             jsonResponse.put (astrAssetID[i] + "::LowerBound", adblAssetLowerBound[i]);

  159.             jsonResponse.put (astrAssetID[i] + "::UpperBound", adblAssetUpperBound[i]);
  160.         }

  161.         for (int i = 0; i < astrAssetID.length; ++i)
  162.             jsonResponse.put (aAC[i].id() + "::WEIGHT", aAC[i].amount());

  163.         jsonResponse.put ("PortfolioNotional", pf.notional());

  164.         try {
  165.             jsonResponse.put ("PortfolioExpectedReturn", pf.expectedReturn (ausp));

  166.             jsonResponse.put ("PortfolioStandardDeviation", java.lang.Math.sqrt (pf.variance (ausp)));

  167.             return jsonResponse;
  168.         } catch (java.lang.Exception e) {
  169.             e.printStackTrace();
  170.         }

  171.         return null;
  172.     }

  173.     /**
  174.      * JSON Based in/out Returns Constrained Mean Variance Allocation Thunker
  175.      *
  176.      * @param jsonParameter Returns Constrained Mean Variance Allocation Parameters
  177.      *
  178.      * @return JSON Returns Constrained Mean Variance Allocation Response
  179.      */

  180.     @SuppressWarnings ("unchecked") public static final org.drip.json.simple.JSONObject
  181.         ReturnsConstrainedAllocator (
  182.             final org.drip.json.simple.JSONObject jsonParameter)
  183.     {
  184.         java.lang.String[] astrAssetID = org.drip.json.parser.Converter.StringArrayEntry (jsonParameter,
  185.             "AssetSet");

  186.         org.drip.portfolioconstruction.allocator.BoundedHoldingsAllocationControl pdp = null;
  187.         int iNumAsset = null == astrAssetID ? 0 : astrAssetID.length;
  188.         double dblPortfolioDesignReturn = java.lang.Double.NaN;
  189.         double[] adblAssetLowerBound = new double[iNumAsset];
  190.         double[] adblAssetUpperBound = new double[iNumAsset];

  191.         if (0 == iNumAsset) return null;

  192.         double[] adblAssetReturnsMean = org.drip.json.parser.Converter.DoubleArrayEntry (jsonParameter,
  193.             "AssetReturnsMean");

  194.         double[][] aadblAssetReturnsCovariance = org.drip.json.parser.Converter.DualDoubleArrayEntry
  195.             (jsonParameter, "AssetReturnsCovariance");

  196.         try {
  197.             dblPortfolioDesignReturn = org.drip.json.parser.Converter.DoubleEntry (jsonParameter,
  198.                 "PortfolioDesignReturn");

  199.             for (int i = 0; i < iNumAsset; ++i) {
  200.                 adblAssetLowerBound[i] = org.drip.json.parser.Converter.DoubleEntry (jsonParameter,
  201.                     astrAssetID[i] + "::LowerBound");

  202.                 adblAssetUpperBound[i] = org.drip.json.parser.Converter.DoubleEntry (jsonParameter,
  203.                     astrAssetID[i] + "::UpperBound");
  204.             }
  205.         } catch (java.lang.Exception e) {
  206.             e.printStackTrace();

  207.             return null;
  208.         }

  209.         org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties ausp =
  210.             org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties.FromMultivariateMetrics
  211.                 (org.drip.measure.statistics.MultivariateMoments.Standard (astrAssetID,
  212.                     adblAssetReturnsMean, aadblAssetReturnsCovariance));

  213.         if (null == ausp) return null;

  214.         org.drip.portfolioconstruction.allocator.ConstrainedMeanVarianceOptimizer cmva = new
  215.             org.drip.portfolioconstruction.allocator.ConstrainedMeanVarianceOptimizer (null, null);

  216.         try {
  217.             pdp = new org.drip.portfolioconstruction.allocator.BoundedHoldingsAllocationControl
  218.                 (astrAssetID, new org.drip.portfolioconstruction.allocator.CustomRiskUtilitySettings (1.,
  219.                     0.), new org.drip.portfolioconstruction.allocator.EqualityConstraintSettings
  220.                         (org.drip.portfolioconstruction.allocator.EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT
  221.                         | org.drip.portfolioconstruction.allocator.EqualityConstraintSettings.RETURNS_CONSTRAINT,
  222.                         dblPortfolioDesignReturn));
  223.         } catch (java.lang.Exception e) {
  224.             e.printStackTrace();

  225.             return null;
  226.         }

  227.         for (int i = 0; i < astrAssetID.length; ++i) {
  228.             if (!pdp.addBound (astrAssetID[i], adblAssetLowerBound[i], adblAssetUpperBound[i])) return null;
  229.         }

  230.         org.drip.portfolioconstruction.allocator.HoldingsAllocation oo = cmva.allocate (pdp, ausp);

  231.         if (null == oo) return null;

  232.         org.drip.portfolioconstruction.asset.Portfolio pf = oo.optimalPortfolio();

  233.         org.drip.portfolioconstruction.asset.AssetComponent[] aAC = pf.assetComponentArray();

  234.         if (null == aAC || aAC.length != iNumAsset) return null;

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

  236.         jsonResponse.put ("AssetSet", org.drip.json.parser.Converter.Array (astrAssetID));

  237.         jsonResponse.put ("AssetReturnsMean", org.drip.json.parser.Converter.Array (adblAssetReturnsMean));

  238.         jsonResponse.put ("AssetReturnsCovariance", org.drip.json.parser.Converter.Array
  239.             (aadblAssetReturnsCovariance));

  240.         for (int i = 0; i < adblAssetReturnsMean.length; ++i) {
  241.             jsonResponse.put (astrAssetID[i] + "::LowerBound", adblAssetLowerBound[i]);

  242.             jsonResponse.put (astrAssetID[i] + "::UpperBound", adblAssetUpperBound[i]);
  243.         }

  244.         for (int i = 0; i < astrAssetID.length; ++i)
  245.             jsonResponse.put (aAC[i].id() + "::WEIGHT", aAC[i].amount());

  246.         jsonResponse.put ("PortfolioNotional", pf.notional());

  247.         try {
  248.             jsonResponse.put ("PortfolioDesignReturn", dblPortfolioDesignReturn);

  249.             jsonResponse.put ("PortfolioExpectedReturn", pf.expectedReturn (ausp));

  250.             jsonResponse.put ("PortfolioStandardDeviation", java.lang.Math.sqrt (pf.variance (ausp)));

  251.             return jsonResponse;
  252.         } catch (java.lang.Exception e) {
  253.             e.printStackTrace();
  254.         }

  255.         return null;
  256.     }
  257. }