/** * Returns the Jensen Shannon divergence (information radius) between * a and b, defined as the average of the kl divergences from a to b * and from b to a. */ public static double jensenShannonDivergence(double[] a, double[] b) { double[] average = pairwiseAdd(a, b); multiplyInPlace(average, .5); return .5 * klDivergence(a, average) + .5 * klDivergence(b, average); }
/** * Returns the standard deviation of a vector of doubles. Any values which * are NaN or infinite are ignored. If the vector contains fewer than two * values, 1.0 is returned. */ public static double safeStdev(double[] v) { double[] u = filterNaNAndInfinite(v); if (numRows(u) < 2) return 1.0; return stdev(u); }
/** * Returns the largest value in a vector of doubles. Any values which * are NaN or infinite are ignored. If the vector is empty, 0.0 is * returned. */ public static double safeMax(double[] v) { double[] u = filterNaNAndInfinite(v); if (numRows(u) == 0) return 0.0; return max(u); }
/** * Returns the smallest value in a vector of doubles. Any values which * are NaN or infinite are ignored. If the vector is empty, 0.0 is * returned. */ public static double safeMin(double[] v) { double[] u = filterNaNAndInfinite(v); if (numRows(u) == 0) return 0.0; return min(u); }
/** * Returns the sum of an array of doubles. */ public static double sum(double[] a) { return sum(a,0,a.length); }
static double[] recenter(double[] x) { double[] r = new double[x.length]; // double logTotal = Double.NEGATIVE_INFINITY; // for (int i = 0; i < x.length; i++) // logTotal = SloppyMath.logAdd(logTotal, x[i]); double logTotal = ArrayMath.logSum(x); for (int i = 0; i < x.length; i++) { r[i] = x[i] - logTotal; } return r; }
/** * Fills the array with sample from 0 to numArgClasses-1 without replacement. */ public static void sampleWithoutReplacement(int[] array, int numArgClasses) { sampleWithoutReplacement(array, numArgClasses, rand); } /**
/** * Computes the significance level by approximate randomization, using a * default value of 1000 iterations. See documentation for other version * of method. */ public static double sigLevelByApproxRand(double[] A, double[] B) { return sigLevelByApproxRand(A, B, 1000); }
/** * Samples from the distribution over values 0 through d.length given by d. * Assumes that the distribution sums to 1.0. * * @param d the distribution to sample from * @return a value from 0 to d.length */ public static int sampleFromDistribution(double[] d) { return sampleFromDistribution(d, rand); }
public static double sumSquaredError(double[] a) { double mean = mean(a); double result = 0.0; for (double anA : a) { double diff = anA - mean; result += (diff * diff); } return result; }
private static void smoothDistribution(double [] dist) { //perform Laplace smoothing double epsilon = 1e-6; for(int i = 0; i < dist.length; i++) dist[i] += epsilon; ArrayMath.normalize(dist); }
@Override public long longValue() { return (long) doubleValue(); }
private static double[] matrixMultiply(double[][] matrix, double[] vector) { double[] result = new double[matrix.length]; for (int i = 0; i < matrix.length; i++) { result[i] = ArrayMath.dotProduct(matrix[i], vector); } return result; }