Example usage for java.lang Math exp

List of usage examples for java.lang Math exp

Introduction

In this page you can find the example usage for java.lang Math exp.

Prototype

@HotSpotIntrinsicCandidate
public static double exp(double a) 

Source Link

Document

Returns Euler's number e raised to the power of a double value.

Usage

From source file:com.opengamma.analytics.math.integration.GaussHermiteQuadratureIntegrator1D.java

/**
 * {@inheritDoc}/*from  ww w . j  a v  a 2 s . c o m*/
 * The function $f(x)$ that is to be integrated is transformed into a form
 * suitable for this quadrature method using:
 * $$
 * \begin{align*}
 * \int_{-\infty}^{\infty} f(x) dx
 * &= \int_{-\infty}^{\infty} f(x) e^{x^2} e^{-x^2} dx\\
 * &= \int_{-\infty}^{\infty} g(x) e^{-x^2} dx
 * \end{align*} 
 * $$
 * @throws UnsupportedOperationException If the lower limit is not $-\infty$ or the upper limit is not $\infty$
 */
@Override
public Function1D<Double, Double> getIntegralFunction(final Function1D<Double, Double> function,
        final Double lower, final Double upper) {
    Validate.notNull(function, "function");
    Validate.notNull(lower, "lower");
    Validate.notNull(upper, "upper");
    if (lower.equals(LIMITS[0]) && upper.equals(LIMITS[1])) {
        return new Function1D<Double, Double>() {

            @Override
            public Double evaluate(final Double x) {
                return Math.exp(x * x) * function.evaluate(x);
            }

        };
    }
    throw new UnsupportedOperationException("Limits for this integration method are +/-infinity");
}

From source file:org.wallerlab.yoink.density.service.densityProperties.AtomicDensityPropertiesCalculator.java

/**
 * calculate a density point's properties from an atom
 * //from ww w . j av a  2 s. c o m
 * @param densityPoint
 *            -{@link org.wallerlab.yoink.api.model.density.DensityPoint}
 * @param atom
 *            -{@link org.wallerlab.yoink.api.model.molecular.Atom}
 * @return densityPoint -
 *         {@link org.wallerlab.yoink.api.model.density.DensityPoint}
 */
public DensityPoint calculate(DensityPoint densityPoint, Atom atom) {
    // initialize
    Vector distanceVector = getDistanceVectorAndAtomName(densityPoint, atom);
    Element atomName = atom.getElementType();
    double distance = Math.max(distanceVector.getNorm(), Constants.DISTANCE_DEFAULT);
    double distanceReciprocal = 1.0 / distance;
    Vector distanceUnitVector = distanceVector.scalarMultiply(distanceReciprocal);
    double exp1 = Math.exp(-distance / atomName.z1());
    double exp2 = Math.exp(-distance / atomName.z2());
    double exp3 = Math.exp(-distance / atomName.z3());
    double fac1 = atomName.cz1() * exp1 + atomName.cz2() * exp2 + atomName.cz3() * exp3;// first derivative of density
    //get the values of densityPoint
    densityPoint = wrapDensityBasedProperties(densityPoint, distanceVector, atomName, distanceReciprocal,
            distanceUnitVector, exp1, exp2, exp3, fac1);
    return densityPoint;
}

From source file:com.opengamma.analytics.financial.model.option.pricing.analytic.FloatingStrikeLookbackOptionModel.java

@Override
public Function1D<StandardOptionWithSpotTimeSeriesDataBundle, Double> getPricingFunction(
        final FloatingStrikeLookbackOptionDefinition definition) {
    Validate.notNull(definition, "definition");
    return new Function1D<StandardOptionWithSpotTimeSeriesDataBundle, Double>() {

        @SuppressWarnings("synthetic-access")
        @Override//from ww  w  .  j  a  v  a2s  .  co m
        public Double evaluate(final StandardOptionWithSpotTimeSeriesDataBundle data) {
            Validate.notNull(data, "data");
            final DoubleTimeSeries<?> ts = data.getSpotTimeSeries();
            final double s = data.getSpot();
            final ZonedDateTime date = data.getDate();
            final double t = definition.getTimeToExpiry(date);
            final boolean isCall = definition.isCall();
            final double k = isCall ? ts.minValue() : ts.maxValue();
            final int sign = isCall ? 1 : -1;
            final double sigma = data.getVolatility(t, k);
            final double r = data.getInterestRate(t);
            final double b = data.getCostOfCarry();
            final double d1 = getD1(s, k, t, sigma, b);
            final double d2 = getD2(d1, sigma, t);
            final double df1 = Math.exp(t * (b - r));
            final double df2 = Math.exp(-r * t);
            final double cdf1 = NORMAL.getCDF(d1);
            double y;
            if (CompareUtils.closeEquals(b, 0, 1e-15)) {
                final double x = isCall ? cdf1 - 1 : cdf1;
                y = CompareUtils.closeEquals(sigma, 0, 1e-15) ? 0
                        : s * df2 * sigma * Math.sqrt(t) * (NORMAL.getPDF(d1) + d1 * x);
            } else {
                y = CompareUtils.closeEquals(sigma, 0, 1e-15) ? 0
                        : s * df2 * sigma * sigma
                                * (sign * Math.pow(s / k, -2 * b / sigma / sigma)
                                        * NORMAL.getCDF(sign * (2 * b * Math.sqrt(t) / sigma - d1))
                                        - sign * Math.exp(b * t) * NORMAL.getCDF(-sign * d1))
                                / 2 / b;
            }
            return sign * (s * df1 * NORMAL.getCDF(sign * d1) - k * df2 * NORMAL.getCDF(sign * d2)) + y;
        }
    };
}

From source file:com.opengamma.analytics.financial.model.interestrate.HullWhiteTwoFactorInterestRateModel.java

protected Double getB(final Double dt, final Double a) {
    return (1 - Math.exp(-a * dt)) / a;
}

From source file:com.opengamma.analytics.math.curve.DoublesCurveNelsonSiegelTest.java

@Test
public void yValue() {
    final int nbPoint = 10;
    final double timeMax = 9.0;
    final double valueComputed0 = CURVE_NS.getYValue(0.0);
    final double valueExpected0 = BETA0 + BETA1;
    assertEquals("DoublesCurveNelsonSiegel: value", valueExpected0, valueComputed0, TOLERANCE_YIELD);
    for (int loopt = 1; loopt <= nbPoint; loopt++) {
        // Implementation note: start at 1 to avoid 0 (treated tested separately)
        final double t = loopt * timeMax / nbPoint;
        final double valueComputed = CURVE_NS.getYValue(t);
        final double tl = t / LAMBDA;
        final double exptl = Math.exp(-tl);
        final double valueExpected = BETA0 + BETA1 * (1 - exptl) / tl + BETA2 * ((1 - exptl) / tl - exptl);
        assertEquals("DoublesCurveNelsonSiegel: value", valueExpected, valueComputed, TOLERANCE_YIELD);
    }/*from   ww w . ja  v a2  s . com*/
}

From source file:statistics.distribution.ExponentialDistribution.java

private double CDF(double x) {
    return 1 - Math.exp(-1 * lambda * x);
}

From source file:com.opengamma.analytics.math.statistics.distribution.NonCentralChiSquaredDistribution.java

/**
 * @param degrees The number of degrees of freedom, not negative or zero
 * @param nonCentrality The non-centrality parameter, not negative
 *//*  w  w w .  j a  v a2 s  . c o  m*/
public NonCentralChiSquaredDistribution(final double degrees, final double nonCentrality) {
    Validate.isTrue(degrees > 0, "degrees of freedom must be > 0, have " + degrees);
    Validate.isTrue(nonCentrality >= 0, "non-centrality must be >= 0, have " + nonCentrality);
    _dofOverTwo = degrees / 2.0;
    _lambdaOverTwo = nonCentrality / 2.0;
    _k = (int) Math.round(_lambdaOverTwo);

    if (_lambdaOverTwo == 0) {
        _pStart = 0.0;
    } else {
        final double logP = -_lambdaOverTwo + _k * Math.log(_lambdaOverTwo) - Gamma.logGamma(_k + 1);
        _pStart = Math.exp(logP);
    }
}

From source file:com.opengamma.analytics.financial.model.option.pricing.analytic.SupershareOptionModel.java

/**
 * {@inheritDoc}/* ww w  .  j ava2  s. c  o m*/
 */
@Override
public Function1D<StandardOptionDataBundle, Double> getPricingFunction(
        final SupershareOptionDefinition definition) {
    Validate.notNull(definition, "definition");
    return new Function1D<StandardOptionDataBundle, Double>() {

        @SuppressWarnings("synthetic-access")
        @Override
        public Double evaluate(final StandardOptionDataBundle data) {
            Validate.notNull(data, "data");
            final double s = data.getSpot();
            final double t = definition.getTimeToExpiry(data.getDate());
            final double r = data.getInterestRate(t);
            final double b = data.getCostOfCarry();
            final double lower = definition.getLowerBound();
            final double upper = definition.getUpperBound();
            final double sigma = data.getVolatility(t, lower);
            final double d1 = getD1(s, lower, t, sigma, b);
            final double d2 = getD1(s, upper, t, sigma, b);
            return s * Math.exp(t * (b - r)) * (NORMAL.getCDF(d1) - NORMAL.getCDF(d2)) / lower;
        }

    };
}

From source file:com.opengamma.analytics.financial.model.finitedifference.MarkovChainSmallTimeApprox.java

public double price(final double forward, final double df, final double strike, final double expiry) {

    final EuropeanVanillaOption option = new EuropeanVanillaOption(strike, expiry, true);
    final Function1D<BlackFunctionData, Double> priceFunc = _black.getPriceFunction(option);

    final Function1D<Double, Double> fun1 = new Function1D<Double, Double>() {

        @SuppressWarnings("synthetic-access")
        @Override/*www. j  ava 2  s .  c  o m*/
        public Double evaluate(final Double tau) {
            double sigma = _vol1 * _vol1 * tau / expiry + _vol2 * _vol2 * (1 - tau / expiry);
            sigma = Math.sqrt(sigma);
            final BlackFunctionData data = new BlackFunctionData(forward, df, sigma);
            return _lambda12 * priceFunc.evaluate(data) * Math.exp(-_lambda12 * tau);
        }
    };

    final Function1D<Double, Double> fun2 = new Function1D<Double, Double>() {

        @SuppressWarnings("synthetic-access")
        @Override
        public Double evaluate(final Double tau) {
            double sigma = _vol1 * _vol1 * (1 - tau / expiry) + _vol2 * _vol2 * tau / expiry;
            sigma = Math.sqrt(sigma);
            final BlackFunctionData data = new BlackFunctionData(forward, df, sigma);
            return _lambda21 * priceFunc.evaluate(data) * Math.exp(-_lambda21 * tau);
        }
    };

    double p1 = 0;
    double p2 = 0;

    if (_probState1 > 0.0) {
        final BlackFunctionData data = new BlackFunctionData(forward, df, _vol1);
        p1 = _integrator.integrate(fun1, 0.0, expiry)
                + priceFunc.evaluate(data) * Math.exp(-_lambda12 * expiry);
    }

    if (_probState1 < 1.0) {
        final BlackFunctionData data = new BlackFunctionData(forward, df, _vol2);
        p2 = _integrator.integrate(fun2, 0.0, expiry)
                + priceFunc.evaluate(data) * Math.exp(-_lambda21 * expiry);
    }
    return _probState1 * p1 + (1 - _probState1) * p2;
}

From source file:com.opengamma.analytics.math.statistics.distribution.BivariateNormalDistribution.java

/**
 * @param x The parameters for the function, $(x, y, \rho$, with $-1 \geq \rho \geq 1$, not null 
 * @return The cdf//  w  w  w . j a v a 2s .c o  m
 */
@Override
public double getCDF(final double[] x) {
    Validate.notNull(x);
    Validate.isTrue(x.length == 3, "Need a, b and rho values");
    Validate.isTrue(x[2] >= -1 && x[2] <= 1, "Correlation must be >= -1 and <= 1");
    final double a = x[0];
    double b = x[1];
    final double rho = x[2];
    if (a == Double.POSITIVE_INFINITY || b == Double.POSITIVE_INFINITY) {
        return 1;
    }
    if (a == Double.NEGATIVE_INFINITY || b == Double.NEGATIVE_INFINITY) {
        return 0;
    }
    final double sumSq = (a * a + b * b) / 2.;
    double rho1, rho2, rho3, ab, absDiff, h5, c, d, mult = 0, rho3Sq, eab, e, result;
    if (Math.abs(rho) >= 0.7) {
        rho1 = 1 - rho * rho;
        rho2 = Math.sqrt(rho1);
        if (rho < 0) {
            b *= -1;
        }
        ab = a * b;
        eab = Math.exp(-ab / 2.);
        if (Math.abs(rho) < 1) {
            absDiff = Math.abs(a - b);
            h5 = absDiff * absDiff / 2.;
            absDiff = absDiff / rho2;
            c = 0.5 - ab / 8.;
            d = 3. - 2. * c * h5;
            mult = 0.13298076 * absDiff * d * (1 - NORMAL.getCDF(absDiff))
                    - Math.exp(-h5 / rho1) * (d + c * rho1) * 0.053051647;
            for (int i = 0; i < 5; i++) {
                rho3 = rho2 * X[i];
                rho3Sq = rho3 * rho3;
                rho1 = Math.sqrt(1 - rho3Sq);
                if (eab == 0) {
                    e = 0;
                } else {
                    e = Math.exp(-ab / (1 + rho1)) / rho1 / eab;
                }
                mult = mult - Y[i] * Math.exp(-h5 / rho3Sq) * (e - 1 - c * rho3Sq);
            }
        }
        result = mult * rho2 * eab + NORMAL.getCDF(Math.min(a, b));
        if (rho < 0) {
            result = NORMAL.getCDF(a) - result;
        }
        return result;
    }
    ab = a * b;
    if (rho != 0) {
        for (int i = 0; i < 5; i++) {
            rho3 = rho * X[i];
            rho1 = 1 - rho3 * rho3;
            mult = mult + Y[i] * Math.exp((rho3 * ab - sumSq) / rho1) / Math.sqrt(rho1);
        }
    }
    return NORMAL.getCDF(a) * NORMAL.getCDF(b) + rho * mult;
}