code: public static double klDivergence(double[] a, double[] b) {
          check(a, b);
  
          double divergence = 0;
  
          // Iterate over all values and ignore any that are zero.
          for (int i = 0; i < a.length; ++i) {
              // Ignore values from a that are zero, since they would cause a
              // divide by zero error.
              if (b[i] == 0d) 
                  throw new IllegalArgumentException(
                      "The KL-divergence is not defined when a[i] > 0 and " +
                      "b[i] == 0.");
              else if (a[i] != 0d)
                  divergence += a[i] * Math.log(a[i]/ b[i]);
          }
  
          return divergence;
      }
docs: Computes the K-L Divergence of two probability distributions {@code A}
  and {@code B} where the vectors {@code a} and {@code b} correspond to
  {@code n} samples from each respective distribution.  The divergence
  between two samples is non-symmetric and is frequently used as a distance
  metric between vectors from a semantic space.  This metric is described
  in more detail in the following paper:
  
  <li style="font-family:Garamond, Georgia, serif">S. Kullback and R. A.
  Leibler, "On Information and Sufficiency", <i>The Annals of
  Mathematical Statistics</i> 1951.
  </li>
  
  @throws IllegalArgumentException when the length of the two vectors are
  not the same.