List of usage examples for java.lang Math log
@HotSpotIntrinsicCandidate public static double log(double a)
From source file:bide.math.NormalDistribution.java
/** * Normal distribution truncated at both side * //from w w w .j a va2s.c o m * @param x * @return */ public static double[] logPdfBT(double[] x, double m, double sd, double lLim, double uLim) { int n = x.length; if (n == 0) { double[] dnorm = { 0 }; return dnorm; } else { double[] dnorm = new double[n]; try { double limit = Math.log(NormalDistribution.cdf(uLim, m, sd) - NormalDistribution.cdf(lLim, m, sd)); double d; for (int i = 0; i < n; i++) { d = x[i]; if (d > lLim && d < uLim) { dnorm[i] = NormalDistribution.logPdf(d, m, sd) - limit; } else { dnorm[i] = 0; } } } catch (Exception e) { e.printStackTrace(); } return dnorm; } }
From source file:com.insightml.utils.ui.UiUtils.java
public static String humanReadableByteCount(final long bytes, final boolean si) { final int unit = si ? 1000 : 1024; if (bytes < unit) { return bytes + " B"; }/*from w w w . j ava2s .c om*/ final int exp = (int) (Math.log(bytes) / Math.log(unit)); final String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i"); return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); }
From source file:com.itemanalysis.psychometrics.polycor.PolychoricLogLikelihoodML.java
/** * Loglikelihood function to be minimized. * * Parameters are stored such that x[0] is the correlation * x[1:validNrow] are the row thresholds * x[validNrow:(1+validNrow+validNcol] are the column thresholds * * @param x/*from www . j a va 2 s .c o m*/ * @return */ public double value(double[] x) { double logLike = 0.0; if (x[0] > 1.0) { rho = maxcor; } else if (x[0] < -1.0) { rho = -maxcor; } else { rho = x[0]; } for (int i = 0; i < validNrow; i++) { alpha[i] = x[i + 1]; } alpha[validNrow] = 10; for (int i = 0; i < validNcol; i++) { beta[i] = x[i + 1 + validNrow]; } beta[validNcol] = 10; double[][] Pd = new double[nrow][ncol]; double[][] Pc = new double[nrow][ncol]; for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { Pd[i][j] = bvnorm.cumulativeProbability(alpha[i], beta[j], rho); Pc[i][j] = Pd[i][j]; if (i > 0) { Pd[i][j] -= Pc[i - 1][j]; } if (j > 0) { Pd[i][j] -= Pc[i][j - 1]; } if (i > 0 && j > 0) { Pd[i][j] += Pc[i - 1][j - 1]; } logLike += data[i][j] * Math.log(Pd[i][j]); } } return -logLike; }
From source file:LogFormat.java
/** * Calculates the log of a given value./*from w ww . ja v a 2s. c o m*/ * * @param value the value. * * @return The log of the value. */ private double calculateLog(double value) { return Math.log(value) / this.baseLog; }
From source file:com.datumbox.framework.core.statistics.distributions.ContinuousDistributions.java
/** * Calculates the probability from -INF to X under Student's Distribution * Ported to PHP from Javascript implementation found at http://www.math.ucla.edu/~tom/distributions/tDist.html * /* w w w. j av a2 s .com*/ * @param x * @param df * @return */ public static double studentsCdf(double x, int df) { if (df <= 0) { throw new IllegalArgumentException("The degrees of freedom need to be positive."); } double A = df / 2.0; double S = A + 0.5; double Z = df / (df + x * x); double BT = Math.exp(logGamma(S) - logGamma(0.5) - logGamma(A) + A * Math.log(Z) + 0.5 * Math.log(1.0 - Z)); double betacdf; if (Z < (A + 1.0) / (S + 2.0)) { betacdf = BT * betinc(Z, A, 0.5); } else { betacdf = 1 - BT * betinc(1.0 - Z, 0.5, A); } double tcdf; if (x < 0) { tcdf = betacdf / 2.0; } else { tcdf = 1.0 - betacdf / 2.0; } return tcdf; }
From source file:com.opengamma.analytics.financial.model.finitedifference.applications.TwoStateMarkovChainDensity.java
private Function1D<Double, Double> getInitialCondition(final ForwardCurve forward, final double initialProb) { //using a log-normal distribution with a very small Standard deviation as a proxy for a Dirac delta return new Function1D<Double, Double>() { private final double _volRootTOffset = 0.01; @Override/*w w w .j a v a 2 s .c o m*/ public Double evaluate(final Double s) { if (s <= 0 || initialProb == 0) { return 0.0; } final double x = Math.log(s / forward.getSpot()); final NormalDistribution dist = new NormalDistribution(0, _volRootTOffset); return initialProb * dist.getPDF(x) / s; } }; }
From source file:com.cloudera.oryx.kmeans.common.ClusterValidityStatistics.java
/** * Calculates the normalized variation-of-information for the contingency contingencyMatrix. * * @return the normalized variation-of-information for the contingency contingencyMatrix *//*from w w w. jav a 2 s . c om*/ private static double normVarInformation(RealMatrix contingencyMatrix, final double[] rowSums, final double[] colSums, final double n) { double den = n * (entropy(rowSums, n) + entropy(colSums, n)); if (den == 0) { return Double.NaN; } double num = contingencyMatrix.walkInOptimizedOrder(new RealMatrixPreservingVisitor() { private double sum = 0.0; @Override public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { sum = 0.0; } @Override public void visit(int row, int column, double value) { if (value > 0.0) { sum += value * (Math.log(value * n) - Math.log(rowSums[row]) - Math.log(colSums[column])); } } @Override public double end() { return sum; } }); return 1.0 + 2.0 * (num / den); }
From source file:com.datumbox.framework.statistics.distributions.ContinuousDistributions.java
/** * Calculates the probability from -INF to X under Student's Distribution * Ported to PHP from Javascript implementation found at http://www.math.ucla.edu/~tom/distributions/tDist.html * /*from w w w.j av a 2 s .com*/ * @param x * @param df * @return * @throws IllegalArgumentException */ public static double StudentsCdf(double x, int df) throws IllegalArgumentException { if (df <= 0) { throw new IllegalArgumentException(); } double tcdf = 0.0; double A = df / 2.0; double S = A + 0.5; double Z = df / (df + x * x); double BT = Math.exp(LogGamma(S) - LogGamma(0.5) - LogGamma(A) + A * Math.log(Z) + 0.5 * Math.log(1.0 - Z)); double betacdf = 0.0; if (Z < (A + 1.0) / (S + 2.0)) { betacdf = BT * Betinc(Z, A, 0.5); } else { betacdf = 1 - BT * Betinc(1.0 - Z, 0.5, A); } if (x < 0) { tcdf = betacdf / 2.0; } else { tcdf = 1.0 - betacdf / 2.0; } return tcdf; }
From source file:change_point_detection.CpdDP.java
public int/*estimated change point*/ detectChange() throws Exception { int estimatedChangePoint = -1; int N = this.dynamicWindow.size(); this.cushion = Math.max(100, (int) Math.floor(Math.pow(N, gamma))); //mean conf. should not fall below 0.3 double preChangeMean, postChangeMean, wholeMean; wholeMean = calculateMean(0, N - 1); if ((N > (2 * this.cushion) && wholeMean <= 0.3) || this.dynamicWindow.size() > this.dim) return N - 1; double threshold = -Math.log(this.sensitivity); double w = 0; int kAtMaxW = -1; for (int k = this.cushion; k <= N - this.cushion; k++) { double skn = 0; int prevN = this.trackN[k]; preChangeMean = prevN == -1 ? calculateMean(0, k - 1) : this.cusumElementArr[prevN][k].getPreChangeMean(); postChangeMean = calculateMean(k, N - 1); if (postChangeMean <= (1 - this.secAlgMarSlack) * preChangeMean) {//signal from secondary if (prevN == -1) { //calculate from scratch /* estimate pre and post change parameters */ double alphaPreChange = calcBetaDistAlpha(0, k - 1, preChangeMean); double betaPreChange = calculateBetaDistBeta(alphaPreChange, 0, k - 1, preChangeMean); double alphaPostChange = calcBetaDistAlpha(k, N - 1, postChangeMean); double betaPostChange = calculateBetaDistBeta(alphaPostChange, k, N - 1, postChangeMean); BetaDistributionImpl preBetaDist = new BetaDistributionImpl(alphaPreChange, betaPreChange); BetaDistributionImpl postBetaDist = new BetaDistributionImpl(alphaPostChange, betaPostChange); for (int i = k; i < N; i++) { try { skn += Math.log(postBetaDist.density(this.dynamicWindow.get(i).doubleValue()) / preBetaDist.density(this.dynamicWindow.get(i).doubleValue())); } catch (Exception e) { e.printStackTrace(); System.out.println("continuing..."); skn = 0;/*from www. j a va 2 s . co m*/ break; } } this.cusumElementArr[N - 1][k] = new CusumElement(preBetaDist, postBetaDist, preChangeMean, skn); } else {//warning and calculate recursively double alphaPostChange2 = calcBetaDistAlpha(k, N - 1, postChangeMean); double betaPostChange2 = calculateBetaDistBeta(alphaPostChange2, k, N - 1, postChangeMean); BetaDistributionImpl postBetaDist2 = new BetaDistributionImpl(alphaPostChange2, betaPostChange2); skn += this.cusumElementArr[prevN][k].getCusumScore(); for (int i = prevN + 1; i < N; i++) { try { skn += Math.log(postBetaDist2.density(this.dynamicWindow.get(i).doubleValue()) / this.cusumElementArr[prevN][k].getPreChangeDist() .density(this.dynamicWindow.get(i).doubleValue())); } catch (Exception e) { e.printStackTrace(); System.out.println("continuing..."); skn = 0; break; } } this.cusumElementArr[N - 1][k] = new CusumElement( this.cusumElementArr[prevN][k].getPreChangeDist(), postBetaDist2, preChangeMean, skn); } this.trackN[k] = N - 1; } if (skn > w) { w = skn; kAtMaxW = k; } } if (w >= threshold && kAtMaxW != -1) { System.out.println("\nChangePoint Found!"); estimatedChangePoint = kAtMaxW; System.out.println("Estimated change point is " + estimatedChangePoint + ", detected at point: " + N); } //force change point if confidence falls down terribly if (estimatedChangePoint == -1 && N >= 100 && wholeMean < 0.3) estimatedChangePoint = N - 1; return estimatedChangePoint; }