Java Double Number Divide divergenceKL(double... a)

Here you can find the source of divergenceKL(double... a)

Description

divergence KL

License

Open Source License

Declaration

public static double divergenceKL(double... a) 

Method Source Code

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

Related

  1. div(Double dividend, Double quotient)
  2. div(double divisor, int dividend, int preciseFigures)
  3. div(double left, double right)
  4. div(final double a, final double b)
  5. divdivmin(double a, double b, double c, double d)
  6. divHex(double num)
  7. divide(Double a, Double b)
  8. divide(double a, double b)
  9. divide(Double a, Double b)