Repository Public
1import Foundation2import Testing3import AnalyticsTestSupport4@testable import Statistics56private let mvFixture = Fixture.load("multivariate", .module)78/// Renumbers labels in order of first appearance, so two labelings can be compared as partitions.9private func canonical(_ labels: [Int]) -> [Int] {10 var map = [Int: Int]()11 return labels.map { l in12 if let v = map[l] { return v }13 map[l] = map.count14 return map[l]!15 }16}1718@Suite struct MultivariatePCATests {19 let f = mvFixture["pca"]20 var x: Matrix { Matrix(f["X"].mat) }2122 /// Compares components and scores up to the sign of each component, then checks our sign convention.23 func check(_ r: PCAResult, _ ref: JSON, _ label: String, rel: Double = 1e-9) {24 let comps = ref["components"].mat, scores = ref["scores"].mat25 #expect(r.components.rows == comps.count, "\(label) component count")26 #expect(allClose(r.explainedVariance, ref["variance"].arr, rel: rel), "\(label) variance")27 #expect(allClose(r.explainedVarianceRatio, ref["ratio"].arr, rel: rel), "\(label) ratio")28 #expect(allClose(r.singularValues, ref["singular"].arr, rel: rel), "\(label) singular values")29 for (i, want) in comps.enumerated() {30 let got = r.components.row(i)31 let sign: Double = zip(got, want).reduce(0) { $0 + $1.0 * $1.1 } < 0 ? -1 : 132 #expect(allClose(got, want.map { $0 * sign }, rel: 1e-8, abs: 1e-9), "\(label) component \(i)")33 #expect(allClose(r.scores.column(i), scores.map { $0[i] * sign }, rel: 1e-7, abs: 1e-8), "\(label) scores \(i)")34 #expect(isClose(got.reduce(0) { $0 + $1 * $1 }, 1, rel: 1e-12), "\(label) unit length")35 let big = got.max { abs($0) < abs($1) }!36 #expect(big > 0, "\(label) sign convention")37 }38 }3940 @Test func covariancePCAMatchesScikitLearn() throws {41 let r = try PCA.fit(x)42 check(r, f["cov"], "cov")43 #expect(allClose(r.means, f["cov"]["mean"].arr, rel: 1e-12))44 #expect(allClose(r.explainedVariance, f["cov_matrix_eig"].arr, rel: 1e-9))45 #expect(isClose(r.cumulativeVarianceRatio.last!, 1, rel: 1e-12))46 #expect(!r.standardized && r.scales == [1, 1, 1, 1, 1])47 let r2 = try PCA.fit(x, components: 2)48 check(r2, f["cov_k2"], "cov k=2")49 #expect(r2.scores.cols == 2 && r2.loadings.rows == 5 && r2.loadings.cols == 2)50 // total variance still counts every component51 #expect(isClose(r2.totalVariance, r.explainedVariance.reduce(0, +), rel: 1e-12))52 }5354 @Test func correlationPCAMatchesScikitLearn() throws {55 let r = try PCA.fit(x, standardize: true, names: ["a", "b", "c", "d", "e"])56 check(r, f["corr"], "corr")57 #expect(allClose(r.scales, f["sd"].arr, rel: 1e-12))58 #expect(allClose(r.explainedVariance, f["corr_matrix_eig"].arr, rel: 1e-9))59 #expect(isClose(r.totalVariance, 5, rel: 1e-12))60 // loadings of a correlation PCA are the variable–component correlations61 let l = r.loadings62 for v in 0..<5 {63 for c in 0..<5 {64 #expect(isClose(l[v, c], Descriptive.pearson(x.column(v), r.scores.column(c)), rel: 1e-8, abs: 1e-9), "loading \(v),\(c)")65 }66 }67 #expect(r.table().count == 11 && r.table()[6][0] == .text("a"))68 }6970 @Test func scoresAreUncorrelatedAndTransformProjectsNewData() throws {71 let r = try PCA.fit(x)72 for i in 0..<5 {73 #expect(isClose(Descriptive.variance(r.scores.column(i)), r.explainedVariance[i], rel: 1e-9))74 for j in 0..<i { #expect(abs(Descriptive.covariance(r.scores.column(i), r.scores.column(j))) < 1e-9 * r.explainedVariance[0]) }75 }76 let proj = try r.transform(Matrix(f["new"].mat))77 let want = f["new_scores_cov"].mat78 for i in 0..<5 {79 let sign: Double = zip(r.components.row(i), f["cov"]["components"].mat[i]).reduce(0) { $0 + $1.0 * $1.1 } < 0 ? -1 : 180 #expect(allClose(proj.column(i), want.map { $0[i] * sign }, rel: 1e-8, abs: 1e-8))81 }82 #expect(allClose(try r.transform(x).data, r.scores.data, rel: 1e-10, abs: 1e-10))83 #expect(throws: StatError.self) { try r.transform(Matrix(rows: 1, cols: 3)) }84 }8586 @Test func wideDataAndErrors() throws {87 let w = Matrix(f["wide"]["X"].mat) // 4 observations, 7 variables → at most 3 components88 let r = try PCA.fit(w)89 #expect(r.components.rows == 3)90 #expect(allClose(r.explainedVariance, Array(f["wide"]["variance"].arr.prefix(3)), rel: 1e-9))91 #expect(throws: StatError.self) { try PCA.fit(Matrix(rows: 1, cols: 3)) }92 #expect(throws: StatError.self) { try PCA.fit(x, components: 9) }93 #expect(throws: StatError.self) { try PCA.fit(x, names: ["a"]) }94 #expect(throws: StatError.self) { try PCA.fit(Matrix([[1, 2], [1, 3], [1, 5]]), standardize: true) }95 }96}9798@Suite struct MultivariateKMeansTests {99 let f = mvFixture["kmeans"]100 var x: Matrix { Matrix(f["X"].mat) }101102 @Test func lloydFromFixedCentroidsMatchesScikitLearn() throws {103 for c in f["cases"].list {104 let k = c["k"].i105 let r = try KMeans.fit(x, k: k, initialCentroids: Matrix(c["init"].mat))106 #expect(r.converged && r.runs == 1, "k=\(k)")107 #expect(r.labels == c["labels"].ints, "k=\(k) labels")108 #expect(allClose(r.centroids.data, c["centers"].mat.flatMap { $0 }, rel: 1e-10, abs: 1e-11), "k=\(k) centres")109 #expect(isClose(r.inertia, c["inertia"].d, rel: 1e-10), "k=\(k) inertia")110 #expect(isClose(r.silhouette ?? .nan, c["silhouette"].d, rel: 1e-10), "k=\(k) silhouette")111 let s = KMeans.silhouette(x, labels: r.labels)112 #expect(allClose(s.samples, c["samples"].arr, rel: 1e-9, abs: 1e-11), "k=\(k) silhouette samples")113 #expect(r.clusterSizes.reduce(0, +) == x.rows)114 }115 }116117 @Test func seededPlusPlusFindsTheGlobalOptimumReproducibly() throws {118 let a = try KMeans.fit(x, k: 4, seed: 11, restarts: 20)119 let b = try KMeans.fit(x, k: 4, seed: 11, restarts: 20)120 #expect(a == b)121 // four well-separated groups: the best of 20 k-means++ starts reaches scikit-learn's best of 50122 #expect(isClose(a.inertia, f["best4_inertia"].d, rel: 1e-9), "\(a.inertia) vs \(f["best4_inertia"].d)")123 #expect(isClose(a.silhouette!, f["best4_silhouette"].d, rel: 1e-9))124 let three = try KMeans.fit(x, k: 3, seed: 5, restarts: 30)125 #expect(isClose(three.inertia, f["best3_inertia"].d, rel: 1e-9))126 let other = try KMeans.fit(x, k: 4, seed: 12, restarts: 1)127 #expect(other.inertia >= a.inertia - 1e-9)128 // inertia decreases with k129 let five = try KMeans.fit(x, k: 5, seed: 3, restarts: 20)130 #expect(five.inertia < a.inertia && a.inertia < three.inertia)131 }132133 @Test func plusPlusSeedsAreDataPointsAndSpreadOut() {134 var g = Xoshiro256StarStar(seed: 1)135 let c = KMeans.plusPlus(x, 4, &g)136 let rows = Set(x.rowArrays.map { $0.map(\.bitPattern) })137 for i in 0..<4 { #expect(rows.contains(c.row(i).map(\.bitPattern))) }138 #expect(Set(c.rowArrays.map { $0.map(\.bitPattern) }).count == 4)139 }140141 @Test func edgeCases() throws {142 let one = try KMeans.fit(x, k: 1)143 #expect(one.silhouette == nil && one.labels.allSatisfy { $0 == 0 })144 // inertia with one cluster = total sum of squares about the mean145 let tss = (0..<3).reduce(0.0) { $0 + Descriptive.sumSquaredDeviations(x.column($1)) }146 #expect(isClose(one.inertia, tss, rel: 1e-11))147 let small = Matrix([[0, 0], [0, 1], [10, 0], [10, 1]])148 let two = try KMeans.fit(small, k: 2, seed: 3)149 #expect(canonical(two.labels) == [0, 0, 1, 1] && two.inertia == 1)150 // an empty cluster (two identical starting centres) is re-seeded, not left empty151 let reseeded = try KMeans.fit(small, k: 2, initialCentroids: Matrix([[0, 0.5], [0, 0.5]]))152 #expect(reseeded.clusterSizes.allSatisfy { $0 > 0 })153 let all = try KMeans.fit(small, k: 4, seed: 9)154 #expect(all.inertia == 0)155 #expect(throws: StatError.self) { try KMeans.fit(small, k: 5) }156 #expect(throws: StatError.self) { try KMeans.fit(small, k: 0) }157 #expect(throws: StatError.self) { try KMeans.fit(small, k: 2, initialCentroids: Matrix(rows: 3, cols: 2)) }158 #expect(two.table().count == 8)159 }160}161162@Suite struct MultivariateHierarchicalTests {163 let f = mvFixture["hier"]164165 @Test func linkageMatchesSciPy() throws {166 let x = Matrix(f["X"].mat)167 for method in HierarchicalClustering.Linkage.allCases {168 let ref = f[method.rawValue]169 let r = try HierarchicalClustering.linkage(x, method: method)170 let z = ref["Z"].mat171 #expect(r.merges.count == z.count && r.n == 45)172 for (m, row) in zip(r.merges, z) {173 #expect(m.first == Int(row[0]) && m.second == Int(row[1]), "\(method) merge order")174 #expect(isClose(m.distance, row[2], rel: 1e-10), "\(method) distance")175 #expect(m.size == Int(row[3]))176 }177 #expect(canonical(try r.labels(clusters: 3)) == canonical(ref["labels3"].ints), "\(method) 3 clusters")178 #expect(canonical(try r.labels(clusters: 5)) == canonical(ref["labels5"].ints), "\(method) 5 clusters")179 #expect(Set(try r.labels(clusters: 45)).count == 45)180 #expect(Set(try r.labels(clusters: 1)) == [0])181 }182 }183184 @Test func handExample() throws {185 // points on a line: 0, 1, 3, 7186 let r = try HierarchicalClustering.linkage(Matrix([[0], [1], [3], [7]]), method: .single)187 #expect(r.merges.map(\.distance) == [1, 2, 4])188 #expect(r.merges[0].first == 0 && r.merges[0].second == 1 && r.merges[1].first == 2 && r.merges[1].second == 4)189 let c = try HierarchicalClustering.linkage(Matrix([[0], [1], [3], [7]]), method: .complete)190 #expect(c.merges.map(\.distance) == [1, 3, 7])191 #expect(try r.labels(clusters: 2) == [0, 0, 0, 1])192 #expect(r.table().count == 4)193 #expect(throws: StatError.self) { try HierarchicalClustering.linkage(Matrix([[1]])) }194 #expect(throws: StatError.self) { try r.labels(clusters: 9) }195 }196}197