ReturnsConstrainedAllocationClient.java

  1. package org.drip.sample.service;

  2. import org.drip.json.parser.Converter;
  3. import org.drip.json.simple.JSONObject;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.service.json.KeyHoleSkeleton;

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

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

  51. /**
  52.  * ReturnsConstrainedAllocationClient demonstrates the Invocation and Examination of the JSON-based
  53.  *  Weight Normalized/Returns Constrained Portfolio Allocation Service Client.
  54.  *
  55.  * @author Lakshmi Krishnamurthy
  56.  */

  57. public class ReturnsConstrainedAllocationClient {

  58.     @SuppressWarnings ("unchecked") public static final void main (
  59.         final String[] astrArgs)
  60.         throws Exception
  61.     {
  62.         EnvManager.InitEnv ("");

  63.         String[] astrAssetName = new String[] {
  64.             "TOK",
  65.             "EWJ",
  66.             "HYG",
  67.             "LQD",
  68.             "EMD",
  69.             "GSG",
  70.             "BWX"
  71.         };

  72.         double[] adblAssetExpectedReturns = new double[] {
  73.             0.008355,
  74.             0.007207,
  75.             0.006279,
  76.             0.002466,
  77.             0.004472,
  78.             0.006821,
  79.             0.001570
  80.         };

  81.         double[][] aadblAssetReturnsCovariance = new double[][] {
  82.             {0.002733, 0.002083, 0.001593, 0.000488, 0.001172, 0.002312, 0.000710},
  83.             {0.002083, 0.002768, 0.001302, 0.000457, 0.001105, 0.001647, 0.000563},
  84.             {0.001593, 0.001302, 0.001463, 0.000639, 0.001050, 0.001110, 0.000519},
  85.             {0.000488, 0.000457, 0.000639, 0.000608, 0.000663, 0.000042, 0.000370},
  86.             {0.001172, 0.001105, 0.001050, 0.000663, 0.001389, 0.000825, 0.000661},
  87.             {0.002312, 0.001647, 0.001110, 0.000042, 0.000825, 0.005211, 0.000749},
  88.             {0.000710, 0.000563, 0.000519, 0.000370, 0.000661, 0.000749, 0.000703}
  89.         };

  90.         double[] adblAssetLowerBound = new double[] {
  91.             0.05,
  92.             0.05,
  93.             0.05,
  94.             0.10,
  95.             0.05,
  96.             0.05,
  97.             0.03
  98.         };

  99.         double[] adblAssetUpperBound = new double[] {
  100.             0.40,
  101.             0.40,
  102.             0.30,
  103.             0.60,
  104.             0.35,
  105.             0.15,
  106.             0.50
  107.         };

  108.         double[][] aadblBound = new double[adblAssetExpectedReturns.length][2];

  109.         for (int i = 0; i < adblAssetExpectedReturns.length; ++i) {
  110.             aadblBound[i][0] = adblAssetLowerBound[i];
  111.             aadblBound[i][1] = adblAssetUpperBound[i];
  112.         }

  113.         double dblPortfolioDesignReturn = 0.005262;

  114.         JSONObject jsonParameters = new JSONObject();

  115.         jsonParameters.put ("AssetSet", Converter.Array (astrAssetName));

  116.         jsonParameters.put ("AssetReturnsMean", Converter.Array (adblAssetExpectedReturns));

  117.         jsonParameters.put ("AssetReturnsCovariance", Converter.Array (aadblAssetReturnsCovariance));

  118.         jsonParameters.put ("PortfolioDesignReturn", dblPortfolioDesignReturn);

  119.         for (int i = 0; i < adblAssetExpectedReturns.length; ++i) {
  120.             jsonParameters.put (astrAssetName[i] + "::LowerBound", aadblBound[i][0]);

  121.             jsonParameters.put (astrAssetName[i] + "::UpperBound", aadblBound[i][1]);
  122.         }

  123.         JSONObject jsonRequest = new JSONObject();

  124.         jsonRequest.put ("API", "PORTFOLIOALLOCATION::RETURNSCONSTRAINEDMEANVARIANCE");

  125.         jsonRequest.put ("Parameters", jsonParameters);

  126.         System.out.println ("\n\t|---------------- JSON REQUEST -----------------|\n");

  127.         System.out.println (jsonRequest.toJSONString());

  128.         System.out.println ("\n\t|---------------- JSON RESPONSE ----------------|\n");

  129.         System.out.println (KeyHoleSkeleton.Thunker (jsonRequest.toJSONString()));
  130.     }
  131. }