Package com.opengamma.analytics.financial.montecarlo

Examples of com.opengamma.analytics.financial.montecarlo.DecisionSchedule


   */
  public MultipleCurrencyAmount presentValue(final InstrumentDerivative instrument, final Currency ccy, final HullWhiteOneFactorProviderInterface hwData) {
    // TODO: remove currency and dsc curve name (should be available from the instrument)
    final MulticurveProviderInterface multicurves = hwData.getMulticurveProvider();
    final HullWhiteOneFactorPiecewiseConstantParameters parameters = hwData.getHullWhiteParameters();
    final DecisionSchedule decision = instrument.accept(DC, multicurves);
    final double[] decisionTime = decision.getDecisionTime();
    final double[][] impactTime = decision.getImpactTime();
    final int nbJump = decisionTime.length;
    final double numeraireTime = decisionTime[nbJump - 1];
    final double pDN = multicurves.getDiscountFactor(ccy, numeraireTime);
    // Discount factor to numeraire date for rebasing.
    final double[][] pDI = new double[nbJump][];
    // Initial discount factors to each impact date.
    for (int loopjump = 0; loopjump < nbJump; loopjump++) {
      pDI[loopjump] = new double[impactTime[loopjump].length];
      for (int i = 0; i < impactTime[loopjump].length; i++) {
        pDI[loopjump][i] = multicurves.getDiscountFactor(ccy, impactTime[loopjump][i]) / pDN;
      }
    }
    final double[] gamma = new double[nbJump];
    final double[][] cov = new double[nbJump][nbJump];
    for (int loopjump = 0; loopjump < nbJump; loopjump++) {
      gamma[loopjump] = MODEL.beta(parameters, 0.0, decisionTime[loopjump]);
      gamma[loopjump] = gamma[loopjump] * gamma[loopjump];
      cov[loopjump][loopjump] = gamma[loopjump];
      for (int j = loopjump + 1; j < nbJump; j++) {
        cov[j][loopjump] = gamma[loopjump];
        cov[loopjump][j] = gamma[loopjump];
      }
    }
    final double[][] h = MODEL.volatilityMaturityPart(parameters, numeraireTime, impactTime); // jump/cf
    final double[][] h2 = new double[nbJump][];
    for (int i = 0; i < nbJump; i++) {
      h2[i] = new double[h[i].length];
      for (int j = 0; j < h[i].length; j++) {
        h2[i][j] = h[i][j] * h[i][j] / 2;
      }
    }
    // To remove the 0 (fixed coupons)
    int nbZero = 0;
    while (cov[nbZero][nbZero] < 1.0E-12) {
      nbZero++;
    }
    final double[][] cov2 = new double[nbJump - nbZero][nbJump - nbZero];
    for (int loopjump = 0; loopjump < nbJump - nbZero; loopjump++) {
      for (int loopjump2 = 0; loopjump2 < nbJump - nbZero; loopjump2++) {
        cov2[loopjump][loopjump2] = cov[loopjump + nbZero][loopjump2 + nbZero];
      }
    }
    final CholeskyDecompositionCommons cd = new CholeskyDecompositionCommons();
    final CholeskyDecompositionResult cdr2 = cd.evaluate(new DoubleMatrix2D(cov2));
    final double[][] covCD2 = cdr2.getL().toArray();
    final double[][] covCD = new double[nbJump][nbJump];
    for (int loopjump = 0; loopjump < nbJump - nbZero; loopjump++) {
      for (int loopjump2 = 0; loopjump2 < nbJump - nbZero; loopjump2++) {
        covCD[loopjump + nbZero][loopjump2 + nbZero] = covCD2[loopjump][loopjump2];
      }
    }
    final int nbBlock = (int) Math.round(Math.ceil(getNbPath() / ((double) BLOCK_SIZE)));
    final int[] nbPath2 = new int[nbBlock];
    for (int i = 0; i < nbBlock - 1; i++) {
      nbPath2[i] = BLOCK_SIZE;
    }
    nbPath2[nbBlock - 1] = getNbPath() - (nbBlock - 1) * BLOCK_SIZE;
    final double[][] impactAmount = decision.getImpactAmount();
    double pv = 0;
    for (int loopblock = 0; loopblock < nbBlock; loopblock++) {
      final double[][] x = getNormalArray(nbJump, nbPath2[loopblock]);
      final double[][] y = new double[nbJump][nbPath2[loopblock]]; // jump/path
      for (int looppath = 0; looppath < nbPath2[loopblock]; looppath++) {
View Full Code Here


  public MultipleCurrencyAmount presentValue(final InstrumentDerivative instrument, final Currency ccy, final LiborMarketModelDisplacedDiffusionProvider lmmData) {
    final MulticurveProviderInterface multicurves = lmmData.getMulticurveProvider();
    final LiborMarketModelDisplacedDiffusionParameters parameters = lmmData.getLMMParameters();
    // The numeraire is the last time in the LMM description.
    final DecisionSchedule decision = instrument.accept(DC, multicurves);
    final int[][] impactIndex = index(decision.getImpactTime(), parameters);

    final int nbPeriodLMM = parameters.getNbPeriod();
    final double[] initL = new double[nbPeriodLMM];
    final double[] deltaLMM = parameters.getAccrualFactor();
    final double[] dfL = new double[nbPeriodLMM + 1];
    for (int loopper = 0; loopper < nbPeriodLMM + 1; loopper++) {
      dfL[loopper] = multicurves.getDiscountFactor(ccy, parameters.getIborTime()[loopper]);
    }
    for (int loopper = 0; loopper < nbPeriodLMM; loopper++) {
      initL[loopper] = (dfL[loopper] / dfL[loopper + 1] - 1.0) / deltaLMM[loopper];
    }

    final int nbBlock = (int) Math.round(Math.ceil(getNbPath() / ((double) BLOCK_SIZE)));
    final int[] nbPath2 = new int[nbBlock];
    for (int i = 0; i < nbBlock - 1; i++) {
      nbPath2[i] = BLOCK_SIZE;
    }
    nbPath2[nbBlock - 1] = getNbPath() - (nbBlock - 1) * BLOCK_SIZE;

    double price = 0.0;
    for (int loopblock = 0; loopblock < nbBlock; loopblock++) {
      final double[][] initLPath = new double[nbPeriodLMM][nbPath2[loopblock]];
      for (int loopper = 0; loopper < nbPeriodLMM; loopper++) {
        for (int looppath = 0; looppath < nbPath2[loopblock]; looppath++) {
          initLPath[loopper][looppath] = initL[loopper];
        }
      }
      final double[][][] pathIbor = pathgeneratorlibor(decision.getDecisionTime(), initLPath, parameters);
      price += instrument.accept(MCC, new MonteCarloIborRateDataBundle(pathIbor, deltaLMM, decision.getImpactAmount(), impactIndex));
    }
    price *= multicurves.getDiscountFactor(ccy, parameters.getIborTime()[parameters.getIborTime().length - 1]) / getNbPath();
    return MultipleCurrencyAmount.of(ccy, price);
  }
View Full Code Here

   * @return The present value.
   */
  public MultipleCurrencyAmount presentValue(final InstrumentDerivative instrument, final Currency ccy, final G2ppProviderInterface g2Data) {
    MulticurveProviderInterface multicurves = g2Data.getMulticurveProvider();
    G2ppPiecewiseConstantParameters parameters = g2Data.getG2ppParameters();
    final DecisionSchedule decision = instrument.accept(DC, multicurves);
    final double[] decisionTime = decision.getDecisionTime();
    final double[][] impactTime = decision.getImpactTime();
    final int nbJump = decisionTime.length;
    final double numeraireTime = decisionTime[nbJump - 1];
    final double pDN = multicurves.getDiscountFactor(ccy, numeraireTime);
    // Discount factor to numeraire date for rebasing.
    final double[][] pDI = new double[nbJump][];
    // Initial discount factors to each impact date.
    for (int loopjump = 0; loopjump < nbJump; loopjump++) {
      pDI[loopjump] = new double[impactTime[loopjump].length];
      for (int i = 0; i < impactTime[loopjump].length; i++) {
        pDI[loopjump][i] = multicurves.getDiscountFactor(ccy, impactTime[loopjump][i]) / pDN;
      }
    }
    final double rhog2pp = parameters.getCorrelation();
    final double[][][] h = MODEL.volatilityMaturityPart(parameters, numeraireTime, impactTime); // factor/jump/cf
    final double[][][] gamma = new double[nbJump][2][2]; // jump/factor/factor
    final double[][] cov = new double[2 * nbJump][2 * nbJump]; // factor 0 - factor 1
    for (int loopjump = 0; loopjump < nbJump; loopjump++) {
      gamma[loopjump] = MODEL.gamma(parameters, 0.0, decisionTime[loopjump]);
      for (int j = loopjump; j < nbJump; j++) {
        cov[j][loopjump] = gamma[loopjump][0][0];
        cov[loopjump][j] = gamma[loopjump][0][0];
        cov[nbJump + j][nbJump + loopjump] = gamma[loopjump][1][1];
        cov[nbJump + loopjump][nbJump + j] = gamma[loopjump][1][1];
        cov[j][nbJump + loopjump] = rhog2pp * gamma[loopjump][0][1];
        cov[loopjump][nbJump + j] = rhog2pp * gamma[loopjump][0][1];
        cov[nbJump + j][loopjump] = rhog2pp * gamma[loopjump][0][1];
        cov[nbJump + loopjump][j] = rhog2pp * gamma[loopjump][0][1];
      }
    }
    final double[][][] alpha = new double[2][nbJump][]; // factor/jump/cf
    final double[][] tau2 = new double[nbJump][];
    for (int loopjump = 0; loopjump < nbJump; loopjump++) {
      tau2[loopjump] = new double[impactTime[loopjump].length];
      alpha[0][loopjump] = new double[impactTime[loopjump].length];
      alpha[1][loopjump] = new double[impactTime[loopjump].length];
      for (int loopcf = 0; loopcf < impactTime[loopjump].length; loopcf++) {
        alpha[0][loopjump][loopcf] = Math.sqrt(gamma[loopjump][0][0]) * h[0][loopjump][loopcf];
        alpha[1][loopjump][loopcf] = Math.sqrt(gamma[loopjump][1][1]) * h[1][loopjump][loopcf];
        tau2[loopjump][loopcf] = alpha[0][loopjump][loopcf] * alpha[0][loopjump][loopcf] + alpha[1][loopjump][loopcf] * alpha[1][loopjump][loopcf] + 2 * rhog2pp * gamma[loopjump][0][1]
            * h[0][loopjump][loopcf] * h[1][loopjump][loopcf];
      }
    }
    final CholeskyDecompositionCommons cd = new CholeskyDecompositionCommons();
    final CholeskyDecompositionResult cdr = cd.evaluate(new DoubleMatrix2D(cov));
    final double[][] covCD = cdr.getL().getData();
    final int nbBlock = (int) Math.round(Math.ceil(getNbPath() / ((double) BLOCK_SIZE)));
    final int[] nbPath2 = new int[nbBlock];
    for (int i = 0; i < nbBlock - 1; i++) {
      nbPath2[i] = BLOCK_SIZE;
    }
    nbPath2[nbBlock - 1] = getNbPath() - (nbBlock - 1) * BLOCK_SIZE;
    final double[][] impactAmount = decision.getImpactAmount();
    double pv = 0;
    for (int loopblock = 0; loopblock < nbBlock; loopblock++) {
      final double[][] x = getNormalArray(2 * nbJump, nbPath2[loopblock]);
      final double[][] y = new double[2 * nbJump][nbPath2[loopblock]]; // jump/path
      for (int looppath = 0; looppath < nbPath2[loopblock]; looppath++) {
View Full Code Here

    final double[][] impactAmount = new double[1][cfe.getNumberOfPayments()];
    for (int loopcf = 0; loopcf < cfe.getNumberOfPayments(); loopcf++) {
      impactTime[0][loopcf] = cfe.getNthPayment(loopcf).getPaymentTime();
      impactAmount[0][loopcf] = cfe.getNthPayment(loopcf).getAmount();
    }
    final DecisionSchedule decision = new DecisionSchedule(decisionTime, impactTime, impactAmount);
    return decision;
  }
View Full Code Here

    // Ibor leg
    for (int loopcf = 0; loopcf < nbCfeIbor; loopcf++) {
      impactTime[0][nbCpnFixed + loopcf] = cfeIbor.getNthPayment(loopcf).getPaymentTime();
      impactAmount[0][nbCpnFixed + loopcf] = cfeIbor.getNthPayment(loopcf).getAmount();
    }
    final DecisionSchedule decision = new DecisionSchedule(decisionTime, impactTime, impactAmount);
    return decision;
  }
View Full Code Here

    final double[][] impactAmount = new double[1][];
    double forward = multicurves.getForwardRate(payment.getIndex(), payment.getFixingPeriodStartTime(), payment.getFixingPeriodEndTime(), payment.getFixingAccrualFactor());
    final double beta = (1.0 + payment.getFixingAccrualFactor() * forward) * multicurves.getDiscountFactor(payment.getCurrency(), payment.getFixingPeriodEndTime())
        / multicurves.getDiscountFactor(payment.getCurrency(), payment.getFixingPeriodStartTime());
    impactAmount[0] = new double[] {beta, -1.0, 1.0};
    final DecisionSchedule decision = new DecisionSchedule(decisionTime, impactTime, impactAmount);
    return decision;
  }
View Full Code Here

      for (int loopcf = 0; loopcf < cfe.getNumberOfPayments(); loopcf++) {
        impactTime[loopcpn][loopcf] = cfe.getNthPayment(loopcf).getPaymentTime();
        impactAmount[loopcpn][loopcf] = cfe.getNthPayment(loopcf).getAmount();
      }
    }
    final DecisionSchedule decision = new DecisionSchedule(decisionTime, impactTime, impactAmount);
    return decision;
  }
View Full Code Here

  private static final DecisionScheduleCalculator DC = DecisionScheduleCalculator.getInstance();

  @Test
  public void swaption() {
    final Annuity<Coupon> leg2 = SWAP_PAYER.getSecondLeg();
    final DecisionSchedule swaptionLongSchedule = SWAPTION_PAYER_LONG.accept(DC, CURVES);
    assertEquals("Decision schedule", 1, swaptionLongSchedule.getDecisionTime().length);
    assertEquals("Decision schedule", leg2.getNumberOfPayments() + 1, swaptionLongSchedule.getImpactTime()[0].length);
    assertEquals("Decision schedule", SWAPTION_PAYER_LONG.getTimeToExpiry(), swaptionLongSchedule.getDecisionTime()[0], 1E-10);
    final DecisionSchedule swaptionShortSchedule = SWAPTION_PAYER_SHORT.accept(DC, CURVES);
    ArrayAsserts.assertArrayEquals(swaptionLongSchedule.getDecisionTime(), swaptionShortSchedule.getDecisionTime(), 1.0E-8);
    ArrayAsserts.assertArrayEquals(swaptionLongSchedule.getImpactTime()[0], swaptionShortSchedule.getImpactTime()[0], 1.0E-8);
    ArrayAsserts.assertArrayEquals(swaptionLongSchedule.getImpactAmount()[0], swaptionShortSchedule.getImpactAmount()[0], 1.0E-8);
  }
View Full Code Here

TOP

Related Classes of com.opengamma.analytics.financial.montecarlo.DecisionSchedule

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.