Float HierarchicalClustering::computeClusterVariance( const ClusterInfo &cluster, const MatrixFloat &data ){ VectorFloat mean(N,0); VectorFloat std(N,0); //Compute the mean UINT numSamples = cluster.getNumSamplesInCluster(); for(UINT j=0; j<N; j++){ for(UINT i=0; i<numSamples; i++){ UINT index = cluster[i]; mean[j] += data[ index ][j]; } mean[j] /= Float( numSamples ); } //Compute the std dev for(UINT j=0; j<N; j++){ for(UINT i=0; i<numSamples; i++){ std[j] += grt_sqr( data[ cluster[i] ][j] - mean[j] ); } std[j] = grt_sqrt( std[j] / Float( numSamples-1 ) ); } Float variance = 0; for(UINT j=0; j<N; j++){ variance += std[j]; } return variance/N; }
Float HierarchicalClustering::computeClusterDistance( const ClusterInfo &clusterA, const ClusterInfo &clusterB ){ Float minDist = grt_numeric_limits< Float >::max(); const UINT numSamplesA = clusterA.getNumSamplesInCluster(); const UINT numSamplesB = clusterB.getNumSamplesInCluster(); //Find the minimum distance between the two clusters for(UINT i=0; i<numSamplesA; i++){ for(UINT j=0; j<numSamplesB; j++){ if( distanceMatrix[ clusterA[i] ][ clusterB[j] ] < minDist ){ minDist = distanceMatrix[ clusterA[i] ][ clusterB[j] ]; } } } return minDist; }