Repository Public
1import Foundation2import Testing3import SheetCore4import SheetUI5import Statistics6import AnalysisKit7@testable import StatsBasic89@MainActor @Suite struct DescribeTests {10 @Test func summaryMatchesTheEngineAndCountsMissingValues() throws {11 let x: [Double?] = [1, 2, nil, 4, 10], y: [Double?] = [5, 3, 8, nil, 1]12 let session = columnSession([("x", x), ("y", y)])13 let r = try run(DescriptiveSummaryTool.descriptor, on: session, range: "A1:B6") { $0.set("vars", columns: [0, 1]) }14 let t = try #require(r.table(titled: "Summary statistics"))15 let ref = Descriptive.summary([1, 2, 4, 10])16 #expect(t.rows[0] == [.text("Statistic"), .text("x"), .text("y")])17 #expect(t.value("Observations") == 4 && t.value("Missing") == 1)18 #expect(close(t.value("Mean"), ref.mean) && close(t.value("Standard deviation (n − 1)"), ref.standardDeviation))19 #expect(close(t.value("Skewness"), ref.skewness) && close(t.value("Third quartile"), ref.q3))20 #expect(close(t.value("Mean", 2), 17.0 / 4))21 // listwise: only rows 1, 2 and 5 are complete22 let l = try run(DescriptiveSummaryTool.descriptor, on: session, range: "A1:B6") { $0.set("vars", columns: [0, 1]); $0.set("missing", choice: "listwise") }23 let lt = try #require(l.table(titled: "Summary statistics"))24 #expect(lt.value("Observations") == 3 && close(lt.value("Mean"), 13.0 / 3))25 #expect(r.charts.count == 2)26 }2728 @Test func summaryWithoutHeaderUsesColumnLetters() throws {29 let session = makeSession([["1", "4"], ["2", "5"], ["3", "9"]])30 let r = try run(DescriptiveSummaryTool.descriptor, on: session, range: "A1:B3", header: false) { $0.set("vars", columns: [0, 1]) }31 let t = try #require(r.table(titled: "Summary statistics"))32 #expect(t.rows[0] == [.text("Statistic"), .text("A"), .text("B")])33 #expect(t.value("Observations") == 3 && t.value("Mean", 2) == 6)34 }3536 @Test func summaryNeedsAColumn() {37 let session = makeSession([["a"], ["x"], ["y"]])38 #expect(throws: ToolError.self) { try run(DescriptiveSummaryTool.descriptor, on: session, range: "A1:A3") }39 }4041 @Test func binRules() {42 let x = (1...100).map(Double.init)43 #expect(HistogramTool.binCount(x, rule: "sturges", fixed: 0) == 8) // ⌈log2 100⌉ + 144 #expect(HistogramTool.binCount(x, rule: "fixed", fixed: 13) == 13)45 let fd = Int(ceil(99 / (2 * Descriptive.interquartileRange(x) * pow(100, -1.0 / 3))))46 #expect(HistogramTool.binCount(x, rule: "fd", fixed: 0) == fd)47 #expect(HistogramTool.binCount(x, rule: "auto", fixed: 0) == max(8, fd))48 let scott = Int(ceil(99 / (3.49 * Descriptive.standardDeviation(x) * pow(100, -1.0 / 3))))49 #expect(HistogramTool.binCount(x, rule: "scott", fixed: 0) == scott)50 #expect(HistogramTool.binCount([5, 5, 5], rule: "auto", fixed: 0) == 1)51 }5253 @Test func histogramTableAgreesWithTheEngineAndTheChart() throws {54 let x = sampleA + sampleB55 let session = columnSession([("v", x.map { $0 }), ("w", [nil])])56 let r = try run(HistogramTool.descriptor, on: session, range: "A1:A21") { $0.set("x", column: 0); $0.set("rule", choice: "fixed"); $0.set("bins", int: 4) }57 let t = try #require(r.table(titled: "Frequency table"))58 let h = Descriptive.histogram(x, binCount: 4)59 #expect(t.rows.count == 5)60 for i in 0..<4 {61 #expect(t.rows[i + 1][1].numberValue == h.edges[i] && t.rows[i + 1][2].numberValue == h.edges[i + 1])62 #expect(t.rows[i + 1][3].numberValue == Double(h.counts[i]))63 }64 #expect(t.rows[4][5].numberValue == 20 && t.rows[4][6].numberValue == 1)65 #expect(r.metric("Bins") == 4 && r.metric("Observations") == 20)66 // the chart shows the same counts67 let spec = try #require(r.charts.first?.spec)68 guard case .literal(let counts)? = spec.series.first?.values else { Issue.record("chart without literal counts"); return }69 #expect(counts == h.counts.map(Double.init))70 }7172 @Test func histogramRejectsATextColumn() {73 let session = makeSession([["v", "t"], ["1", "a"], ["2", "b"]])74 #expect(throws: (any Error).self) { try run(HistogramTool.descriptor, on: session, range: "A1:B3") { $0.set("x", column: 1) } }75 }76}7778@MainActor @Suite struct CorrelationTests {79 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])])8081 @Test func matrixAndPValuesMatchTheEngine() throws {82 for (id, method) in [("pearson", CorrelationMethod.pearson), ("spearman", .spearman), ("kendall", .kendall)] {83 let r = try run(CorrelationTool.descriptor, on: session, range: "A1:C11") { $0.set("vars", columns: [0, 1]); $0.set("method", choice: id) }84 let test = try Inference.correlationTest(sampleA, sampleB, method: method)85 let m = try #require(r.tables.first { $0.title?.hasPrefix("Correlation matrix") == true })86 #expect(close(m.rows[1][2].numberValue, Descriptive.correlation(sampleA, sampleB, method: method)))87 #expect(m.rows[1][1].numberValue == 1)88 let p = try #require(r.table(titled: "p-values (two-sided)"))89 #expect(close(p.rows[1][2].numberValue, test.pValue) && p.rows[1][1].isBlank)90 #expect(close(r.metric("p-value"), test.pValue))91 #expect(r.charts.count == 1)92 #expect(r.interpretations.first?.contains("null hypothesis") == true)93 }94 }9596 @Test func listwiseAndPairwiseCountsDiffer() throws {97 let listwise = try run(CorrelationTool.descriptor, on: session, range: "A1:C11") { $0.set("vars", columns: [0, 1, 2]) }98 let pairwise = try run(CorrelationTool.descriptor, on: session, range: "A1:C11") { $0.set("vars", columns: [0, 1, 2]); $0.set("missing", choice: "pairwise") }99 let nl = try #require(listwise.table(titled: "Observations per pair")), np = try #require(pairwise.table(titled: "Observations per pair"))100 #expect(nl.rows[1][2].numberValue == 9 && np.rows[1][2].numberValue == 10 && np.rows[1][3].numberValue == 9)101 var a = sampleA, b = sampleB102 a.remove(at: 2); b.remove(at: 2)103 let ml = try #require(listwise.tables.first), mp = try #require(pairwise.tables.first)104 #expect(close(ml.rows[1][2].numberValue, Descriptive.pearson(a, b)))105 #expect(close(mp.rows[1][2].numberValue, Descriptive.pearson(sampleA, sampleB)))106 #expect(listwise.interpretations.count == 1 && listwise.charts.isEmpty)107 }108109 @Test func needsTwoColumns() {110 #expect(throws: ToolError.self) { try run(CorrelationTool.descriptor, on: session, range: "A1:C11") { $0.set("vars", columns: [0]) } }111 }112}113