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.qaant.optionModels.QBlackScholes.java

@Override
//public void runModel(){
public void run() {

    pModelName = "Black-Scholes QAANT";
    modelNumber = 1;/*from  w w  w. ja  v a2s . co  m*/
    tipoEjercicio = EUROPEAN;
    //

    double 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

    //drift=Math.exp((q-rate)*dayYear);
    double drift = (tipoContrato == 'F') ? z : 1;
    double x = (tipoContrato == 'F') ? 1 : 0;

    double d1 = (Math.log(underlyingNPV / strike) + dayYear * (rate - q + volatModel * volatModel / 2))
            / (volatModel * sqrDayYear);
    double d2 = d1 - volatModel * sqrDayYear;

    double CNDFd1 = new NormalDistribution().cumulativeProbability(d1);
    double CNDFd2 = new NormalDistribution().cumulativeProbability(d2);
    double PDFd1 = new NormalDistribution().density(d1);

    //gamma y vega son iguales para call y put

    gamma = PDFd1 * drift / (underlyingNPV * volatModel * sqrDayYear);
    vega = underlyingNPV * drift * sqrDayYear * PDFd1 / 100;

    switch (callPut) {

    case CALL:
        prima = underlyingValue * Math.exp(-q * dayYear) * CNDFd1 - z * strike * CNDFd2;
        delta = Math.exp(-q * dayYear) * CNDFd1;
        theta = (-(underlyingNPV * drift * volatModel * PDFd1 / (2 * sqrDayYear)) - strike * 1 * rate * CNDFd2)
                / (365);

        rho = z * dayYear * (strike * CNDFd2 - x * underlyingNPV * CNDFd1) / 100;
        break;

    case PUT:
        double CNDF_d1 = new NormalDistribution().cumulativeProbability(-d1);
        double CNDF_d2 = new NormalDistribution().cumulativeProbability(-d2);

        prima = -underlyingValue * Math.exp(-q * dayYear) * CNDF_d1 + z * strike * CNDF_d2;
        delta = Math.exp(-q * dayYear) * (CNDFd1 - 1);
        theta = (-(underlyingNPV * drift * volatModel * PDFd1 / (2 * sqrDayYear)) + strike * 1 * rate * CNDF_d2)
                / 365;
        rho = -z * dayYear * (strike * CNDF_d2 - x * underlyingNPV * CNDF_d1) / 100;
        break;

    default:
        prima = delta = gamma = theta = rho = 0;
        break;

    }//end switch

}

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

public double getVolatility(final double forward, final double strike, final SVIFormulaData data) {
    Validate.isTrue(forward > 0, "Need forward >= 0");
    Validate.isTrue(strike > 0, "Need strike >= 0");
    Validate.notNull(data, "null SVI parameters");

    final double kappa = Math.log(strike / forward);
    return getVolatility(kappa, data);
}

From source file:net.bioclipse.brunn.ui.editors.plateEditor.model.HillCurveIC50Calculator.java

public double calculateIC50(double[] conc, double[] si) {

    LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();

    CurveFitter fitter = new CurveFitter(optimizer);

    for (int i = 0; i < si.length; i++) {
        fitter.addObservedPoint(conc[i], si[i] / 100);
        System.out.println("Added point: " + conc[i] + "; " + si[i] / 100);
    }// w w  w  .  j a  v a2s . co  m

    ParametricRealFunction function = new ParametricRealFunction() {

        @Override
        public double value(double c, double[] paramaters) throws FunctionEvaluationException {

            double d = paramaters[0];
            double n = paramaters[1];
            double c_pow_n = Math.pow(c, n);

            return c_pow_n / (c_pow_n + Math.pow(d, n));
        }

        @Override
        public double[] gradient(double c, double[] paramaters) throws FunctionEvaluationException {

            double d = paramaters[0];
            double n = paramaters[1];
            double c_pow_n = Math.pow(c, n);
            double d_pow_n = Math.pow(d, n);

            double ddd = -n * c_pow_n * Math.pow(d, n - 1) / Math.pow(c_pow_n + d_pow_n, 2);

            double ddn = (c_pow_n * d_pow_n * (Math.log(c) - Math.log(d))) / Math.pow(c_pow_n + d_pow_n, 2);

            return new double[] { ddd, ddn };
        }

    };

    double[] params = null;
    try {
        params = fitter.fit(function, new double[] { 1, 1 });
    } catch (Exception e) {
        Logger.getLogger(HillCurveIC50Calculator.class)
                .debug("Caught Exception while fitting dose response curve", e);
        return Double.NaN;
    }

    double d = params[0];
    double n = params[1];

    System.out.println("d=" + d);
    System.out.println("n=" + n);

    return Math.pow(-(0.5 - 1) * Math.pow(d, -n) / 0.5, -1 / n);
}

From source file:com.sangupta.passcode.HashStream.java

public int generate(int n, int base, boolean inner) {
    int value = n;
    int k = (int) Math.ceil(Math.log(n) / Math.log(base));
    int r = (int) Math.pow(base, k) - n;

    while (value >= n) {
        List<Integer> chunk = this.shift(base, k);

        if (chunk == null) {
            return inner ? n : 0;
        }/*from   www  .  ja  va2 s.c  o  m*/

        value = this.evaluate(chunk, base);
        if (value >= n) {
            if (r == 1d) {
                continue;
            }

            this.push(r, value - n);
            value = this.generate(n, r, true);
        }
    }

    return value;
}

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

private void opcionConVida() {

    tipoEjercicio = EUROPEAN;/* w ww  .  j a va 2s.  c o  m*/

    double 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

    //drift=Math.exp((q-rate)*dayYear);
    double drift = (tipoContrato == 'F') ? z : 1;
    double x = (tipoContrato == 'F') ? 1 : 0;

    double d1 = (Math.log(underlyingNPV / strike) + dayYear * (rate - q + volatModel * volatModel / 2))
            / (volatModel * sqrDayYear);
    double d2 = d1 - volatModel * sqrDayYear;

    double CNDFd1 = new NormalDistribution().cumulativeProbability(d1);
    double CNDFd2 = new NormalDistribution().cumulativeProbability(d2);
    double PDFd1 = new NormalDistribution().density(d1);

    //gamma y vega son iguales para call y put

    gamma = PDFd1 * drift / (underlyingNPV * volatModel * sqrDayYear);
    vega = underlyingNPV * drift * sqrDayYear * PDFd1 / 100;

    switch (callPut) {

    case CALL:
        prima = underlyingValue * Math.exp(-q * dayYear) * CNDFd1 - z * strike * CNDFd2;
        delta = Math.exp(-q * dayYear) * CNDFd1;
        theta = (-(underlyingNPV * drift * volatModel * PDFd1 / (2 * sqrDayYear)) - strike * 1 * rate * CNDFd2)
                / (365);

        rho = z * dayYear * (strike * CNDFd2 - x * underlyingNPV * CNDFd1) / 100;
        break;

    case PUT:
        double CNDF_d1 = new NormalDistribution().cumulativeProbability(-d1);
        double CNDF_d2 = new NormalDistribution().cumulativeProbability(-d2);

        prima = -underlyingValue * Math.exp(-q * dayYear) * CNDF_d1 + z * strike * CNDF_d2;
        delta = Math.exp(-q * dayYear) * (CNDFd1 - 1);
        theta = (-(underlyingNPV * drift * volatModel * PDFd1 / (2 * sqrDayYear)) + strike * 1 * rate * CNDF_d2)
                / 365;
        rho = -z * dayYear * (strike * CNDF_d2 - x * underlyingNPV * CNDF_d1) / 100;
        break;

    default:
        prima = delta = gamma = theta = rho = 0;
        break;

    }//end switch

}

From source file:com.analog.lyric.chimple.monkeys.ChimpBeta.java

@Override
public double calculateLogLikelihood(Object result, Object[] parameters) {
    /*//  ww  w  .ja  va 2 s . co m
     *     alphas=[alpha beta];
    value=[value 1-value];
    %fast computation without normalization
    %result=-sum((alphas-1).*log(value));
            
    %exact computation, but the constant term should hopefully be useless
            
    result=sum(gammaln(alphas))-gammaln(sum(alphas))-sum((alphas-1).*log(value));
            
     */
    double value = (Double) result;
    double alpha = (Double) parameters[0];
    double beta = (Double) parameters[1];

    //double alphaBetaSum = alpha+beta;

    double retval = Gamma.logGamma(alpha);
    retval += Gamma.logGamma(beta);
    retval -= Gamma.logGamma(alpha + beta);
    retval -= (alpha - 1) * Math.log(value);
    retval -= (beta - 1) * Math.log(1 - value);

    return retval;
}

From source file:com.mapr.synth.ForeignKeySamplerTest.java

private void check(int n, DoubleFunction distribution, Sampler<JsonNode> s) {
    int[] counts = new int[n];
    for (int i = 0; i < 100000; i++) {
        counts[s.sample().asInt()]++;//w w  w  .j  a  v a2 s.co m
    }

    double sum = 0;
    double[] p = new double[n];
    for (int i = 0; i < n; i++) {
        p[i] = distribution.apply(i);
        sum += p[i];
    }

    double z1 = 0;
    double z2 = 0;
    for (int i = 0; i < n; i++) {
        z1 += (p[i] - counts[i] / 100000.0) * Math.log(p[i] / sum);
        double expected = p[i] * 100000;
        double deviation = expected - counts[i];
        z2 += deviation * deviation / expected;
    }
    System.out.printf("%.4f %.4f\n", z1, z2);
}

From source file:ch.algotrader.option.OptionUtil.java

/**
 * Gets the fair-price of a {@link Option}.
 *//*from  w  w w .  ja va2 s. c  om*/
public static double getOptionPrice(double underlyingSpot, double strike, double volatility, double years,
        double intrest, double dividend, OptionType type) {

    if (years <= 0) {
        return getIntrinsicValue(underlyingSpot, strike, type);
    }

    double costOfCarry = intrest - dividend;
    double d1 = (Math.log(underlyingSpot / strike) + (costOfCarry + volatility * volatility / 2.0) * years)
            / (volatility * Math.sqrt(years));
    double d2 = d1 - volatility * Math.sqrt(years);
    double term1 = underlyingSpot * Math.exp((costOfCarry - intrest) * years);
    double term2 = strike * Math.exp(-intrest * years);

    double result = 0.0;
    if (OptionType.CALL.equals(type)) {
        result = term1 * Gaussian.Phi(d1) - term2 * Gaussian.Phi(d2);
    } else {
        result = term2 * Gaussian.Phi(-d2) - term1 * Gaussian.Phi(-d1);
    }

    return result;
}

From source file:gedi.atac.DirichletTest.java

public static double logProbability(double[] alpha, double[] p) {
    double re = 0;
    double asum = 0;
    double gsum = 0;
    for (int i = 0; i < p.length; i++) {
        re += Math.log(p[i]) * (alpha[i] - 1);
        asum += alpha[i];/*from   w w w.java 2s. co  m*/
        gsum += Gamma.logGamma(alpha[i]);
    }
    return re + Gamma.logGamma(asum) - gsum;
}