Package com.opengamma.analytics.math

Examples of com.opengamma.analytics.math.MathException


        integralPart = discountFactorTp * integrator.integrate(integrant, strike, strike + getIntegrationInterval());
      } else {
        integralPart = discountFactorTp * integrator.integrate(integrant, 0.0, strike);
      }
    } catch (final Exception e) {
      throw new MathException(e);
    }
    final double priceCMS = (strikePart + integralPart) * cmsCapFloor.getNotional() * cmsCapFloor.getPaymentYearFraction();
    return MultipleCurrencyAmount.of(cmsCapFloor.getCurrency(), priceCMS);
  }
View Full Code Here


    double pv = 0.0;
    try {
      pv = 1.0 / (2.0 * Math.PI * Math.sqrt(1 - rhobar * rhobar))
          * integrator2D.integrate(integrant, new Double[] {-INTEGRATION_LIMIT, -INTEGRATION_LIMIT }, new Double[] {INTEGRATION_LIMIT, INTEGRATION_LIMIT });
    } catch (final Exception e) {
      throw new MathException(e);
    }
    return CurrencyAmount.of(cmsSpread.getCurrency(), dftp * pv * cmsSpread.getNotional() * cmsSpread.getPaymentYearFraction());
  }
View Full Code Here

      double c = Math.log(strike / forward);
      double a = timeToExpiry / 2;
      double b = (-c + mu - theta * theta * timeToExpiry / 2) / theta;
      double arg = b * b - 4 * a * c;
      if (arg < 0) {
        throw new MathException("cannot solve for sigma");
      }
      double root = Math.sqrt(arg);
      double volGuess = isCall ? (-b - root) / 2 / a : (-b + root) / 2 / a;
      return volGuess;
    }
    if (!isCall && p >= strike) {
      double c = Math.log(strike / forward);
      double a = timeToExpiry / 2;
      double b = (c + mu - theta * theta * timeToExpiry / 2) / theta;
      double arg = b * b - 4 * a * c;
      if (arg < 0) {
        throw new MathException("cannot solve for sigma");
      }
      double root = Math.sqrt(arg);
      double volGuess = isCall ? (-b - root) / 2 / a : (-b + root) / 2 / a;
      return volGuess;
    }
View Full Code Here

        integralPart = discountFactor * integrator.integrate(integrant, strike, strike + getIntegrationInterval());
      } else {
        integralPart = discountFactor * integrator.integrate(integrant, 0.0, strike);
      }
    } catch (final Exception e) {
      throw new MathException(e);
    }
    final double priceCMS = (strikePart + integralPart) * cmsCapFloor.getNotional() * cmsCapFloor.getPaymentYearFraction();
    return CurrencyAmount.of(cmsCapFloor.getCurrency(), priceCMS);
  }
View Full Code Here

      final int maxInt = 100000;
      final double omega = 1.0;
      final double[] invD = new double[_nNodesX];
      for (int ii = 0; ii < _nNodesX; ii++) {
        if (d[ii] == 0.0) {
          throw new MathException("Cannot solve by PSOR - zero on diagonal");
        }
        invD[ii] = 1.0 / d[ii];
      }

      final int n = b.length;
      double maxErr = 1.0;
      int count = 0;
      double temp;

      for (int ii = 0; ii < n; ii++) {
        x[ii] = Math.max(minVal[ii], x[ii]);
      }
      final double small = 1e-15;

      while (count < maxInt && maxErr > 1e-8) {
        temp = Math.max(minVal[0], (1 - omega) * x[0] + omega * invD[0] * (b[0] - u[0] * x[1]));
        // errSqr = (temp - x[0]) * (temp - x[0]);
        maxErr = Math.abs(temp - x[0]) / (Math.abs(x[0]) + small);
        x[0] = temp;
        for (int ii = 1; ii < n - 1; ii++) {
          temp = Math.max(minVal[ii], (1 - omega) * x[ii] + omega * invD[ii] * (b[ii] - l[ii - 1] * x[ii - 1] - u[ii] * x[ii + 1]));
          // errSqr += (temp - x[ii]) * (temp - x[ii]);
          maxErr = Math.max(Math.abs(temp - x[ii]) / (Math.abs(x[ii]) + small), maxErr);
          x[ii] = temp;
        }
        temp = Math.max(minVal[n - 1], (1 - omega) * x[n - 1] + omega * invD[n - 1] * (b[n - 1] - l[n - 2] * x[n - 2]));
        maxErr = Math.max(Math.abs(temp - x[n - 1]) / (Math.abs(x[n - 1]) + small), maxErr);
        //errSqr += (temp - x[n - 1]) * (temp - x[n - 1]);
        x[n - 1] = temp;

        //   errSqr = Math.sqrt(errSqr);
        count++;
      }

      if (count == maxInt) {
        throw new MathException("PSOR failed to converge");
      }
      return x;
    }
View Full Code Here

    Validate.notNull(data, "data bundle");
    final int n = data.size();
    final double[] keys = data.getKeys();
    final double[] values = data.getValues();
    if (n <= _degree) {
      throw new MathException("Need at least " + (_degree + 1) + " data points to perform polynomial interpolation of degree " + _degree);
    }
    if (data.getLowerBoundIndex(value) == n - 1) {
      return values[n - 1];
    }
    final int lower = data.getLowerBoundIndex(value);
    final int lowerBound = lower - _offset;
    final int upperBound = _degree + 1 + lowerBound;
    if (lowerBound < 0) {
      throw new MathException("Could not get lower bound: index " + lowerBound + " must be greater than or equal to zero");
    }
    if (upperBound > n + 1) {
      throw new MathException("Could not get upper bound: index " + upperBound + " must be less than or equal to " + (n + 1));
    }
    final double[] x = Arrays.copyOfRange(keys, lowerBound, upperBound);
    final double[] y = Arrays.copyOfRange(values, lowerBound, upperBound);
    try {
      final PolynomialFunctionLagrangeForm lagrange = _interpolator.interpolate(x, y);
      return CommonsMathWrapper.unwrap(lagrange).evaluate(value);
    } catch (final org.apache.commons.math.MathException e) {
      throw new MathException(e);
    }
  }
View Full Code Here

        integralPart = discountFactor * integrator.integrate(integrant, strike, strike + getIntegrationInterval());
      } else {
        integralPart = discountFactor * integrator.integrate(integrant, 0.0, strike);
      }
    } catch (final Exception e) {
      throw new MathException(e);
    }
    final double priceCMS = (strikePart + integralPart) * cmsCapFloor.getNotional() * cmsCapFloor.getPaymentYearFraction();
    return MultipleCurrencyAmount.of(cmsCapFloor.getCurrency(), priceCMS);
  }
View Full Code Here

        integralPart = integrator.integrate(integrant, cap.getStrike(), cap.getStrike() + _integrationInterval);
      } else {
        integralPart = integrator.integrate(integrant, 0.0, cap.getStrike());
      }
    } catch (final Exception e) {
      throw new MathException(e);
    }
    integralPart *= 2.0 * cap.getFixingAccrualFactor();
    final double pv = (strikePart + integralPart) / beta;
    return MultipleCurrencyAmount.of(cap.getCurrency(), pv);
  }
View Full Code Here

  @Override
  public Double interpolate(final Interpolator1DDataBundle data, final Double value) {
    Validate.notNull(value, "value");
    Validate.notNull(data, "data bundle");
    if (data.size() < _degree) {
      throw new MathException("Cannot interpolate " + data.size() + " data points with rational functions of degree " + _degree);
    }
    final int m = data.size();
    final double[] x = data.getKeys();
    final double[] y = data.getValues();
    if (data.getLowerBoundIndex(value) == m - 1) {
View Full Code Here

    final RungeKuttaIntegrator1D integrator = new RungeKuttaIntegrator1D(absoluteTolerance, relativeTolerance, NB_INTEGRATION);
    double pv = 0.0;
    try {
      pv = 1.0 / Math.sqrt(2.0 * Math.PI) * integrator.integrate(integrant, -limit, limit) * dfPayment * cms.getNotional() * cms.getPaymentYearFraction();
    } catch (final Exception e) {
      throw new MathException(e);
    }
    return MultipleCurrencyAmount.of(cms.getCurrency(), pv);
  }
View Full Code Here

TOP

Related Classes of com.opengamma.analytics.math.MathException

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.