Repository Public MIT
1// ORIX Calc2// File: Packages/ORIXStatistics/Tests/ORIXStatisticsTests/DistributionReferenceTests.swift3// Description: Every distribution's pdf/pmf, cdf, survival and quantile against the mpmath reference grid (tails and edge parameters included).4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9@testable import ORIXStatistics1011/// Builds a distribution from the names used by Reference/gen_distributions.py.12func makeDistribution(_ name: String, _ p: [Double]) throws -> any ProbabilityDistribution {13 switch name {14 case "normal": return try NormalDistribution(mean: p[0], standardDeviation: p[1])15 case "t": return try StudentTDistribution(degreesOfFreedom: p[0])16 case "chi2": return try ChiSquareDistribution(degreesOfFreedom: p[0])17 case "gamma": return try GammaDistribution(shape: p[0], rate: p[1])18 case "F": return try FDistribution(numeratorDegreesOfFreedom: p[0], denominatorDegreesOfFreedom: p[1])19 case "beta": return try BetaDistribution(alpha: p[0], beta: p[1])20 case "exponential": return try ExponentialDistribution(rate: p[0])21 case "uniform": return try UniformDistribution(lowerBound: p[0], upperBound: p[1])22 case "binomial": return try BinomialDistribution(trials: Int(p[0]), probability: p[1])23 case "poisson": return try PoissonDistribution(mean: p[0])24 case "geometric": return try GeometricDistribution(probability: p[0])25 case "geometricFailures": return try GeometricDistribution(probability: p[0], counting: .failures)26 case "negativeBinomial": return try NegativeBinomialDistribution(successes: p[0], probability: p[1])27 case "discreteUniform": return try DiscreteUniformDistribution(lowerBound: Int(p[0]), upperBound: Int(p[1]))28 default: throw StatisticsError.invalidArgument("unknown distribution \(name)")29 }30}3132/// Measured worst relative error per (distribution, function), split into the central range and the far tails.33/// "Central" = reference value ≥ 1e-12 for probabilities and densities; quantiles are central when 1e-12 ≤ p.34func distributionAccuracy() throws -> [String: ErrorTracker] {35 var trackers: [String: ErrorTracker] = [:]36 for c in try Reference.cases("distributions") {37 let name = c.string("dist"), params = c.doubles("params"), fn = c.string("fn"), x = c.double("x"), e = c.double("expected")38 let d = try makeDistribution(name, params)39 let label = "\(name)\(params) \(fn)(\(x))"40 if let disc = d as? any DiscreteDistribution {41 switch fn {42 case "pmf": trackers["\(name) pmf" + (e >= 1e-12 ? "" : " [tail]"), default: .init()].record(disc.pmf(Int(x)), e, "\(label) = \(disc.pmf(Int(x))) vs \(e)")43 case "cdf": trackers["\(name) cdf" + (e >= 1e-12 ? "" : " [tail]"), default: .init()].record(d.cdf(x), e, "\(label) = \(d.cdf(x)) vs \(e)")44 case "sf": trackers["\(name) sf" + (e >= 1e-12 ? "" : " [tail]"), default: .init()].record(d.survival(x), e, "\(label) = \(d.survival(x)) vs \(e)")45 case "ppf", "isf":46 let k = (try? (fn == "ppf" ? d.quantile(x) : d.quantile(upperTail: x))) ?? .nan47 let ok = k == e || (c.has("alt") && k == c.double("alt"))48 trackers["\(name) \(fn) [exact]", default: .init()].record(ok ? 1 : 0, 1, "\(label) = \(k) vs \(e)")49 default: break50 }51 continue52 }53 let cont = d as! any ContinuousDistribution54 switch fn {55 case "pdf": trackers["\(name) pdf" + (e >= 1e-12 ? "" : " [tail]"), default: .init()].record(cont.pdf(x), e, "\(label) = \(cont.pdf(x)) vs \(e)")56 case "logpdf":57 let v = cont.logPDF(x)58 trackers["\(name) logpdf [abs]", default: .init()].record(1 + (v - e) / max(1, abs(e)), 1, "\(label) = \(v) vs \(e)")59 case "cdf": trackers["\(name) cdf" + (e >= 1e-12 ? "" : " [tail]"), default: .init()].record(d.cdf(x), e, "\(label) = \(d.cdf(x)) vs \(e)")60 case "sf": trackers["\(name) sf" + (e >= 1e-12 ? "" : " [tail]"), default: .init()].record(d.survival(x), e, "\(label) = \(d.survival(x)) vs \(e)")61 case "ppf", "isf":62 let v = (try? (fn == "ppf" ? d.quantile(x) : d.quantile(upperTail: x))) ?? .nan63 trackers["\(name) \(fn)" + (x >= 1e-12 ? "" : " [tail]"), default: .init()].record(v, e, "\(label) = \(v) vs \(e)")64 default: break65 }66 }67 return trackers68}6970@Suite struct DistributionReferenceTests {71 @Test func everyFunctionOfEveryDistributionMatchesMpmath() throws {72 let trackers = try distributionAccuracy()73 #expect(trackers.values.reduce(0) { $0 + $1.count } > 9000)74 if ProcessInfo.processInfo.environment["ORIX_ACCURACY_REPORT"] != nil {75 for (k, t) in trackers.sorted(by: { $0.key < $1.key }) { print("ACC|\(k)|\(t.count)|\(t.worst)|\(t.worstCase)") }76 }77 for (key, t) in trackers {78 // Measured worst case over the whole grid, far tails included: 7e-14 (see VALIDATION.md); the specification asks for 1e-10.79 let limit: Double = key.hasSuffix("[exact]") ? 0 : 1e-1280 #expect(t.worst <= limit, "\(key): worst relative error \(t.worst) at \(t.worstCase)")81 }82 }83}84