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.financial.model.option.pricing.analytic.BjerksundStenslandModel.java

/**
 * Get the price of an American option by the Bjerksund and Stensland (2002) approximation. We ensure that the price is the maximum of the no early excise (Black-Scholes price), 
 * the immediate exercise value and the Bjerksund-Stensland approximation value
 * @param s0 The spot//www  .  j  a  v a 2s  .  c  om
 * @param k The strike
 * @param r The risk-free rate
 * @param b The cost-of-carry
 * @param t The time-to-expiry
 * @param sigma The volatility
 * @param isCall true for calls
 * @return The American option price
 */
public double price(final double s0, final double k, final double r, final double b, final double t,
        final double sigma, final boolean isCall) {

    final double fwd = s0 * Math.exp(b * t);
    final double df = Math.exp(-r * t);
    final double bsPrice = df * BlackFormulaRepository.price(fwd, k, t, sigma, isCall);

    if (isCall) {
        if (b >= r) { //no early exercise in this case
            return bsPrice;
        }
        return Math.max(bsPrice, getCallPrice(s0, k, r, b, t, sigma));
    }
    //min price is  maximum of immediate exercise and Black-Scholes price 
    final double minPrice = Math.max(k - s0, bsPrice);
    final double temp = (2 * b + sigma * sigma) / 2 / sigma;
    final double minR = b - 0.5 * temp * temp;
    //this does not give the best possible lower bound. Bjerksund-Stensland will give an answer for r < 0, but will fail for r < minR (complex beta) 
    //TODO review the Bjerksund-Stensland formalisation to see if a general r < 0 (for puts) solution is possible 
    if (r < minR) {
        return minPrice;
    }
    //put using put-call transformation 
    return Math.max(minPrice, getCallPrice(k, s0, r - b, -b, t, sigma));
}

From source file:edu.duke.cs.osprey.voxq.QuadraticQFunction.java

private double cumulDistr(double x) {
    //integral of exp(ax^2+bx+c) from xLo to x
    //this should be fairly resistant to under/overflow once the distribution is normalized
    if (Math.abs(a) < 1e-14) {//linear case
        return (Math.exp(b * x + c) - Math.exp(b * xLo + c)) / b;
    } else {/*from w w w .jav  a2 s  .  com*/
        double C = 0.5 * Math.exp(c - 0.25 * b * b / a) * Math.sqrt(-Math.PI / a);
        return C * (Erf.erf(cdErfArg(xLo), cdErfArg(x)));
    }
}

From source file:com.datumbox.framework.core.statistics.distributions.ContinuousDistributions.java

/**
 * Internal function used by gammaCdf/*www.  jav  a 2  s. c  o  m*/
 * 
 * @param x
 * @param A
 * @return 
 */
private static double gSer(double x, double A) {
    // Good for X<A+1.
    double T9 = 1 / A;
    double G = T9;
    double I = 1;
    while (T9 > G * 0.00001) {
        T9 = T9 * x / (A + I);
        G = G + T9;
        ++I;
    }
    G = G * Math.exp(A * Math.log(x) - x - logGamma(A));

    return G;
}

From source file:com.datumbox.framework.statistics.distributions.ContinuousDistributions.java

/**
 * Internal function used by GammaCdf/*from   w w w  . j ava  2  s .c o  m*/
 * 
 * @param x
 * @param A
 * @return 
 */
protected static double Gser(double x, double A) {
    // Good for X<A+1.
    double T9 = 1 / A;
    double G = T9;
    double I = 1;
    while (T9 > G * 0.00001) {
        T9 = T9 * x / (A + I);
        G = G + T9;
        ++I;
    }
    G = G * Math.exp(A * Math.log(x) - x - LogGamma(A));

    return G;
}

From source file:beast.evolution.operators.GMRFMultilocusSkyrideBlockUpdateOperator.java

private static DenseVector gradient(double[] data1, double[] data2, DenseVector value, SymmTridiagMatrix Q) {

    DenseVector returnValue = new DenseVector(value.size());
    Q.mult(value, returnValue);/*w w w  .  jav  a  2  s.com*/
    for (int i = 0; i < value.size(); i++) {
        returnValue.set(i, -returnValue.get(i) - data1[i] + data2[i] * Math.exp(-value.get(i)));
    }
    return returnValue;
}

From source file:com.opengamma.analytics.financial.interestrate.annuity.YieldSensitivityCalculator.java

/**
 *  For a set of cash flows calculates the nth derivative of its PV with respect to its continuously compounded yield multiplied by the 
 *  factor (-1)^n which just keeps the sign positive when cash flows are positive 
 * @param annuity Set of known cash flows 
 * @param yield Continuously compounded constant interest rate 
 * @param order The order of the derivative 
 * @return nth order yield sensitivity (times (-1)^n)
 *///w  w  w . j  a v  a2 s .  c o  m
public double calculateNthOrderSensitivityFromYield(final AnnuityCouponFixed annuity, final double yield,
        final int order) {
    Validate.notNull(annuity, "annuity");
    Validate.isTrue(order >= 0, "order must be positive");
    double sum = 0;

    double t;
    double tPower;
    final int n = annuity.getNumberOfPayments();
    CouponFixed temp;
    for (int i = 0; i < n; i++) {
        temp = annuity.getNthPayment(i);
        t = temp.getPaymentTime();
        tPower = Math.pow(t, order);
        sum += temp.getAmount() * tPower * Math.exp(-yield * t);
    }
    return sum;
}

From source file:bachelorthesis.methods.detection.bayesian.BayesianDetection.java

private double[] expSum(double[][] likelihoods) {
    double[] result = new double[likelihoods.length];
    for (double[] subLikelihood : likelihoods) {
        for (int i = 0; i < subLikelihood.length; i++) {
            result[i] = result[i] + Math.exp(subLikelihood[i]);
        }/*from   ww  w  .j a v a2 s.  co  m*/
    }
    return result;
}

From source file:com.itemanalysis.psychometrics.irt.model.Irm3PL.java

private double probRight(double theta) {
    double z = Math.exp(D * discrimination * (theta - difficulty));
    double prob = guessing + (slipping - guessing) * z / (1 + z);
    return prob;/*  w  w  w  . j a  va2 s . com*/
}

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

private double getI3(final double t) {
    final double t2 = t * t;
    final double t3 = t2 * t;

    final double lt = _lambda * t;
    if (lt < 1e-7) {
        return _probState1;
    }/*  w w  w.j  a va  2  s  .  c o m*/

    final double a = _theta;
    final double a2 = a * a;
    final double a3 = a2 * a;
    final double b = _lambda;
    final double b2 = b * b;
    final double b3 = b2 * b;
    final double p = _probState1;

    final double temp1 = a3 * b3 + (3 * a2 * b2 * p - 9 * a3 * b2 + 6 * a2 * b2) / t
            + (-18 * a2 * b * p + 12 * a * b * p + 36 * a3 * b - 36 * a2 * b + 6 * a * b) / t2
            + (36 * a2 * p - 36 * a * p + 6 * p - 60 * a3 + 72 * a2 - 18 * a) / t3;
    final double temp2 = (-3 * a2 * b2 * p + 6 * a * b2 * p - 3 * b2 * p + 3 * a3 * b2 - 6 * a2 * b2
            + 3 * a * b2) / t
            + (-18 * a2 * b * p + 24 * a * b * p - 6 * b * p + 24 * a3 * b - 36 * a2 * b + 12 * a * b) / t2
            + (-36 * a2 * p + 36 * a * p - 6 * p + 60 * a3 - 72 * a2 + 18 * a) / t3;

    return (temp1 + temp2 * Math.exp(-b * t)) / b3;
}

From source file:com.opengamma.analytics.financial.model.volatility.BlackFormulaRepository.java

public static double strikeForDelta(final double forward, final double forwardDelta, final double timeToExpiry,
        final double lognormalVol, final boolean isCall) {
    ArgumentChecker.isTrue(forward >= 0.0, "negative/NaN forward; have {}", forward);
    ArgumentChecker.isTrue((isCall && forwardDelta > 0 && forwardDelta < 1)
            || (!isCall && forwardDelta > -1 && forwardDelta < 0), "delta out of range", forwardDelta);
    ArgumentChecker.isTrue(timeToExpiry >= 0.0, "negative/NaN timeToExpiry; have {}", timeToExpiry);
    ArgumentChecker.isTrue(lognormalVol >= 0.0, "negative/NaN lognormalVol; have {}", lognormalVol);

    final int sign = isCall ? 1 : -1;
    final double d1 = sign * NORMAL.getInverseCDF(sign * forwardDelta);

    double sigmaSqT = lognormalVol * lognormalVol * timeToExpiry;
    if (Double.isNaN(sigmaSqT)) {
        s_logger.info("lognormalVol * Math.sqrt(timeToExpiry) ambiguous");
        sigmaSqT = 1.;//from w  w  w  . j  a va 2 s  .  com
    }

    return forward * Math.exp(-d1 * Math.sqrt(sigmaSqT) + 0.5 * sigmaSqT);
}