Package com.opengamma.analytics.math.statistics.distribution

Examples of com.opengamma.analytics.math.statistics.distribution.NormalDistribution


    final PDEFullResults1D res = (PDEFullResults1D) solver.solve(PDE_DATA_BUNDLE);

    // PDEUtilityTools.printSurface("", res);

    final NormalDistribution dist = new NormalDistribution((RATE - ATM_VOL * ATM_VOL / 2) * T, ATM_VOL * Math.sqrt(T));
    double pdf;
    double s;
    for (int i = 0; i < X_NODES; i++) {
      s = res.getSpaceValue(i);
      if (s == 0.0) {
        pdf = 0.0;
      } else {
        final double x = Math.log(s / SPOT);
        pdf = dist.getPDF(x) / s;
      }
      //  System.out.println(res.getSpaceValue(i) + "\t" + pdf + "\t" + res.getFunctionValue(i));
      assertEquals("PDF test", pdf, res.getFunctionValue(i), 1e-4);
    }

View Full Code Here


    final double[] times = PREMIUM_LEG_SCHEDULE_CALCULATOR.convertTenorsToDoubles(calibrationTenors, valuationDate, ACT_365);

    final DoublesCurve spreadCurve = InterpolatedDoublesCurve.fromSorted(times, marketSpreads, INTERPOLATOR);

    final NormalDistribution normal = new NormalDistribution(0.0, 1.0);

    double presentValue = 0.0;
    double frontendProtection = 0.0;

    // ----------------------------------------------------------------------------------------------------------------------------------------

    // Create a copy of the underlying CDS for the purposes of calibration of the hazard rate term structure
    final LegacyVanillaCreditDefaultSwapDefinition underlyingCalibrationCDS = (LegacyVanillaCreditDefaultSwapDefinition) cdsSwaption.getUnderlyingCDS();

    // Build a hazard rate curve object based on the input market data
    //final HazardRateCurve calibratedHazardRateCurve = creditDefaultSwap.calibrateHazardRateCurve(valuationDate, underlyingCalibrationCDS, calibrationTenors, marketSpreads, yieldCurve);
    final HazardRateCurve calibratedHazardRateCurve = cdsCalibrator.isdaCalibrateHazardRateCurve(valuationDate, underlyingCalibrationCDS, calibrationTenors, marketSpreads, yieldCurve);

    // ----------------------------------------------------------------------------------------------------------------------------------------

    // Get the underlying CDS in the swaption contract (don't really need to do this if we fix the LegacyVanilla issue above)
    final CreditDefaultSwapDefinition underlyingCDS = cdsSwaption.getUnderlyingCDS();

    CreditDefaultSwapDefinition shortCDS = cdsSwaption.getUnderlyingCDS();

    shortCDS = shortCDS.withStartDate(cdsSwaption.getStartDate());
    shortCDS = shortCDS.withEffectiveDate(cdsSwaption.getStartDate().plusDays(1));
    shortCDS = shortCDS.withMaturityDate(cdsSwaption.getOptionExerciseDate());

    // Generate the cashflow schedule for the (forward) premium leg
    final ZonedDateTime[] underlyingCDSPremiumLegSchedule = PREMIUM_LEG_SCHEDULE_CALCULATOR.constructCreditDefaultSwapPremiumLegSchedule(underlyingCDS);

    final ZonedDateTime[] shortCDSPremiumLegSchedule = PREMIUM_LEG_SCHEDULE_CALCULATOR.constructCreditDefaultSwapPremiumLegSchedule(shortCDS);

    // Get the option expiry date
    final ZonedDateTime optionExpiryDate = cdsSwaption.getOptionExerciseDate();

    // Get the maturity date of the underlying CDS
    final ZonedDateTime cdsMaturityDate = cdsSwaption.getUnderlyingCDS().getMaturityDate();

    // Get the option strike
    final double optionStrike = cdsSwaption.getOptionStrike() / 10000.0;

    // Calculate the remaining time to option expiry (as a double)
    final double optionExpiryTime = TimeCalculator.getTimeBetween(valuationDate, cdsSwaption.getOptionExerciseDate());

    // ----------------------------------------------------------------------------------------------------------------------------------------

    // If we are not exactly at the option expiry time ...

    if (Double.doubleToLongBits(optionExpiryTime) != 0) {

      // ... the option still has some value (and the calculation shouldn't fall over)

      // Calculate the forward risky dV01 as seen at the valuation date for the period [optionExpiryDate, cdsMaturityDate]
      final double riskydV01 = calculateForwardRiskydV01(
          valuationDate,
          optionExpiryDate,
          cdsMaturityDate,
          cdsSwaption,
          underlyingCDSPremiumLegSchedule,
          yieldCurve,
          calibratedHazardRateCurve/*hazardRateCurve*/);

      // Calculate the forward spread as seen at the valuation date for the period [optionExpiryDate, cdsMaturityDate]
      final double forwardSpread = calculateForwardSpread(
          valuationDate,
          optionExpiryDate,
          cdsMaturityDate,
          cdsSwaption,
          underlyingCDSPremiumLegSchedule,
          shortCDSPremiumLegSchedule,
          spreadCurve,
          yieldCurve,
          calibratedHazardRateCurve/*hazardRateCurve*/) / 10000.0;

      // ----------------------------------------------------------------------------------------------------------------------------------------

      // Calculate the value of the CDS swaption

      final double d1 = (Math.log(forwardSpread / optionStrike) + 0.5 * sigma * sigma * optionExpiryTime) / (sigma * Math.sqrt(optionExpiryTime));
      final double d2 = (Math.log(forwardSpread / optionStrike) - 0.5 * sigma * sigma * optionExpiryTime) / (sigma * Math.sqrt(optionExpiryTime));

      if (cdsSwaption.isPayer()) {
        presentValue = riskydV01 * (forwardSpread * normal.getCDF(d1) - optionStrike * normal.getCDF(d2));
      } else {
        presentValue = riskydV01 * (optionStrike * normal.getCDF(-d2) - forwardSpread * normal.getCDF(-d1));
      }

      // Do we need to add the front end protection
      if (!cdsSwaption.isKnockOut()) {
        frontendProtection = calculateFrontendProtection(valuationDate, cdsSwaption, yieldCurve, calibratedHazardRateCurve/*hazardRateCurve*/);
 
View Full Code Here

  public void test() {
    final int n = 500000;
    final double eps = 1e-2;
    final double mu = -1.3;
    final double sigma = 0.4;
    final ProbabilityDistribution<Double> p1 = new NormalDistribution(mu, sigma, new MersenneTwister64(MersenneTwister.DEFAULT_SEED));
    final double[] x = new double[n];
    for (int i = 0; i < n; i++) {
      x[i] = p1.nextRandom();
    }
    final NormalDistribution p2 = (NormalDistribution) ESTIMATOR.evaluate(x);
    assertEquals(p2.getMean(), mu, eps);
    assertEquals(p2.getStandardDeviation(), sigma, eps);
  }
View Full Code Here

  @Test
  public void test() {
    final int n = 500000;
    final double mu = 4.5;
    final double sigma = 0.86;
    final ProbabilityDistribution<Double> p1 = new NormalDistribution(mu, sigma, new MersenneTwister64(MersenneTwister.DEFAULT_SEED));
    final double[] x = new double[n];
    for (int i = 0; i < n; i++) {
      x[i] = p1.nextRandom();
    }
    final NormalDistribution p2 = (NormalDistribution) CALCULATOR.evaluate(x);
    assertEquals(p2.getMean(), mu, 2.5e-2);
    assertEquals(p2.getStandardDeviation(), sigma, 2.5e-2);
  }
View Full Code Here

public class NormalRandomNumberGenerator implements RandomNumberGenerator {
  private final ProbabilityDistribution<Double> _normal;

  public NormalRandomNumberGenerator(final double mean, final double sigma) {
    ArgumentChecker.notNegativeOrZero(sigma, "standard deviation");
    _normal = new NormalDistribution(mean, sigma);
  }
View Full Code Here

  }

  public NormalRandomNumberGenerator(final double mean, final double sigma, final RandomEngine engine) {
    ArgumentChecker.notNegativeOrZero(sigma, "standard deviation");
    Validate.notNull(engine, "engine");
    _normal = new NormalDistribution(mean, sigma, engine);
  }
View Full Code Here

TOP

Related Classes of com.opengamma.analytics.math.statistics.distribution.NormalDistribution

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.