SPB Git

Repository Public

CorrelationTests.swift 4.2 KB 58 lines swift

1import Foundation23extension Inference {4    /// Test of H0: no association between two paired series.5    /// - Pearson: t = r·√((n−2)/(1−r²)) with n−2 df (exact under bivariate normality); CI by Fisher's z transform.6    /// - Spearman: the same t approximation applied to ρ (SciPy's default); no CI.7    /// - Kendall τ-b: normal approximation with the full tie-corrected variance (SciPy `method="asymptotic"`); no CI.8    public static func correlationTest(_ x: [Double], _ y: [Double], method: CorrelationMethod = .pearson,9                                       alternative: Alternative = .twoSided, level: Double = 0.95) throws -> HypothesisTest {10        guard x.count == y.count else { throw StatError.invalidInput("the two series must have the same length") }11        let a = try clean(x, min: 3, "first series"), b = try clean(y, min: 3, "second series")12        try checkLevel(level)13        let n = Double(a.count)14        switch method {15        case .pearson, .spearman:16            let r = Descriptive.correlation(a, b, method: method)17            guard r.isFinite else { throw StatError.invalidInput("a series is constant: the correlation is undefined") }18            let df = n - 219            let t = abs(r) >= 1 ? (r > 0 ? Double.infinity : -Double.infinity) : r * (df / (1 - r * r)).squareRoot()20            let p = StudentTDistribution(df: df).pValue(t, alternative: alternative)21            var ci: ConfidenceInterval?22            if method == .pearson, a.count > 3 {23                let z = atanh(r), se = 1 / (n - 3).squareRoot()24                let h = NormalDistribution.standard.isf((1 - level) / 2) * se25                ci = ConfidenceInterval(estimate: r, lower: tanh(z - h), upper: tanh(z + h), level: level, method: "Fisher z transformation")26            }27            return HypothesisTest(name: method == .pearson ? "Pearson correlation test" : "Spearman rank correlation test",28                                  statisticName: "t", statistic: t, df: df, pValue: p, alternative: alternative,29                                  estimate: NamedValue(method == .pearson ? "r" : "ρ", r), confidenceInterval: ci,30                                  effectSize: NamedValue(method == .pearson ? "r²" : "ρ²", r * r), details: [NamedValue("n", n)],31                                  assumptions: method == .pearson32                                      ? ["Pairs are independent", "The relationship is linear", "The two variables are jointly normal (for the exact t distribution)"]33                                      : ["Pairs are independent", "The relationship is monotonic", "t approximation: n of about 10 or more"])34        case .kendall:35            let c = Descriptive.kendallCounts(a, b)36            guard c.tau.isFinite else { throw StatError.invalidInput("a series is constant: τ is undefined") }37            func groups(_ v: [Double]) -> (Double, Double, Double) {      // Σt(t−1), Σt(t−1)(t−2), Σt(t−1)(2t+5)38                var s1 = 0.0, s2 = 0.0, s3 = 0.039                for g in Descriptive.tieGroupSizes(v) {40                    let t = Double(g)41                    s1 += t * (t - 1); s2 += t * (t - 1) * (t - 2); s3 += t * (t - 1) * (2 * t + 5)42                }43                return (s1, s2, s3)44            }45            let (x1, x2, x3) = groups(a), (y1, y2, y3) = groups(b)46            let m = n * (n - 1)47            let variance = (m * (2 * n + 5) - x3 - y3) / 18 + x1 * y1 / (2 * m) + x2 * y2 / (9 * m * (n - 2))48            let z = Double(c.concordant - c.discordant) / variance.squareRoot()49            return HypothesisTest(name: "Kendall rank correlation test", statisticName: "z", statistic: z,50                                  pValue: NormalDistribution.standard.pValue(z, alternative: alternative), alternative: alternative,51                                  pValueDescription: alternative.label + ", normal approximation with tie correction",52                                  estimate: NamedValue("τ-b", c.tau),53                                  details: [NamedValue("n", n), NamedValue("Concordant pairs", Double(c.concordant)), NamedValue("Discordant pairs", Double(c.discordant))],54                                  assumptions: ["Pairs are independent", "The relationship is monotonic", "Normal approximation: n of about 10 or more"])55        }56    }57}58
Files main
Menu

Theme