IntegrandQuadrature.java

  1. package org.drip.sample.numerical;

  2. import org.drip.function.definition.R1ToR1;
  3. import org.drip.function.r1tor1.*;
  4. import org.drip.numerical.common.*;
  5. import org.drip.numerical.integration.R1ToR1Integrator;

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

  54. /**
  55.  * IntegrandQuadrature shows samples for the following routines for integrating the objective function:
  56.  *  - Mid-Point Scheme
  57.  *  - Trapezoidal Scheme
  58.  *  - Simpson/Simpson38 schemes
  59.  *  - Boole Scheme
  60.  *
  61.  * @author Lakshmi Krishnamurthy
  62.  */

  63. public class IntegrandQuadrature {

  64.     /*
  65.      * Compute the Integrand Quadrature for the specified Univariate Function using the various methods.
  66.      *
  67.      *  WARNING: Insufficient Error Checking, so use caution
  68.      */

  69.     private static void ComputeQuadrature (
  70.         final R1ToR1 au,
  71.         final double dblActual,
  72.         final double dblStart,
  73.         final double dblEnd)
  74.         throws Exception
  75.     {
  76.         int iRightDecimal = 8;

  77.         System.out.println ("\t\tActual      : " +
  78.             FormatUtil.FormatDouble (dblActual, 1, iRightDecimal, 1.)
  79.         );

  80.         System.out.println ("\t\tLinear      : " +
  81.             FormatUtil.FormatDouble (
  82.                 R1ToR1Integrator.LinearQuadrature (
  83.                     au,
  84.                     dblStart,
  85.                     dblEnd
  86.                 ),
  87.                 1,
  88.                 iRightDecimal,
  89.                 1.
  90.             )
  91.         );

  92.         System.out.println ("\t\tMidPoint     : " +
  93.             FormatUtil.FormatDouble (
  94.                 R1ToR1Integrator.MidPoint (
  95.                     au,
  96.                     dblStart,
  97.                     dblEnd
  98.                 ),
  99.                 1,
  100.                 iRightDecimal,
  101.                 1.
  102.             )
  103.         );

  104.         System.out.println ("\t\tTrapezoidal  : " +
  105.             FormatUtil.FormatDouble (
  106.                 R1ToR1Integrator.Trapezoidal (
  107.                     au,
  108.                     dblStart,
  109.                     dblEnd
  110.                 ),
  111.                 1,
  112.                 iRightDecimal,
  113.                 1.
  114.             )
  115.         );

  116.         System.out.println ("\t\tSimpson      : " +
  117.             FormatUtil.FormatDouble (
  118.                 R1ToR1Integrator.Simpson (
  119.                     au,
  120.                     dblStart,
  121.                     dblEnd
  122.                 ),
  123.                 1,
  124.                 iRightDecimal,
  125.                 1.
  126.             )
  127.         );

  128.         System.out.println ("\t\tSimpson 38   : " +
  129.             FormatUtil.FormatDouble (
  130.                 R1ToR1Integrator.Simpson38 (
  131.                     au,
  132.                     dblStart,
  133.                     dblEnd
  134.                 ),
  135.                 1,
  136.                 iRightDecimal,
  137.                 1.
  138.             )
  139.         );

  140.         System.out.println ("\t\tBoole        : " +
  141.             FormatUtil.FormatDouble (
  142.                 R1ToR1Integrator.Boole (
  143.                     au,
  144.                     dblStart,
  145.                     dblEnd
  146.                 ),
  147.                 1,
  148.                 iRightDecimal,
  149.                 1.
  150.             )
  151.         );
  152.     }

  153.     /*
  154.      * Compute the Integrand Quadrature for the various Univariate Functions using the different methods.
  155.      *
  156.      *  WARNING: Insufficient Error Checking, so use caution
  157.      */

  158.     private static void IntegrandQuadratureSample()
  159.         throws Exception
  160.     {
  161.         double dblStart = 0.;
  162.         double dblEnd = 1.;

  163.         R1ToR1 auExp = new ExponentialTension (
  164.             Math.E,
  165.             1.
  166.         );

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

  168.         ComputeQuadrature (
  169.             auExp,
  170.             auExp.evaluate (dblEnd) - auExp.evaluate (dblStart),
  171.             dblStart,
  172.             dblEnd
  173.         );

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

  175.         R1ToR1 au1 = new R1ToR1 (null) {
  176.             @Override public double evaluate (
  177.                 final double dblVariate)
  178.                 throws Exception
  179.             {
  180.                 return Math.cos (dblVariate) - dblVariate * dblVariate * dblVariate;
  181.             }
  182.         };

  183.         ComputeQuadrature (
  184.             au1,
  185.             Math.sin (dblEnd) - Math.sin (dblStart) - 0.25 * (dblEnd * dblEnd * dblEnd * dblEnd - dblStart * dblStart * dblStart * dblStart),
  186.             dblStart,
  187.             dblEnd
  188.         );

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

  190.         R1ToR1 au2 = new R1ToR1 (null) {
  191.             @Override public double evaluate (
  192.                 final double dblVariate)
  193.                 throws Exception
  194.             {
  195.                 return dblVariate * dblVariate * dblVariate - 3. * dblVariate * dblVariate + 2. * dblVariate;
  196.             }
  197.         };

  198.         ComputeQuadrature (
  199.             au2,
  200.             0.25 * (dblEnd * dblEnd * dblEnd * dblEnd - dblStart * dblStart * dblStart * dblStart) -
  201.                 (dblEnd * dblEnd * dblEnd - dblStart * dblStart * dblStart) +
  202.                 (dblEnd * dblEnd - dblStart * dblStart),
  203.             dblStart,
  204.             dblEnd
  205.         );

  206.         System.out.println ("\n\t-------------------------------------\n");
  207.     }

  208.     public static void main (
  209.         final String astrArgs[])
  210.         throws Exception
  211.     {
  212.         IntegrandQuadratureSample();
  213.     }
  214. }