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.MertonJumpDiffusionModel.java

/**
 * {@inheritDoc}//from   ww  w .j  a  v a 2s. c  om
 */
@Override
public Function1D<MertonJumpDiffusionModelDataBundle, Double> getPricingFunction(
        final OptionDefinition definition) {
    Validate.notNull(definition);
    final Function1D<MertonJumpDiffusionModelDataBundle, Double> pricingFunction = new Function1D<MertonJumpDiffusionModelDataBundle, Double>() {

        @SuppressWarnings("synthetic-access")
        @Override
        public Double evaluate(final MertonJumpDiffusionModelDataBundle data) {
            Validate.notNull(data);
            final ZonedDateTime date = data.getDate();
            final double k = definition.getStrike();
            final double t = definition.getTimeToExpiry(date);
            final double sigma = data.getVolatility(t, k);
            final double lambda = data.getLambda();
            final double gamma = data.getGamma();
            final double sigmaSq = sigma * sigma;
            final double delta = Math.sqrt(gamma * sigmaSq / lambda);
            final double z = Math.sqrt(sigmaSq - lambda * delta * delta);
            final double zSq = z * z;
            double sigmaAdjusted = z;
            final double lambdaT = lambda * t;
            double mult = Math.exp(-lambdaT);
            final StandardOptionDataBundle bsmData = new StandardOptionDataBundle(data.getInterestRateCurve(),
                    data.getCostOfCarry(), new VolatilitySurface(ConstantDoublesSurface.from(sigmaAdjusted)),
                    data.getSpot(), date);
            final Function1D<StandardOptionDataBundle, Double> bsmFunction = BSM.getPricingFunction(definition);
            double price = mult * bsmFunction.evaluate(bsmData);
            for (int i = 1; i < N; i++) {
                sigmaAdjusted = Math.sqrt(zSq + delta * delta * i / t);
                mult *= lambdaT / i;
                price += mult * bsmFunction.evaluate(bsmData.withVolatilitySurface(
                        new VolatilitySurface(ConstantDoublesSurface.from(sigmaAdjusted))));
            }
            return price;
        }
    };
    return pricingFunction;
}

From source file:com.opengamma.analytics.financial.model.volatility.smile.function.SABRPaulotVolatilityFunction.java

@Override
public Function1D<SABRFormulaData, Double> getVolatilityFunction(final EuropeanVanillaOption option,
        final double forward) {
    Validate.notNull(option, "option");
    final double strike = option.getStrike();

    final double cutoff = forward * CUTOFF_MONEYNESS;
    final double k;
    if (strike < cutoff) {
        s_logger.info("Given strike of " + strike + " is less than cutoff at " + cutoff
                + ", therefore the strike is taken as " + cutoff);
        k = cutoff;//  ww w .ja va 2 s .co  m
    } else {
        k = strike;
    }

    final double t = option.getTimeToExpiry();
    return new Function1D<SABRFormulaData, Double>() {

        @SuppressWarnings("synthetic-access")
        @Override
        public final Double evaluate(final SABRFormulaData data) {
            Validate.notNull(data, "data");
            final double alpha = data.getAlpha();
            final double beta = data.getBeta();
            final double rho = data.getRho();
            final double nu = data.getNu();

            double sigma0, sigma1;

            final double beta1 = 1 - beta;

            final double x = Math.log(k / forward);
            if (CompareUtils.closeEquals(nu, 0, EPS)) {
                if (CompareUtils.closeEquals(beta, 1.0, EPS)) {
                    return alpha; // this is just log-normal
                }
                throw new NotImplementedException("Have not implemented the case where nu = 0, beta != 0");
            }

            // the formula behaves very badly close to ATM
            if (CompareUtils.closeEquals(x, 0.0, 1e-3)) {
                final double delta = 1.01e-3;
                final double a0 = (HAGAN.getVolatilityFunction(option, forward)).evaluate(data);
                double kPlus, kMinus;
                kPlus = forward * Math.exp(delta);
                kMinus = forward * Math.exp(-delta);
                EuropeanVanillaOption other = new EuropeanVanillaOption(kPlus, option.getTimeToExpiry(),
                        option.isCall());
                final double yPlus = getVolatilityFunction(other, forward).evaluate(data);
                other = new EuropeanVanillaOption(kMinus, option.getTimeToExpiry(), option.isCall());
                final double yMinus = getVolatilityFunction(other, forward).evaluate(data);
                final double a2 = (yPlus + yMinus - 2 * a0) / 2 / delta / delta;
                final double a1 = (yPlus - yMinus) / 2 / delta;
                return a2 * x * x + a1 * x + a0;
            }
            final double tScale = nu * nu * t;
            final double alphaScale = alpha / nu;

            double q;
            if (CompareUtils.closeEquals(beta, 1.0, EPS)) {
                q = x;
            } else {
                q = (Math.pow(k, beta1) - Math.pow(forward, beta1)) / beta1;
            }

            final double vMin = Math.sqrt(alphaScale * alphaScale + 2 * rho * alphaScale * q + q * q);
            final double logTerm = Math.log((vMin + rho * alphaScale + q) / (1 + rho) / alphaScale);
            sigma0 = x / logTerm;

            final double cTilde = getCTilde(forward, k, alphaScale, beta, rho, q);
            sigma1 = -(cTilde + Math.log(sigma0 * Math.sqrt(k * forward))) / square(logTerm);
            return nu * sigma0 * (1 + sigma1 * tScale);
        }
    };
}

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

/**
 * {@inheritDoc}//from   w w w .  j  ava2s. c o m
 */
@Override
public Function1D<StandardOptionDataBundle, Double> getPricingFunction(
        final PoweredOptionDefinition definition) {
    Validate.notNull(definition);
    final Function1D<StandardOptionDataBundle, Double> pricingFunction = new Function1D<StandardOptionDataBundle, Double>() {

        /**
         * @throws OptionPricingException If the power is not an integer.
         */
        @SuppressWarnings("synthetic-access")
        @Override
        public Double evaluate(final StandardOptionDataBundle data) {
            Validate.notNull(data);
            if (Math.abs(definition.getPower() - Math.round(definition.getPower())) > 1e-15) {
                throw new OptionPricingException(
                        "Analytic powered option pricing model can only be used when then power is an integer");
            }
            final double s = data.getSpot();
            final double k = definition.getStrike();
            final double t = definition.getTimeToExpiry(data.getDate());
            final double b = data.getCostOfCarry();
            final double r = data.getInterestRate(t);
            final double sigma = data.getVolatility(t, k);
            final long power = Math.round(definition.getPower());
            final int sign = definition.isCall() ? 1 : -1;
            final double sigmaSq = sigma * sigma;
            final double sigmaT = sigma * Math.sqrt(t);
            final double x = (Math.log(s / k) + t * (b - 0.5 * sigma * sigma)) / sigmaT;
            long diff;
            double price = 0;
            for (int i = 0; i <= power; i++) {
                diff = power - i;
                price += getCombinatorial(power, i) * Math.pow(sign * s, diff) * Math.pow(-sign * k, i)
                        * Math.exp((diff - 1) * (r + diff * sigmaSq / 2.) * t - diff * (r - b) * t)
                        * NORMAL.getCDF(sign * getD(x, diff, sigmaT, sigmaSq, t));
            }
            return price;
        }
    };
    return pricingFunction;
}

From source file:com.opengamma.analytics.math.interpolation.ExponentialExtrapolator1D.java

private Double leftExtrapolateDerivative(final Interpolator1DDataBundle data, final Double value) {
    Validate.notNull(data, "data");
    Validate.notNull(value, "value");
    final double x = data.firstKey();
    final double y = data.firstValue();
    final double m = Math.log(y) / x;
    return m * Math.exp(m * value);
}

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

protected Double getEta(final double t1, final double t2, final double t3, final double a, final double b,
        final double sigma1, final double sigma2, final double rho) {
    final double dt12 = t2 - t1;
    final double dt13 = t3 - t1;
    final double dt23 = t3 - t2;
    final double b12 = getB(dt12, a);
    final double b12Sq = b12 * b12;
    final double b13 = getB(dt13, a);
    final double b13Sq = b13 * b13;
    final double b23 = getB(dt23, a);
    final double b23Sq = b23 * b23;
    final double c12 = getC(dt12, a, b);
    final double c12Sq = c12 * c12;
    final double c13 = getC(dt13, a, b);
    final double c13Sq = c13 * c13;
    final double c23 = getC(dt23, a, b);
    final double c23Sq = c23 * c23;
    final double abP = a + b;
    final double abM = a - b;
    final double gamma1 = Math.exp(-abP * dt13) * (Math.exp(abP * dt12) - 1) / (abP * abM)
            - Math.exp(-2 * a * dt13) * (Math.exp(2 * a * dt12) - 1) / (2 * a * abM);
    final double gamma2 = (gamma1 + c23 - c13 + 0.5 * b23Sq - 0.5 * b13Sq + dt12 / a
            - (Math.exp(-a * dt23) - Math.exp(-a * dt13)) / (a * a)) / (a * b);
    final double gamma3 = -(Math.exp(-abP * dt12) - 1) / (abP * abM)
            + (Math.exp(-2 * a * dt12) - 1) / (2 * a * abM);
    final double gamma4 = (gamma3 - c12 - 0.5 * b12Sq + dt12 / a + (Math.exp(-a * dt12) - 1) / (a * a))
            / (a * b);// w w w .  j a va2 s. c  om
    final double gamma5 = (0.5 * (c23Sq - c13Sq) + gamma2) / b;
    final double gamma6 = (gamma4 - 0.5 * c12Sq) / b;
    return sigma1 * sigma1 * (1 - Math.exp(-2 * a * dt12)) * b23Sq / (4 * a)
            - rho * sigma1 * sigma2 * (b12 * c12 * b23 + gamma4 - gamma2)
            - 0.5 * sigma2 * sigma2 * (c12Sq * b23 + gamma6 - gamma5);
}

From source file:dr.app.bss.Utils.java

public static double rLogNormal(double stdev, double mean) {

    double rNorm = random.nextGaussian() * stdev + mean;
    double rLognormal = Math.exp(rNorm);

    return rLognormal;
}

From source file:org.jfree.data.function.NormalDistributionFunction2D.java

/**
 * Returns the function value./*from  w  w w  .ja v  a2 s. c  o  m*/
 *
 * @param x  the x-value.
 *
 * @return The value.
 */
@Override
public double getValue(double x) {
    double z = x - this.mean;
    return this.factor * Math.exp(-z * z / this.denominator);
}

From source file:edu.oregonstate.eecs.mcplan.domains.voyager.Voyager.java

private static double winProbability(final int a, final int d, final double dadv) {
    // TOOD: Parameters should be in VoyagerParameters
    // The 'temperature' parameter controls the size of the numerical
    // advantage effect. High temperature makes numerical advantage less
    // important. The 'defender_advantage' parameter favors the defender
    // when *less than 1*.
    final double temperature = 0.5;
    final double p = 1.0 / (1.0 + Math.exp(-(1.0 / temperature) * (a - d) / (a + d)));
    return dadv * p;
}

From source file:br.ufrgs.enq.jcosmo.test.VLEdiagramsGAMESS.java

public JPanel calcAcetoneWater() throws Exception {
    super.setTitle("P vs x1");
    double T = 298.15;
    setLayout(new BorderLayout());

    COSMOSACDataBase db = COSMOSACDataBase.getInstance();
    COSMOSACCompound c1 = db.getComp("ethyl-acetate");
    COSMOSACCompound c2 = db.getComp("water");

    double[] cavityVolume = new double[2];
    cavityVolume[0] = c1.Vcosmo;//from w w  w  .j  a v  a  2  s  .c  o m
    cavityVolume[1] = c2.Vcosmo;

    double[][] sigma = new double[2][];

    sigma[0] = c1.sigma;
    sigma[1] = c2.sigma;

    SigmaProfileGenerator c1Sigma = new SigmaProfileGenerator(SigmaProfileGenerator.FileType.GAMESS,
            c1.name.toLowerCase() + ".log");
    SigmaProfileGenerator c2Sigma = new SigmaProfileGenerator(SigmaProfileGenerator.FileType.GAMESS,
            c2.name.toLowerCase() + ".log");
    sigma[0] = c1Sigma.getSigmaProfile();
    sigma[1] = c2Sigma.getSigmaProfile();

    COSMOSAC cosmosac = new COSMOSAC();
    cosmosac.setParameters(cavityVolume, c1.charge, sigma);
    cosmosac.setIgnoreSG(true);
    cosmosac.setSigmaHB(COSMOSAC.SIGMAHB * 1.2);

    cosmosac.setTemperature(T);

    double[] x1 = new double[n];
    double[] x2 = new double[n];
    double[] gamma1 = new double[n];
    double[] gamma2 = new double[n];
    double[] z = new double[2];
    double[] lnGamma = new double[2];
    z[0] = 0.00;
    int j = 0;
    while (z[0] < 1.0001) {
        z[1] = 1 - z[0];
        x1[j] = z[0];
        x2[j] = z[1];
        cosmosac.setComposition(z);
        cosmosac.activityCoefficient(lnGamma);
        gamma1[j] = Math.exp(lnGamma[0]);
        gamma2[j] = Math.exp(lnGamma[1]);
        z[0] += 0.05;
        j++;
    }

    double[] Psat = new double[2];
    Psat[0] = 13000;
    Psat[1] = 3000;
    double[] P = calcPx(x1, x2, gamma1, gamma2, Psat);
    double[] y1 = calcY(x1, gamma1, Psat, P);

    XYPlot plot1;
    XYSeriesCollection dataset = new XYSeriesCollection();
    XYSeries liq = new XYSeries("liquid");
    XYSeries vap = new XYSeries("vapor");
    XYSeries raoult = new XYSeries("Raoult's Law");
    for (int i = 0; i < n; i++) {
        liq.add(x1[i], P[i]);
        vap.add(y1[i], P[i]);
    }
    raoult.add(0, Psat[1]);
    raoult.add(1, Psat[0]);
    dataset.addSeries(liq);
    dataset.addSeries(vap);
    dataset.addSeries(raoult);

    JFreeChart chart = ChartFactory.createXYLineChart(null, "Mole Fraction: x1, y1", "P/KPa", null,
            PlotOrientation.VERTICAL, true, true, false);
    plot1 = (XYPlot) chart.getPlot();
    plot1.getDomainAxis().setRange(new Range(0.0, 1.0));

    plot1.setDataset(dataset);

    //      XYSplineRenderer r = new XYSplineRenderer();
    //      BasicStroke stroke = new BasicStroke(2f);
    //      r.setStroke(stroke);
    //      plot1.setRenderer(r);
    //      r.setBaseShapesVisible(false);

    ChartPanel chartPanel = new ChartPanel(chart);
    JPanel jp1 = new JPanel(new BorderLayout());
    jp1.add(chartPanel);

    add(jp1, BorderLayout.CENTER);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 500);

    return jp1;
}

From source file:com.opengamma.analytics.financial.equity.future.pricing.EquityFutureDividendYield.java

/**
 * @param future EquityFuture derivative
 * @param dataBundle Contains funding curve, spot value and continuous dividend yield 
 * @return The change in the present value given a unit value change in the discount rate
 *///from  w  ww  .  j  a v a2  s  .  c om
@Override
public double ratesDelta(final EquityFuture future, final EquityFutureDataBundle dataBundle) {
    Validate.notNull(future, "Future");
    Validate.notNull(dataBundle);
    Validate.notNull(dataBundle.getFundingCurve());
    Validate.notNull(dataBundle.getSpotValue());
    Validate.notNull(dataBundle.getDividendYield());

    double timeToExpiry = future.getTimeToSettlement();
    double discountRate = dataBundle.getFundingCurve().getInterestRate(timeToExpiry);
    double costOfCarry = Math.exp(timeToExpiry * (discountRate - dataBundle.getDividendYield()));
    double fwdPrice = dataBundle.getSpotValue() * costOfCarry;
    return timeToExpiry * fwdPrice * future.getUnitAmount();
}