Here you can find the source of divergenceKL(double... a)
public static double divergenceKL(double... a)
//package com.java2s; public class Main { private static final double EPSILON = 1.0E-10; public static double divergenceKL(double... a) { double result = 0.0; double mean = boundBelow(mean(a), 0.0); for (int i = 0; i < a.length; i++) { result += divergenceKL(a[i], mean); }//w ww . ja v a2 s . c om return result / a.length; } public static double divergenceKL(double pa, double pb) { pa = boundBelow(pa, 0.0); double pA = boundBelow(1.0 - pa, 0.0); double pB = 1.0 - pb; double result = pa * (Math.log(pa) - Math.log(pb)) + pA * (Math.log(pA) - Math.log(pB)); if (Double.isNaN(result)) { System.out.println(pa + " " + pb); System.out.println(Math.log(pa) + " " + Math.log(pb)); System.out.println(Math.log(pA) + " " + Math.log(pB)); } return result; } public static double boundBelow(double a, double lower) { if (a <= lower + EPSILON) return lower + EPSILON; return a; } public static double mean(double[] a) { return mean(a, 0, a.length); } public static double mean(double[] a, int from, int to) { double mean = 0.0; for (int i = from; i < to; i++) { mean += a[i]; } return mean / (to - from); } }