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:gedi.util.math.stat.testing.DirichletLikelihoodRatioTest.java

public static double logProbability(NumericArray alpha1, double[] p) {
    double re = 0;
    double asum = 0;
    double gsum = 0;
    for (int i = 0; i < p.length; i++) {
        re += Math.log(p[i]) * (alpha1.getDouble(i));
        asum += alpha1.getDouble(i) + 1;
        gsum += Gamma.logGamma(alpha1.getDouble(i) + 1);
    }/*w  w w .  java2 s.c o m*/
    return re + Gamma.logGamma(asum) - gsum;
}

From source file:net.anthonypoon.ngram.rollingregression.RollingRegressionReducer.java

@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
        throws IOException, InterruptedException {
    TreeMap<String, Double> currElement = new TreeMap();
    boolean pastThreshold = false;
    for (Text val : values) {
        String[] strArray = val.toString().split("\t");
        if (Double.valueOf(strArray[1]) > threshold) {
            pastThreshold = true;/*from  w w  w .  j a  va 2s.  c o m*/
        }
        currElement.put(strArray[0], Math.log(Double.valueOf(strArray[1])));
    }
    if (pastThreshold) {
        for (Integer i = 0; i <= upbound - lowbound; i++) {
            if (!currElement.containsKey(String.valueOf(lowbound + i))) {
                if (i != 0) {
                    currElement.put(String.valueOf(lowbound + i),
                            currElement.get(String.valueOf(lowbound + i - 1)));
                } else {
                    currElement.put(String.valueOf(lowbound + i), 0.0);
                }
            }

        }
        TreeMap<String, Double> result = new TreeMap();
        for (Integer i = 0 + range; i <= upbound - lowbound - range; i++) {
            SimpleRegression regression = new SimpleRegression();
            for (Integer l = -range; l <= range; l++) {
                regression.addData(l.doubleValue(), currElement.get(String.valueOf(i + lowbound + l)));
            }
            if (!Double.isNaN(regression.getSlope())) {
                if (!positiveOnly || regression.getSlope() > 0) {
                    result.put(String.valueOf(lowbound + i), regression.getSlope());
                }
            }
        }
        for (Map.Entry<String, Double> pair : result.entrySet()) {
            context.write(key, new Text(pair.getKey() + "\t" + String.format("%.5f", pair.getValue())));
        }
    }
}

From source file:com.opengamma.analytics.financial.interestrate.PeriodicInterestRate.java

@Override
public ContinuousInterestRate toContinuous() {
    return new ContinuousInterestRate(Math.log(_oneYearValue));
}

From source file:Methods.CalculusNewtonRaphson.java

public static void newtonRaphson2(double xold, double decPoint) {//method used calculate root point acording the paramethers that enter the method and store the data in a global paramether linked list

    double xnew, fxold, fxnew, fdashxold, diff;
    int iteration;
    iteration = 0;//from  w  w w.  j  av a 2s.co m
    xNewLinkedList.head = null;//Clearing the linked list before using it
    xLinkedList.head = null;

    do {
        iteration += 1;
        fxold = (Math.log(Math.abs(xold + 1.0))) + 1.0;
        fdashxold = (1.0 / (xold + 1.0));
        xnew = xold - (fxold / fdashxold);
        fxnew = xnew - Math.pow(xnew, 2.0);
        System.out.println("Approx for iteration{}" + iteration + " is " + xnew);

        if (iteration == 1) {//Block used to insert data in the linked list
            xNewLinkedList.addFirst(xold, fxold, iteration);
            xLinkedList.addFirst(xnew, fxnew, iteration);
        } else {
            xNewLinkedList.addMid(xold, fxold, iteration, iteration - 1);
            xLinkedList.addMid(xnew, fxnew, iteration, iteration - 1);
        }

        diff = Math.abs(xnew - xold);
        xold = xnew;
    } while (diff > decPoint);
    xNewLinkedList.addMid(xnew, 0.0, iteration + 1, iteration);//Block used to insert data in the linked list
    xLinkedList.addMid(xnew, 0.0, iteration + 1, iteration);
    System.out.println("root to six decimal places is " + xnew);

}

From source file:mastodon.algorithms.MHBisectionAlgorithm.java

protected void initialize() {
    stub = "MH Bi.";

    pruningFreq = new HashMap<Integer, Integer>();
    for (int i = 0; i < bts.getTaxaCount(); i++) {
        pruningFreq.put(i, 0);//from w  w w  .  jav a  2  s .co m
    }

    kLeft = minPrunedSpeciesCount;
    kRight = maxPrunedSpeciesCount;
    currPrunedSpeciesCount = (int) ((kRight + kLeft) / 2);

    int numberOfSteps = (int) ((Math.log(maxPrunedSpeciesCount - minPrunedSpeciesCount)) / Math.log(2));
    stepIterations = totalIterations / numberOfSteps;
    totalIterations = numberOfSteps * stepIterations; //adjust for rounding

    maxScore = new double[2];

    iterationCounter = 0;
}

From source file:com.itemanalysis.psychometrics.kernel.LikelihoodCrossValidation.java

public double value(double h) {
    double sum = 0.0;
    for (int i = 0; i < n; i++) {
        sum += Math.log(densityAt(i, h));
    }// w ww.jav  a2s.c  om
    return sum /= (double) n;
}

From source file:Main.java

/**
 * Returns the natural <code>log</code> of the <a
 * href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial
 * Coefficient</a>, "<code>n choose k</code>", the number of
 * <code>k</code>-element subsets that can be selected from an
 * <code>n</code>-element set.
 * <p>/*www. j  a  v  a  2  s.c o  m*/
 * <Strong>Preconditions</strong>:
 * <ul>
 * <li> <code>0 <= k <= n </code> (otherwise
 * <code>IllegalArgumentException</code> is thrown)</li>
 * </ul></p>
 * 
 * @param n the size of the set
 * @param k the size of the subsets to be counted
 * @return <code>n choose k</code>
 * @throws IllegalArgumentException if preconditions are not met.
 */
public static double binomialCoefficientLog(final int n, final int k) {
    if (n < k) {
        throw new IllegalArgumentException("must have n >= k for binomial coefficient (n,k)");
    }
    if (n < 0) {
        throw new IllegalArgumentException("must have n >= 0 for binomial coefficient (n,k)");
    }
    if ((n == k) || (k == 0)) {
        return 0;
    }
    if ((k == 1) || (k == n - 1)) {
        return Math.log((double) n);
    }
    double logSum = 0;

    // n!/k!
    for (int i = k + 1; i <= n; i++) {
        logSum += Math.log((double) i);
    }

    // divide by (n-k)!
    for (int i = 2; i <= n - k; i++) {
        logSum -= Math.log((double) i);
    }

    return logSum;
}

From source file:com.mapr.synth.samplers.SequenceSampler.java

@Override
public JsonNode sample() {
    Preconditions.checkState(array != null || base != null, "Need to specify either base or array");
    ArrayNode r = nodeFactory.arrayNode();
    if (base != null) {
        int n = (int) Math.floor(-averageLength * Math.log(gen.nextDouble()));
        for (int i = 0; i < n; i++) {
            r.add(base.sample());/*from   w w  w .  j  ava 2  s.c o  m*/
        }
    } else {
        for (FieldSampler fieldSampler : array) {
            r.add(fieldSampler.sample());
        }
    }
    return r;
}

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

@Override
public Function1D<HullWhiteTwoFactorDataBundle, Double> getDiscountBondFunction(final ZonedDateTime time,
        final ZonedDateTime maturity) {
    Validate.notNull(time, "time");
    Validate.notNull(maturity, "maturity");
    return new Function1D<HullWhiteTwoFactorDataBundle, Double>() {

        @Override/*from www  .j a va  2  s .c o m*/
        public Double evaluate(final HullWhiteTwoFactorDataBundle data) {
            Validate.notNull(data, "data");
            final double t1 = 0;
            final double t2 = DateUtils.getDifferenceInYears(data.getDate(), time);
            final double t3 = DateUtils.getDifferenceInYears(data.getDate(), maturity);
            final double r2 = data.getShortRate(t2);
            final double r3 = data.getShortRate(t3);
            final double p2 = Math.exp(-r2 * t2);
            final double p3 = Math.exp(-r3 * t3);
            final double alpha = data.getFirstSpeed();
            final double beta = data.getSecondSpeed();
            final double sigma1 = data.getShortRateVolatility(t1);
            final double sigma2 = data.getSecondVolatility(t1);
            final double rho = data.getCorrelation();
            final double eta = getEta(t1, t2, t3, alpha, beta, sigma1, sigma2, rho);
            final double b = getB(t3 - t2, alpha);
            final double c = getC(t3 - t2, alpha, beta);
            final double u = data.getMeanReversionLevel();
            final double f = data.getForwardRate(t1);
            final double lnA = Math.log(p3 / p2) + b * f - eta;
            return Math.exp(lnA - r2 * b - u * c);
        }

    };
}

From source file:com.itemanalysis.psychometrics.factoranalysis.GeominCriteria.java

public void computeValues(RealMatrix L) {
    //        vgQ.geomin <- function(L, delta=.01){
    //            k <- ncol(L)
    //            p <- nrow(L)
    //            L2 <- L^2 + delta
    //            pro <- exp(rowSums(log(L2))/k)
    //            list(Gq=(2/k)*(L/L2)*matrix(rep(pro,k),p),
    //                    f= sum(pro),
    //                    Method="Geomin")
    //        }//from   w ww. ja  v  a 2 s.  c o  m

    int p = L.getRowDimension();
    int k = L.getColumnDimension();
    final RealMatrix L2 = MatrixUtils.multiplyElements(L, L).scalarAdd(delta);
    final double[] rowSums = new double[p];

    L2.walkInRowOrder(new DefaultRealMatrixPreservingVisitor() {
        @Override
        public void visit(int row, int column, double value) {
            rowSums[row] += Math.log(value);
        }
    });

    double sum = 0.0;
    for (int i = 0; i < p; i++) {
        rowSums[i] = Math.exp(rowSums[i] / (double) k);
        sum += rowSums[i];
    }
    functionValue = sum;

    final RealMatrix M = new Array2DRowRealMatrix(p, k);
    M.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            return rowSums[row];
        }
    });

    final double c = (2.0 / (double) k);
    gradient = L.copy();
    gradient.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            return c * (value / L2.getEntry(row, column)) * M.getEntry(row, column);
        }
    });

}