Returns the KL divergence, K(p1 || p2). - Java java.lang

Java examples for java.lang:Math Function

Description

Returns the KL divergence, K(p1 || p2).

Demo Code


//package com.java2s;

public class Main {
    public static final double log2 = Math.log(2);

    /**// ww w. j  ava 2  s  . c  o  m
     * Returns the KL divergence, K(p1 || p2).
     *
     * The log is w.r.t. base 2. <p>
     *
     * *Note*: If any value in <tt>p2</tt> is <tt>0.0</tt> then the KL-divergence
     * is <tt>infinite</tt>. 
     * 
     */
    public static double klDivergence(double[] p1, double[] p2) {
        assert (p1.length == p2.length);
        double klDiv = 0.0;
        for (int i = 0; i < p1.length; ++i) {
            if (p1[i] == 0) {
                continue;
            }
            if (p2[i] == 0) {
                return Double.POSITIVE_INFINITY;
            }
            klDiv += p1[i] * Math.log(p1[i] / p2[i]);
        }
        return klDiv / log2; // moved this division out of the loop -DM
    }
}

Related Tutorials