/** * 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 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 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)); }
/** * 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)); }