SPB Git

Repository Public MIT

RandomTests.swift 8.7 KB 162 lines swift

1// ORIX Calc2// File: Packages/ORIXStatistics/Tests/ORIXStatisticsTests/RandomTests.swift3// Description: Generator test vectors, seed reproducibility, and statistical checks of every sampler against its distribution.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9@testable import ORIXStatistics1011@Suite struct GeneratorTests {12    @Test func splitMix64PublishedVector() {13        // Reference outputs of SplitMix64 for seed 1234567 (Vigna's splitmix64.c).14        var g = SplitMix64(seed: 1234567)15        let expected: [UInt64] = [6457827717110365317, 3203168211198807973, 9817491932198370423, 4593380528125082431, 16408922859458223821]16        #expect((0..<5).map { _ in g.next() } == expected)17    }1819    @Test func xoshiro256StarStarPublishedVector() {20        // Reference outputs of xoshiro256** from the state {1, 2, 3, 4} (test vector of the rand_xoshiro crate).21        var g = SeededGenerator(state: (1, 2, 3, 4))22        let expected: [UInt64] = [11520, 0, 1509978240, 1215971899390074240, 1216172134540287360, 607988272756665600,23                                  16172922978634559625, 8476171486693032832, 10595114339597558777, 2904607092377533576]24        #expect((0..<10).map { _ in g.next() } == expected)25    }2627    @Test func sameSeedSameStream() {28        var a = SeededGenerator(seed: 42), b = SeededGenerator(seed: 42)29        #expect((0..<1000).map { _ in a.next() } == (0..<1000).map { _ in b.next() })30        var c = SeededGenerator(seed: 43)31        var a2 = SeededGenerator(seed: 42)32        #expect((0..<8).map { _ in a2.next() } != (0..<8).map { _ in c.next() })33        var s1 = SeededGenerator(seed: 42, stream: 1), s0 = SeededGenerator(seed: 42, stream: 0), plain = SeededGenerator(seed: 42)34        let x1 = (0..<8).map { _ in s1.next() }, x0 = (0..<8).map { _ in s0.next() }35        #expect(x0 == (0..<8).map { _ in plain.next() })36        #expect(x1 != x0)37        #expect(SeededGenerator(seed: 7) == SeededGenerator(seed: 7))38    }3940    @Test func copiesContinueIdentically() {41        var a = SeededGenerator(seed: 2026)42        _ = (0..<17).map { _ in a.next() }43        var b = a44        #expect((0..<50).map { _ in a.next() } == (0..<50).map { _ in b.next() })45    }4647    @Test func uniformConversions() {48        var g = SeededGenerator(seed: 1)49        var lo = 1.0, hi = 0.0, sum = 0.050        for _ in 0..<200_000 {51            let u = Sampling.uniform(using: &g)52            #expect(u >= 0 && u < 1)53            lo = min(lo, u); hi = max(hi, u); sum += u54            let v = Sampling.openUniform(using: &g)55            #expect(v > 0 && v < 1)56        }57        #expect(abs(sum / 200_000 - 0.5) < 4 * (1.0 / 12 / 200_000).squareRoot())58        #expect(lo < 1e-4 && hi > 1 - 1e-4)59    }6061    @Test func uniformIntegerIsUnbiased() throws {62        var g = SeededGenerator(seed: 99)63        let k = 7, n = 140_00064        var counts = [Double](repeating: 0, count: k)65        for _ in 0..<n { counts[Sampling.uniformInteger(below: k, using: &g)] += 1 }66        let r = try HypothesisTests.chiSquareGoodnessOfFit(observed: counts)67        #expect(r.test.pValue > 0.001)68        #expect(Sampling.uniformInteger(below: 1, using: &g) == 0)69    }7071    @Test func shuffleAndResample() {72        var g = SeededGenerator(seed: 5), h = SeededGenerator(seed: 5)73        let items = Array(0..<50)74        let s = Sampling.shuffled(items, using: &g)75        #expect(s.sorted() == items && s != items)76        #expect(Sampling.shuffled(items, using: &h) == s)77        let r = Sampling.resample(items, using: &g)78        #expect(r.count == 50 && Set(r).count < 50 && r.allSatisfy { items.contains($0) })79        #expect(Sampling.resample([Int](), using: &g).isEmpty)80    }81}8283@Suite struct SamplerTests {84    static let n = 200_0008586    /// Mean within 5 standard errors, variance within 5 standard errors (from the sample fourth moment), and the empirical CDF87    /// within 5 binomial standard errors of the exact CDF at seven quantiles.88    static func check(_ d: any ProbabilityDistribution, seed: UInt64, sourceLocation: SourceLocation = #_sourceLocation) throws {89        var g = SeededGenerator(seed: seed)90        let x = d.samples(n, using: &g)91        let nd = Double(n)92        let mean = try #require(d.mean), variance = try #require(d.variance)93        let m = try Descriptive.mean(x), v = try Descriptive.variance(x)94        #expect(abs(m - mean) <= 5 * (variance / nd).squareRoot(), "\(d.name): sample mean \(m) vs \(mean)", sourceLocation: sourceLocation)95        let m4 = try Descriptive.centralMoment(x, order: 4)96        let seVar = ((m4 - v * v) / nd).squareRoot()97        #expect(abs(v - variance) <= 5 * seVar, "\(d.name): sample variance \(v) vs \(variance) (se \(seVar))", sourceLocation: sourceLocation)98        for p in [0.01, 0.1, 0.3, 0.5, 0.7, 0.9, 0.99] {99            let q = try d.quantile(p)100            let exact = d.cdf(q)101            let empirical = Double(x.filter { $0 <= q }.count) / nd102            #expect(abs(empirical - exact) <= 5 * (exact * (1 - exact) / nd).squareRoot() + 1e-12,103                    "\(d.name): empirical CDF \(empirical) vs \(exact) at \(q)", sourceLocation: sourceLocation)104        }105        #expect(x.allSatisfy { $0 >= d.supportLowerBound && $0 <= d.supportUpperBound }, sourceLocation: sourceLocation)106    }107108    @Test func continuousSamplersFollowTheirDistribution() throws {109        try Self.check(NormalDistribution(mean: -2, standardDeviation: 3.5), seed: 11)110        try Self.check(StandardNormalDistribution(), seed: 12)111        try Self.check(StudentTDistribution(degreesOfFreedom: 9), seed: 13)112        try Self.check(ChiSquareDistribution(degreesOfFreedom: 5), seed: 14)113        try Self.check(ChiSquareDistribution(degreesOfFreedom: 0.7), seed: 15)114        try Self.check(FDistribution(numeratorDegreesOfFreedom: 6, denominatorDegreesOfFreedom: 15), seed: 16)115        try Self.check(GammaDistribution(shape: 0.4, rate: 2), seed: 17)116        try Self.check(GammaDistribution(shape: 7.5, scale: 0.25), seed: 18)117        try Self.check(BetaDistribution(alpha: 0.3, beta: 0.6), seed: 19)118        try Self.check(BetaDistribution(alpha: 4, beta: 9), seed: 20)119        try Self.check(ExponentialDistribution(rate: 0.2), seed: 21)120        try Self.check(UniformDistribution(lowerBound: -3, upperBound: 8), seed: 22)121    }122123    @Test func discreteSamplersFollowTheirDistribution() throws {124        try Self.check(BinomialDistribution(trials: 12, probability: 0.3), seed: 31)          // inversion125        try Self.check(BinomialDistribution(trials: 5000, probability: 0.37), seed: 32)       // beta splitting126        try Self.check(BinomialDistribution(trials: 400, probability: 0.985), seed: 33)127        try Self.check(PoissonDistribution(mean: 3.2), seed: 34)                              // inversion128        try Self.check(PoissonDistribution(mean: 480), seed: 35)                              // gamma method129        try Self.check(GeometricDistribution(probability: 0.15), seed: 36)130        try Self.check(GeometricDistribution(probability: 0.15, counting: .failures), seed: 37)131        try Self.check(NegativeBinomialDistribution(successes: 4.5, probability: 0.3), seed: 38)132        try Self.check(DiscreteUniformDistribution(lowerBound: 1, upperBound: 6), seed: 39)133    }134135    @Test func samplersAreReproducible() throws {136        let ds: [any ProbabilityDistribution] = [137            try NormalDistribution(mean: 1, standardDeviation: 2), try StudentTDistribution(degreesOfFreedom: 4), try GammaDistribution(shape: 0.5, rate: 1),138            try BetaDistribution(alpha: 2, beta: 3), try BinomialDistribution(trials: 1000, probability: 0.2), try PoissonDistribution(mean: 100),139            try NegativeBinomialDistribution(successes: 3, probability: 0.4),140        ]141        for d in ds {142            var a = SeededGenerator(seed: 777), b = SeededGenerator(seed: 777), c = SeededGenerator(seed: 778)143            let x = d.samples(200, using: &a)144            #expect(x == d.samples(200, using: &b), "\(d.name)")145            #expect(x != d.samples(200, using: &c), "\(d.name)")146        }147    }148149    @Test func degenerateSamplerParameters() throws {150        var g = SeededGenerator(seed: 3)151        #expect(Sampling.binomial(n: 0, p: 0.5, using: &g) == 0)152        #expect(Sampling.binomial(n: 1000, p: 0, using: &g) == 0)153        #expect(Sampling.binomial(n: 1000, p: 1, using: &g) == 1000)154        #expect(Sampling.poisson(mean: 0, using: &g) == 0)155        #expect(Sampling.geometricFailures(p: 1, using: &g) == 0)156        #expect(Sampling.negativeBinomial(r: 2, p: 1, using: &g) == 0)157        // Tiny beta shapes: the log-space path must stay inside [0, 1] and finite.158        for _ in 0..<2000 { let b = Sampling.beta(1e-3, 2e-3, using: &g); #expect(b >= 0 && b <= 1) }159        for _ in 0..<2000 { let v = Sampling.gamma(shape: 1e-3, using: &g); #expect(v >= 0 && v.isFinite) }160    }161}162
Files main
Menu

Theme