ComputeClient.java

  1. package org.drip.service.engine;

  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>ComputeClient</i> contains the Functionality behind the DROP API Compute Service Client.
  79.  *
  80.  * <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  84.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/README.md">Environment, Product/Definition Containers, and Scenario/State Manipulation APIs</a></li>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/engine/README.md">Compute Engine Request-Response Thunker</a></li>
  86.  *  </ul>
  87.  * <br><br>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class ComputeClient {
  92.     private int _iComputeServerPort = -1;
  93.     private java.lang.String _strComputeServerHost = "";
  94.     private java.net.Socket _socketComputeServer = null;

  95.     /**
  96.      * Construct Standard LocalHost-based Instance of the ComputeClient
  97.      *
  98.      * @return The Standard LocalHost-based Instance of the ComputeClient
  99.      */

  100.     public static final ComputeClient Standard()
  101.     {
  102.         try {
  103.             ComputeClient cc = new ComputeClient ("127.0.0.1",
  104.                 org.drip.service.engine.ComputeServer.DRIP_COMPUTE_ENGINE_PORT);

  105.             return cc.initialize() ? cc : null;
  106.         } catch (java.lang.Exception e) {
  107.             e.printStackTrace();
  108.         }

  109.         return null;
  110.     }

  111.     /**
  112.      * ComputeClient Constructor
  113.      *
  114.      * @param strComputeServerHost The Compute Server Host
  115.      * @param iComputeServerPort The Compute Server Port
  116.      *
  117.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  118.      */

  119.     public ComputeClient (
  120.         final java.lang.String strComputeServerHost,
  121.         final int iComputeServerPort)
  122.         throws java.lang.Exception
  123.     {
  124.         if (null == (_strComputeServerHost = strComputeServerHost) || 0 >= (_iComputeServerPort =
  125.             iComputeServerPort))
  126.             throw new java.lang.Exception ("ComputeClient Constructor => Invalid Inputs!");
  127.     }

  128.     /**
  129.      * Retrieve the Compute Server Host
  130.      *
  131.      * @return The Compute Server Host
  132.      */

  133.     public java.lang.String computeServerHost()
  134.     {
  135.         return _strComputeServerHost;
  136.     }

  137.     /**
  138.      * Retrieve the Compute Server Port
  139.      *
  140.      * @return The Compute Server Port
  141.      */

  142.     public int computeServerPort()
  143.     {
  144.         return _iComputeServerPort;
  145.     }

  146.     /**
  147.      * Establish a Connection to the Compute Server Engine
  148.      *
  149.      * @return TRUE - Connection to the Compute Server Engine Established
  150.      */

  151.     public boolean initialize()
  152.     {
  153.         try {
  154.             _socketComputeServer = new java.net.Socket (_strComputeServerHost, _iComputeServerPort);

  155.             return true;
  156.         } catch (java.lang.Exception e) {
  157.             e.printStackTrace();
  158.         }

  159.         return false;
  160.     }

  161.     /**
  162.      * Invoke a Request on the Compute Server and Retrieve the Response
  163.      *
  164.      * @param jsonRequest The Input JSON Request
  165.      *
  166.      * @return The Processed JSON Response
  167.      */

  168.     public org.drip.json.simple.JSONObject invoke (
  169.         final org.drip.json.simple.JSONObject jsonRequest)
  170.     {
  171.         if (!org.drip.service.engine.RequestResponseDecorator.AffixRequestHeaders (jsonRequest)) return null;

  172.         try {
  173.             java.io.PrintWriter pw = new java.io.PrintWriter (_socketComputeServer.getOutputStream(), true);

  174.             pw.write (jsonRequest.toJSONString() + "\n");

  175.             pw.flush();

  176.             java.lang.Object objResponse = org.drip.json.simple.JSONValue.parse (new java.io.BufferedReader
  177.                 (new java.io.InputStreamReader (_socketComputeServer.getInputStream())).readLine());

  178.             return null == objResponse || !(objResponse instanceof org.drip.json.simple.JSONObject) ? null :
  179.                 (org.drip.json.simple.JSONObject) objResponse;
  180.         } catch (java.lang.Exception e) {
  181.             e.printStackTrace();
  182.         }

  183.         return null;
  184.     }

  185.     @SuppressWarnings ("unchecked") public static final void main (
  186.         final java.lang.String[] astrArgs)
  187.         throws java.lang.Exception
  188.     {
  189.         org.drip.service.env.EnvManager.InitEnv ("");

  190.         String[] astrAssetName = new String[] {
  191.             "TOK",
  192.             "EWJ",
  193.             "HYG",
  194.             "LQD",
  195.             "EMD",
  196.             "GSG",
  197.             "BWX"
  198.         };

  199.         double[] adblAssetExpectedReturns = new double[] {
  200.             0.008355,
  201.             0.007207,
  202.             0.006279,
  203.             0.002466,
  204.             0.004472,
  205.             0.006821,
  206.             0.001570
  207.         };

  208.         double[][] aadblAssetReturnsCovariance = new double[][] {
  209.             {0.002733, 0.002083, 0.001593, 0.000488, 0.001172, 0.002312, 0.000710},
  210.             {0.002083, 0.002768, 0.001302, 0.000457, 0.001105, 0.001647, 0.000563},
  211.             {0.001593, 0.001302, 0.001463, 0.000639, 0.001050, 0.001110, 0.000519},
  212.             {0.000488, 0.000457, 0.000639, 0.000608, 0.000663, 0.000042, 0.000370},
  213.             {0.001172, 0.001105, 0.001050, 0.000663, 0.001389, 0.000825, 0.000661},
  214.             {0.002312, 0.001647, 0.001110, 0.000042, 0.000825, 0.005211, 0.000749},
  215.             {0.000710, 0.000563, 0.000519, 0.000370, 0.000661, 0.000749, 0.000703}
  216.         };

  217.         double[] adblAssetLowerBound = new double[] {
  218.             0.05,
  219.             0.05,
  220.             0.05,
  221.             0.10,
  222.             0.05,
  223.             0.05,
  224.             0.03
  225.         };

  226.         double[] adblAssetUpperBound = new double[] {
  227.             0.40,
  228.             0.40,
  229.             0.30,
  230.             0.60,
  231.             0.35,
  232.             0.15,
  233.             0.50
  234.         };

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

  236.         for (int i = 0; i < adblAssetExpectedReturns.length; ++i) {
  237.             aadblBound[i][0] = adblAssetLowerBound[i];
  238.             aadblBound[i][1] = adblAssetUpperBound[i];
  239.         }

  240.         org.drip.json.simple.JSONObject jsonParameters = new org.drip.json.simple.JSONObject();

  241.         jsonParameters.put ("AssetSet", org.drip.json.parser.Converter.Array (astrAssetName));

  242.         jsonParameters.put ("AssetExpectedReturns", org.drip.json.parser.Converter.Array
  243.             (adblAssetExpectedReturns));

  244.         jsonParameters.put ("AssetReturnsCovariance", org.drip.json.parser.Converter.Array
  245.             (aadblAssetReturnsCovariance));

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

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

  250.         org.drip.json.simple.JSONObject jsonRequest = new org.drip.json.simple.JSONObject();

  251.         jsonRequest.put ("API", "PORTFOLIOALLOCATION::BUDGETCONSTRAINEDMEANVARIANCE");

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

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

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

  255.         ComputeClient cc = Standard();

  256.         org.drip.json.simple.JSONObject jsonResponse = cc.invoke (jsonRequest);

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

  258.         System.out.println (jsonResponse.toJSONString());
  259.     }
  260. }