code: public static double KullbackLeiblerDivergence(SparseArray x, double[] y) {
          if (x.isEmpty()) {
              throw new IllegalArgumentException("List x is empty.");
          }
  
          Iterator<SparseArray.Entry> iter = x.iterator();
  
          boolean intersection = false;
          double kl = 0.0;
          while (iter.hasNext()) {
              SparseArray.Entry b = iter.next();
              int i = b.i;
              if (y[i] > 0) {
                  intersection = true;
                  kl += b.x * Math.log(b.x / y[i]);
              }
          }
  
          if (intersection) {
              return kl;
          } else {
              return Double.POSITIVE_INFINITY;
          }
      }
docs: Kullback-Leibler divergence. The Kullback-Leibler divergence (also
  information divergence, information gain, relative entropy, or KLIC)
  is a non-symmetric measure of the difference between two probability
  distributions P and Q. KL measures the expected number of extra bits
  required to code samples from P when using a code based on Q, rather
  than using a code based on P. Typically P represents the "true"
  distribution of data, observations, or a precise calculated theoretical
  distribution. The measure Q typically represents a theory, model,
  description, or approximation of P.
  <p>
  Although it is often intuited as a distance metric, the KL divergence is
  not a true metric - for example, the KL from P to Q is not necessarily
  the same as the KL from Q to P.