Matrix matrix, Matrix[] gapMatrices,
double[] gapResults, double[] gapStds,
Assignments[] gapAssignments) {
int k = i+startSize;
double numGaps = gapMatrices.length;
CriterionFunction function =
ReflectionUtil.getObjectInstance(methodName);
verbose("Clustering reference data for %d clusters\n", k);
// Compute the score for the reference data sets with k
// clusters.
double referenceScore = 0;
double[] referenceScores = new double[gapMatrices.length];
for (int j = 0; j < gapMatrices.length; ++j) {
verbose("Clustering reference data %d \n", j);
Assignments result = DirectClustering.cluster(
gapMatrices[j], k, 1, function);
referenceScores[j] = Math.log(function.score());
referenceScore += referenceScores[j];
}
referenceScore /= numGaps;
// Compute the standard deviation for the reference scores.
double referenceStdev = 0;
for (double score : referenceScores)
referenceStdev += Math.pow(score - referenceScore, 2);
referenceStdev /= numGaps;
referenceStdev = Math.sqrt(referenceStdev) * Math.sqrt(1 + 1/numGaps);
verbose("Clustering original data for %d clusters\n", k);
// Compute the score for the original data set with k
// clusters.
Assignments result = DirectClustering.cluster(
matrix, k, 1, function);
// Compute the difference between the two scores. If the
// current score is less than the previous score, then the
// previous assignment is considered best.
double gap = Math.log(function.score());
verbose("Completed iteration with referenceScore: %f, gap:%f\n",
referenceScore, gap);
gap = referenceScore - gap;
System.out.printf("k: %d gap: %f std: %f\n", i, gap, referenceStdev);