SPB Git

Repository Public

CoreTests.swift 19.4 KB 370 lines swift

1import Foundation2import Testing3import AnalyticsTestSupport4@testable import Statistics56let coreFixture = Fixture.load("core", .module)78@Suite struct SpecialFunctionTests {9    @Test func incompleteGamma() {10        for row in coreFixture["gamma"].list {11            let r = row.arr12            let (p, q) = SpecialFunctions.gammaPQ(r[0], r[1])13            #expect(isClose(p, r[2], rel: 1e-11, abs: 1e-300), "P(\(r[0]),\(r[1])) = \(p) vs \(r[2])")14            #expect(isClose(q, r[3], rel: 1e-11, abs: 1e-300), "Q(\(r[0]),\(r[1])) = \(q) vs \(r[3])")15        }16    }1718    @Test func incompleteBeta() {19        for row in coreFixture["beta"].list {20            let r = row.arr21            let (p, q) = SpecialFunctions.betaIPair(r[0], r[1], x: r[2], y: 1 - r[2])22            #expect(isClose(p, r[3], rel: 1e-11, abs: 1e-300), "I(\(r[0]),\(r[1]),\(r[2])) = \(p) vs \(r[3])")23            #expect(isClose(q, r[4], rel: 1e-11, abs: 1e-300), "Ic(\(r[0]),\(r[1]),\(r[2])) = \(q) vs \(r[4])")24        }25    }2627    @Test func logGamma() {28        for row in coreFixture["lgamma"].list {29            #expect(isClose(SpecialFunctions.logGamma(row[0].d), row[1].d, rel: 1e-14, abs: 1e-15))30        }31    }32}3334@Suite struct DistributionTests {35    /// Checks pdf/cdf/sf at the fixture points and the two inverses at the fixture probabilities.36    func check(_ d: some ContinuousDistribution, _ f: JSON, _ name: String, rel: Double = 1e-10) {37        let x = f["x"].arr38        for (i, xi) in x.enumerated() {39            #expect(isClose(d.pdf(xi), f["pdf"][i].d, rel: rel, abs: 1e-300), "\(name) pdf(\(xi)) = \(d.pdf(xi)) vs \(f["pdf"][i].d)")40            #expect(isClose(d.cdf(xi), f["cdf"][i].d, rel: rel, abs: 1e-300), "\(name) cdf(\(xi)) = \(d.cdf(xi)) vs \(f["cdf"][i].d)")41            #expect(isClose(d.sf(xi), f["sf"][i].d, rel: rel, abs: 1e-300), "\(name) sf(\(xi)) = \(d.sf(xi)) vs \(f["sf"][i].d)")42        }43        for (i, p) in f["p"].arr.enumerated() {44            let want = f["ppf"][i].d45            #expect(isClose(d.quantile(p), want, rel: rel, abs: 1e-14), "\(name) quantile(\(p)) = \(d.quantile(p)) vs \(want)")46        }47        for (i, q) in f["q"].arr.enumerated() {48            let want = f["isf"][i].d49            #expect(isClose(d.isf(q), want, rel: rel, abs: 1e-14), "\(name) isf(\(q)) = \(d.isf(q)) vs \(want)")50        }51    }5253    @Test func normal() {54        check(NormalDistribution.standard, coreFixture["normal"], "normal")55        let f = coreFixture["normal_loc_scale"]56        check(NormalDistribution(mean: f["mean"].d, sd: f["sd"].d), f, "normal(μ,σ)")57        #expect(NormalDistribution.standard.quantile(0) == -.infinity)58        #expect(NormalDistribution.standard.quantile(1) == .infinity)59        #expect(NormalDistribution.standard.quantile(1.5).isNaN)60    }6162    @Test func studentT() {63        for f in coreFixture["t"].list { check(StudentTDistribution(df: f["df"].d), f, "t(\(f["df"].d))") }64        #expect(StudentTDistribution(df: 7).quantile(0.5) == 0)65        #expect(StudentTDistribution(df: 7).cdf(.infinity) == 1)66    }6768    @Test func chiSquared() {69        for f in coreFixture["chi2"].list { check(ChiSquaredDistribution(df: f["df"].d), f, "chi2(\(f["df"].d))") }70        #expect(ChiSquaredDistribution(df: 3).cdf(-1) == 0)71        #expect(ChiSquaredDistribution(df: 3).sf(-1) == 1)72    }7374    @Test func fisherF() {75        for f in coreFixture["f"].list { check(FisherFDistribution(d1: f["d1"].d, d2: f["d2"].d), f, "F(\(f["d1"].d),\(f["d2"].d))") }76    }7778    @Test func beta() {79        for f in coreFixture["betadist"].list { check(BetaDistribution(a: f["a"].d, b: f["b"].d), f, "beta(\(f["a"].d),\(f["b"].d))") }80    }8182    @Test func roundTrips() {83        // quantile(cdf(x)) = x across distributions (independent of the references)84        let t = StudentTDistribution(df: 4.2), c = ChiSquaredDistribution(df: 6.5), f = FisherFDistribution(d1: 3, d2: 17)85        for x in [0.05, 0.4, 1.3, 2.9, 7.5] {86            #expect(isClose(t.quantile(t.cdf(x)), x, rel: 1e-11))87            #expect(isClose(t.isf(t.sf(x)), x, rel: 1e-11))88            #expect(isClose(c.quantile(c.cdf(x)), x, rel: 1e-11))89            #expect(isClose(c.isf(c.sf(x)), x, rel: 1e-11))90            #expect(isClose(f.quantile(f.cdf(x)), x, rel: 1e-11))91            #expect(isClose(f.isf(f.sf(x)), x, rel: 1e-11))92        }93    }9495    @Test func extremeTailsRoundTrip() {96        // far beyond where SciPy's inverses are usable: the inverse must reproduce the tail probability97        for q in [1e-300, 1e-200, 1e-100, 1e-50] {98            for df in [0.5, 1, 2.5, 5, 30, 1000] {99                let t = StudentTDistribution(df: df)100                let x = t.isf(q)101                if df * 300 < -log10(q) { #expect(x == .infinity); continue }    // the quantile itself overflows102                #expect(x.isFinite && x > 0, "t(\(df)) isf(\(q)) = \(x)")103                #expect(isClose(t.sf(x), q, rel: 1e-9), "t(\(df)) sf(isf(\(q))) = \(t.sf(x))")104                #expect(t.quantile(q) == -x)105            }106            for df in [1.0, 4, 100, 5000] {107                let c = ChiSquaredDistribution(df: df)108                #expect(isClose(c.sf(c.isf(q)), q, rel: 1e-9), "chi2(\(df)) sf(isf(\(q)))")109                if c.quantile(q) > 1e-290 { #expect(isClose(c.cdf(c.quantile(q)), q, rel: 1e-9), "chi2(\(df)) cdf(quantile(\(q)))") }110            }111            let f = FisherFDistribution(d1: 4, d2: 9)112            #expect(isClose(f.sf(f.isf(q)), q, rel: 1e-9))113            #expect(isClose(f.cdf(f.quantile(q)), q, rel: 1e-9))114        }115        // F(2,2) closed form: sf(x) = 1/(1+x)116        for q in [1e-100, 1e-10, 0.2] { #expect(isClose(FisherFDistribution(d1: 2, d2: 2).isf(q), (1 - q) / q, rel: 1e-11)) }117        // Cauchy closed form: isf(q) = cot(πq)118        #expect(isClose(StudentTDistribution(df: 1).isf(1e-300), 1 / (Double.pi * 1e-300), rel: 1e-12))119        #expect(isClose(StudentTDistribution(df: 1).isf(0.01), 1 / tan(Double.pi * 0.01), rel: 1e-12))120    }121122    @Test func pValueTails() {123        let n = NormalDistribution.standard124        #expect(isClose(n.pValue(1.96, alternative: .twoSided), 2 * n.sf(1.96), rel: 1e-15))125        #expect(isClose(n.pValue(-1.96, alternative: .twoSided), 2 * n.sf(1.96), rel: 1e-12))126        #expect(n.pValue(1.0, alternative: .less) == n.cdf(1.0))127        #expect(n.pValue(1.0, alternative: .greater) == n.sf(1.0))128    }129}130131@Suite struct RandomTests {132    @Test func xoshiroMatchesReference() {133        for (seedText, f) in coreFixture["xoshiro"].dict {134            let seed = UInt64(seedText)!135            var g = Xoshiro256StarStar(seed: seed)136            for want in f["u64"].strings { #expect(g.next() == UInt64(want)!, "seed \(seed)") }137            g = Xoshiro256StarStar(seed: seed)138            for want in f["uniform"].arr { #expect(g.nextUniform() == want) }139            g = Xoshiro256StarStar(seed: seed)140            for want in f["normal"].arr { #expect(isClose(g.nextNormal(), want, rel: 1e-12, abs: 1e-14)) }141            g = Xoshiro256StarStar(seed: seed)142            g.jump()143            for want in f["after_jump"].strings { #expect(g.next() == UInt64(want)!, "jump, seed \(seed)") }144        }145    }146147    @Test func seededStreamsAreReproducibleAndDistinct() {148        var a = Xoshiro256StarStar(seed: 7), b = Xoshiro256StarStar(seed: 7), c = Xoshiro256StarStar(seed: 8)149        let xa = (0..<100).map { _ in a.next() }, xb = (0..<100).map { _ in b.next() }, xc = (0..<100).map { _ in c.next() }150        #expect(xa == xb)151        #expect(xa != xc)152    }153154    @Test func uniformAndNormalMoments() {155        var g = Xoshiro256StarStar(seed: 2026)156        let n = 200_000157        var u = [Double](), z = [Double]()158        for _ in 0..<n { u.append(g.nextUniform()); z.append(g.nextNormal()) }159        #expect(u.allSatisfy { $0 >= 0 && $0 < 1 })160        // tolerances are ~5 standard errors of each estimator161        #expect(abs(Descriptive.mean(u) - 0.5) < 5 * (1.0 / 12).squareRoot() / Double(n).squareRoot())162        #expect(abs(Descriptive.variance(u) - 1.0 / 12) < 0.001)163        #expect(abs(Descriptive.mean(z)) < 5 / Double(n).squareRoot())164        #expect(abs(Descriptive.variance(z) - 1) < 5 * (2.0 / Double(n)).squareRoot())165    }166167    @Test func boundedIntegersAreUniform() {168        var g = Xoshiro256StarStar(seed: 99)169        var counts = [Int](repeating: 0, count: 7)170        let n = 70_000171        for _ in 0..<n { counts[g.nextInt(below: 7)] += 1 }172        // chi-square goodness of fit against the uniform: 6 df, 99.9 % critical value 22.46173        let expected = Double(n) / 7174        let chi = counts.reduce(0.0) { $0 + (Double($1) - expected) * (Double($1) - expected) / expected }175        #expect(chi < 22.46)176        var items = Array(0..<50)177        g.shuffle(&items)178        #expect(items.sorted() == Array(0..<50))179        #expect(items != Array(0..<50))180    }181182    @Test func inputDistributionQuantilesAndMoments() {183        let f = coreFixture["input"]184        let us = f["u"].arr185        func check(_ d: InputDistribution, _ key: String, rel: Double = 1e-10) {186            #expect(d.validationError == nil, "\(key)")187            let sampler = DistributionSampler(d)188            for (i, u) in us.enumerated() {189                #expect(isClose(d.quantile(u), f[key]["q"][i].d, rel: rel), "\(key) q(\(u)) = \(d.quantile(u)) vs \(f[key]["q"][i].d)")190                #expect(sampler.quantile(u) == d.quantile(u), "\(key) sampler")191            }192            #expect(isClose(d.mean, f[key]["mean"].d, rel: 1e-12), "\(key) mean")193            #expect(isClose(d.variance, f[key]["var"].d, rel: 1e-12), "\(key) variance")194        }195        check(.normal(mean: 10, sd: 2), "normal")196        check(.lognormal(mu: 1.2, sigma: 0.4), "lognormal")197        check(.uniform(min: -2, max: 5), "uniform")198        check(.triangular(min: 1, mode: 4, max: 11), "triangular")199        check(.pert(min: 2, mode: 5, max: 14, lambda: 4), "pert")200        check(.discrete(values: f["discrete"]["values"].arr, probabilities: f["discrete"]["probs"].arr), "discrete")201        check(.empirical(f["empirical"]["data"].arr), "empirical")202        let ln = f["lognormal_mean_sd"]203        guard case .lognormal(let mu, let sigma) = InputDistribution.lognormal(mean: ln["mean"].d, sd: ln["sd"].d) else { Issue.record("not lognormal"); return }204        #expect(isClose(mu, ln["mu"].d, rel: 1e-13))205        #expect(isClose(sigma, ln["sigma"].d, rel: 1e-13))206        #expect(isClose(InputDistribution.lognormal(mean: 120, sd: 45).mean, 120, rel: 1e-12))207        #expect(isClose(InputDistribution.lognormal(mean: 120, sd: 45).variance, 45 * 45, rel: 1e-12))208    }209210    @Test func inputDistributionValidationAndSampling() {211        #expect(InputDistribution.normal(mean: 0, sd: -1).validationError != nil)212        #expect(InputDistribution.uniform(min: 2, max: 1).validationError != nil)213        #expect(InputDistribution.triangular(min: 0, mode: 5, max: 3).validationError != nil)214        #expect(InputDistribution.pert(min: 0, mode: 1, max: 3, lambda: 0).validationError != nil)215        #expect(InputDistribution.discrete(values: [1, 2], probabilities: [1]).validationError != nil)216        #expect(InputDistribution.empirical([]).validationError != nil)217        #expect(InputDistribution.constant(4).quantile(0.3) == 4)218        // sample moments converge to the theoretical ones (5 standard errors)219        let cases: [InputDistribution] = [.normal(mean: 3, sd: 2), .lognormal(mu: 0.1, sigma: 0.5), .uniform(min: 1, max: 4),220                                           .triangular(min: 0, mode: 2, max: 10), .pert(min: 0, mode: 2, max: 10, lambda: 4),221                                           .discrete(values: [0, 1, 10], probabilities: [5, 3, 2])]222        for d in cases {223            var g = Xoshiro256StarStar(seed: 314159)224            let sampler = DistributionSampler(d)225            let n = 40_000226            let xs = (0..<n).map { _ in sampler.sample(using: &g) }227            let se = (d.variance / Double(n)).squareRoot()228            #expect(abs(Descriptive.mean(xs) - d.mean) < 5 * se, "\(d.label)")229            #expect(isClose(Descriptive.variance(xs), d.variance, rel: 0.06), "\(d.label)")230        }231    }232}233234@Suite struct LinearAlgebraTests {235    let f = coreFixture["linalg"]236    var a: Matrix { Matrix(f["A"].mat) }237    var s: Matrix { Matrix(f["S"].mat) }238239    @Test func productAndTranspose() {240        let ata = a.transposed * a241        #expect(allClose(ata.data, f["AtA"].mat.flatMap { $0 }, rel: 1e-13, abs: 1e-14))242        #expect(a.gram == ata)243        #expect(a.transposed.transposed == a)244        #expect(Matrix(columns: a.columnArrays) == a)245        #expect((Matrix.identity(4) * [1, 2, 3, 4]) == [1, 2, 3, 4])246    }247248    @Test func qr() throws {249        let (q, r) = try LinearAlgebra.qr(a)250        #expect(allClose((q * r).data, a.data, rel: 1e-12, abs: 1e-13))251        #expect(allClose((q.transposed * q).data, Matrix.identity(4).data, rel: 1e-12, abs: 1e-13))252        #expect(allClose(r.data.map { abs($0) }, f["R_abs"].mat.flatMap { $0 }, rel: 1e-12, abs: 1e-13))253        for i in 1..<4 { for j in 0..<i { #expect(r[i, j] == 0) } }254    }255256    @Test func svd() throws {257        let (u, sv, vt) = try LinearAlgebra.svd(a)258        #expect(allClose(sv, f["sv"].arr, rel: 1e-12))259        #expect(allClose((u * Matrix.diagonal(sv) * vt).data, a.data, rel: 1e-12, abs: 1e-13))260        #expect(isClose(try LinearAlgebra.conditionNumber(a), f["cond"].d, rel: 1e-12))261    }262263    @Test func eigenCholeskySolveInverse() throws {264        let (w, v) = try LinearAlgebra.symmetricEigen(s)265        #expect(allClose(w, f["eig"].arr, rel: 1e-12))266        #expect(allClose((v * Matrix.diagonal(w) * v.transposed).data, s.data, rel: 1e-11, abs: 1e-12))267        let l = try LinearAlgebra.cholesky(s)268        #expect(allClose(l.data, f["chol"].mat.flatMap { $0 }, rel: 1e-12, abs: 1e-14))269        let b = Matrix(f["B"].mat)270        #expect(allClose(try LinearAlgebra.solve(s, b).data, f["solve"].mat.flatMap { $0 }, rel: 1e-11, abs: 1e-13))271        #expect(allClose(try LinearAlgebra.inverse(s).data, f["inv"].mat.flatMap { $0 }, rel: 1e-11, abs: 1e-13))272        #expect(isClose(try LinearAlgebra.logDeterminantSPD(s), f["logdet"].d, rel: 1e-12))273        let (_, r) = try LinearAlgebra.qr(a)274        let x = try LinearAlgebra.backSubstitute(r, [1, 2, 3, 4])275        #expect(allClose(r * x, [1, 2, 3, 4], rel: 1e-12))276    }277278    @Test func failuresAreReported() {279        let singular = Matrix([[1, 2], [2, 4]])280        #expect(throws: StatError.self) { try LinearAlgebra.solve(singular, [1, 1]) }281        #expect(throws: StatError.self) { try LinearAlgebra.inverse(singular) }282        #expect(throws: StatError.self) { try LinearAlgebra.cholesky(Matrix([[1, 2], [2, 1]])) }283        #expect(throws: StatError.self) { try LinearAlgebra.qr(Matrix(rows: 2, cols: 3)) }284    }285}286287@Suite struct OptimizationTests {288    let f = coreFixture["optim"]289290    @Test func brentRoots() throws {291        let r1 = try RootFinder.brent({ cos($0) - $0 }, lower: 0, upper: 1)292        #expect(r1.converged && isClose(r1.root, f["root_cos"].d, rel: 1e-12))293        let r2 = try RootFinder.brent({ $0 * $0 * $0 - 2 * $0 - 5 }, lower: 2, upper: 3)294        #expect(r2.converged && isClose(r2.root, f["root_cubic"].d, rel: 1e-12))295        let r3 = try RootFinder.brent({ exp(-$0) - 1e-9 }, lower: 0, upper: 100)296        #expect(r3.converged && isClose(r3.root, f["root_exp"].d, rel: 1e-9))297        #expect(isClose(r3.root, -log(1e-9), rel: 1e-9))298        #expect(throws: StatError.self) { try RootFinder.brent({ $0 * $0 + 1 }, lower: -1, upper: 1) }299        let exact = try RootFinder.brent({ $0 - 1 }, lower: 1, upper: 2)300        #expect(exact.root == 1 && exact.iterations == 0)301    }302303    @Test func bracketAndSecant() throws {304        let b = RootFinder.bracket({ $0 * $0 - 612 }, guess: 1)305        #expect(b != nil)306        if let b {307            let r = try RootFinder.brent({ $0 * $0 - 612 }, lower: b.lower, upper: b.upper)308            #expect(isClose(abs(r.root), 612.0.squareRoot(), rel: 1e-12))309        }310        #expect(RootFinder.bracket({ $0 * $0 + 1 }, guess: 0) == nil)311        let clamped = RootFinder.bracket({ $0 - 50 }, guess: 1, min: 0, max: 10)312        #expect(clamped == nil)313        let s = RootFinder.secant({ $0 * $0 - 612 }, x0: 10, x1: 30)314        #expect(s.converged && isClose(s.root, 612.0.squareRoot(), rel: 1e-10))315        let flat = RootFinder.secant({ _ in 3 }, x0: 0, x1: 1)316        #expect(!flat.converged)317    }318319    @Test func scalarMinimisation() {320        let g: (Double) -> Double = { ($0 - 2) * ($0 - 2) * ($0 + 1) + sin(3 * $0) }321        let b = Minimizer.brent(g, lower: 0, upper: 4, xtol: 1e-12)322        #expect(b.converged && isClose(b.x[0], f["min_g1_x"].d, rel: 1e-7))323        #expect(isClose(b.value, f["min_g1_f"].d, rel: 1e-12))324        let gs = Minimizer.goldenSection(g, lower: 0, upper: 4, xtol: 1e-10)325        #expect(gs.converged && isClose(gs.x[0], f["min_g1_x"].d, rel: 1e-6))326        #expect(gs.functionEvaluations > b.functionEvaluations)327        let parab = Minimizer.brent({ ($0 - 1.25) * ($0 - 1.25) + 3 }, lower: -10, upper: 10)328        #expect(isClose(parab.x[0], 1.25, rel: 1e-7) && isClose(parab.value, 3, rel: 1e-14))329    }330331    @Test func nelderMead() throws {332        var opt = Minimizer.NelderMeadOptions()333        opt.xtol = 1e-10; opt.ftol = 1e-10; opt.maxIterations = 5000; opt.maxEvaluations = 5000334        let rosen = try Minimizer.nelderMead({ p in 100 * pow(p[1] - p[0] * p[0], 2) + pow(1 - p[0], 2) }, start: [-1.2, 1], options: opt)335        #expect(rosen.converged)336        #expect(allClose(rosen.x, [1, 1], rel: 1e-7))                      // analytic minimum337        #expect(allClose(rosen.x, f["rosen_x"].arr, rel: 1e-7))            // SciPy lands on the same point338        #expect(rosen.value < 1e-14)339        let quad: ([Double]) -> Double = { p in pow(p[0] - 3, 2) + 2 * pow(p[1] + 1, 2) + 0.5 * pow(p[2] - 0.25, 2) + 0.1 * p[0] * p[1] }340        opt.ftol = 1e-12341        let bounded = try Minimizer.nelderMead(quad, start: [0.5, 0.5, 0.5], bounds: [0...2, 0...5, -1...1], options: opt)342        #expect(allClose(bounded.x, [2, 0, 0.25], rel: 1e-6, abs: 1e-7))  // analytic constrained minimum343        #expect(isClose(bounded.value, 3, rel: 1e-10))344        // plain Nelder–Mead with clipping (what SciPy runs) collapses onto the bound short of the minimum; restarts fix it345        opt.restarts = 0346        let plain = try Minimizer.nelderMead(quad, start: [0.5, 0.5, 0.5], bounds: [0...2, 0...5, -1...1], options: opt)347        #expect(plain.value >= bounded.value)348        #expect(bounded.value <= f["bounded_f"].d + 1e-12)349        #expect(throws: StatError.self) { try Minimizer.nelderMead({ _ in 0 }, start: []) }350        #expect(throws: StatError.self) { try Minimizer.nelderMead({ $0[0] }, start: [1], bounds: [0...1, 0...1]) }351    }352}353354@Suite struct BasicDescriptiveTests {355    @Test func basics() {356        let x = [2.0, 4, 4, 4, 5, 5, 7, 9]357        #expect(Descriptive.mean(x) == 5)358        #expect(Descriptive.variance(x, sample: false) == 4)359        #expect(isClose(Descriptive.variance(x), 32.0 / 7, rel: 1e-15))360        #expect(Descriptive.standardDeviation(x, sample: false) == 2)361        #expect(Descriptive.mean([]).isNaN && Descriptive.variance([1]).isNaN)362        #expect(Descriptive.ranks([10, 20, 10, 30, 20, 10]) == [2, 4.5, 2, 6, 4.5, 2])363        #expect(Descriptive.tieGroupSizes([10, 20, 10, 30, 20, 10]) == [3, 2])364        #expect(isClose(Descriptive.pearson([1, 2, 3, 4], [2, 4, 6, 8.5]), coreFixture["pearson"]["r"].d, rel: 1e-14))365        #expect(isClose(Descriptive.covariance([1, 2, 3, 4], [2, 4, 6, 8.5]), coreFixture["pearson"]["cov"].d, rel: 1e-14))366        // compensated summation keeps tiny terms that naive summation drops367        #expect(Descriptive.sum([1e16, 1, -1e16, 1]) == 2)368    }369}370
Files main
Menu

Theme