Repository Public MIT
1// ORIX Calc2// File: Packages/ORIXFeatureStatistics/Tests/ORIXFeatureStatisticsTests/DistributionTests.swift3// Description: Tests of the Distributions section — demo values, shaded region equal to the displayed probability, parameter errors, moments, seeded samples, and the native functions.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9import ORIXExpression10import ORIXStatistics11import ORIXUI12@testable import ORIXFeatureStatistics1314@MainActor @Suite struct DistributionModelTests {15 @Test func demoReproducesKnownValues() throws {16 let m = DistributionModel()17 m.loadDemo()18 let r = m.results19 #expect(r.parameterError == nil)20 #expect(r.name.contains("Normal"))21 // P(μ − σ ≤ X ≤ μ + σ) = erf(1/√2)22 #expect(close(try #require(r.between), erf(1 / 2.0.squareRoot()), 1e-13))23 #expect(close(try #require(r.atMost), 0.5 * (1 + erf(1 / 2.0.squareRoot())), 1e-13))24 #expect(close(try #require(r.atLeast), 0.5 * erfc(1 / 2.0.squareRoot()), 1e-13))25 #expect(close(try #require(r.density), exp(-0.5) / (15 * (2 * Double.pi).squareRoot()), 1e-13))26 let q = try #require(r.quantile?.value)27 #expect(close(q.value, 100 + 15 * 1.6448536269514722, 1e-12))28 #expect(q.verification.isVerified, "the round-trip check of the engine ran and passed")29 #expect(r.mean == 100 && r.variance == 225 && r.standardDeviation == 15)30 #expect(r.shadedProbability == r.between)31 #expect(m.sample?.values.count == 50)32 }3334 /// The shaded region of the chart and the number displayed are the same probability: the density integrated numerically35 /// over the region (independently of the engine's CDF) gives the displayed value.36 @Test func shadedAreaEqualsDisplayedProbabilityContinuous() throws {37 let specs: [(DistSpec, Double, Double, Double)] = [38 (DistSpec(.normal, ["mean": 100, "sd": 15]), 112, 85, 121),39 (DistSpec(.standardNormal), -0.4, -1.96, 1.96),40 (DistSpec(.studentT, ["df": 7]), 1.3, -2, 0.5),41 (DistSpec(.chiSquare, ["df": 6]), 9, 2, 11),42 (DistSpec(.f, ["d1": 5, "d2": 12]), 2.1, 0.5, 3),43 (DistSpec(.exponential, ["rate": 0.5]), 3, 1, 4),44 (DistSpec(.uniform, ["a": 2, "b": 8]), 5.5, 3, 4.25),45 (DistSpec(.gamma, ["shape": 2.5, "rate": 1.5]), 2, 0.5, 3),46 (DistSpec(.beta, ["alpha": 2, "beta": 5]), 0.4, 0.1, 0.6),47 ]48 for (spec, x, a, b) in specs {49 let d = try #require(try spec.make() as? any ContinuousDistribution)50 let far = (lower: try d.quantile(1e-12), upper: try d.quantile(upperTail: 1e-12))51 for region in ProbabilityRegion.allCases {52 var s = DistState()53 s.kind = spec.kind; s.spec = spec; s.x = x; s.a = a; s.b = b; s.region = region54 let r = DistResults.compute(s)55 let shown = try #require(r.shadedProbability)56 let plot = r.plot57 #expect(plot.hasRegion)58 let lo = max(plot.regionLower, far.lower), hi = min(plot.regionUpper, far.upper)59 let area = simpson({ d.pdf($0) }, lo, hi)60 #expect(abs(area - shown) < 2e-7, "\(spec.kind) \(region): area \(area) vs shown \(shown)")61 // The shaded plot points are exactly the grid points inside the region, and its finite limits are grid points.62 #expect(plot.points.allSatisfy { $0.shaded == ($0.x >= plot.regionLower && $0.x <= plot.regionUpper) })63 for edge in [plot.regionLower, plot.regionUpper] where edge.isFinite {64 if let first = plot.points.first?.x, let last = plot.points.last?.x, edge > first, edge < last {65 #expect(plot.points.contains { $0.x == edge }, "\(spec.kind): limit \(edge) is a grid point")66 }67 }68 switch region {69 case .atMost: #expect(shown == r.atMost)70 case .atLeast: #expect(shown == r.atLeast)71 case .between: #expect(shown == r.between)72 }73 }74 }75 }7677 @Test func shadedBarsEqualDisplayedProbabilityDiscrete() throws {78 let specs: [(DistSpec, Double, Double, Double)] = [79 (DistSpec(.binomial, ["n": 20, "p": 0.3]), 7, 4, 7),80 (DistSpec(.poisson, ["mean": 4]), 5, 2, 6),81 (DistSpec(.geometric, ["p": 0.25]), 3, 2, 5),82 (DistSpec(.negativeBinomial, ["r": 3, "p": 0.4]), 4, 1, 6),83 (DistSpec(.discreteUniform, ["a": 1, "b": 6]), 4, 2, 5),84 (DistSpec(.binomial, ["n": 20, "p": 0.3]), 6.5, 3.2, 7.9),85 ]86 for (spec, x, a, b) in specs {87 let d = try #require(try spec.make() as? any DiscreteDistribution)88 for region in ProbabilityRegion.allCases {89 var s = DistState()90 s.kind = spec.kind; s.spec = spec; s.x = x; s.a = a; s.b = b; s.region = region91 let r = DistResults.compute(s)92 let shown = try #require(r.shadedProbability)93 let plot = r.plot94 #expect(plot.isDiscrete && !plot.thinned)95 // Sum of the mass function over every integer of the region (far beyond the plotted window).96 let lo = max(plot.regionLower.rounded(.up), d.supportLowerBound), hi = min(plot.regionUpper.rounded(.down), min(d.supportUpperBound, 2000))97 var total = 0.098 if hi >= lo { for k in Int(lo)...Int(hi) { total += d.pmf(k) } }99 #expect(abs(total - shown) < 1e-12, "\(spec.kind) \(region): sum \(total) vs shown \(shown)")100 // Inside the window, the shaded bars are exactly the integers of the region.101 #expect(plot.points.allSatisfy { $0.shaded == ($0.x >= plot.regionLower && $0.x <= plot.regionUpper) })102 let shadedInWindow = plot.points.filter(\.shaded).map(\.density).reduce(0, +)103 #expect(shadedInWindow <= shown + 1e-12)104 #expect(shown - shadedInWindow < 0.01, "the window shows nearly all of the shaded mass")105 }106 }107 }108109 @Test func invalidParametersShowTheEngineMessage() throws {110 let m = DistributionModel()111 m.state.kind = .normal112 m.setParameter("sd", -1)113 var engineMessage = ""114 do { _ = try NormalDistribution(mean: 0, standardDeviation: -1) } catch { engineMessage = statMessage(error) }115 #expect(m.results.parameterError == engineMessage, "the engine's own sentence is shown")116 #expect(m.results.parameterError?.isEmpty == false)117 #expect(m.results.shadedProbability == nil && m.results.plot.points.isEmpty)118 m.setParameter("sd", nil)119 #expect(m.results.parameterError?.contains("Enter a value") == true)120 m.state.kind = .binomial121 m.setParameter("n", 10.5)122 #expect(m.results.parameterError?.contains("whole number") == true)123 m.setParameter("n", 10); m.setParameter("p", 1.2)124 #expect(m.results.parameterError?.isEmpty == false)125 m.setParameter("p", 0.5)126 #expect(m.results.parameterError == nil)127 // Switching family keeps each family's parameters.128 m.state.kind = .normal129 m.setParameter("sd", 2)130 m.state.kind = .binomial131 #expect(m.parameter("n") == 10)132 m.state.kind = .normal133 #expect(m.parameter("sd") == 2)134 }135136 @Test func undefinedAndInfiniteMoments() {137 var s = DistState()138 s.kind = .studentT; s.spec = DistSpec(.studentT, ["df": 1])139 var r = DistResults.compute(s)140 #expect(r.mean == nil && r.variance == nil, "Cauchy: undefined")141 s.spec = DistSpec(.studentT, ["df": 2])142 r = DistResults.compute(s)143 #expect(r.mean == 0 && r.variance == .infinity, "ν = 2: infinite variance")144 s.p = 1.5145 #expect(DistResults.compute(s).quantile?.failure?.isEmpty == false, "p outside [0, 1] is the engine's error")146 }147148 @Test func everyDistributionComputesAndPlots() throws {149 for kind in DistKind.allCases {150 var s = DistState()151 s.kind = kind152 let r = DistResults.compute(s)153 #expect(r.parameterError == nil, "\(kind)")154 #expect(!r.plot.points.isEmpty, "\(kind)")155 #expect(r.plot.points.allSatisfy { $0.density.isFinite && $0.cumulative >= 0 && $0.cumulative <= 1 })156 #expect(r.isDiscrete == kind.isDiscrete)157 #expect(r.quantile?.value != nil, "\(kind)")158 }159 // Unbounded densities are clipped, not dropped.160 var s = DistState()161 s.kind = .chiSquare; s.spec = DistSpec(.chiSquare, ["df": 1])162 #expect(DistResults.compute(s).plot.densityCeiling.isFinite)163 s.kind = .geometric164 var g = DistSpec(.geometric, ["p": 0.25]); g.countsFailures = true165 s.spec = g; s.x = 0166 #expect(close(DistResults.compute(s).density ?? .nan, 0.25), "failures convention: P(X = 0) = p")167 }168169 @Test func seededSamplesAreReproducible() throws {170 let m = DistributionModel()171 m.state.kind = .gamma172 m.state.seed = 77; m.state.sampleSize = 200173 let a = try #require(m.generateSample())174 let b = try #require(m.generateSample())175 #expect(a == b, "same seed, same sample")176 var g = SeededGenerator(seed: 77)177 #expect(a.values == (try GammaDistribution(shape: 2, rate: 1)).samples(200, using: &g), "the engine's generator, seeded as shown")178 m.state.seed = 78179 #expect(try #require(m.generateSample()).values != a.values)180 m.state.sampleSize = 0181 #expect(m.generateSample() == nil && m.sampleError != nil)182 m.state.sampleSize = 2.5183 #expect(m.generateSample() == nil && m.sampleError?.contains("whole number") == true)184 }185186 @Test func sampleCanBecomeTheDataset() throws {187 let store = StatisticsStore()188 store.section = .distributions189 store.dist.state.sampleSize = 40190 let s = try #require(store.dist.generateSample())191 store.sendSampleToData()192 #expect(store.section == .data)193 #expect(try store.data.dataset.values(0) == s.values)194 #expect(store.data.dataset.title.contains("seed \(s.seed)"))195 }196197 @Test func logsTheProbabilityOnRequest() {198 let app = makeApp()199 let store = StatisticsStore()200 store.dist.loadDemo()201 #expect(store.logDistribution(app))202 #expect(app.history.first?.mode == .statistics)203 #expect(app.history.first?.input.contains("P(85 ≤ X ≤ 115)") == true)204 store.dist.setParameter("sd", 0)205 #expect(!store.logDistribution(app), "nothing is logged without a result")206 }207}208209@MainActor @Suite struct NativeFunctionTests {210 private func call(_ name: String, _ args: Double...) throws -> Double {211 let f = try #require(StatisticsNatives.all.first { $0.name == name }, "missing native \(name)")212 return try f.body(args)213 }214215 @Test func requiredFunctionsExistWithSummaries() {216 let names = Set(StatisticsNatives.all.map(\.name))217 for required in ["normpdf", "normcdf", "norminv", "tpdf", "tcdf", "tinv", "chi2cdf", "chi2inv", "fcdf", "finv", "binompdf", "binomcdf",218 "poissonpdf", "poissoncdf", "expcdf", "gammacdf", "betacdf", "unifcdf"] {219 #expect(names.contains(required), "\(required)")220 }221 #expect(names.count == StatisticsNatives.all.count, "no duplicate names")222 for f in StatisticsNatives.all {223 #expect(f.summary.hasPrefix(f.name + "("), "\(f.name): the summary starts with the call form")224 #expect(!Builtins.isFunction(f.name), "\(f.name) does not shadow a built-in function")225 }226 }227228 @Test func everyNativeMatchesTheEngine() throws {229 let normal = try NormalDistribution(mean: 100, standardDeviation: 15), z = StandardNormalDistribution()230 let t = try StudentTDistribution(degreesOfFreedom: 12), chi = try ChiSquareDistribution(degreesOfFreedom: 6)231 let F = try FDistribution(numeratorDegreesOfFreedom: 5, denominatorDegreesOfFreedom: 12)232 let e = try ExponentialDistribution(rate: 0.5), g = try GammaDistribution(shape: 2.5, rate: 1.5)233 let beta = try BetaDistribution(alpha: 2, beta: 5), u = try UniformDistribution(lowerBound: 2, upperBound: 8)234 let bin = try BinomialDistribution(trials: 20, probability: 0.3), pois = try PoissonDistribution(mean: 4)235 let geo = try GeometricDistribution(probability: 0.25)236 #expect(try call("normpdf", 0.7) == z.pdf(0.7))237 #expect(try call("normpdf", 110, 100, 15) == normal.pdf(110))238 #expect(try call("normcdf", 1.96) == z.cdf(1.96))239 #expect(try call("normcdf", 110, 100, 15) == normal.cdf(110))240 #expect(try call("norminv", 0.975) == (try z.quantile(0.975)))241 #expect(try call("norminv", 0.9, 100, 15) == (try normal.quantile(0.9)))242 #expect(try call("tpdf", 1.1, 12) == t.pdf(1.1))243 #expect(try call("tcdf", 1.1, 12) == t.cdf(1.1))244 #expect(try call("tinv", 0.975, 12) == (try t.quantile(0.975)))245 #expect(try call("chi2pdf", 4, 6) == chi.pdf(4))246 #expect(try call("chi2cdf", 4, 6) == chi.cdf(4))247 #expect(try call("chi2inv", 0.95, 6) == (try chi.quantile(0.95)))248 #expect(try call("fpdf", 2, 5, 12) == F.pdf(2))249 #expect(try call("fcdf", 2, 5, 12) == F.cdf(2))250 #expect(try call("finv", 0.95, 5, 12) == (try F.quantile(0.95)))251 #expect(try call("exppdf", 3, 0.5) == e.pdf(3))252 #expect(try call("expcdf", 3, 0.5) == e.cdf(3))253 #expect(try call("expinv", 0.5, 0.5) == (try e.quantile(0.5)))254 #expect(try call("gammapdf", 2, 2.5, 1.5) == g.pdf(2))255 #expect(try call("gammacdf", 2, 2.5, 1.5) == g.cdf(2))256 #expect(try call("gammainv", 0.3, 2.5, 1.5) == (try g.quantile(0.3)))257 #expect(try call("betapdf", 0.4, 2, 5) == beta.pdf(0.4))258 #expect(try call("betacdf", 0.4, 2, 5) == beta.cdf(0.4))259 #expect(try call("betainv", 0.4, 2, 5) == (try beta.quantile(0.4)))260 #expect(try call("unifpdf", 5, 2, 8) == u.pdf(5))261 #expect(try call("unifcdf", 5, 2, 8) == u.cdf(5))262 #expect(try call("unifinv", 0.25, 2, 8) == (try u.quantile(0.25)))263 #expect(try call("binompdf", 7, 20, 0.3) == bin.pmf(7))264 #expect(try call("binomcdf", 7, 20, 0.3) == bin.cdf(7))265 #expect(try call("binominv", 0.9, 20, 0.3) == (try bin.quantile(0.9)))266 #expect(try call("poissonpdf", 5, 4) == pois.pmf(5))267 #expect(try call("poissoncdf", 5, 4) == pois.cdf(5))268 #expect(try call("poissoninv", 0.5, 4) == (try pois.quantile(0.5)))269 #expect(try call("geompdf", 3, 0.25) == geo.pmf(3))270 #expect(try call("geomcdf", 3, 0.25) == geo.cdf(3))271 // Every native is covered above.272 #expect(StatisticsNatives.all.count == 32)273 }274275 @Test func knownValues() throws {276 #expect(close(try call("normcdf", 1.96), 0.9750021048517795, 1e-14))277 #expect(close(try call("norminv", 0.975), 1.959963984540054, 1e-14))278 #expect(close(try call("tinv", 0.975, 12), 2.178812829667228, 1e-12))279 #expect(close(try call("binompdf", 2, 4, 0.5), 0.375, 1e-14))280 #expect(close(try call("binomcdf", 1, 4, 0.5), 0.3125, 1e-14))281 #expect(close(try call("poissonpdf", 0, 2), exp(-2), 1e-14))282 #expect(close(try call("expcdf", 2, 0.5), 1 - exp(-1), 1e-14))283 #expect(close(try call("unifcdf", 5, 2, 8), 0.5, 1e-14))284 #expect(close(try call("chi2cdf", 2, 2), 1 - exp(-1), 1e-13))285 #expect(try call("binompdf", 2.5, 4, 0.5) == 0, "no mass between integers")286 }287288 @Test func invalidParametersThrowAndNeverReturnNaN() throws {289 #expect(throws: StatisticsError.self) { try call("normcdf", 1, 0, -2) }290 #expect(throws: StatisticsError.self) { try call("normcdf", 1, 0) }291 #expect(throws: StatisticsError.self) { try call("norminv", 1.5) }292 #expect(throws: StatisticsError.self) { try call("tcdf", 1, 0) }293 #expect(throws: StatisticsError.self) { try call("tinv", -0.1, 5) }294 #expect(throws: StatisticsError.self) { try call("chi2cdf", 1, -3) }295 #expect(throws: StatisticsError.self) { try call("fcdf", 1, 0, 5) }296 #expect(throws: StatisticsError.self) { try call("binompdf", 1, 4.5, 0.5) }297 #expect(throws: StatisticsError.self) { try call("binomcdf", 1, 4, 1.5) }298 #expect(throws: StatisticsError.self) { try call("poissonpdf", 1, -1) }299 #expect(throws: StatisticsError.self) { try call("expcdf", 1, 0) }300 #expect(throws: StatisticsError.self) { try call("gammacdf", 1, -1, 1) }301 #expect(throws: StatisticsError.self) { try call("betacdf", 0.5, 0, 1) }302 #expect(throws: StatisticsError.self) { try call("unifcdf", 1, 3, 2) }303 #expect(throws: StatisticsError.self) { try call("normcdf", .nan) }304 #expect(throws: StatisticsError.self) { try call("tcdf") }305 for f in StatisticsNatives.all {306 for probe in [-1.0, 0, 0.5, 1, 3] {307 let args = [probe] + Array(repeating: 2.0, count: f.arity.upperBound - 1)308 if let v = try? f.body(args) { #expect(!v.isNaN, "\(f.name)(\(args)) returned NaN") }309 }310 }311 }312313 @Test func usableInTheSharedExpressionEngine() async throws {314 let app = makeApp()315 app.registerNatives(StatisticsNatives.all)316 let value = try await app.evaluateCanonical("normcdf(1.96) - normcdf(-1.96)", mode: .scientific).get().value?.doubleValue317 #expect(close(try #require(value), 0.950004209703559, 1e-12))318 let t = try await app.evaluateCanonical("tinv(0.975, 12)", mode: .scientific).get().value?.doubleValue319 #expect(close(try #require(t), 2.178812829667228, 1e-12))320 if case .success = await app.evaluateCanonical("normcdf(1, 0, -1)", mode: .scientific) { Issue.record("an invalid σ must be an error") }321 // Graph: compiled expressions call the natives too.322 let expr = try Parser.parse("normpdf(x, 0, 2)", options: ParserOptions(functions: Set(app.engine.context.natives.keys), variables: ["x"]))323 let compiled = try CompiledExpression(expr, variables: ["x"], context: app.engine.context)324 #expect(close(compiled(1), (try NormalDistribution(mean: 0, standardDeviation: 2)).pdf(1), 1e-15))325 }326}327