code: private void setMeanDistancesToCentroids() {
          meanDistancesToCentroids = new HashMap<>();
          for (int i = 0; i < clusterCentroids.numInstances(); i++) {    //For each centroid
              int countAssignedInstances = 0;
              double sumDistancesToCentroid = 0.0;
              Instance centroidInstance = clusterCentroids.instance(i);
              for (int j = 0; j < trainingData.numInstances(); j++) {       //For each data point
                   if (i == centroidAssignments[j]) {
                       Instance valueInstance = trainingData.instance(j);
                       double distanceToCentroid = Math.abs(valueInstance.value(0) -
                                                      centroidInstance.value(0));
                       sumDistancesToCentroid += distanceToCentroid;
                       countAssignedInstances++;
                   }
              }
              double meanDistanceToCentroid = sumDistancesToCentroid / countAssignedInstances;
              meanDistancesToCentroids.put(centroidInstance, meanDistanceToCentroid);
          }
      }
docs: For each cluster, caches the mean distance from data points in the
  cluster to the cluster centroid. Mean distances are used later in
  anomaly score calculations.