docs: @Reference(authors = "D. Pelleg, A. Moore", //
        title = "X-means: Extending K-means with Efficient Estimation on the Number of Clusters", //
        booktitle = "Proc. 17th Int. Conf. on Machine Learning (ICML 2000)", //
        url = "http://www.pelleg.org/shared/hp/download/xmeans.ps", //
        bibkey = "DBLP:conf/icml/PellegM00")
    public static <V extends NumberVector> double logLikelihood(Relation<V> relation, Clustering<? extends MeanModel> clustering, NumberVectorDistanceFunction<? super V> distanceFunction) {
      List<? extends Cluster<? extends MeanModel>> clusters = clustering.getAllClusters();
      // number of clusters
      final int m = clusters.size();
  
      // number of objects in the clustering
      int n = 0;
      // cluster sizes
      int[] n_i = new int[m];
      // total variance
      double d = 0.;
      // variances
      double[] d_i = new double[m];
  
      // Iterate over clusters:
      Iterator<? extends Cluster<? extends MeanModel>> it = clusters.iterator();
      for(int i = 0; it.hasNext(); ++i) {
        Cluster<? extends MeanModel> cluster = it.next();
        n += n_i[i] = cluster.size();
        d += d_i[i] = varianceOfCluster(cluster, distanceFunction, relation);
      }
  
      // No remaining variance, if every point is on its own:
      if(n <= m) {
        return Double.NEGATIVE_INFINITY;
      }
  
      // Total variance (corrected for bias)
      final double logv = FastMath.log(d / (n - m));
  
      final int dim = RelationUtil.dimensionality(relation);
      // log likelihood of this clustering
      double logLikelihood = 0.;
  
      // Aggregate
      for(int i = 0; i < m; i++) {
        logLikelihood += n_i[i] * FastMath.log(n_i[i]) // Post. entropy Rn log Rn
            - n_i[i] * .5 * MathUtil.LOGTWOPI // Rn/2 log2pi
            - n_i[i] * dim * .5 * logv // Rn M/2 log sigma^2
            - (d_i[i] - m) * .5; // (Rn-K)/2
      }
      logLikelihood -= n * FastMath.log(n); // Prior entropy, sum_i Rn log R
      return logLikelihood;
    }