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.itemanalysis.psychometrics.irt.estimation.ItemLogLikelihood.java

/**
 * Log-likelihood for an item.//  w  w  w . ja  v  a  2  s.  co m
 *
 * @return log-likelihood
 */
public double logLikelihood(double[] iparam) {
    double ll = 0.0;
    double p = 0.0;

    for (int t = 0; t < nPoints; t++) {
        for (int k = 0; k < model.getNcat(); k++) {
            p = model.probability(latentDistribution.getPointAt(t), iparam, k, model.getScalingConstant());//assumes item uses the default score weights that start at zero.
            p = Math.min(1.0 - EPSILON, Math.max(EPSILON, p)); //always use value strictly between 0 and 1
            ll += r.getRjktAt(k, t) * Math.log(p);
        }
    }

    //add item priors
    ll = model.addPriorsToLogLikelihood(ll, iparam);
    //Return negative because minimizing
    return -ll;
}

From source file:com.opengamma.analytics.math.statistics.distribution.NonCentralChiSquaredDistribution.java

/**
 * {@inheritDoc}/*  ww  w  .ja v  a 2  s . co  m*/
 */
@Override
public double getCDF(final Double x) {
    Validate.notNull(x, "x");
    if (x < 0) {
        return 0.0;
    }

    if ((_dofOverTwo + _lambdaOverTwo) > 1000) {
        return getFraserApproxCDF(x);
    }

    double regGammaStart = 0;
    final double halfX = x / 2.0;
    final double logX = Math.log(halfX);
    try {
        regGammaStart = Gamma.regularizedGammaP(_dofOverTwo + _k, halfX);
    } catch (final org.apache.commons.math.MathException ex) {
        throw new MathException(ex);
    }

    double sum = _pStart * regGammaStart;
    double oldSum = Double.NEGATIVE_INFINITY;
    double p = _pStart;
    double regGamma = regGammaStart;
    double temp;
    int i = _k;

    // first add terms below _k
    while (i > 0 && Math.abs(sum - oldSum) / sum > _eps) {
        i--;
        p *= (i + 1) / _lambdaOverTwo;
        temp = (_dofOverTwo + i) * logX - halfX - Gamma.logGamma(_dofOverTwo + i + 1);
        regGamma += Math.exp(temp);
        oldSum = sum;
        sum += p * regGamma;
    }

    p = _pStart;
    regGamma = regGammaStart;
    oldSum = Double.NEGATIVE_INFINITY;
    i = _k;
    while (Math.abs(sum - oldSum) / sum > _eps) {
        i++;
        p *= _lambdaOverTwo / i;
        temp = (_dofOverTwo + i - 1) * logX - halfX - Gamma.logGamma(_dofOverTwo + i);
        regGamma -= Math.exp(temp);
        oldSum = sum;
        sum += p * regGamma;
    }

    return sum;
}

From source file:com.opengamma.strata.math.impl.statistics.distribution.NonCentralChiSquaredDistribution.java

/**
 * {@inheritDoc}/*from   www  .ja  v a2s  . c  o  m*/
 */
@Override
public double getCDF(Double x) {
    ArgChecker.notNull(x, "x");
    if (x < 0) {
        return 0.0;
    }

    if ((_dofOverTwo + _lambdaOverTwo) > 1000) {
        return getFraserApproxCDF(x);
    }

    double regGammaStart = 0;
    double halfX = x / 2.0;
    double logX = Math.log(halfX);
    try {
        regGammaStart = Gamma.regularizedGammaP(_dofOverTwo + _k, halfX);
    } catch (MaxCountExceededException ex) {
        throw new MathException(ex);
    }

    double sum = _pStart * regGammaStart;
    double oldSum = Double.NEGATIVE_INFINITY;
    double p = _pStart;
    double regGamma = regGammaStart;
    double temp;
    int i = _k;

    // first add terms below _k
    while (i > 0 && Math.abs(sum - oldSum) / sum > _eps) {
        i--;
        p *= (i + 1) / _lambdaOverTwo;
        temp = (_dofOverTwo + i) * logX - halfX - Gamma.logGamma(_dofOverTwo + i + 1);
        regGamma += Math.exp(temp);
        oldSum = sum;
        sum += p * regGamma;
    }

    p = _pStart;
    regGamma = regGammaStart;
    oldSum = Double.NEGATIVE_INFINITY;
    i = _k;
    while (Math.abs(sum - oldSum) / sum > _eps) {
        i++;
        p *= _lambdaOverTwo / i;
        temp = (_dofOverTwo + i - 1) * logX - halfX - Gamma.logGamma(_dofOverTwo + i);
        regGamma -= Math.exp(temp);
        oldSum = sum;
        sum += p * regGamma;
    }

    return sum;
}

From source file:electrical_parameters.Admittance.java

public void calcAll(String type) {
    for (int i = 0; i < this.fv + this.gw; i++) {
        for (int j = 0; j < this.fv + this.gw; j++) {
            if (i == j) {
                this.P.setEntry(i, j, this.cnst * Math.log(this.hx2[i] / this.r_cnd[i]));
            } else {
                this.P.setEntry(i, j,
                        this.cnst * Math.log(this.Dik_mirror.getEntry(i, j) / this.Dik.getEntry(i, j)));
            }/*from   www . j  a v a2  s  .c om*/
        }
    }
    this.P_red = makeRealKronReduction(this.P, this.gw);
    this.C = this.P_red.inverse();
    this.B = C.scalarMultiply(this.omega);
    this.Y = makeComplexMatrix(new Array2DRowRealMatrix(fv, fv), this.B);

    if (type.equals("all")) {
        this.Y_symm = phase2symm(this.Y);
        this.B_symm = Complex2ImagMatrix(this.Y_symm);
        this.C_symm = this.B_symm.scalarMultiply((double) 1 / this.omega);
    }
}

From source file:com.analog.lyric.dimple.factorfunctions.NegativeExpGamma.java

@Override
public final double evalEnergy(Value[] arguments) {
    int index = 0;
    if (!_parametersConstant) {
        _alpha = arguments[index++].getDouble(); // First input is alpha parameter (must be non-negative)
        if (_alpha <= 0)
            return Double.POSITIVE_INFINITY;
        _beta = arguments[index++].getDouble(); // Second input is beta parameter (must be non-negative)
        if (_beta <= 0)
            return Double.POSITIVE_INFINITY;
        _alphaMinusOne = _alpha - 1;//from w w  w  . ja va2 s  .  co m
        _logGammaAlphaMinusAlphaLogBeta = org.apache.commons.math3.special.Gamma.logGamma(_alpha)
                - _alpha * Math.log(_beta);
    }
    final int length = arguments.length;
    final int N = length - index; // Number of non-parameter variables
    double sum = 0;
    for (; index < length; index++) {
        final double x = arguments[index].getDouble(); // Remaining inputs are NegativeExpGamma variables
        sum += x * _alphaMinusOne + Math.exp(-x) * _beta;
    }
    return sum + N * _logGammaAlphaMinusAlphaLogBeta;
}

From source file:hivemall.utils.math.MathUtils.java

/**
 * Returns the inverse erf. This code is based on erfInv() in
 * org.apache.commons.math3.special.Erf.
 * <p>/*ww  w .ja v a 2s. co m*/
 * This implementation is described in the paper: <a
 * href="http://people.maths.ox.ac.uk/gilesm/files/gems_erfinv.pdf">Approximating the erfinv
 * function</a> by Mike Giles, Oxford-Man Institute of Quantitative Finance, which was published
 * in GPU Computing Gems, volume 2, 2010. The source code is available <a
 * href="http://gpucomputing.net/?q=node/1828">here</a>.
 * </p>
 * 
 * @param x the value
 * @return t such that x = erf(t)
 */
public static double inverseErf(final double x) {

    // beware that the logarithm argument must be
    // commputed as (1.0 - x) * (1.0 + x),
    // it must NOT be simplified as 1.0 - x * x as this
    // would induce rounding errors near the boundaries +/-1
    double w = -Math.log((1.0 - x) * (1.0 + x));
    double p;

    if (w < 6.25) {
        w = w - 3.125;
        p = -3.6444120640178196996e-21;
        p = -1.685059138182016589e-19 + p * w;
        p = 1.2858480715256400167e-18 + p * w;
        p = 1.115787767802518096e-17 + p * w;
        p = -1.333171662854620906e-16 + p * w;
        p = 2.0972767875968561637e-17 + p * w;
        p = 6.6376381343583238325e-15 + p * w;
        p = -4.0545662729752068639e-14 + p * w;
        p = -8.1519341976054721522e-14 + p * w;
        p = 2.6335093153082322977e-12 + p * w;
        p = -1.2975133253453532498e-11 + p * w;
        p = -5.4154120542946279317e-11 + p * w;
        p = 1.051212273321532285e-09 + p * w;
        p = -4.1126339803469836976e-09 + p * w;
        p = -2.9070369957882005086e-08 + p * w;
        p = 4.2347877827932403518e-07 + p * w;
        p = -1.3654692000834678645e-06 + p * w;
        p = -1.3882523362786468719e-05 + p * w;
        p = 0.0001867342080340571352 + p * w;
        p = -0.00074070253416626697512 + p * w;
        p = -0.0060336708714301490533 + p * w;
        p = 0.24015818242558961693 + p * w;
        p = 1.6536545626831027356 + p * w;
    } else if (w < 16.0) {
        w = Math.sqrt(w) - 3.25;
        p = 2.2137376921775787049e-09;
        p = 9.0756561938885390979e-08 + p * w;
        p = -2.7517406297064545428e-07 + p * w;
        p = 1.8239629214389227755e-08 + p * w;
        p = 1.5027403968909827627e-06 + p * w;
        p = -4.013867526981545969e-06 + p * w;
        p = 2.9234449089955446044e-06 + p * w;
        p = 1.2475304481671778723e-05 + p * w;
        p = -4.7318229009055733981e-05 + p * w;
        p = 6.8284851459573175448e-05 + p * w;
        p = 2.4031110387097893999e-05 + p * w;
        p = -0.0003550375203628474796 + p * w;
        p = 0.00095328937973738049703 + p * w;
        p = -0.0016882755560235047313 + p * w;
        p = 0.0024914420961078508066 + p * w;
        p = -0.0037512085075692412107 + p * w;
        p = 0.005370914553590063617 + p * w;
        p = 1.0052589676941592334 + p * w;
        p = 3.0838856104922207635 + p * w;
    } else if (!Double.isInfinite(w)) {
        w = Math.sqrt(w) - 5.0;
        p = -2.7109920616438573243e-11;
        p = -2.5556418169965252055e-10 + p * w;
        p = 1.5076572693500548083e-09 + p * w;
        p = -3.7894654401267369937e-09 + p * w;
        p = 7.6157012080783393804e-09 + p * w;
        p = -1.4960026627149240478e-08 + p * w;
        p = 2.9147953450901080826e-08 + p * w;
        p = -6.7711997758452339498e-08 + p * w;
        p = 2.2900482228026654717e-07 + p * w;
        p = -9.9298272942317002539e-07 + p * w;
        p = 4.5260625972231537039e-06 + p * w;
        p = -1.9681778105531670567e-05 + p * w;
        p = 7.5995277030017761139e-05 + p * w;
        p = -0.00021503011930044477347 + p * w;
        p = -0.00013871931833623122026 + p * w;
        p = 1.0103004648645343977 + p * w;
        p = 4.8499064014085844221 + p * w;
    } else {
        // this branch does not appears in the original code, it
        // was added because the previous branch does not handle
        // x = +/-1 correctly. In this case, w is positive infinity
        // and as the first coefficient (-2.71e-11) is negative.
        // Once the first multiplication is done, p becomes negative
        // infinity and remains so throughout the polynomial evaluation.
        // So the branch above incorrectly returns negative infinity
        // instead of the correct positive infinity.
        p = Double.POSITIVE_INFINITY;
    }

    return p * x;
}

From source file:it.unibo.alchemist.modelchecker.AlchemistASMC.java

/**
 * Given the approximation and the confidence, computes an upper bound for
 * the number of runs to execute (see manual).
 * /*from w  w  w  . j a va 2s.  c  om*/
 * @param delta
 *            approximation
 * @param alpha
 *            confidence
 * @return number of runs
 */
public static int computeSampleSizeUB(final double delta, final double alpha) {
    return (int) Math.ceil(LOG_MUL * Math.log(2 / alpha) / (delta * delta));
}

From source file:com.opengamma.analytics.financial.model.option.pricing.fourier.FFTPricer.java

/**
 * Price a European option across a range of strikes using a FFT. The terminal price is assumed to be of the form S = F*exp(x), where F is the forward,
 * and x is a random variable with a known characteristic function.
 * @param forward The forward value of the underlying
 * @param discountFactor /* w  w  w .j  a  v a  2s . c  o  m*/
 * @param t Time to expiry
 * @param isCall true for call 
 * @param ce The Characteristic Exponent (log of characteristic function) of the returns of the underlying
 * @param lowestStrike The lowest strike to return (the actual value will depend on the set up, but is guaranteed to be less than this) 
 * @param highestStrike The highest strike to return (the actual value will depend on the set up, but is guaranteed to be greater than this) 
 * @param minStrikesDisplayed minimum number of strikes returned (actual number depends on set up) 
 * @param limitSigma An estimate of the implied vol used to calculate limits in the numerical routines 
 * @param alpha Regularization factor. Values of 0 or -1 are not allowed. -0.5 is recommended  
 * @param tol Tolerance - smaller values give higher accuracy 
 * @return array of arrays of strikes and prices 
 */
public double[][] price(final double forward, final double discountFactor, final double t, final boolean isCall,
        final MartingaleCharacteristicExponent ce, final double lowestStrike, final double highestStrike,
        final int minStrikesDisplayed, final double limitSigma, final double alpha, final double tol) {

    Validate.notNull(ce, "characteristic exponent");
    Validate.isTrue(tol > 0.0, "need tol > 0");
    Validate.isTrue(alpha != 0.0 && alpha != -1.0, "alpha cannot be -1 or 0");
    Validate.isTrue(highestStrike >= lowestStrike, "need highestStrike >= lowestStrike");
    //   Validate.isTrue(lowestStrike <= forward, "need lowestStrike <= forward");
    //    Validate.isTrue(highestStrike >= forward, "need highestStrike >= forward");
    Validate.isTrue(limitSigma > 0.0, "need limitSigma > 0");

    double kMax;
    final double limitSigmaRootT = limitSigma * Math.sqrt(t);
    final double atm = NORMAL.getCDF(limitSigmaRootT / 2.0);

    if (alpha > 0) {
        kMax = -Math.log((2 * atm - 1) * tol) / alpha;
    } else if (alpha < -1.0) {
        kMax = Math.log((2 * atm - 1) * tol) / (1 + alpha);
    } else {
        kMax = -Math.log(2 * (1 - atm) * tol) * Math.max(-1.0 / alpha, 1 / (1 + alpha));
    }

    final EuropeanCallFourierTransform psi = new EuropeanCallFourierTransform(ce);
    final Function1D<ComplexNumber, ComplexNumber> psiFunction = psi.getFunction(t);
    final double xMax = LIMIT_CALCULATOR.solve(psiFunction, alpha, tol);

    double deltaK;
    if (highestStrike == lowestStrike) {
        deltaK = Math.PI / xMax;
    } else {
        deltaK = Math.min(Math.log(highestStrike / lowestStrike) / (minStrikesDisplayed - 1), Math.PI / xMax);
    }

    final double log2 = Math.log(2);
    final int twoPow = (int) Math.ceil(Math.log(kMax / deltaK) / log2);

    final int n = (int) Math.pow(2, twoPow);
    final double delta = 2 * Math.PI / n / deltaK;
    final int m = (int) (xMax * deltaK * n / 2 / Math.PI);

    final int nLowStrikes = (int) Math.max(0, Math.ceil(Math.log(forward / lowestStrike) / deltaK));
    final int nHighStrikes = (int) Math.max(0, Math.ceil(Math.log(highestStrike / forward) / deltaK));

    return price(forward, discountFactor, t, isCall, ce, nLowStrikes, nHighStrikes, alpha, delta, n, m);
}

From source file:com.compomics.pepshell.view.statistics.RatioComparisonPane.java

private CategoryDataset createRatioDataset(PepshellProtein aPepshellProtein) {
    DefaultCategoryDataset returnset = new DefaultCategoryDataset();

    PepshellProtein protein = referenceExperiment.getProteins()
            .get(referenceExperiment.getProteins().indexOf(aPepshellProtein));
    List<PeptideGroup> sortedCopy = protein.getPeptideGroups().stream().sorted(Comparator.comparing(e -> {
        return e.getRepresentativePeptide().getBeginningProteinMatch();
    })).collect(Collectors.toList());

    for (PeptideGroup aPeptideGroup : sortedCopy) {
        PeptideInterface aPeptide = aPeptideGroup.getRepresentativePeptide();

        if (aPeptide instanceof QuantedPeptide && ((QuantedPeptide) aPeptide).getRatio() != null) {
            Double value = Math.log(((QuantedPeptide) aPeptide).getRatio()) / Math.log(2);
            returnset.addValue(value, (Integer) 1, (Integer) aPeptide.getBeginningProteinMatch());
            //returnset.addValue(((QuantedPeptide) aPeptide).getRatio(), String.valueOf(aPeptide.getBeginningProteinMatch()), String.valueOf(groupcounter));
        }/*from   w  w w .  j ava 2  s  .c om*/

    }

    protein = experimentToCompareTo.getProteins()
            .get(referenceExperiment.getProteins().indexOf(aPepshellProtein));
    sortedCopy = protein.getPeptideGroups().stream()
            .sorted(Comparator
                    .comparing(e -> ((PeptideGroup) e).getRepresentativePeptide().getBeginningProteinMatch()))
            .collect(Collectors.toList());

    for (PeptideGroup aPeptideGroup : sortedCopy) {
        PeptideInterface aPeptide = aPeptideGroup.getRepresentativePeptide();

        if (aPeptide instanceof QuantedPeptide && ((QuantedPeptide) aPeptide).getRatio() != null) {
            Double value = Math.log(((QuantedPeptide) aPeptide).getRatio()) / Math.log(2);
            returnset.addValue(value, (Integer) aPeptide.getBeginningProteinMatch(), (Integer) 2);
            //returnset.addValue(((QuantedPeptide) aPeptide).getRatio(), String.valueOf(aPeptide.getBeginningProteinMatch()), String.valueOf(groupcounter));
        }

    }

    return returnset;
}

From source file:com.almende.eve.algorithms.DAAValueBean.java

/**
 * Generate the random array to represent this value;.
 *
 * @param value/*from ww  w.j  av  a  2s  .  com*/
 *            the value
 * @return the value bean
 */
public DAAValueBean generate(final double value) {
    if (value <= 0.0) {
        Arrays.fill(valueArray, Double.MAX_VALUE);
    } else {
        for (int i = 0; i < width; i++) {
            Double expRand = -Math.log(Math.random()) / value;
            valueArray[i] = expRand;
        }
        // Noise cancelation:
        final Double mean = computeMean();
        final Double goal = 1.0 / value;
        offset = goal / mean;
        for (int i = 0; i < width; i++) {
            valueArray[i] = valueArray[i] * offset;
        }
    }
    return this;
}