Repository Public
1import Foundation23extension Inference {4 private static func tieTerm(_ values: [Double]) -> Double {5 Descriptive.tieGroupSizes(values).reduce(0.0) { $0 + Double($1 * $1 * $1 - $1) }6 }78 /// Mann–Whitney U test (Wilcoxon rank-sum) by the NORMAL APPROXIMATION with tie correction:9 /// U₁ = R₁ − n₁(n₁+1)/2 (reported statistic, for the first sample), E[U] = n₁n₂/2,10 /// Var[U] = n₁n₂/12·((N+1) − Σ(t³−t)/(N(N−1))). `continuityCorrection` (default true) moves U by ½11 /// towards its mean. This matches `scipy.stats.mannwhitneyu(method="asymptotic")`; no exact small-sample12 /// distribution is computed, so prefer n₁, n₂ ≳ 8. `alternative: .greater` means the first sample tends to be larger.13 /// Effect size: rank-biserial correlation r = 2U₁/(n₁n₂) − 1.14 public static func mannWhitneyU(_ x: [Double], _ y: [Double], alternative: Alternative = .twoSided,15 continuityCorrection: Bool = true) throws -> HypothesisTest {16 let a = try clean(x, min: 1, "first sample"), b = try clean(y, min: 1, "second sample")17 let n1 = Double(a.count), n2 = Double(b.count), n = n1 + n218 let all = a + b19 let r = Descriptive.ranks(all)20 let r1 = Descriptive.sum(Array(r[0..<a.count]))21 let u1 = r1 - n1 * (n1 + 1) / 2, u2 = n1 * n2 - u122 let mu = n1 * n2 / 223 let variance = n1 * n2 / 12 * ((n + 1) - tieTerm(all) / (n * (n - 1)))24 guard variance > 0 else { throw StatError.invalidInput("all observations are tied: the test is undefined") }25 let sd = variance.squareRoot(), cc = continuityCorrection ? 0.5 : 0.026 let norm = NormalDistribution.standard27 let p: Double, z: Double28 switch alternative {29 case .twoSided:30 z = (max(u1, u2) - mu - cc) / sd31 p = min(1, 2 * norm.sf(z))32 case .greater:33 z = (u1 - mu - cc) / sd34 p = norm.sf(z)35 case .less:36 z = (u2 - mu - cc) / sd37 p = norm.sf(z)38 }39 return HypothesisTest(name: "Mann–Whitney U test", statisticName: "U", statistic: u1, pValue: p, alternative: alternative,40 pValueDescription: alternative.label + ", normal approximation with tie correction" + (continuityCorrection ? " and continuity correction" : ""),41 effectSize: NamedValue("Rank-biserial correlation", 2 * u1 / (n1 * n2) - 1),42 details: [NamedValue("n1", n1), NamedValue("n2", n2), NamedValue("U2", u2), NamedValue("z", z), NamedValue("Rank sum 1", r1)],43 assumptions: ["The two samples are independent", "Observations are at least ordinal",44 "For a statement about medians the two distributions must have the same shape"],45 notes: ["Normal approximation: unreliable for very small samples (no exact distribution is computed)"])46 }4748 /// Wilcoxon signed-rank test for paired data or a single sample of differences, by the NORMAL49 /// APPROXIMATION with tie correction. Zero differences are discarded (Wilcoxon's method); the50 /// remaining |d| are ranked with average ranks; z = (W⁺ − n(n+1)/4) / √(n(n+1)(2n+1)/24 − Σ(t³−t)/48).51 /// The reported statistic is min(W⁺, W⁻) for the two-sided test and W⁺ otherwise52 /// (`scipy.stats.wilcoxon(method="asymptotic")`). `continuityCorrection` is off by default, as in SciPy.53 /// Effect size: matched-pairs rank-biserial correlation (W⁺ − W⁻)/(n(n+1)/2).54 public static func wilcoxonSignedRank(_ x: [Double], _ y: [Double]? = nil, alternative: Alternative = .twoSided,55 continuityCorrection: Bool = false) throws -> HypothesisTest {56 var d = try clean(x, min: 1, "sample")57 if let y {58 guard y.count == x.count else { throw StatError.invalidInput("paired samples must have the same length") }59 d = zip(d, try clean(y, min: 1, "second sample")).map { $0 - $1 }60 }61 let zeros = d.filter { $0 == 0 }.count62 d = d.filter { $0 != 0 }63 guard !d.isEmpty else { throw StatError.invalidInput("all differences are zero") }64 let n = Double(d.count)65 let absd = d.map { abs($0) }66 let r = Descriptive.ranks(absd)67 var wPlus = 0.0, wMinus = 0.068 for (i, v) in d.enumerated() { if v > 0 { wPlus += r[i] } else { wMinus += r[i] } }69 let mean = n * (n + 1) / 470 let variance = n * (n + 1) * (2 * n + 1) / 24 - tieTerm(absd) / 4871 guard variance > 0 else { throw StatError.invalidInput("too few distinct differences") }72 var num = wPlus - mean73 if continuityCorrection {74 switch alternative {75 case .twoSided: num -= 0.5 * (num > 0 ? 1 : (num < 0 ? -1 : 0))76 case .greater: num -= 0.577 case .less: num += 0.578 }79 }80 let z = num / variance.squareRoot()81 let p = NormalDistribution.standard.pValue(z, alternative: alternative)82 return HypothesisTest(name: "Wilcoxon signed-rank test", statisticName: "W", statistic: alternative == .twoSided ? min(wPlus, wMinus) : wPlus,83 pValue: p, alternative: alternative,84 pValueDescription: alternative.label + ", normal approximation with tie correction" + (continuityCorrection ? " and continuity correction" : ""),85 effectSize: NamedValue("Matched-pairs rank-biserial correlation", (wPlus - wMinus) / (n * (n + 1) / 2)),86 details: [NamedValue("Non-zero differences", n), NamedValue("Zero differences dropped", Double(zeros)),87 NamedValue("W+", wPlus), NamedValue("W-", wMinus), NamedValue("z", z)],88 assumptions: ["Pairs are independent", "The differences are symmetric about their median", "Differences are at least interval-scaled"],89 notes: ["Normal approximation: unreliable below about 10 non-zero differences (no exact distribution is computed)"])90 }9192 /// Kruskal–Wallis H test (one-way ANOVA on ranks) with tie correction:93 /// H = [12/(N(N+1))·Σ Rᵢ²/nᵢ − 3(N+1)] / [1 − Σ(t³−t)/(N³−N)], referred to χ²(k−1), upper tail.94 /// Effect size: ε² = H/(N−1).95 public static func kruskalWallis(_ groups: [[Double]]) throws -> HypothesisTest {96 guard groups.count >= 2 else { throw StatError.insufficientData("need at least two groups") }97 let gs = try groups.enumerated().map { try clean($1, min: 1, "group \($0 + 1)") }98 let all = gs.flatMap { $0 }99 let n = Double(all.count), k = Double(gs.count)100 let r = Descriptive.ranks(all)101 var h = 0.0, offset = 0102 for g in gs {103 let rs = Descriptive.sum(Array(r[offset..<(offset + g.count)]))104 h += rs * rs / Double(g.count)105 offset += g.count106 }107 h = 12 / (n * (n + 1)) * h - 3 * (n + 1)108 let correction = 1 - tieTerm(all) / (n * n * n - n)109 guard correction > 0 else { throw StatError.invalidInput("all observations are tied: the test is undefined") }110 h /= correction111 return HypothesisTest(name: "Kruskal–Wallis H test", statisticName: "H", statistic: h, df: k - 1,112 pValue: ChiSquaredDistribution(df: k - 1).sf(h), alternative: .greater,113 pValueDescription: "upper tail of χ²(\(Int(k - 1))), tie-corrected",114 effectSize: NamedValue("Epsilon squared", h / (n - 1)),115 details: [NamedValue("Groups", k), NamedValue("Observations", n), NamedValue("Tie correction factor", correction)],116 assumptions: ["Groups are independent samples", "Observations are at least ordinal",117 "χ² approximation: each group should have about 5 or more observations"])118 }119}120