SPB Git

Repository Public

Normality.swift 4.9 KB 96 lines swift

1import Foundation23extension Inference {4    /// Jarque–Bera test of normality: JB = n/6·(S² + K²/4) with the population skewness S and excess5    /// kurtosis K; asymptotically χ²(2), upper-tail p-value. Needs large samples (the χ² approximation is poor for n < ~50).6    public static func jarqueBera(_ x: [Double]) throws -> HypothesisTest {7        let v = try clean(x, min: 3, "sample")8        let s = Descriptive.skewness(v, sample: false), k = Descriptive.kurtosis(v, sample: false)9        guard s.isFinite, k.isFinite else { throw StatError.invalidInput("the sample is constant") }10        let jb = Double(v.count) / 6 * (s * s + k * k / 4)11        return HypothesisTest(name: "Jarque–Bera normality test", statisticName: "JB", statistic: jb, df: 2,12                              pValue: ChiSquaredDistribution(df: 2).sf(jb), alternative: .greater, pValueDescription: "upper tail of χ²(2)",13                              details: [NamedValue("n", Double(v.count)), NamedValue("Skewness (population)", s), NamedValue("Excess kurtosis (population)", k)],14                              assumptions: ["Observations are independent and identically distributed", "Large sample (asymptotic χ² reference distribution)"])15    }1617    private static func poly(_ c: [Double], _ x: Double) -> Double {18        var r = 0.019        for coef in c.reversed() { r = r * x + coef }20        return r21    }2223    /// Shapiro–Wilk test of normality by Royston's algorithm AS R94 (1995), valid for 3 ≤ n ≤ 5000.24    /// W close to 1 supports normality; the p-value is the probability of a W this small or smaller25    /// under normality (Royston's normalising transformation; exact for n = 3).26    public static func shapiroWilk(_ x: [Double]) throws -> HypothesisTest {27        let v = try clean(x, min: 3, "sample").sorted()28        let n = v.count29        guard n <= 5000 else { throw StatError.invalidInput("Royston's approximation is only valid up to n = 5000") }30        let range = v[n - 1] - v[0]31        guard range > 0 else { throw StatError.invalidInput("the sample is constant") }32        let nn2 = n / 233        var a = [Double](repeating: 0, count: nn2)      // weights of the pairs (x(n+1−i) − x(i))34        if n == 3 {35            a[0] = 0.5.squareRoot()36        } else {37            let an = Double(n)38            var m = [Double](repeating: 0, count: nn2)39            var summ2 = 0.040            for i in 0..<nn2 {41                m[i] = SpecialFunctions.normalQuantile((Double(i + 1) - 0.375) / (an + 0.25))42                summ2 += m[i] * m[i]43            }44            summ2 *= 245            let ssumm2 = summ2.squareRoot()46            let rsn = 1 / an.squareRoot()47            let a1 = poly([0, 0.221157, -0.147981, -2.071190, 4.434685, -2.706056], rsn) - m[0] / ssumm248            var first: Int, fac: Double49            if n > 5 {50                let a2 = -m[1] / ssumm2 + poly([0, 0.042981, -0.293762, -1.752461, 5.682633, -3.582633], rsn)51                fac = ((summ2 - 2 * m[0] * m[0] - 2 * m[1] * m[1]) / (1 - 2 * a1 * a1 - 2 * a2 * a2)).squareRoot()52                a[0] = a1; a[1] = a253                first = 254            } else {55                fac = ((summ2 - 2 * m[0] * m[0]) / (1 - 2 * a1 * a1)).squareRoot()56                a[0] = a157                first = 158            }59            if first < nn2 { for i in first..<nn2 { a[i] = -m[i] / fac } }60        }61        var b = 0.062        for i in 0..<nn2 { b += a[i] * (v[n - 1 - i] - v[i]) }63        let ss = Descriptive.sumSquaredDeviations(v)64        let w = min(1, b * b / ss)6566        var p: Double67        if n == 3 {68            p = max(0, 6 / Double.pi * (asin(w.squareRoot()) - asin(0.75.squareRoot())))69        } else {70            let an = Double(n)71            let y = log(1 - w)72            if n <= 11 {73                let gamma = poly([-2.273, 0.459], an)74                if y >= gamma {75                    p = 076                } else {77                    let yy = -log(gamma - y)78                    let mm = poly([0.5440, -0.39978, 0.025054, -6.714e-4], an)79                    let s = exp(poly([1.3822, -0.77857, 0.062767, -0.0020322], an))80                    p = NormalDistribution.standard.sf((yy - mm) / s)81                }82            } else {83                let xx = log(an)84                let mm = poly([-1.5861, -0.31082, -0.083751, 0.0038915], xx)85                let s = exp(poly([-0.4803, -0.082676, 0.0030302], xx))86                p = NormalDistribution.standard.sf((y - mm) / s)87            }88        }89        if w == 1 { p = 1 }90        return HypothesisTest(name: "Shapiro–Wilk normality test", statisticName: "W", statistic: w, pValue: p, alternative: .less,91                              pValueDescription: "probability of W this small or smaller under normality (Royston 1995)",92                              details: [NamedValue("n", Double(n))],93                              assumptions: ["Observations are independent and identically distributed", "3 ≤ n ≤ 5000; heavy ties (rounded data) reduce W"])94    }95}96
Files main
Menu

Theme