/** * Returns * <code>KullbackLeibler(dist, reference) + KullbackLeibler(reference, dist)</code> * . * * @see #KullbackLeibler(double[], double[]) */ public static double symmetricKL(double[] dist, double[] reference) { return KullbackLeibler(dist, reference) + KullbackLeibler(reference, dist); }
/** * Returns the squared difference between the two specified distributions, * which must have the same number of elements. This is defined as the sum * over all <code>i</code> of the square of * <code>(dist[i] - reference[i])</code>. */ public static double squaredError(double[] dist, double[] reference) { double error = 0; checkLengths(dist, reference); for (int i = 0; i < dist.length; i++) { double difference = dist[i] - reference[i]; error += difference * difference; } return error; }
ArrayList<double[]> locations = new ArrayList<double[]>(elements.values()); double[] mean = DiscreteDistribution.mean(locations); max_movement = Math.max(max_movement, Math.sqrt(DiscreteDistribution.squaredError(centroid, mean))); new_centroids.add(mean);
/** * Returns the mean of the specified <code>Collection</code> of * distributions, which are assumed to be normalized arrays of * <code>double</code> values. * @see #mean(double[][]) * @param distributions the distributions whose mean is to be calculated * @return the mean of the distributions */ public static double[] mean(Collection<double[]> distributions) { if (distributions.isEmpty()) throw new IllegalArgumentException("Distribution collection must be non-empty"); Iterator<double[]> iter = distributions.iterator(); double[] first = iter.next(); double[][] d_array = new double[distributions.size()][first.length]; d_array[0] = first; for (int i = 1; i < d_array.length; i++) d_array[i] = iter.next(); return mean(d_array); }
double distance = DiscreteDistribution.squaredError(location, closest); double dist_cur = DiscreteDistribution.squaredError(location, centroid); if (dist_cur < distance)
elements.values()); double[] mean = DiscreteDistribution.mean(locations); max_movement = Math.max(max_movement, Math.sqrt( DiscreteDistribution.squaredError(centroid, mean))); new_centroids.add(mean);
/** * Returns the mean of the specified <code>Collection</code> of * distributions, which are assumed to be normalized arrays of * <code>double</code> values. * @see #mean(double[][]) */ public static double[] mean(Collection<double[]> distributions) { if (distributions.isEmpty()) throw new IllegalArgumentException("Distribution collection must be non-empty"); Iterator<double[]> iter = distributions.iterator(); double[] first = iter.next(); double[][] d_array = new double[distributions.size()][first.length]; d_array[0] = first; for (int i = 1; i < d_array.length; i++) d_array[i] = iter.next(); return mean(d_array); }
double distance = DiscreteDistribution.squaredError(location, closest); double dist_cur = DiscreteDistribution.squaredError(location, centroid); if (dist_cur < distance)
ArrayList<double[]> locations = new ArrayList<double[]>(elements.values()); double[] mean = DiscreteDistribution.mean(locations); max_movement = Math.max(max_movement, Math.sqrt(DiscreteDistribution.squaredError(centroid, mean))); new_centroids.add(mean);
/** * Returns the mean of the specified <code>Collection</code> of * distributions, which are assumed to be normalized arrays of * <code>double</code> values. * * @see #mean(double[][]) */ public static double[] mean(Collection<double[]> distributions) { if (distributions.isEmpty()) { throw new IllegalArgumentException( "Distribution collection must be non-empty"); } Iterator<double[]> iter = distributions.iterator(); double[] first = iter.next(); double[][] d_array = new double[distributions.size()][first.length]; d_array[0] = first; for (int i = 1; i < d_array.length; i++) { d_array[i] = iter.next(); } return mean(d_array); }
/** * @param dist the distribution whose divergence from {@code reference} is being measured * @param reference the reference distribution * @return <code>KullbackLeibler(dist, reference) + KullbackLeibler(reference, dist)</code> * @see #KullbackLeibler(double[], double[]) */ public static double symmetricKL(double[] dist, double[] reference) { return KullbackLeibler(dist, reference) + KullbackLeibler(reference, dist); }
double distance = DiscreteDistribution.squaredError(location, closest); double dist_cur = DiscreteDistribution.squaredError(location, centroid); if (dist_cur < distance) {
/** * Returns the squared difference between the * two specified distributions, which must have the same * number of elements. This is defined as * the sum over all <code>i</code> of the square of * <code>(dist[i] - reference[i])</code>. */ public static double squaredError(double[] dist, double[] reference) { double error = 0; checkLengths(dist, reference); for (int i = 0; i < dist.length; i++) { double difference = dist[i] - reference[i]; error += difference * difference; } return error; }
/** * Returns <code>KullbackLeibler(dist, reference) + KullbackLeibler(reference, dist)</code>. * @see #KullbackLeibler(double[], double[]) */ public static double symmetricKL(double[] dist, double[] reference) { return KullbackLeibler(dist, reference) + KullbackLeibler(reference, dist); }
/** * Returns the Kullback-Leibler divergence between the two specified * distributions, which must have the same number of elements. This is * defined as the sum over all <code>i</code> of * <code>dist[i] * Math.log(dist[i] / reference[i])</code>. Note that this * value is not symmetric; see <code>symmetricKL</code> for a symmetric * variant. * * @see #symmetricKL(double[], double[]) */ public static double KullbackLeibler(double[] dist, double[] reference) { double distance = 0; checkLengths(dist, reference); for (int i = 0; i < dist.length; i++) { if (dist[i] > 0 && reference[i] > 0) { distance += dist[i] * Math.log(dist[i] / reference[i]); } } return distance; }
/** * Returns the Kullback-Leibler divergence between the * two specified distributions, which must have the same * number of elements. This is defined as * the sum over all <code>i</code> of * <code>dist[i] * Math.log(dist[i] / reference[i])</code>. * Note that this value is not symmetric; see * <code>symmetricKL</code> for a symmetric variant. * @see #symmetricKL(double[], double[]) */ public static double KullbackLeibler(double[] dist, double[] reference) { double distance = 0; checkLengths(dist, reference); for (int i = 0; i < dist.length; i++) { if (dist[i] > 0 && reference[i] > 0) distance += dist[i] * Math.log(dist[i] / reference[i]); } return distance; }
/** * Returns the cosine distance between the two * specified distributions, which must have the same number * of elements. The distributions are treated as vectors * in <code>dist.length</code>-dimensional space. * Given the following definitions * <ul> * <li/><code>v</code> = the sum over all <code>i</code> of <code>dist[i] * dist[i]</code> * <li/><code>w</code> = the sum over all <code>i</code> of <code>reference[i] * reference[i]</code> * <li/><code>vw</code> = the sum over all <code>i</code> of <code>dist[i] * reference[i]</code> * </ul> * the value returned is defined as <code>vw / (Math.sqrt(v) * Math.sqrt(w))</code>. */ public static double cosine(double[] dist, double[] reference) { double v_prod = 0; // dot product x*x double w_prod = 0; // dot product y*y double vw_prod = 0; // dot product x*y checkLengths(dist, reference); for (int i = 0; i < dist.length; i++) { vw_prod += dist[i] * reference[i]; v_prod += dist[i] * dist[i]; w_prod += reference[i] * reference[i]; } // cosine distance between v and w return vw_prod / (Math.sqrt(v_prod) * Math.sqrt(w_prod)); }