Repository Public
1import Foundation2import Testing3import SheetCore4import SheetUI5import Statistics6import AnalysisKit7@testable import StatsModels89@MainActor @Suite struct MultivariateTests {10 /// Three well-separated groups in three variables.11 static let points: [[Double]] = {12 var rng = Xoshiro256StarStar(seed: 12)13 let centres: [[Double]] = [[0, 0, 10], [8, 1, 50], [3, 9, 90]]14 return (0..<45).map { i in centres[i % 3].enumerated().map { j, c in c + (j == 2 ? 4 : 0.6) * rng.nextNormal() } }15 }()16 var s: WorkbookSession { session(["a", "b", "c"], Self.points.map { $0.map { $0 as Any? } }) }17 var matrix: Matrix { Matrix(Self.points) }1819 @Test func pcaMatchesTheEngineAndWritesScores() throws {20 let session = s21 let r = try run(PCATool.descriptor, session, range: "A1:C46") { $0.set("vars", columns: [0, 1, 2]) }22 let ref = try PCA.fit(matrix, standardize: true, names: ["a", "b", "c"])23 let v = try #require(r.table(titled: "Explained variance"))24 #expect(close(v.number(1, 1), ref.explainedVariance[0], 1e-9) && close(v.number(3, 3), 1, 1e-9))25 let l = try #require(r.table(titled: "Loadings"))26 #expect(close(l.number(2, 1), ref.loadings[1, 0], 1e-9))27 let before = snapshot(session.workbook)28 _ = try #require(r.actions.first { $0.id == "write-scores" }).perform(session)29 let sh = session.workbook.sheets[0]30 #expect(sh.value(0, 3) == .text("Score PC1") && sh.value(0, 5) == .text("Score PC3"))31 #expect(close(sh.value(45, 4).numberValue ?? .nan, ref.scores[44, 1], 1e-9))32 session.undo()33 #expect(snapshot(session.workbook) == before)34 // covariance PCA with two components35 let c = try run(PCATool.descriptor, session, range: "A1:C46") { $0.set("vars", columns: [0, 1, 2]); $0.set("standardize", bool: false); $0.set("components", int: 2) }36 #expect(try #require(c.table(titled: "Explained variance")).rows.count == 3)37 #expect(close(c.metric("PC1 share of variance") ?? .nan, try PCA.fit(matrix, components: 2).explainedVarianceRatio[0], 1e-9))38 }3940 @Test func kMeansIsReproducibleForASeedAndWritesLabels() throws {41 let session = s42 let configure: (inout FormValues) -> Void = { $0.set("vars", columns: [0, 1, 2]); $0.set("standardize", bool: false); $0.set("seed", int: 7) }43 let a = try run(KMeansTool.descriptor, session, range: "A1:C46", configure)44 let b = try run(KMeansTool.descriptor, session, range: "A1:C46", configure)45 #expect(a.tables == b.tables)46 let ref = try KMeans.fit(matrix, k: 3, seed: 7)47 #expect(close(a.metric("Within-cluster sum of squares") ?? .nan, ref.inertia) && close(a.metric("Mean silhouette") ?? .nan, ref.silhouette ?? .nan))48 let cent = try #require(a.table(titled: "Centroids"))49 #expect(cent.number(1, 1) == 15 && close(cent.number(1, 2), ref.centroids[0, 0]))50 _ = try #require(a.actions.first).perform(session)51 let sh = session.workbook.sheets[0]52 #expect(sh.value(0, 3) == .text("Cluster (k-means)"))53 #expect((1...45).map { sh.value($0, 3).numberValue } == ref.labels.map { Double($0 + 1) })54 #expect(throws: ToolError.self) { try run(KMeansTool.descriptor, session, range: "A1:C46") { $0.set("vars", columns: [0]); $0.set("k", int: 99) } }55 }5657 @Test func hierarchicalClusteringMatchesTheEngine() throws {58 let r = try run(HierarchicalTool.descriptor, s, range: "A1:C46") { $0.set("vars", columns: [0, 1, 2]); $0.set("standardize", bool: false); $0.set("linkage", choice: "average") }59 let ref = try HierarchicalClustering.linkage(matrix, method: .average)60 let t = try #require(r.table(titled: "Linkage table"))61 #expect(t.rows.count == 45 && close(t.number(44, 3), ref.merges[43].distance) && t.number(44, 4) == 45)62 #expect(t.rows[1][1].textValue == "Obs. \(ref.merges[0].first + 1)")63 let profile = try #require(r.table(titled: "Cluster means (original units)"))64 #expect(profile.rows.count == 4 && profile.number(1, 1) == 15)65 // the O(n³) guard66 let big = session(["x"], (0..<1600).map { [Double($0)] })67 do { _ = try run(HierarchicalTool.descriptor, big, range: "A1:A1601") { $0.set("vars", columns: [0]) }; Issue.record("expected the size guard") }68 catch { #expect(ToolError.describe(error).contains("1500")) }69 }70}7172@MainActor @Suite struct ResamplingTests {73 static let x: [Double] = { var r = Xoshiro256StarStar(seed: 2); return (0..<40).map { _ in 50 + 10 * r.nextNormal() } }()74 static let y: [Double] = { var r = Xoshiro256StarStar(seed: 6); return (0..<40).map { _ in 56 + 10 * r.nextNormal() } }()75 var s: WorkbookSession { session(["x", "y"], Self.x.indices.map { [Self.x[$0], Self.y[$0]] }) }7677 @Test func bootstrapMatchesTheEngineAndIsReproducible() throws {78 let configure: (inout FormValues) -> Void = { $0.set("x", column: 0); $0.set("statistic", choice: "median"); $0.set("resamples", int: 999); $0.set("seed", int: 42) }79 let a = try run(BootstrapTool.descriptor, s, range: "A1:B41", configure)80 let b = try run(BootstrapTool.descriptor, s, range: "A1:B41", configure)81 #expect(a.tables == b.tables)82 let ref = try Bootstrap.run(Self.x, resamples: 999, confidenceLevel: 0.95, seed: 42) { Descriptive.median($0) }83 let ci = try #require(a.table(titled: "Confidence intervals"))84 #expect(close(ci.number(1, 1), ref.percentile.lower) && close(ci.number(1, 2), ref.percentile.upper))85 #expect(close(ci.number(2, 1), ref.bca?.lower ?? .nan) && close(ci.number(2, 2), ref.bca?.upper ?? .nan))86 let sum = try #require(a.table(titled: "Bootstrap summary"))87 #expect(close(sum.value("Estimate"), ref.estimate) && close(sum.value("Bootstrap standard error"), ref.standardError) && sum.value("Resamples") == 999)88 // another seed gives another interval; a quantile statistic uses its probability89 let c = try run(BootstrapTool.descriptor, s, range: "A1:B41") { configure(&$0); $0.set("seed", int: 43) }90 #expect(c.tables != a.tables)91 let q = try run(BootstrapTool.descriptor, s, range: "A1:B41") { $0.set("x", column: 0); $0.set("statistic", choice: "quantile"); $0.set("prob", number: 0.75); $0.set("resamples", int: 199) }92 #expect(close(try #require(q.table(titled: "Bootstrap summary")).value("Estimate"), Descriptive.quantile(Self.x, 0.75)))93 }9495 @Test func aLongBootstrapCanBeCancelled() throws {96 let session = s97 let before = snapshot(session.workbook)98 #expect(throws: CancellationError.self) {99 try run(BootstrapTool.descriptor, session, range: "A1:B41", { $0.set("x", column: 0); $0.set("resamples", int: 500_000) }, isCancelled: { true })100 }101 #expect(snapshot(session.workbook) == before && !session.engine.canUndo)102 }103104 @Test func permutationTestMatchesTheEngineAndIsReproducible() throws {105 let configure: (inout FormValues) -> Void = { $0.set("x", column: 0); $0.set("y", column: 1); $0.set("resamples", int: 1999); $0.set("seed", int: 11) }106 let a = try run(PermutationTool.descriptor, s, range: "A1:B41", configure)107 #expect(a.tables == (try run(PermutationTool.descriptor, s, range: "A1:B41", configure)).tables)108 let ref = try PermutationTest.twoSample(Self.x, Self.y, resamples: 1999, seed: 11)109 let t = try #require(a.table(titled: "Permutation test"))110 #expect(close(t.value("Difference of means"), ref.statistic) && close(t.value("p-value"), ref.pValue))111 #expect(t.value("Arrangements evaluated") == Double(ref.permutations))112 #expect(t.text("Type")?.contains("Monte Carlo") == true)113 let sentence = a.blocks.compactMap { b -> String? in if case .text(let t) = b, t.kind == .interpretation { t.lines.first } else { nil } }.first ?? ""114 #expect(sentence.contains(ref.pValue < 0.05 ? "reject the null hypothesis" : "fail to reject"))115 // paired, median, one-sided116 let p = try run(PermutationTool.descriptor, s, range: "A1:B41") {117 configure(&$0); $0.set("design", choice: "paired"); $0.set("statistic", choice: "median"); $0.set("alternative", choice: "less")118 }119 let refPaired = try PermutationTest.paired(Self.x, Self.y, alternative: .less, resamples: 1999, seed: 11) { Descriptive.median($0) }120 #expect(close(try #require(p.table(titled: "Permutation test")).value("p-value"), refPaired.pValue))121 // small samples are enumerated exactly122 let small = session(["x", "y"], [[1.0, 4.0], [2.0, 6.0], [3.0, 5.0], [2.5, 7.0]])123 let e = try run(PermutationTool.descriptor, small, range: "A1:B5") { $0.set("x", column: 0); $0.set("y", column: 1) }124 #expect(try #require(e.table(titled: "Permutation test")).text("Type")?.contains("exact") == true)125 #expect(throws: ToolError.self) { try run(PermutationTool.descriptor, small, range: "A1:B5") { $0.set("x", column: 0); $0.set("y", column: 0) } }126 }127}128