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:com.insightml.math.distributions.GaussianDistribution.java

public double klDivergence(final GaussianDistribution other) {
    return (Math.pow(mean - other.mean, 2) + sigmaSquare - other.sigmaSquare) / (2 * other.sigmaSquare)
            + Math.log(Math.sqrt(other.sigmaSquare) / Math.sqrt(sigmaSquare));
}

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

@Override
public Function1D<StandardOptionDataBundle, Double> getPricingFunction(
        final EuropeanStandardBarrierOptionDefinition definition) {
    Validate.notNull(definition, "definition");
    final Barrier barrier = definition.getBarrier();
    final boolean isKnockIn = barrier.getKnockType() == KnockType.IN;
    final boolean isDown = barrier.getBarrierType() == BarrierType.DOWN;
    final double h = barrier.getBarrierLevel();
    final int phi = definition.isCall() ? 1 : -1;
    final double eta = isDown ? 1 : -1;
    return new Function1D<StandardOptionDataBundle, Double>() {

        @SuppressWarnings("synthetic-access")
        @Override// w  ww  .j  a  v  a  2s  . com
        public Double evaluate(final StandardOptionDataBundle data) {
            Validate.notNull(data, "data");
            final boolean isCall = definition.isCall();
            final double s = data.getSpot();
            final double b = data.getCostOfCarry();
            final double t = definition.getTimeToExpiry(data.getDate());
            final double rebate = definition.getRebate();
            final double k = definition.getStrike();
            final double r = data.getInterestRate(t);
            final double sigma = data.getVolatility(t, h); //REVIEW emcleod 19-7-10 will only work if volatility is constant
            final double df1 = Math.exp(t * (b - r));
            final double df2 = Math.exp(-r * t);
            if (CompareUtils.closeEquals(sigma, 0, 1e-16)) {
                return df1 * definition.getPayoffFunction().getPayoff(data, null);
            }
            final double sigmaSq = sigma * sigma;
            final double sigmaT = sigma * Math.sqrt(t);
            final double mu = (b - 0.5 * sigmaSq) / sigmaSq;
            final double lambda = Math.sqrt(mu * mu + 2 * r / sigmaSq);
            final double m1 = sigmaT * (1 + mu);
            final double x1 = Math.log(s / k) / sigmaT + m1;
            final double x2 = Math.log(s / h) / sigmaT + m1;
            final double y1 = Math.log(h * h / s / k) / sigmaT + m1;
            final double y2 = Math.log(h / s) / sigmaT + m1;
            final double z = Math.log(h / s) / sigmaT + lambda * sigmaT;
            final double xA = getA(s, k, df1, df2, x1, sigmaT, phi);
            final double xB = getA(s, k, df1, df2, x2, sigmaT, phi);
            final double xC = getC(s, k, df1, df2, y1, sigmaT, h, mu, phi, eta);
            final double xD = getC(s, k, df1, df2, y2, sigmaT, h, mu, phi, eta);
            final double xE = isKnockIn ? getE(s, rebate, df2, x2, y2, sigmaT, h, mu, eta)
                    : getF(s, rebate, z, sigmaT, h, mu, lambda, eta);
            if (isKnockIn) {
                if (isDown) {
                    if (isCall) {
                        return k > h ? xC + xE : xA - xB + xD + xE;
                    }
                    return k > h ? xB - xC + xD + xE : xA + xE;
                }
                if (isCall) {
                    return k > h ? xA + xE : xB - xC + xD + xE;
                }
                return k > h ? xA - xB + xD + xE : xC + xE;
            }
            if (isDown) {
                if (isCall) {
                    return k > h ? xA - xC + xE : xB - xD + xE;
                }
                return k > h ? xA - xB + xC - xD + xE : xE;
            }
            if (isCall) {
                return k > h ? xE : xA - xB + xC - xD + xE;
            }
            return k > h ? xB - xD + xE : xA - xC + xE;
        }

    };
}

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

/**
 * Return the Black price and its derivatives.
 * @param option The option.//from   w w  w  .  j ava  2  s  .  c  o  m
 * @param data The Black data.
 * @return An array with [0] the price, [1] the derivative with respect to the forward, [2] the derivative with respect to the volatility and 
 * [3] the derivative with respect to the strike.
 */
//TODO Refactor the method call to have the price as output and the derivatives as an array (like getPriceAdjoint2).
public double[] getPriceAdjoint(final EuropeanVanillaOption option, final BlackFunctionData data) {
    /**
     * The array storing the price and derivatives.
     */
    double[] priceAdjoint = new double[4];
    /**
     * The cut-off for small time and strike.
     */
    final double eps = 1E-16;

    final double strike = option.getStrike();
    final double timeToExpiry = option.getTimeToExpiry();
    final double vol = data.getBlackVolatility();
    final double forward = data.getForward();
    final boolean isCall = option.isCall();
    final double discountFactor = data.getDiscountFactor();
    double sqrttheta = Math.sqrt(timeToExpiry);
    double omega = isCall ? 1 : -1;
    // Implementation Note: Forward sweep.
    double volblack = 0, kappa = 0, d1 = 0, d2 = 0;
    double x = 0;
    if (strike < eps || sqrttheta < eps) {
        x = omega * (forward - strike);
        priceAdjoint[0] = (x > 0 ? discountFactor * x : 0.0);
    } else {
        volblack = vol * sqrttheta;
        kappa = Math.log(forward / strike) / volblack - 0.5 * volblack;
        d1 = NORMAL.getCDF(omega * (kappa + volblack));
        d2 = NORMAL.getCDF(omega * kappa);
        priceAdjoint[0] = discountFactor * omega * (forward * d1 - strike * d2);
    }
    // Implementation Note: Backward sweep.
    double pBar = 1.0;
    double forwardBar = 0, strikeBar = 0, volblackBar = 0, volatilityBar = 0;
    if (strike < eps || sqrttheta < eps) {
        forwardBar = (x > 0 ? discountFactor * omega : 0.0);
        strikeBar = (x > 0 ? -discountFactor * omega : 0.0);
    } else {
        double d1Bar = discountFactor * omega * forward * pBar;
        double density1 = NORMAL.getPDF(omega * (kappa + volblack));
        // Implementation Note: kappa_bar = 0; no need to implement it.
        // Methodology Note: kappa_bar is optimal exercise boundary. The
        // derivative at the optimal point is 0.
        forwardBar = discountFactor * omega * d1 * pBar;
        strikeBar = -discountFactor * omega * d2 * pBar;
        volblackBar = density1 * omega * d1Bar;
        volatilityBar = sqrttheta * volblackBar;
    }
    priceAdjoint[1] = forwardBar;
    priceAdjoint[2] = volatilityBar;
    priceAdjoint[3] = strikeBar;
    return priceAdjoint;
}

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

/**
 * Log Gamma Function//from   ww w .j  av  a2  s  .c om
 * 
 * @param Z
 * @return 
 */
public static double logGamma(double Z) {
    double S = 1.0 + 76.18009173 / Z - 86.50532033 / (Z + 1.0) + 24.01409822 / (Z + 2.0)
            - 1.231739516 / (Z + 3.0) + 0.00120858003 / (Z + 4.0) - 0.00000536382 / (Z + 5.0);
    double LG = (Z - 0.5) * Math.log(Z + 4.5) - (Z + 4.5) + Math.log(S * 2.50662827465);

    return LG;
}

From source file:com.qaant.threadModels.TWhaley.java

private void wWhaley() {

    tipoEjercicio = AMERICAN;/*from   w w  w  . ja  va2s.co m*/

    //     q=(tipoContrato==STOCK) ? dividendRate:rate; 
    //q: si es una accion q es el dividendo, si es un futuro q se toma la rate para descontar el valor futr a presente 
    //Se hace este reemplazo para poder usar la misma form en STOCK y FUTURO

    double xx;

    switch (tipoContrato) {
    case STOCK:
        q = dividendRate;
        b = rate;
        break;

    case FUTURES:
        q = rate;
        b = 0;
        break;
    }
    double vlt2 = volatModel * volatModel;
    double VltSqrDayYear = volatModel * sqrDayYear;
    double h = 1 - z; //descuento para valor presente

    double alfa = 2 * rate / vlt2;
    double beta = 2 * (b - q) / vlt2;

    double lambda = (-(beta - 1) + cpFlag * Math.sqrt((beta - 1) * (beta - 1) + 4 * alfa / h)) / 2;

    double eex = Math.exp(-q * dayYear);//descuento por dividendos

    double s1 = strike;
    double zz = 1 / Math.sqrt(2 * Math.PI);
    double zerror = 1;
    do {
        double d1 = (Math.log(s1 / strike) + ((rate - q) + vlt2 / 2) * dayYear) / VltSqrDayYear;
        xx = (1 - eex * new NormalDistribution().cumulativeProbability(cpFlag * d1));

        double corr = s1 / lambda * xx;

        QBlackScholes option = new QBlackScholes(tipoContrato, s1, volatModel, dividendRate, callPut, strike,
                daysToExpiration, rate, 0);

        double mBlackScholes = option.getPrima();
        double rhs = mBlackScholes + cpFlag * corr;

        double lhs = cpFlag * (s1 - strike);
        zerror = lhs - rhs;
        double nd1 = zz * Math.exp(-0.5 * d1 * d1); //standard normal prob?
        double slope = cpFlag * (1 - 1 / lambda) * xx + 1 / lambda * (eex * nd1) * 1 / VltSqrDayYear;
        s1 = s1 - zerror / slope;

    } while (Math.abs(zerror) > 0.000001);

    double a = cpFlag * s1 / lambda * xx;

    switch (callPut) {
    case CALL: //Call
        if (underlyingValue >= s1) {
            prima = underlyingValue - strike;
        } else {
            prima += a * Math.pow((underlyingValue / s1), lambda);
        }
        break;

    case PUT: //Put
        if (underlyingValue <= s1) {
            prima = strike - underlyingValue;
        } else {
            prima += a * Math.pow((underlyingValue / s1), lambda);
        }
        //prima=10.1;
        break;
    }
}

From source file:com.act.biointerpretation.metadata.ProteinMetadataComparator.java

public int score(ProteinMetadata pmd) {
    double out = 0.0;

    // Score enzyme efficiency, will result in picking the highest values as 
    // the dominant consideration, biased on kcatkm
    if (pmd.kcatkm != null) {
        out += (Math.log(pmd.kcatkm)) * 20;
    }/*w ww .j av a2  s  .  co m*/

    if (pmd.specificActivity != null) {
        out += (Math.log(pmd.specificActivity)) * 20;
    }

    //Score modifications
    if (pmd.modifications == null) {
        //No prediction no change
    } else if (pmd.modifications == true) {
        out += -50;
    } else {
        out += 30;
    }

    //Score subunits
    if (pmd.heteroSubunits == null) {
        //No prediction no change
    } else if (pmd.heteroSubunits == true) {
        //If needs multiple subunits, this is potentially problematic
        out += -10;
    } else {
        //Great if there is a clear indication that there are no subunits
        out += 30;
    }

    //Score cloned
    if (pmd.cloned != null) {
        Integer cloned = pmd.cloned.get(host);
        if (cloned == null) {
            //No prediction no change
        } else {
            //Will be positive or negative, scales with organism similarity up to 140 (or -140)
            out += cloned * 20;
        }
    }

    //Score localization
    if (pmd.localization != null) {
        Localization prediction = pmd.localization.get(host);
        if (prediction == Localization.unknown) {
            //No prediction no change
        } else if (prediction != Localization.questionable) {
            out += -20; //Small penalty for ambiguity about the location
        } else if (prediction != localization) {
            out += -30; //larger penalty if the prediction is not where you want it
        } else if (prediction == localization) {
            out += 20; //Otherwise a small bonus if things match up
        } else {
            System.err.println("This should never happen - localization");
        }
    }

    double round = Math.round(out);
    return (int) round;
}

From source file:com.itemanalysis.psychometrics.irt.estimation.ItemParamPriorLogNormal.java

/**
 * First derivative of log density. Only compute part of the log of the density that depends on the parameter.
 *
 * @param p//from  w  w  w. j  av  a 2s  . co  m
 * @return
 */
public double logDensityDeriv1(double p) {
    //Outside limits of distribution density does not change, so derivative is zero
    if (zeroDensity(p))
        return 0.0;
    double value = Math.log(p) - parameters[0] + variance;
    value /= variance * p;
    return -value;
}

From source file:com.opengamma.analytics.financial.model.option.DistributionFromImpliedVolatility.java

private double getD1(final double k, final double sigmaRootT) {
    final double numerator = (Math.log(_f / k) + sigmaRootT * sigmaRootT / 2);
    if (CompareUtils.closeEquals(numerator, 0, 1e-16)) {
        return 0;
    }//from w w w .  ja v  a  2 s.c  o  m
    return numerator / sigmaRootT;
}

From source file:dr.math.distributions.NegativeBinomialDistribution.java

public static double logPdf(double x, double mean, double alpha) {
    if (x < 0)
        return Double.NEGATIVE_INFINITY;
    //        double r = -1 * (mean*mean) / (mean - stdev*stdev);
    //        double p = mean / (stdev*stdev);
    //        return Math.log(Math.pow(1-p,x)) + Math.log(Math.pow(p, r)) + GammaFunction.lnGamma(r+x) - GammaFunction.lnGamma(r) - GammaFunction.lnGamma(x+1);
    double theta = 1.0 / alpha;

    double p = theta / (theta + mean);
    return Math.log(1 - p) * x + Math.log(p) * theta + GammaFunction.lnGamma(theta + x)
            - GammaFunction.lnGamma(theta) - GammaFunction.lnGamma(x + 1);
}

From source file:dr.math.distributions.GammaDistribution.java

public double logPdf(double x) {
    if (TRY_COLT) {
        return Math.log(coltGamma.pdf(x));
    } else {//w w w  .  j  a  v  a2s . c om
        return logPdf(x, shape, scale);
    }
}