| 261 | 'distribution, evaluated at x.', |
| 262 | complexity: 7500, |
| 263 | signature: '(distribution, number) -> number', |
| 264 | evaluate: ([dist, x], { numericApproximation, engine: ce }) => { |
| 265 | if (!dist || !x || !isDistributionExpression(dist)) return undefined; |
| 266 | const r = distributionPDF(ce, dist, x); |
| 267 | if (!r) return undefined; |
| 268 | return numericApproximation ? r.N() : r.evaluate(); |
| 269 | }, |
| 270 | }, |
| 271 | |
| 272 | CDF: { |
| 273 | description: |
| 274 | 'Cumulative distribution function P(X ≤ x) of a distribution.', |
| 275 | complexity: 7500, |
| 276 | signature: '(distribution, number) -> number', |
| 277 | evaluate: ([dist, x], { numericApproximation, engine: ce }) => { |
| 278 | if (!dist || !x || !isDistributionExpression(dist)) return undefined; |
| 279 | const r = distributionCDF(ce, dist, x); |
| 280 | if (!r) return undefined; |
| 281 | return numericApproximation ? r.N() : r.evaluate(); |
| 282 | }, |
| 283 | }, |
| 284 | |
| 285 | Quantile: { |
| 286 | description: |
| 287 | 'Quantile (inverse CDF): the least x with CDF(x) ≥ p, for p in [0, 1]. ' + |
| 288 | 'The first argument may also be a data collection, in which case the ' + |
| 289 | 'empirical quantile is returned.', |
| 290 | complexity: 7500, |
| 291 | signature: '(distribution | collection<any>, number) -> number', |
| 292 | evaluate: ([dist, p], { numericApproximation, engine: ce }) => { |
| 293 | if (!dist || !p) return undefined; |
| 294 | const pv = litVal(p); |
| 295 | if (pv !== undefined && (pv < 0 || pv > 1)) |
| 296 | return rangeError(ce, '0 ≤ p ≤ 1', p); |
| 297 | // Empirical quantile of a data collection (distinguished from a |
| 298 | // distribution by the first argument's shape). |
| 299 | if (!isDistributionExpression(dist)) { |
| 300 | if (!dist.isFiniteCollection) return undefined; |
| 301 | const r = empiricalQuantile(ce, dist, p, pv); |
| 302 | if (!r) return undefined; |
| 303 | return numericApproximation ? r.N() : r.evaluate(); |
| 304 | } |
| 305 | const r = distributionQuantile(ce, dist, p, pv, !!numericApproximation); |
| 306 | if (!r) return undefined; |
| 307 | return numericApproximation ? r.N() : r.evaluate(); |
| 308 | }, |
| 309 | }, |
| 310 | }, |
| 311 | ]; |
| 312 | |
| 313 | // |
| 314 | // Closed-form lowering. All construction uses `ce.function('Add'|…)` (never the |
| 315 | // `.add()`/`.mul()` methods, which fold exact literal pairs to floats), and no |
| 316 | // handler calls `.simplify()`. The returned expression is evaluated by the |
| 317 | // caller (exact under `evaluate`, float under `.N()`). |
| 318 | // |
| 319 | |
| 320 | function distributionPDF( |