SPB Git

Repository Public

CompareTests.swift 19.9 KB 251 lines swift

1import Foundation2import Testing3import SheetCore4import SheetUI5import Statistics6import AnalysisKit7@testable import StatsBasic89@MainActor @Suite struct MeanTests {10    let session = columnSession([("a", sampleA.map { $0 }), ("b", sampleB.map { $0 }), ("c", [11.0, 12.4, nil, 13.1, 12.2, 11.7, 12.9, 10.4, 11.5, 12.6])])1112    @Test func confidenceIntervals() throws {13        let tool = ConfidenceIntervalTool.descriptor14        let mean = try run(tool, on: session, range: "A1:C11") { $0.set("x", column: 0); $0.set("level", number: 0.9) }15        let ref = try Inference.meanInterval(sampleA, level: 0.9)16        #expect(close(mean.metric("Lower bound"), ref.lower) && close(mean.metric("Upper bound"), ref.upper) && close(mean.metric("Estimate"), ref.estimate))17        let z = try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "meanZ"); $0.set("x", column: 0); $0.set("sigma", number: 1.5) }18        #expect(close(z.metric("Lower bound"), try Inference.meanInterval(sampleA, knownSigma: 1.5).lower))19        let variance = try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "variance"); $0.set("x", column: 1) }20        #expect(close(variance.metric("Upper bound"), try Inference.varianceInterval(sampleB).upper))21        let diff = try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "difference"); $0.set("x", column: 0); $0.set("y", column: 1); $0.set("pooled", bool: true) }22        #expect(close(diff.metric("Lower bound"), try Inference.meanDifferenceInterval(sampleA, sampleB, equalVariances: true).lower))23        // a proportion needs no data range at all24        let p = try run(tool, on: session) { $0.set("kind", choice: "proportion"); $0.set("successes", int: 37); $0.set("trials", int: 120); $0.set("pmethod", choice: "clopperPearson") }25        let pref = try Inference.proportionInterval(successes: 37, trials: 120, method: .clopperPearson)26        #expect(close(p.metric("Lower bound"), pref.lower) && close(p.metric("Upper bound"), pref.upper))27        #expect(throws: ToolError.self) { try run(tool, on: session) { $0.set("kind", choice: "proportion"); $0.set("successes", int: 9); $0.set("trials", int: 5) } }28        #expect(throws: ToolError.self) { try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "meanZ"); $0.set("x", column: 0); $0.set("sigma", number: 0) } }29    }3031    @Test func tTestsMatchTheEngine() throws {32        let tool = TTestTool.descriptor33        let one = try run(tool, on: session, range: "A1:C11") { $0.set("x", column: 0); $0.set("mu", number: 13); $0.set("alternative", choice: "greater") }34        let r1 = try Inference.tTest(sampleA, mu: 13, alternative: .greater)35        #expect(close(one.metric("Statistic (t)"), r1.statistic) && close(one.metric("p-value"), r1.pValue))36        #expect(one.title == "One-sample t-test")37        let welch = try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "two"); $0.set("x", column: 0); $0.set("y", column: 1) }38        let r2 = try Inference.tTest(sampleA, sampleB)39        #expect(close(welch.metric("Statistic (t)"), r2.statistic) && close(welch.metric("p-value"), r2.pValue))40        let table = try #require(welch.tables.first)41        #expect(close(table.value("Degrees of freedom"), r2.df ?? .nan))42        #expect(table.text("p-value refers to") == "two-sided")43        let lower = table.rows.first { $0.first?.textValue?.hasSuffix("confidence interval, lower") == true }?[1].numberValue44        #expect(close(lower, r2.confidenceInterval?.lower ?? .nan))45        let pooled = try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "two"); $0.set("x", column: 0); $0.set("y", column: 1); $0.set("variances", choice: "pooled") }46        #expect(close(pooled.metric("Statistic (t)"), try Inference.tTest(sampleA, sampleB, equalVariances: true).statistic))47        #expect(pooled.tables.first?.value("Degrees of freedom") == 18)48        // independent samples: the missing value of c is simply skipped49        let c = [11.0, 12.4, 13.1, 12.2, 11.7, 12.9, 10.4, 11.5, 12.6]50        let two = try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "two"); $0.set("x", column: 0); $0.set("y", column: 2) }51        #expect(close(two.metric("Statistic (t)"), try Inference.tTest(sampleA, c).statistic))52        #expect(two.notes.contains { $0.contains("1 missing values") })53        // paired: the incomplete pair is dropped from both columns54        var a = sampleA55        a.remove(at: 2)56        let paired = try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "paired"); $0.set("x", column: 0); $0.set("y", column: 2) }57        let r3 = try Inference.pairedTTest(a, c)58        #expect(close(paired.metric("Statistic (t)"), r3.statistic) && paired.tables.first?.value("Pairs") == 9)59        #expect(paired.notes.contains { $0.contains("9 of 10 rows") })60        #expect(throws: ToolError.self) { try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "paired"); $0.set("x", column: 0); $0.set("y", column: 0) } }61    }6263    @Test func interpretationFollowsAlpha() throws {64        let tool = TTestTool.descriptor65        // p ≈ 0.0006 for a against b66        let reject = try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "two"); $0.set("x", column: 0); $0.set("y", column: 1) }67        let text = try #require(reject.interpretations.first)68        #expect(text.contains("reject the null hypothesis") && !text.contains("fail to reject"))69        #expect(text.contains("Null hypothesis: the difference of the two population means equals 0."))70        let strict = try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "two"); $0.set("x", column: 0); $0.set("y", column: 1); $0.set("alpha", number: 0.0001) }71        #expect(strict.interpretations.first?.contains("fail to reject the null hypothesis") == true)72        #expect(strict.interpretations.first?.lowercased().contains("accept") == false)73        #expect(throws: ToolError.self) { try run(tool, on: session, range: "A1:C11") { $0.set("x", column: 0); $0.set("alpha", number: 1.5) } }74    }7576    @Test func zTests() throws {77        let tool = ZTestTool.descriptor78        let one = try run(tool, on: session, range: "A1:C11") { $0.set("x", column: 0); $0.set("mu", number: 13); $0.set("sigmaX", number: 1.4) }79        #expect(close(one.metric("Statistic (z)"), try Inference.zTest(sampleA, mu: 13, sigma: 1.4).statistic))80        #expect(one.notes.isEmpty)81        let two = try run(tool, on: session, range: "A1:C11") { $0.set("kind", choice: "mean2"); $0.set("x", column: 0); $0.set("y", column: 1) }82        #expect(close(two.metric("p-value"), try Inference.zTest(sampleA, sampleB).pValue))83        #expect(two.notes.contains { $0.contains("sample standard deviation was used") })84        let p1 = try run(tool, on: session) { $0.set("kind", choice: "prop1"); $0.set("k1", int: 62); $0.set("n1", int: 100); $0.set("p0", number: 0.5); $0.set("alternative", choice: "greater") }85        let r = try Inference.proportionZTest(successes: 62, trials: 100, p0: 0.5, alternative: .greater)86        #expect(close(p1.metric("Statistic (z)"), r.statistic) && close(p1.metric("p-value"), r.pValue))87        let p2 = try run(tool, on: session) { $0.set("kind", choice: "prop2"); $0.set("k1", int: 45); $0.set("n1", int: 100); $0.set("k2", int: 30); $0.set("n2", int: 110) }88        #expect(close(p2.metric("Statistic (z)"), try Inference.proportionZTest(successes1: 45, trials1: 100, successes2: 30, trials2: 110).statistic))89        #expect(throws: ToolError.self) { try run(tool, on: session) { $0.set("kind", choice: "prop1"); $0.set("k1", int: 200); $0.set("n1", int: 100) } }90    }91}9293@MainActor @Suite struct ChiSquareTests {94    @Test func goodnessOfFit() throws {95        let session = makeSession([["Face", "Count", "Prob"], ["one", "18", "1"], ["two", "22", "1"], ["three", "16", "2"], ["four", "44", "4"]])96        let tool = ChiSquareGoodnessOfFitTool.descriptor97        let equal = try run(tool, on: session, range: "A1:C5") { $0.set("observed", column: 1); $0.set("labels", column: 0) }98        let r = try Inference.chiSquareGoodnessOfFit(observed: [18, 22, 16, 44])99        #expect(close(equal.metric("Statistic (χ²)"), r.statistic) && close(equal.metric("p-value"), r.pValue))100        let t = try #require(equal.table(titled: "Observed and expected counts"))101        #expect(t.rows[1][0] == .text("one") && t.rows[1][2].numberValue == 25 && t.value("Total") == 100)102        #expect(close(t.value("Total", 4), r.statistic))103        let prob = try run(tool, on: session, range: "A1:C5") { $0.set("observed", column: 1); $0.set("expectedKind", choice: "probabilities"); $0.set("expected", column: 2) }104        #expect(close(prob.metric("Statistic (χ²)"), try Inference.chiSquareGoodnessOfFit(observed: [18, 22, 16, 44], probabilities: [1, 1, 2, 4]).statistic))105        #expect(equal.warnings.isEmpty)106        #expect(throws: ToolError.self) { try run(tool, on: session, range: "A1:C5") { $0.set("observed", column: 1); $0.set("expectedKind", choice: "counts") } }107        // small expected counts are flagged108        let small = makeSession([["Count"], ["1"], ["2"], ["6"]])109        let flagged = try run(tool, on: small, range: "A1:A4") { $0.set("observed", column: 0) }110        #expect(flagged.warnings.count == 1)111    }112113    @Test func independenceFromATableAndFromTwoCategoricalColumnsAgree() throws {114        let tool = ChiSquareIndependenceTool.descriptor115        let counts: [[Double]] = [[12, 5, 7], [6, 14, 9]]116        let table = makeSession([["Group", "low", "mid", "high"], ["treated", "12", "5", "7"], ["control", "6", "14", "9"]])117        let a = try run(tool, on: table, range: "A1:D3")118        let ref = try Inference.chiSquareIndependence(counts)119        #expect(close(a.metric("Statistic (χ²)"), ref.test.statistic) && close(a.metric("p-value"), ref.test.pValue))120        #expect(close(a.metric("Effect size (Cramér's V)"), ref.cramersV))121        let observed = try #require(a.table(titled: "Observed counts"))122        #expect(observed.rows[1][0] == .text("treated") && observed.rows[0][2] == .text("mid") && observed.rows[3][4].numberValue == 53)123        let expected = try #require(a.table(titled: "Expected counts under independence"))124        #expect(close(expected.rows[1][1].numberValue, ref.expected[0, 0]))125        #expect(a.tables.first?.value("Degrees of freedom") == 2)126127        // the same data as one row per observation128        var rows = [["Group", "Level"]]129        for (i, g) in ["treated", "control"].enumerated() { for (j, l) in ["low", "mid", "high"].enumerated() {130            rows += Array(repeating: [g, l], count: Int(counts[i][j]))131        } }132        rows.append(["treated", ""])                          // dropped: blank level133        let raw = makeSession(rows)134        let b = try run(tool, on: raw, range: "A1:B\(rows.count)") { $0.set("source", choice: "columns"); $0.set("rowFactor", column: 0); $0.set("colFactor", column: 1) }135        #expect(close(b.metric("Statistic (χ²)"), ref.test.statistic))136        #expect(b.table(titled: "Observed counts")?.rows[2][0] == .text("control"))137        #expect(b.notes.contains { $0.contains("1 rows with a blank value") })138        #expect(b.interpretations.first?.contains("Null hypothesis: the row and column variables are independent.") == true)139    }140141    @Test func crossTabAndYates() throws {142        let tab = ChiSquareIndependenceTool.crossTab(["a", "b", "a", "", "b", "a"], ["x", "y", "y", "x", "y", "x"])143        #expect(tab.rows == ["a", "b"] && tab.cols == ["x", "y"] && tab.counts == [[2, 1], [0, 2]] && tab.dropped == 1)144        let session = makeSession([["", "yes", "no"], ["m", "12", "8"], ["f", "5", "15"]])145        let tool = ChiSquareIndependenceTool.descriptor146        let y = try run(tool, on: session, range: "A1:C3") { $0.set("yates", bool: true) }147        #expect(close(y.metric("Statistic (χ²)"), try Inference.chiSquareIndependence([[12, 8], [5, 15]], yatesCorrection: true).test.statistic))148        #expect(y.title == "Chi-Square Test of Independence")149        let one = makeSession([["g", "n"], ["a", "3"], ["b", "4"]])150        #expect(throws: ToolError.self) { try run(tool, on: one, range: "A1:B3") }151    }152}153154@MainActor @Suite struct AnovaTests {155    let wide = columnSession([("A", sampleA.map { $0 }), ("B", sampleB.map { $0 }), ("C", sampleC.dropLast(2).map { $0 })])156157    @Test func oneWayFromColumnsAndFromLongFormatAgree() throws {158        let tool = OneWayAnovaTool.descriptor159        let groups = [sampleA, sampleB, Array(sampleC.dropLast(2))]160        let ref = try Inference.oneWayANOVA(groups)161        let r = try run(tool, on: wide, range: "A1:C11") { $0.set("groupCols", columns: [0, 1, 2]) }162        #expect(close(r.metric("F"), ref.rows[0].f ?? .nan) && close(r.metric("p-value"), ref.rows[0].pValue ?? .nan))163        let t = try #require(r.table(titled: "Analysis of variance"))164        #expect(close(t.value("Between groups"), ref.rows[0].sumOfSquares) && t.value("Within groups", 2) == 25 && t.value("Total", 2) == 27)165        #expect(r.table(titled: "Groups")?.value("C") == 8)                       // unequal sizes: blanks are not data166        let tukey = try #require(r.table(titled: "Tukey HSD pairwise comparisons"))167        let tref = try Inference.tukeyHSD(groups, level: 0.95)168        #expect(tukey.rows.count == 4 && tukey.rows[1][0] == .text("A − B"))169        #expect(close(tukey.rows[1][3].numberValue, tref.comparisons[0].pValue) && close(tukey.rows[3][4].numberValue, tref.comparisons[2].lower))170        #expect(tukey.rows[1][6] == .text("reject equality"))171        #expect(r.interpretations.first?.contains("all group means are equal") == true)172173        var rows = [["Group", "Score"]]174        for (name, g) in zip(["A", "B", "C"], groups) { rows += g.map { [name, String($0)] } }175        rows.append(["C", "n/a"])                              // not a number: skipped176        let long = makeSession(rows)177        let l = try run(tool, on: long, range: "A1:B\(rows.count)") { $0.set("layout", choice: "long"); $0.set("value", column: 1); $0.set("factor", column: 0); $0.set("tukey", bool: false) }178        #expect(close(l.metric("F"), ref.rows[0].f ?? .nan))179        #expect(l.table(titled: "Tukey HSD pairwise comparisons") == nil)180        #expect(l.notes.contains { $0.contains("1 rows without a usable value") })181        #expect(throws: ToolError.self) { try run(tool, on: self.wide, range: "A1:C11") { $0.set("groupCols", columns: [0]) } }182    }183184    @Test func twoWayWithoutReplication() throws {185        let session = makeSession([["Block", "T1", "T2", "T3"], ["b1", "10", "12", "15"], ["b2", "11", "14", "15"], ["b3", "9", "13", "17"], ["b4", "12", "13", "18"]])186        let r = try run(TwoWayAnovaTool.descriptor, on: session, range: "A1:D5")187        let ref = try Inference.twoWayANOVA([[10, 12, 15], [11, 14, 15], [9, 13, 17], [12, 13, 18]])188        let t = try #require(r.table(titled: "Analysis of variance"))189        #expect(close(t.value("Block", 4), ref.row("Rows")?.f ?? .nan) && close(t.value("Columns", 5), ref.row("Columns")?.pValue ?? .nan))190        #expect(t.value("Error", 2) == 6)191        #expect(r.interpretations.count == 2)192        #expect(r.table(titled: "Levels of Block")?.rows[1][0] == .text("b1"))193        let holes = makeSession([["Block", "T1", "T2"], ["b1", "10", ""], ["b2", "11", "14"]])194        #expect(throws: ToolError.self) { try run(TwoWayAnovaTool.descriptor, on: holes, range: "A1:C3") }195    }196197    @Test func twoWayWithReplicationNeedsABalancedDesign() throws {198        let cells: [[[Double]]] = [[[4.1, 4.5, 4.3], [5.2, 5.6, 5.1]], [[6.0, 6.4, 6.1], [5.5, 5.9, 5.4]]]199        var rows = [["Dose", "Sex", "Y"]]200        for (i, d) in ["low", "high"].enumerated() { for (j, s) in ["f", "m"].enumerated() { rows += cells[i][j].map { [d, s, String($0)] } } }201        let session = makeSession(rows)202        let configure: (inout FormValues) -> Void = { $0.set("design", choice: "replicated"); $0.set("value", column: 2); $0.set("factorA", column: 0); $0.set("factorB", column: 1) }203        let r = try run(TwoWayAnovaTool.descriptor, on: session, range: "A1:C13", configure)204        let ref = try Inference.twoWayANOVA(replicated: cells)205        let t = try #require(r.table(titled: "Analysis of variance"))206        #expect(close(t.value("Dose", 4), ref.row("Rows")?.f ?? .nan) && close(t.value("Sex", 4), ref.row("Columns")?.f ?? .nan))207        #expect(close(t.value("Interaction", 5), ref.row("Interaction")?.pValue ?? .nan) && t.value("Within cells", 2) == 8)208        #expect(r.interpretations.count == 3 && r.title == "Two-Way ANOVA with Replication")209        // one observation removed: unbalanced → clear error, nothing computed210        let unbalanced = makeSession(Array(rows.dropLast()))211        #expect { try run(TwoWayAnovaTool.descriptor, on: unbalanced, range: "A1:C12", configure) } throws: { error in212            (error as? ToolError)?.message.contains("unbalanced") == true213        }214    }215216    @Test func varianceTests() throws {217        let tool = VarianceTestTool.descriptor218        let groups = [sampleA, sampleB, Array(sampleC.dropLast(2))]219        let bf = try run(tool, on: wide, range: "A1:C11") { $0.set("groupCols", columns: [0, 1, 2]) }220        #expect(close(bf.metric("p-value"), try Inference.leveneTest(groups, center: .median).pValue))221        let levene = try run(tool, on: wide, range: "A1:C11") { $0.set("groupCols", columns: [0, 1, 2]); $0.set("test", choice: "levene") }222        #expect(close(levene.metric("Statistic (W)") ?? levene.metric("Statistic (F)"), try Inference.leveneTest(groups, center: .mean).statistic))223        let bartlett = try run(tool, on: wide, range: "A1:C11") { $0.set("groupCols", columns: [0, 1, 2]); $0.set("test", choice: "bartlett") }224        #expect(close(bartlett.metric("p-value"), try Inference.bartlettTest(groups).pValue))225        let f = try run(tool, on: wide, range: "A1:C11") { $0.set("groupCols", columns: [0, 1]); $0.set("test", choice: "f"); $0.set("alternative", choice: "less") }226        let ref = try Inference.varianceFTest(sampleA, sampleB, alternative: .less)227        #expect(close(f.metric("Statistic (F)"), ref.statistic) && close(f.metric("p-value"), ref.pValue))228        #expect(throws: ToolError.self) { try run(tool, on: self.wide, range: "A1:C11") { $0.set("groupCols", columns: [0, 1, 2]); $0.set("test", choice: "f") } }229    }230231    @Test func normalityAndNonparametricTests() throws {232        let n = try run(NormalityTool.descriptor, on: wide, range: "A1:C11") { $0.set("x", column: 0) }233        let t = try #require(n.table(titled: "Normality tests"))234        try #expect(close(t.rows[1][2].numberValue, try Inference.shapiroWilk(sampleA).statistic) && close(t.rows[2][4].numberValue, try Inference.jarqueBera(sampleA).pValue))235        #expect(t.rows[1][5] == .text("fail to reject normality") && n.interpretations.count == 2 && n.charts.count == 2)236        let only = try run(NormalityTool.descriptor, on: wide, range: "A1:C11") { $0.set("x", column: 0); $0.set("test", choice: "jarqueBera") }237        #expect(only.table(titled: "Normality tests")?.rows.count == 2)238239        let tool = NonparametricTool.descriptor240        let mw = try run(tool, on: wide, range: "A1:C11") { $0.set("x", column: 0); $0.set("y", column: 1) }241        let mref = try Inference.mannWhitneyU(sampleA, sampleB)242        #expect(close(mw.metric("Statistic (U)"), mref.statistic) && close(mw.metric("p-value"), mref.pValue))243        let w = try run(tool, on: wide, range: "A1:C11") { $0.set("test", choice: "wilcoxon"); $0.set("x", column: 0); $0.set("y", column: 2); $0.set("continuity", bool: false) }244        let wref = try Inference.wilcoxonSignedRank(Array(sampleA.prefix(8)), Array(sampleC.prefix(8)))245        #expect(close(w.metric("Statistic (W)"), wref.statistic) && close(w.metric("p-value"), wref.pValue))246        let kw = try run(tool, on: wide, range: "A1:C11") { $0.set("test", choice: "kruskalWallis"); $0.set("groupCols", columns: [0, 1, 2]) }247        #expect(close(kw.metric("Statistic (H)"), try Inference.kruskalWallis([sampleA, sampleB, Array(sampleC.dropLast(2))]).statistic))248        #expect(kw.interpretations.first?.contains("all groups come from the same distribution") == true)249    }250}251
Files main
Menu

Theme