function interClusterDensity(clusters, data) {
let meanClusterStd = 0
for (const cluster of clusters) {
const stdMatrix = fstd(cluster.vectors, 0)
meanClusterStd += math.sqrt(math.dot(math.transpose(stdMatrix), stdMatrix))
}
meanClusterStd = math.sqrt(meanClusterStd) / clusters.length
const clusterCentroids = clusters.map(d => d.centroid)
const clusterDensities = clusterCentroids.map(centroid => density(centroid, data, meanClusterStd))
let total = 0
for (let i = 0; i < clusterCentroids.length; i++) {
for (let j = 0; j < clusterCentroids.length; j++) {
if (i == j) continue
const mid = midpoint(clusterCentroids[i], clusterCentroids[j])
const midDensity = (density(mid, data, meanClusterStd))
const iClusterDensity = clusterDensities[i]
const jClusterDensity = clusterDensities[j]
total += midDensity / Math.max(iClusterDensity, jClusterDensity)
}
}
const k = clusters.length
return total / (k * (k - 1))
}