Repository Public MIT
1// ORIX Calc2// File: Packages/ORIXStatistics/Tests/ORIXStatisticsTests/InferenceTests.swift3// Description: Confidence intervals and hypothesis tests against scipy.stats / statsmodels, summary-vs-data inputs, checks and errors.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9import ORIXNumerics10@testable import ORIXStatistics1112private let tol = 1e-101314private func alternative(_ c: [String: Any]) -> Alternative { Alternative(rawValue: c.string("alternative")) ?? .twoSided }1516@Suite struct InferenceReferenceTests {17 static func cases(_ kind: String) throws -> [[String: Any]] {18 let all = try Reference.cases("inference").filter { $0.string("kind") == kind }19 #expect(!all.isEmpty, "no reference case of kind \(kind)")20 return all21 }2223 @Test func oneSampleT() throws {24 for c in try Self.cases("oneSampleT") {25 let x = c.doubles("data"), alpha = c.double("alpha")26 let r = try HypothesisTests.oneSampleT(data: x, mu0: c.double("mu0"), alternative: alternative(c), alpha: alpha)27 #expect(close(r.statistic, c.double("statistic"), rtol: tol))28 #expect(r.degreesOfFreedom == [c.double("df")])29 #expect(close(r.pValue, c.double("p"), rtol: tol), "p \(r.pValue) vs \(c.double("p"))")30 #expect(close(r.criticalValue, c.double("critical"), rtol: tol))31 let ci = try #require(r.confidenceInterval)32 #expect(close(ci.lower, c.double("ciLower"), rtol: tol) && close(ci.upper, c.double("ciUpper"), rtol: tol))33 #expect(close(try #require(r.effectSize).value, c.double("cohenD"), rtol: tol))34 #expect(r.rejectNull == (c.double("p") <= alpha))35 // "Stats" input gives the same answer as "Data" input.36 let s = try HypothesisTests.oneSampleT(SampleSummary(data: x), mu0: c.double("mu0"), alternative: alternative(c), alpha: alpha)37 #expect(s.statistic == r.statistic && s.pValue == r.pValue)38 #expect(!r.verification.isEmpty && r.verification.checks.contains { $0.kind == .residual && $0.status == .passed })39 }40 }4142 @Test func oneSampleZ() throws {43 for c in try Self.cases("oneSampleZ") {44 let x = c.doubles("data")45 let r = try HypothesisTests.oneSampleZ(data: x, sigma: c.double("sigma"), mu0: c.double("mu0"), alternative: alternative(c), alpha: c.double("alpha"))46 #expect(close(r.statistic, c.double("statistic"), rtol: tol) && close(r.pValue, c.double("p"), rtol: tol))47 #expect(close(r.criticalValue, c.double("critical"), rtol: tol) && r.degreesOfFreedom.isEmpty)48 let ci = try #require(r.confidenceInterval)49 #expect(close(ci.lower, c.double("ciLower"), rtol: tol) && close(ci.upper, c.double("ciUpper"), rtol: tol))50 let direct = try ConfidenceIntervals.meanZ(data: x, sigma: c.double("sigma"), level: 1 - c.double("alpha"))51 #expect(direct.lower == ci.lower && direct.upper == ci.upper && direct.degreesOfFreedom == nil)52 }53 }5455 @Test func twoSampleT() throws {56 for c in try Self.cases("twoSampleT") {57 let a = c.doubles("a"), b = c.doubles("b")58 let method: TwoSampleMethod = (c["pooled"] as? Bool) == true ? .pooled : .welch59 let r = try HypothesisTests.twoSampleT(data: a, b, method: method, alternative: alternative(c), alpha: c.double("alpha"))60 #expect(close(r.statistic, c.double("statistic"), rtol: tol), "\(r.name)")61 #expect(close(r.degreesOfFreedom[0], c.double("df"), rtol: tol), "Welch–Satterthwaite df \(r.degreesOfFreedom[0]) vs \(c.double("df"))")62 #expect(close(r.pValue, c.double("p"), rtol: tol))63 #expect(close(r.criticalValue, c.double("critical"), rtol: tol))64 let ci = try #require(r.confidenceInterval)65 #expect(close(ci.lower, c.double("ciLower"), rtol: tol, atol: 1e-12) && close(ci.upper, c.double("ciUpper"), rtol: tol, atol: 1e-12))66 #expect(close(try #require(r.effectSize).value, c.double("cohenD"), rtol: tol))67 // Summary-statistics input (TI "Stats"), with summaries rounded as stored in the JSON.68 let s = c.doubles("summary")69 let fromStats = try HypothesisTests.twoSampleT(SampleSummary(count: Int(s[0]), mean: s[1], standardDeviation: s[2]),70 SampleSummary(count: Int(s[3]), mean: s[4], standardDeviation: s[5]),71 method: method, alternative: alternative(c), alpha: c.double("alpha"))72 #expect(close(fromStats.statistic, c.double("statistic"), rtol: 1e-9) && close(fromStats.pValue, c.double("p"), rtol: 1e-9))73 }74 }7576 @Test func twoSampleZ() throws {77 for c in try Self.cases("twoSampleZ") {78 func run(_ alt: Alternative) throws -> HypothesisTestResult {79 try HypothesisTests.twoSampleZ(mean1: c.double("m1"), sigma1: c.double("s1"), n1: c.int("n1"), mean2: c.double("m2"), sigma2: c.double("s2"), n2: c.int("n2"), alternative: alt)80 }81 let r = try run(.twoSided)82 #expect(close(r.statistic, c.double("statistic"), rtol: tol) && close(r.pValue, c.double("p"), rtol: tol))83 #expect(try close(try run(.less).pValue, c.double("pLess"), rtol: tol) && close(try run(.greater).pValue, c.double("pGreater"), rtol: tol))84 let ci = try #require(r.confidenceInterval)85 #expect(close(ci.lower, c.double("ciLower"), rtol: tol) && close(ci.upper, c.double("ciUpper"), rtol: tol))86 }87 }8889 @Test func pairedT() throws {90 for c in try Self.cases("pairedT") {91 let r = try HypothesisTests.pairedT(c.doubles("a"), c.doubles("b"), alternative: alternative(c))92 #expect(close(r.statistic, c.double("statistic"), rtol: tol) && close(r.pValue, c.double("p"), rtol: tol) && r.degreesOfFreedom == [c.double("df")])93 let ci = try #require(r.confidenceInterval)94 #expect(close(ci.lower, c.double("ciLower"), rtol: tol) && close(ci.upper, c.double("ciUpper"), rtol: tol))95 #expect(close(try #require(r.effectSize).value, c.double("cohenDz"), rtol: tol) && r.effectSize?.name == "Cohen's d_z")96 let direct = try ConfidenceIntervals.pairedMean(c.doubles("a"), c.doubles("b"))97 #expect(close(direct.lower, c.double("ciLower"), rtol: tol))98 }99 }100101 @Test func proportionIntervals() throws {102 for c in try Self.cases("proportionCI") {103 let x = c.int("x"), n = c.int("n"), level = c.double("level")104 for (method, key) in [(ProportionIntervalMethod.wilson, "wilson"), (.clopperPearson, "exact"), (.agrestiCoull, "agrestiCoull"), (.wald, "wald")] {105 let ci = try ConfidenceIntervals.proportion(successes: x, trials: n, level: level, method: method)106 let e = c.doubles(key)107 #expect(close(ci.lower, e[0], rtol: 1e-9, atol: 1e-13) && close(ci.upper, e[1], rtol: 1e-9, atol: 1e-13),108 "\(key) x=\(x) n=\(n) level=\(level): (\(ci.lower), \(ci.upper)) vs \(e)")109 #expect(ci.estimate == Double(x) / Double(n) && ci.level == level)110 }111 // Wilson is the default.112 #expect(try ConfidenceIntervals.proportion(successes: x, trials: n, level: level).method == "Wilson score interval")113 }114 }115116 @Test func oneProportionTests() throws {117 for c in try Self.cases("oneProportionZ") {118 let x = c.int("x"), n = c.int("n"), p0 = c.double("p0"), alt = alternative(c)119 let r = try HypothesisTests.oneProportionZ(successes: x, trials: n, p0: p0, alternative: alt)120 #expect(close(r.statistic, c.double("statistic"), rtol: tol) && close(r.pValue, c.double("p"), rtol: tol, atol: 1e-300))121 #expect(close(try #require(r.effectSize).value, c.double("cohenH"), rtol: tol, atol: 1e-15))122 let cc = try HypothesisTests.oneProportionZ(successes: x, trials: n, p0: p0, continuityCorrection: true, alternative: alt)123 #expect(close(cc.statistic, c.double("statisticCC"), rtol: tol, atol: 1e-15) && close(cc.pValue, c.double("pCC"), rtol: tol, atol: 1e-300))124 let exact = try HypothesisTests.binomialExact(successes: x, trials: n, p0: p0, alternative: alt)125 #expect(close(exact.pValue, c.double("exactP"), rtol: 1e-9, atol: 1e-300), "exact x=\(x) n=\(n) \(alt): \(exact.pValue) vs \(c.double("exactP"))")126 }127 }128129 @Test func twoProportionZ() throws {130 for c in try Self.cases("twoProportionZ") {131 func run(_ alt: Alternative) throws -> HypothesisTestResult {132 try HypothesisTests.twoProportionZ(successes1: c.int("x1"), trials1: c.int("n1"), successes2: c.int("x2"), trials2: c.int("n2"), alternative: alt)133 }134 let r = try run(.twoSided)135 #expect(close(r.statistic, c.double("statistic"), rtol: tol) && close(r.pValue, c.double("p"), rtol: tol))136 #expect(try close(try run(.less).pValue, c.double("pLess"), rtol: tol) && close(try run(.greater).pValue, c.double("pGreater"), rtol: tol))137 let ci = try #require(r.confidenceInterval)138 #expect(close(ci.lower, c.double("ciLower"), rtol: tol) && close(ci.upper, c.double("ciUpper"), rtol: tol))139 }140 }141142 @Test func chiSquareGoodnessOfFit() throws {143 for c in try Self.cases("gof") {144 let props: [Double]? = c.has("proportions") ? c.doubles("proportions") : nil145 let r = try HypothesisTests.chiSquareGoodnessOfFit(observed: c.doubles("observed"), expectedProportions: props, estimatedParameters: c.int("ddof"))146 #expect(close(r.test.statistic, c.double("statistic"), rtol: tol) && close(r.test.pValue, c.double("p"), rtol: tol))147 #expect(r.test.degreesOfFreedom == [c.double("df")] && close(r.test.criticalValue, c.double("critical"), rtol: tol))148 #expect(allClose(r.expected, c.doubles("expected"), rtol: 1e-12))149 #expect(close(r.contributions.reduce(0, +), r.test.statistic, rtol: 1e-13))150 #expect(close(try #require(r.test.effectSize).value, c.double("cohenW"), rtol: tol))151 let viaCounts = try HypothesisTests.chiSquareGoodnessOfFit(observed: c.doubles("observed"), expectedCounts: c.doubles("expected"), estimatedParameters: c.int("ddof"))152 #expect(close(viaCounts.test.statistic, r.test.statistic, rtol: 1e-12))153 // The expected-count warning must fire exactly when an expected count is below 5.154 let small = c.doubles("expected").contains { $0 < 5 }155 #expect(r.test.verification.warnings.contains { $0.name == "Expected counts ≥ 5" } == small)156 }157 }158159 @Test func chiSquareIndependence() throws {160 for c in try Self.cases("independence") {161 let yates = (c["yates"] as? Bool) == true162 let r = try HypothesisTests.chiSquareIndependence(table: c.matrix("table"), yatesCorrection: yates)163 #expect(close(r.test.statistic, c.double("statistic"), rtol: tol), "χ² \(r.test.statistic) vs \(c.double("statistic")) yates=\(yates)")164 #expect(close(r.test.pValue, c.double("p"), rtol: tol) && r.test.degreesOfFreedom == [c.double("df")])165 #expect(close(r.test.criticalValue, c.double("critical"), rtol: tol))166 #expect(r.yatesApplied == ((c["yatesApplied"] as? Bool) == true))167 for (row, expected) in zip(r.expected, c.matrix("expected")) { #expect(allClose(row, expected, rtol: 1e-12)) }168 #expect(close(r.cramersV, c.double("cramersV"), rtol: tol) && r.test.effectSize?.name == "Cramér's V")169 #expect(close(r.rowTotals.reduce(0, +), r.grandTotal, rtol: 1e-15) && close(r.columnTotals.reduce(0, +), r.grandTotal, rtol: 1e-15))170 let small = c.matrix("expected").flatMap { $0 }.contains { $0 < 5 }171 #expect(r.test.verification.warnings.contains { $0.name == "Expected counts ≥ 5" } == small)172 if yates && !r.yatesApplied { #expect(r.test.verification.warnings.contains { $0.name == "Yates correction" }) }173 }174 }175176 @Test func oneWayANOVA() throws {177 for c in try Self.cases("anova") {178 let r = try HypothesisTests.oneWayANOVA(groups: c.matrix("groups"))179 #expect(close(r.test.statistic, c.double("statistic"), rtol: tol) && close(r.test.pValue, c.double("p"), rtol: tol))180 #expect(r.test.degreesOfFreedom == [c.double("df1"), c.double("df2")])181 #expect(close(r.sumOfSquaresBetween, c.double("ssb"), rtol: tol) && close(r.sumOfSquaresWithin, c.double("ssw"), rtol: tol))182 #expect(close(r.meanSquareBetween, c.double("msb"), rtol: tol) && close(r.meanSquareWithin, c.double("msw"), rtol: tol))183 #expect(close(r.etaSquared, c.double("etaSquared"), rtol: tol) && close(r.test.criticalValue, c.double("critical"), rtol: tol))184 #expect(close(r.sumOfSquaresTotal, r.sumOfSquaresBetween + r.sumOfSquaresWithin, rtol: 1e-15))185 #expect(close(r.pooledStandardDeviation, c.double("msw").squareRoot(), rtol: tol))186 let summaries = try c.matrix("summaries").map { try SampleSummary(count: Int($0[0]), mean: $0[1], standardDeviation: $0[2]) }187 let s = try HypothesisTests.oneWayANOVA(summaries: summaries)188 #expect(close(s.test.statistic, c.double("statistic"), rtol: 1e-9) && close(s.test.pValue, c.double("p"), rtol: 1e-9))189 }190 }191192 @Test func fTestAndVarianceIntervals() throws {193 for c in try Self.cases("fTest") {194 let a = c.doubles("a"), b = c.doubles("b")195 let r = try HypothesisTests.fTestVariances(data: a, b)196 #expect(close(r.statistic, c.double("statistic"), rtol: tol) && close(r.pValue, c.double("p"), rtol: tol))197 #expect(r.degreesOfFreedom == [c.double("df1"), c.double("df2")])198 #expect(try close(r.criticalValue, c.double("criticalUpper"), rtol: tol) && close(try #require(r.lowerCriticalValue), c.double("criticalLower"), rtol: tol))199 #expect(close(try HypothesisTests.fTestVariances(data: a, b, alternative: .less).pValue, c.double("pLess"), rtol: tol))200 #expect(close(try HypothesisTests.fTestVariances(data: a, b, alternative: .greater).pValue, c.double("pGreater"), rtol: tol))201 let ci = try #require(r.confidenceInterval)202 #expect(close(ci.lower, c.double("ciLower"), rtol: tol) && close(ci.upper, c.double("ciUpper"), rtol: tol))203 }204 for c in try Self.cases("varianceCI") {205 let x = c.doubles("data"), level = c.double("level")206 let v = try ConfidenceIntervals.variance(data: x, level: level)207 #expect(close(v.lower, c.double("lower"), rtol: tol) && close(v.upper, c.double("upper"), rtol: tol))208 let sd = try ConfidenceIntervals.standardDeviation(SampleSummary(data: x), level: level)209 #expect(close(sd.lower, c.double("lower").squareRoot(), rtol: tol) && close(sd.upper, c.double("upper").squareRoot(), rtol: tol))210 let t = try ConfidenceIntervals.meanT(data: x, level: level)211 #expect(close(t.lower, c.double("tLower"), rtol: tol) && close(t.upper, c.double("tUpper"), rtol: tol))212 #expect(close(t.marginOfError, (c.double("tUpper") - c.double("tLower")) / 2, rtol: 1e-9) && t.contains(t.estimate))213 }214 }215216 @Test func correlation() throws {217 for c in try Self.cases("correlation") {218 let x = c.doubles("x"), y = c.doubles("y")219 let r = try HypothesisTests.correlationTest(x, y)220 #expect(close(try #require(r.estimate), c.double("r"), rtol: tol) && r.degreesOfFreedom == [c.double("df")])221 #expect(close(r.statistic, c.double("statistic"), rtol: 1e-9) && close(r.pValue, c.double("p"), rtol: 1e-9))222 #expect(close(try HypothesisTests.correlationTest(x, y, alternative: .less).pValue, c.double("pLess"), rtol: 1e-9))223 #expect(close(try HypothesisTests.correlationTest(x, y, alternative: .greater).pValue, c.double("pGreater"), rtol: 1e-9))224 let ci = try #require(r.confidenceInterval)225 #expect(close(ci.lower, c.double("ciLower"), rtol: 1e-9) && close(ci.upper, c.double("ciUpper"), rtol: 1e-9))226 }227 }228}229230@Suite struct InferenceBehaviourTests {231 @Test func textbookExamples() throws {232 // z interval: x̄ = 50, σ = 10, n = 25, 95% → 50 ± 1.959964·2233 let z = try ConfidenceIntervals.meanZ(mean: 50, sigma: 10, n: 25)234 #expect(close(z.lower, 50 - 2 * 1.959963984540054, rtol: 1e-14) && close(z.upper, 50 + 2 * 1.959963984540054, rtol: 1e-14))235 #expect(close(try #require(z.criticalValue), 1.959963984540054, rtol: 1e-15) && z.standardError == 2)236 // One-sample z test, hand computed: z = (52 − 50)/(10/5) = 1, two-sided p = 2(1 − Φ(1)).237 let test = try HypothesisTests.oneSampleZ(mean: 52, n: 25, sigma: 10, mu0: 50)238 #expect(test.statistic == 1 && close(test.pValue, 0.31731050786291415, rtol: 1e-13) && !test.rejectNull)239 #expect(test.conclusion.hasPrefix("Fail to reject H₀ at α = 0.05"))240 // Direction of one-sided alternatives.241 #expect(try HypothesisTests.oneSampleZ(mean: 52, n: 25, sigma: 10, mu0: 50, alternative: .greater).pValue < 0.5)242 #expect(try HypothesisTests.oneSampleZ(mean: 52, n: 25, sigma: 10, mu0: 50, alternative: .less).pValue > 0.5)243 #expect(try HypothesisTests.oneSampleZ(mean: 52, n: 25, sigma: 10, mu0: 50, alternative: .less).criticalValue < 0)244 // The decision agrees with "null value outside the two-sided interval".245 for mu0 in [44.0, 47.5, 50, 55.2, 56.5] {246 let r = try HypothesisTests.oneSampleT(SampleSummary(count: 16, mean: 51, standardDeviation: 8), mu0: mu0)247 #expect(r.rejectNull == !(try #require(r.confidenceInterval)).contains(mu0), "μ₀ = \(mu0)")248 }249 // Wilson with x = 0 starts at 0 and Clopper–Pearson with x = n ends at 1 ("rule of three" neighbourhood).250 #expect(try ConfidenceIntervals.proportion(successes: 0, trials: 30).lower == 0)251 #expect(try ConfidenceIntervals.proportion(successes: 30, trials: 30, method: .clopperPearson).upper == 1)252 #expect(close(try ConfidenceIntervals.proportion(successes: 0, trials: 30, method: .clopperPearson).upper, 1 - pow(0.025, 1.0 / 30), rtol: 1e-12))253 }254255 @Test func explanationsCarryTheRealNumbers() throws {256 let r = try HypothesisTests.oneSampleT(SampleSummary(count: 16, mean: 51, standardDeviation: 8), mu0: 47)257 #expect(r.statistic == 2)258 let text = r.explanation.plainText259 #expect(text.contains("One-sample t test") && text.contains("t = (x̄ − μ₀)/(s/√n)") && text.contains("H₀: μ = 47"))260 #expect(text.contains("Test statistic") && text.contains("= 2") && text.contains("Degrees of freedom: 15"))261 #expect(r.explanation.inputs.contains { $0.label == "n" && $0.value == "16" })262 #expect(r.explanation.result.contains(formatNumber(r.pValue)))263 let ci = try ConfidenceIntervals.proportion(successes: 12, trials: 40)264 #expect(ci.explanation.steps.contains { $0.label == "Sample proportion" && $0.value == "0.3" })265 #expect(ci.explanation.result == "(\(formatNumber(ci.lower)), \(formatNumber(ci.upper)))")266 }267268 @Test func verificationWarnsOnRealConditions() throws {269 // Small sample → assumption warning; large sample → none.270 let small = try ConfidenceIntervals.meanT(SampleSummary(count: 8, mean: 1, standardDeviation: 2))271 #expect(small.verification.status == .warning && small.verification.warnings.contains { $0.kind == .assumption })272 let large = try ConfidenceIntervals.meanT(SampleSummary(count: 80, mean: 1, standardDeviation: 2))273 #expect(large.verification.isVerified)274 // Wald interval leaving [0, 1] and failing the success/failure condition.275 let wald = try ConfidenceIntervals.proportion(successes: 1, trials: 12, method: .wald)276 #expect(wald.lower < 0 && wald.verification.warnings.count == 2)277 #expect(try ConfidenceIntervals.proportion(successes: 400, trials: 1000, method: .wald).verification.isVerified)278 // Clopper–Pearson limits are checked by evaluating the binomial tails back.279 let cp = try ConfidenceIntervals.proportion(successes: 7, trials: 20, method: .clopperPearson)280 #expect(cp.verification.checks.filter { $0.kind == .residual && $0.status == .passed }.count == 3)281 // One-proportion test with n·p₀ < 10.282 #expect(try HypothesisTests.oneProportionZ(successes: 2, trials: 30, p0: 0.1).verification.warnings.contains { $0.name.contains("Normal approximation") })283 // ANOVA with unequal spreads.284 let anova = try HypothesisTests.oneWayANOVA(summaries: [SampleSummary(count: 40, mean: 1, standardDeviation: 1), SampleSummary(count: 40, mean: 2, standardDeviation: 5)])285 #expect(anova.test.verification.warnings.contains { $0.name.contains("Equal variances") })286 // Every test reports that its p-value and critical value tell the same story.287 let t = try HypothesisTests.twoSampleT(SampleSummary(count: 40, mean: 1, standardDeviation: 1), SampleSummary(count: 50, mean: 1.4, standardDeviation: 1.2))288 #expect(t.verification.checks.contains { $0.kind == .crossCheck && $0.status == .passed } && t.verification.isVerified)289 }290291 @Test func errorPaths() throws {292 let s = try SampleSummary(count: 10, mean: 5, standardDeviation: 2)293 #expect(throws: StatisticsError.emptyData) { try SampleSummary(data: []) }294 #expect(throws: StatisticsError.insufficientSample(required: 2, actual: 1)) { try SampleSummary(data: [3]) }295 #expect(throws: StatisticsError.self) { try SampleSummary(count: 0, mean: 1, standardDeviation: 1) }296 #expect(throws: StatisticsError.self) { try SampleSummary(count: 5, mean: 1, standardDeviation: -1) }297 #expect(throws: StatisticsError.self) { try SampleSummary(count: 5, mean: .nan, standardDeviation: 1) }298 #expect(throws: StatisticsError.probabilityOutOfRange(1)) { try ConfidenceIntervals.meanT(s, level: 1) }299 #expect(throws: StatisticsError.probabilityOutOfRange(0)) { try ConfidenceIntervals.meanT(s, level: 0) }300 #expect(throws: StatisticsError.probabilityOutOfRange(95)) { try ConfidenceIntervals.meanZ(mean: 1, sigma: 1, n: 5, level: 95) }301 #expect(throws: StatisticsError.self) { try ConfidenceIntervals.meanZ(mean: 1, sigma: 0, n: 5) }302 #expect(throws: StatisticsError.self) { try ConfidenceIntervals.meanZ(mean: 1, sigma: 1, n: 0) }303 #expect(throws: StatisticsError.self) { try ConfidenceIntervals.meanT(SampleSummary(count: 1, mean: 1, standardDeviation: 0)) }304 #expect(throws: StatisticsError.self) { try ConfidenceIntervals.proportion(successes: 5, trials: 0) }305 #expect(throws: StatisticsError.self) { try ConfidenceIntervals.proportion(successes: 11, trials: 10) }306 #expect(throws: StatisticsError.self) { try ConfidenceIntervals.proportion(successes: -1, trials: 10) }307 #expect(throws: StatisticsError.self) { try ConfidenceIntervals.correlation(r: 1, n: 10) }308 #expect(throws: StatisticsError.self) { try ConfidenceIntervals.correlation(r: 0.5, n: 3) }309 #expect(throws: StatisticsError.self) { try ConfidenceIntervals.pairedMean([1, 2, 3], [1, 2]) }310 #expect(throws: StatisticsError.self) { try ConfidenceIntervals.variance(SampleSummary(count: 1, mean: 0, standardDeviation: 0)) }311 #expect(throws: StatisticsError.probabilityOutOfRange(0)) { try HypothesisTests.oneSampleT(s, mu0: 1, alpha: 0) }312 #expect(throws: StatisticsError.probabilityOutOfRange(1.2)) { try HypothesisTests.oneSampleZ(mean: 1, n: 5, sigma: 1, mu0: 0, alpha: 1.2) }313 #expect(throws: StatisticsError.self) { try HypothesisTests.oneSampleT(SampleSummary(count: 5, mean: 1, standardDeviation: 0), mu0: 1) }314 #expect(throws: StatisticsError.self) { try HypothesisTests.oneSampleT(data: [4, 4, 4], mu0: 1) }315 #expect(throws: StatisticsError.self) { try HypothesisTests.oneSampleT(data: [], mu0: 1) }316 #expect(throws: StatisticsError.self) { try HypothesisTests.twoSampleT(data: [1, 2, 3], [5]) }317 #expect(throws: StatisticsError.self) { try HypothesisTests.twoSampleT(data: [2, 2], [5, 5]) }318 #expect(throws: StatisticsError.self) { try HypothesisTests.pairedT([1, 2, 3], [1, 2]) }319 #expect(throws: StatisticsError.self) { try HypothesisTests.oneProportionZ(successes: 3, trials: 10, p0: 0) }320 #expect(throws: StatisticsError.self) { try HypothesisTests.oneProportionZ(successes: 3, trials: 10, p0: 1.5) }321 #expect(throws: StatisticsError.self) { try HypothesisTests.twoProportionZ(successes1: 0, trials1: 10, successes2: 0, trials2: 12) }322 #expect(throws: StatisticsError.self) { try HypothesisTests.chiSquareGoodnessOfFit(observed: [5]) }323 #expect(throws: StatisticsError.self) { try HypothesisTests.chiSquareGoodnessOfFit(observed: [5, -1, 3]) }324 #expect(throws: StatisticsError.self) { try HypothesisTests.chiSquareGoodnessOfFit(observed: [0, 0, 0]) }325 #expect(throws: StatisticsError.self) { try HypothesisTests.chiSquareGoodnessOfFit(observed: [5, 6, 7], expectedProportions: [0.5, 0.5]) }326 #expect(throws: StatisticsError.self) { try HypothesisTests.chiSquareGoodnessOfFit(observed: [5, 6, 7], expectedProportions: [0.5, 0, 0.5]) }327 #expect(throws: StatisticsError.self) { try HypothesisTests.chiSquareGoodnessOfFit(observed: [5, 6, 7], expectedCounts: [5, 6, 8]) }328 #expect(throws: StatisticsError.self) { try HypothesisTests.chiSquareGoodnessOfFit(observed: [5, 6], estimatedParameters: 1) }329 #expect(throws: StatisticsError.self) { try HypothesisTests.chiSquareIndependence(table: [[1, 2, 3]]) }330 #expect(throws: StatisticsError.self) { try HypothesisTests.chiSquareIndependence(table: [[1, 2], [3]]) }331 #expect(throws: StatisticsError.self) { try HypothesisTests.chiSquareIndependence(table: [[1, 2], [0, 0]]) }332 #expect(throws: StatisticsError.self) { try HypothesisTests.chiSquareIndependence(table: [[1, -2], [3, 4]]) }333 #expect(throws: StatisticsError.self) { try HypothesisTests.oneWayANOVA(groups: [[1, 2, 3]]) }334 #expect(throws: StatisticsError.self) { try HypothesisTests.oneWayANOVA(groups: [[1], [2]]) }335 #expect(throws: StatisticsError.self) { try HypothesisTests.oneWayANOVA(groups: [[1, 1], [2, 2]]) }336 #expect(throws: StatisticsError.self) { try HypothesisTests.oneWayANOVA(groups: [[1, 2], []]) }337 #expect(throws: StatisticsError.self) { try HypothesisTests.fTestVariances(data: [1, 1, 1], [1, 2, 3]) }338 #expect(throws: StatisticsError.self) { try HypothesisTests.correlationTest(r: 1.5, n: 10) }339 #expect(throws: StatisticsError.self) { try HypothesisTests.correlationTest(r: 0.5, n: 2) }340 #expect(throws: StatisticsError.self) { try HypothesisTests.correlationTest(r: 1, n: 10) }341 #expect(throws: StatisticsError.self) { try HypothesisTests.binomialExact(successes: 3, trials: 10, p0: 1.5) }342 }343}344