Example usage for java.lang Math log

List of usage examples for java.lang Math log

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double log(double a) 

Source Link

Document

Returns the natural logarithm (base e) of a double value.

Usage

From source file:geogebra.kernel.statistics.AlgoRandomBinomial.java

private double logOfKFactorial(int k) {
    if (k < 10) {
        if (logtable[k] == 0)
            logtable[k] = Math.log(MyMath.factorial(k + 1d));
        return logtable[k];
    }/* w w  w  .j  av  a 2 s. co m*/

    // Stirling approximation
    return halflog2pi + (k + 0.5) * Math.log(k + 1) - (k + 1)
            + (1 / 12.0 - (1 / 360.0 - 1 / 1260.0 / (k + 1) / (k + 1)) / (k + 1) / (k + 1)) / (k + 1);
}

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

/**
 * Gets the pricing function for a European-style product option 
 * @param definition The option definition
 * @return The pricing function/*ww w. ja  v  a  2 s.c  o  m*/
 * @throws IllegalArgumentException If the definition is null
 */
@Override
public Function1D<StandardTwoAssetOptionDataBundle, Double> getPricingFunction(
        final ProductOptionDefinition definition) {
    Validate.notNull(definition, "definition");
    return new Function1D<StandardTwoAssetOptionDataBundle, Double>() {

        @SuppressWarnings("synthetic-access")
        @Override
        public Double evaluate(final StandardTwoAssetOptionDataBundle data) {
            Validate.notNull(data, "data");
            final double s1 = data.getFirstSpot();
            final double s2 = data.getSecondSpot();
            final double k = definition.getStrike();
            final double b1 = data.getFirstCostOfCarry();
            final double b2 = data.getSecondCostOfCarry();
            final double t = definition.getTimeToExpiry(data.getDate());
            final double r = data.getInterestRate(t);
            final double sigma1 = data.getFirstVolatility(t, k);
            final double sigma2 = data.getSecondVolatility(t, k);
            final double rho = data.getCorrelation();
            final double sigma = Math.sqrt(sigma1 * sigma1 + sigma2 * sigma2 + 2 * rho * sigma1 * sigma2);
            final double sigmaT = sigma * Math.sqrt(t);
            final double f = s1 * s2 * Math.exp(t * (b1 + b2 + rho * sigma1 * sigma2));
            final double d1 = (Math.log(f / k) + t * sigma * sigma / 2) / sigmaT;
            final double d2 = d1 - sigmaT;
            final int sign = definition.isCall() ? 1 : -1;
            return Math.exp(-r * t) * sign * (f * NORMAL.getCDF(sign * d1) - k * NORMAL.getCDF(sign * d2));
        }

    };
}

From source file:cc.recommenders.evaluation.OutputUtils.java

public static String humanReadableByteCount(long bytes) {
    Asserts.assertGreaterOrEqual(bytes, 0);
    // boolean si = false;
    // int unit = si ? 1000 : 1024;
    int unit = 1024;
    if (bytes < unit)
        return bytes + " B";
    int exp = (int) (Math.log(bytes) / Math.log(unit));
    // String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" :
    // "i");/*from www. j  a  v a 2 s .com*/
    String pre = "KMGTPE".charAt(exp - 1) + "i";
    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}

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

@Override
public Function1D<SVIFormulaData, double[]> getModelAdjointFunction(final EuropeanVanillaOption option,
        final double forward) {
    Validate.notNull(option, "option");
    Validate.isTrue(forward > 0, "Need forward >= 0");
    final double strike = option.getStrike();
    Validate.isTrue(strike > 0, "Need strike >= 0");
    final double kappa = Math.log(strike / forward);

    return new Function1D<SVIFormulaData, double[]>() {
        @SuppressWarnings("synthetic-access")
        @Override/*from   www .j a v  a 2  s .  c  o  m*/
        public double[] evaluate(final SVIFormulaData data) {
            return getModelAdjoint(kappa, data);
        }
    };
}

From source file:com.opengamma.analytics.financial.interestrate.payments.method.CapFloorIborHullWhiteMethod.java

/**
 * Computes the present value of a cap/floor in the Hull-White one factor model.
 * @param cap The cap/floor./*from w w w .j a va  2 s .  c  om*/
 * @param hwData The Hull-White parameters and the curves.
 * @return The present value.
 */
public CurrencyAmount presentValue(final CapFloorIbor cap,
        final HullWhiteOneFactorPiecewiseConstantDataBundle hwData) {
    ArgumentChecker.notNull(cap, "The cap/floor shoud not be null");
    ArgumentChecker.notNull(hwData, "The Hull-White data shoud not be null");
    final double tp = cap.getPaymentTime();
    final double t0 = cap.getFixingPeriodStartTime();
    final double t1 = cap.getFixingPeriodEndTime();
    final double deltaF = cap.getFixingAccrualFactor();
    final double deltaP = cap.getPaymentYearFraction();
    final double k = cap.getStrike();
    final double dfPay = hwData.getCurve(cap.getFundingCurveName()).getDiscountFactor(tp);
    final double dfForwardT0 = hwData.getCurve(cap.getForwardCurveName()).getDiscountFactor(t0);
    final double dfForwardT1 = hwData.getCurve(cap.getForwardCurveName()).getDiscountFactor(t1);
    final double alpha0 = _model.alpha(hwData.getHullWhiteParameter(), 0.0, cap.getFixingTime(), tp, t0);
    final double alpha1 = _model.alpha(hwData.getHullWhiteParameter(), 0.0, cap.getFixingTime(), tp, t1);
    final double kappa = (Math.log((1 + deltaF * k) * dfForwardT1 / dfForwardT0)
            - (alpha1 * alpha1 - alpha0 * alpha0) / 2.0) / (alpha1 - alpha0);
    final double omega = (cap.isCap() ? 1.0 : -1.0);
    double pv = deltaP / deltaF * dfPay * omega
            * (dfForwardT0 / dfForwardT1 * NORMAL.getCDF(omega * (-kappa - alpha0))
                    - (1.0 + deltaF * k) * NORMAL.getCDF(omega * (-kappa - alpha1)));
    pv *= cap.getNotional();
    return CurrencyAmount.of(cap.getCurrency(), pv);
}

From source file:edu.brown.utils.MathUtil.java

/**
 * Returns the geometric mean of the entries in the input array. If the
 * zero_value is not null, all zeroes will be replaced with that value
 * //from   w w  w. j  a  v a2s  .co  m
 * @param values
 * @param zero_value
 * @return
 */
public static final double geometricMean(final double[] values, final Double zero_value) {
    double sumLog = 0.0d;
    for (double v : values) {
        if (v == 0 && zero_value != null)
            v = zero_value;
        sumLog += Math.log(v);
    }
    return Math.exp(sumLog / (double) values.length);
}

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

/**
 * Computes the price of a barrier option in the Black world.
 * @param option The underlying European vanilla option.
 * @param barrier The barrier.//from   w  w  w  .  j  a v  a  2  s.  com
 * @param rebate The rebate.
 * @param spot The spot price.
 * @param costOfCarry The cost of carry.
 * @param rate The interest rate.
 * @param sigma The Black volatility.
 * @return The price.
 */
public double getPrice(final EuropeanVanillaOption option, final Barrier barrier, final double rebate,
        final double spot, final double costOfCarry, final double rate, final double sigma) {
    Validate.notNull(option, "option");
    Validate.notNull(barrier, "barrier");
    final boolean isKnockIn = (barrier.getKnockType() == KnockType.IN);
    final boolean isDown = (barrier.getBarrierType() == BarrierType.DOWN);
    final double h = barrier.getBarrierLevel();
    Validate.isTrue(!(barrier.getBarrierType() == BarrierType.DOWN && spot < barrier.getBarrierLevel()),
            "The Data is not consistent with an alive barrier (DOWN and spot<barrier).");
    Validate.isTrue(!(barrier.getBarrierType() == BarrierType.UP && spot > barrier.getBarrierLevel()),
            "The Data is not consistent with an alive barrier (UP and spot>barrier).");
    final boolean isCall = option.isCall();
    final double t = option.getTimeToExpiry();
    final double strike = option.getStrike();
    final int phi = isCall ? 1 : -1;
    final double eta = isDown ? 1 : -1;
    final double df1 = Math.exp(t * (costOfCarry - rate));
    final double df2 = Math.exp(-rate * t);
    if (CompareUtils.closeEquals(sigma, 0, 1e-16)) {
        return df1 * rebate;
    }
    final double sigmaSq = sigma * sigma;
    final double sigmaT = sigma * Math.sqrt(t);
    final double mu = (costOfCarry - 0.5 * sigmaSq) / sigmaSq;
    final double lambda = Math.sqrt(mu * mu + 2 * rate / sigmaSq);
    final double m1 = sigmaT * (1 + mu);
    final double x1 = Math.log(spot / strike) / sigmaT + m1;
    final double x2 = Math.log(spot / h) / sigmaT + m1;
    final double y1 = Math.log(h * h / spot / strike) / sigmaT + m1;
    final double y2 = Math.log(h / spot) / sigmaT + m1;
    final double z = Math.log(h / spot) / sigmaT + lambda * sigmaT;
    final double xA = getA(spot, strike, df1, df2, x1, sigmaT, phi);
    final double xB = getA(spot, strike, df1, df2, x2, sigmaT, phi);
    final double xC = getC(spot, strike, df1, df2, y1, sigmaT, h, mu, phi, eta);
    final double xD = getC(spot, strike, df1, df2, y2, sigmaT, h, mu, phi, eta);
    final double xE = isKnockIn ? getE(spot, rebate, df2, x2, y2, sigmaT, h, mu, eta)
            : getF(spot, rebate, z, sigmaT, h, mu, lambda, eta);
    if (isKnockIn) {
        if (isDown) {
            if (isCall) {
                return strike > h ? xC + xE : xA - xB + xD + xE;
            }
            return strike > h ? xB - xC + xD + xE : xA + xE;
        }
        if (isCall) {
            return strike > h ? xA + xE : xB - xC + xD + xE;
        }
        return strike > h ? xA - xB + xD + xE : xC + xE;
    }
    if (isDown) {
        if (isCall) {
            return strike > h ? xA - xC + xE : xB - xD + xE;
        }
        return strike > h ? xA - xB + xC - xD + xE : xE;
    }
    if (isCall) {
        return strike > h ? xE : xA - xB + xC - xD + xE;
    }
    return strike > h ? xB - xD + xE : xA - xC + xE;
}

From source file:LogFormat.java

/**
 * Creates a new instance.//from w w  w  .  j a  v  a2  s  . co  m
 *
 * @param base  the base.
 * @param baseLabel  the base label (<code>null</code> not permitted).
 * @param powerLabel  the power label (<code>null</code> not permitted).
 * @param showBase  a flag that controls whether or not the base value is
 *                  shown.
 *
 * @since 1.0.10
 */
public LogFormat(double base, String baseLabel, String powerLabel, boolean showBase) {
    if (baseLabel == null) {
        throw new IllegalArgumentException("Null 'baseLabel' argument.");
    }
    if (powerLabel == null) {
        throw new IllegalArgumentException("Null 'powerLabel' argument.");
    }
    this.base = base;
    this.baseLog = Math.log(this.base);
    this.baseLabel = baseLabel;
    this.showBase = showBase;
    this.powerLabel = powerLabel;
}

From source file:io.s4.model.GaussianMixtureModel.java

private void allocateTrainDataStructures(int numComp) {

    components = new GaussianModel[numComp];
    for (int i = 0; i < numComp; i++) {
        this.components[i] = new GaussianModel(numElements, true);

        this.weights = new DenseMatrix64F(numComp, 1);
        CommonOps.set(this.weights, 1.0 / numComp);
        this.logWeights = new DenseMatrix64F(numComp, 1);
        CommonOps.set(this.logWeights, Math.log(1.0 / numComp));
        posteriorSum = new DenseMatrix64F(numComp, 1);
        tmpProbs1 = new DenseMatrix64F(numComp, 1);
        tmpProbs2 = new DenseMatrix64F(numComp, 1);
    }//from w  w  w  . ja v  a2s.c  o  m
}

From source file:edu.asu.ca.kaushik.algorithms.permvector.MTPermutationVector.java

private int permVecLLLBound(int t, int k, int v) {
    double vTotm1 = Math.pow(v, t - 1);
    double vTotm2 = Math.pow(v, t - 2);

    double nume1 = CombinatoricsUtils.factorialDouble(t);
    double nume2 = CombinatoricsUtils.binomialCoefficientDouble((int) vTotm1, t);
    double nume3 = v * (vTotm1 - 1) / (v - 1);
    double nume4 = CombinatoricsUtils.binomialCoefficientDouble((int) vTotm2, t);
    double nume = nume1 * (nume2 - nume3 * nume4);
    double dnom = Math.pow(vTotm1, t);
    double q = 1 - (nume / dnom);

    double d = CombinatoricsUtils.binomialCoefficientDouble(k, t)
            - CombinatoricsUtils.binomialCoefficientDouble(k - t, t) - 1;

    return (int) Math.ceil((1 + Math.log(d + 1)) / Math.log(1 / q));
}