src/main/java/pt/it/av/tnav/ml/clustering/cluster/ClusterUtils.java
public static <D extends Distance<D>> double avgSilhouette(List<Cluster<D>> clusters) {
    double rv = -1.0, count = 0;

    if (clusters.size() > 1) {
      // For all data points
      for (int c = 0; c < clusters.size(); c++) {
        Cluster<D> cluster = clusters.get(c);
        if (cluster != null) {
          count += cluster.size();
          for (int d = 0; d < cluster.size(); d++) {
            D dp = cluster.get(d);

            // a(i) -> average distance of datum i to all data points in the cluster
            double ai = cluster.avgDistance(dp);

            // b(i) -> lowest average distance of datum i to all points in other clusters,
            // of which i is not a member
            double bi = Double.MAX_VALUE;
            for (int i = 0; i < clusters.size(); i++) {
              if (i != c && clusters.get(i) != null) {
                double tbi = clusters.get(i).avgDistance(dp);
                if (tbi < bi) {
                  bi = tbi;
                }
              }
            }
            // s(i) = b(i) - a(i) / max{b(i), a(i)}
            rv += ((bi - ai) / Double.max(bi, ai));
          }
        }
      }
      rv /= count;
    }

    return rv;
  }
/**
   * Returns the average Silhouette score for the cluster's list.
   * Null clusters will not be considered.
   *
   * @param clusters the list of clusters.
   * @param <D> Any Class that implements {@link Distance} interface.
   * @return
   */
Returns the average Silhouette score for the cluster's list.
Null clusters will not be considered.

@param clusters the list of clusters.
@param  Any Class that implements  Distance interface.
@return
Returns the average Silhouette score for the cluster ' s list . Null clusters will not be considered . @param clusters the list of clusters . @param Any Class that implements Distance interface . @return
public static <D extends Distance<D>> double avgSilhouette(List<Cluster<D>> clusters) {
    double rv = -1.0, count = 0;
    if (clusters.size() > 1) {
      for (int c = 0; c < clusters.size(); c++) {
        Cluster<D> cluster = clusters.get(c);
        if (cluster != null) {
          count += cluster.size();
          for (int d = 0; d < cluster.size(); d++) {
            D dp = cluster.get(d);
            double ai = cluster.avgDistance(dp);
            double bi = Double.MAX_VALUE;
            for (int i = 0; i < clusters.size(); i++) {
              if (i != c && clusters.get(i) != null) {
                double tbi = clusters.get(i).avgDistance(dp);
                if (tbi < bi) {
                  bi = tbi;
                }
              }
            }
            rv += ((bi - ai) / Double.max(bi, ai));
          }
        }
      }
      rv /= count;
    }
    return rv;
  }
public static < D extends Distance < D > > double avgSilhouette ( List < Cluster < D > > clusters ) { double rv = - 1.0 , count = 0 ; if ( clusters . size ( ) > 1 ) { for ( int c = 0 ; c < clusters . size ( ) ; c ++ ) { Cluster < D > cluster = clusters . get ( c ) ; if ( cluster != null ) { count += cluster . size ( ) ; for ( int d = 0 ; d < cluster . size ( ) ; d ++ ) { D dp = cluster . get ( d ) ; double ai = cluster . avgDistance ( dp ) ; double bi = Double . MAX_VALUE ; for ( int i = 0 ; i < clusters . size ( ) ; i ++ ) { if ( i != c && clusters . get ( i ) != null ) { double tbi = clusters . get ( i ) . avgDistance ( dp ) ; if ( tbi < bi ) { bi = tbi ; } } } rv += ( ( bi - ai ) / Double . max ( bi , ai ) ) ; } } } rv /= count ; } return rv ; }
Returns the average Silhouette score for the cluster's list.
Returns the average Silhouette score for the cluster ' s list .
// For all data points // a(i) -> average distance of datum i to all data points in the cluster // b(i) -> lowest average distance of datum i to all points in other clusters, // of which i is not a member // s(i) = b(i) - a(i) / max{b(i), a(i)}