code: public static double KullbackLeiblerDivergence(SparseArray x, SparseArray y) {
          if (x.isEmpty()) {
              throw new IllegalArgumentException("List x is empty.");
          }
  
          if (y.isEmpty()) {
              throw new IllegalArgumentException("List y is empty.");
          }
  
          Iterator<SparseArray.Entry> iterX = x.iterator();
          Iterator<SparseArray.Entry> iterY = y.iterator();
  
          SparseArray.Entry a = iterX.hasNext() ? iterX.next() : null;
          SparseArray.Entry b = iterY.hasNext() ? iterY.next() : null;
  
          boolean intersection = false;
          double kl = 0.0;
  
          while (a != null && b != null) {
              if (a.i < b.i) {
                  a = iterX.hasNext() ? iterX.next() : null;
              } else if (a.i > b.i) {
                  b = iterY.hasNext() ? iterY.next() : null;
              } else {
                  intersection = true;
                  kl += a.x * Math.log(a.x / b.x);
  
                  a = iterX.hasNext() ? iterX.next() : null;
                  b = iterY.hasNext() ? iterY.next() : null;
              }
          }
  
          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.