PhaseTrackerComparison.java

  1. package org.drip.sample.numerical;

  2. import java.util.Map;

  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.numerical.fourier.PhaseAdjuster;
  5. import org.drip.param.pricer.HestonOptionPricerParams;
  6. import org.drip.pricer.option.HestonStochasticVolatilityAlgorithm;

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

  10. /*!
  11.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2014 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.  * PhaseTrackerComparison demonstrates the Log + Power Complex Number Phase Correction Functionality
  56.  *  implemented by three different ways for the calculation of the Inverse Fourier Transforms. The sample
  57.  *  problem chosen is the stochastic volatility evolution using the Heston Method.
  58.  *
  59.  * @author Lakshmi Krishnamurthy
  60.  */

  61. public class PhaseTrackerComparison {

  62.     private static final Map<Double, Double> PhaseSet (
  63.         final double dblRho,
  64.         final double dblKappa,
  65.         final double dblSigma,
  66.         final double dblTheta,
  67.         final double dblLambda,
  68.         final double dblStrike,
  69.         final double dbTimeToExpiry,
  70.         final double dblRiskFreeRate,
  71.         final double dblSpot,
  72.         final double dblSpotVolatility,
  73.         final int iPhaseTrackerType)
  74.         throws Exception
  75.     {
  76.         HestonOptionPricerParams fphp = new HestonOptionPricerParams (
  77.             HestonStochasticVolatilityAlgorithm.PAYOFF_TRANSFORM_SCHEME_HESTON_1993,
  78.             dblRho,
  79.             dblKappa,
  80.             dblSigma,
  81.             dblTheta,
  82.             dblLambda,
  83.             iPhaseTrackerType
  84.         );

  85.         HestonStochasticVolatilityAlgorithm hsva = new HestonStochasticVolatilityAlgorithm (fphp);

  86.         return hsva.recordPhase (
  87.             dblStrike,
  88.             dbTimeToExpiry,
  89.             dblRiskFreeRate,
  90.             dblSpot,
  91.             dblSpotVolatility,
  92.             true
  93.         );
  94.     }

  95.     public static final void main (
  96.         final String[] astrArgs)
  97.         throws Exception
  98.     {
  99.         double dblRho = 0.3;
  100.         double dblKappa = 1.;
  101.         double dblSigma = 0.5;
  102.         double dblTheta = 0.2;
  103.         double dblLambda = 0.;

  104.         double dblStrike = 1.;
  105.         double dbTimeToExpiry = 0.5;
  106.         double dblRiskFreeRate = 0.0;
  107.         double dblSpot = 1.;
  108.         double dblSpotVolatility = 0.1;

  109.         Map<Double, Double> mapFreqPhaseNoAdjust = PhaseSet (
  110.             dblRho,
  111.             dblKappa,
  112.             dblSigma,
  113.             dblTheta,
  114.             dblLambda,
  115.             dblStrike,
  116.             dbTimeToExpiry,
  117.             dblRiskFreeRate,
  118.             dblSpot,
  119.             dblSpotVolatility,
  120.             PhaseAdjuster.MULTI_VALUE_BRANCH_PHASE_TRACKER_NONE
  121.         );

  122.         Map<Double, Double> mapFreqPhaseRotationCount = PhaseSet (
  123.             dblRho,
  124.             dblKappa,
  125.             dblSigma,
  126.             dblTheta,
  127.             dblLambda,
  128.             dblStrike,
  129.             dbTimeToExpiry,
  130.             dblRiskFreeRate,
  131.             dblSpot,
  132.             dblSpotVolatility,
  133.             PhaseAdjuster.MULTI_VALUE_BRANCH_PHASE_TRACKER_ROTATION_COUNT
  134.         );

  135.         Map<Double, Double> mapFreqPhaseKahlJackel = PhaseSet (
  136.             dblRho,
  137.             dblKappa,
  138.             dblSigma,
  139.             dblTheta,
  140.             dblLambda,
  141.             dblStrike,
  142.             dbTimeToExpiry,
  143.             dblRiskFreeRate,
  144.             dblSpot,
  145.             dblSpotVolatility,
  146.             PhaseAdjuster.MULTI_VALUE_BRANCH_POWER_PHASE_TRACKER_KAHL_JACKEL
  147.         );

  148.         System.out.println ("\t|--------------------------------------------|");

  149.         System.out.println ("\t|  u =>  NO CORECT | ROT COUNT | KAHL JACKEL |");

  150.         System.out.println ("\t|--------------------------------------------|");

  151.         for (Map.Entry<Double, Double> me : mapFreqPhaseKahlJackel.entrySet()) {
  152.             Double dblKey = me.getKey();

  153.             System.out.println ("\t|" +
  154.                 FormatUtil.FormatDouble (dblKey, 2, 0, 1.) + " =>  " +
  155.                 FormatUtil.FormatDouble (mapFreqPhaseNoAdjust.get (dblKey), 1, 6, 1.)  + " | " +
  156.                 FormatUtil.FormatDouble (mapFreqPhaseRotationCount.get (dblKey), 1, 6, 1.)  + " | " +
  157.                 FormatUtil.FormatDouble (mapFreqPhaseKahlJackel.get (dblKey), 1, 6, 1.) + "   |"
  158.             );
  159.         }

  160.         System.out.println ("\t|--------------------------------------------|");
  161.     }
  162. }