2.8 KiB
2.8 KiB
Capability: statistical-distributions
Requirements
Location
- REQ-DIST-001: All distribution functions live in
@suaveplan/randomundersrc/distributions/.
Continuous distributions
- REQ-DIST-010: Exports
normal(mu?: number, sigma?: number, rng?: Rng): () => number— Gaussian via Box-Muller. Defaultsmu=0, sigma=1. - REQ-DIST-011: Exports
logNormal(mu?: number, sigma?: number, rng?: Rng): () => number—exp(normal(mu, sigma)). - REQ-DIST-012: Exports
exponential(lambda: number, rng?: Rng): () => number. Throws iflambda <= 0. - REQ-DIST-013: Exports
gamma(k: number, theta?: number, rng?: Rng): () => number— Marsaglia-Tsang for k≥1, Ahrens-Dieter for k<1. Defaulttheta=1. - REQ-DIST-014: Exports
beta(alpha: number, betaParam: number, rng?: Rng): () => number— ratio of two Gamma samples. - REQ-DIST-015: Exports
pareto(alpha: number, rng?: Rng): () => number. Throws ifalpha <= 0. - REQ-DIST-016: Exports
weibull(k: number, a?: number, b?: number, rng?: Rng): () => number. Defaultsa=0, b=1. - REQ-DIST-017: Exports
cauchy(a?: number, b?: number, rng?: Rng): () => number. Defaultsa=0, b=1. - REQ-DIST-018: Exports
logistic(a?: number, b?: number, rng?: Rng): () => number. Defaultsa=0, b=1.
Discrete distributions
- REQ-DIST-020: Exports
bernoulli(p: number, rng?: Rng): () => 0 | 1. Throws ifpoutside[0, 1]. - REQ-DIST-021: Exports
geometric(p: number, rng?: Rng): () => number. Throws ifpoutside(0, 1]. - REQ-DIST-022: Exports
binomial(n: number, p: number, rng?: Rng): () => number. Throws ifn < 0orpoutside[0, 1]. - REQ-DIST-023: Exports
poisson(lambda: number, rng?: Rng): () => number— Knuth for λ≤30, rejection method above. Throws iflambda <= 0.
Unified dispatcher
- REQ-DIST-030: Exports
DistributionConfig— discriminated union:{ type: 'normal'; mu?: number; sigma?: number } | { type: 'exponential'; lambda: number } | ...(one member per distribution). - REQ-DIST-031: Exports
createSampler(config: DistributionConfig, rng?: Rng): () => number— dispatches to the appropriate distribution factory. - REQ-DIST-032: Exports
sample(config: DistributionConfig, rng?: Rng): number— one-shot convenience:createSampler(config, rng)(). - REQ-DIST-033:
DistributionConfigis a plain JSON-serialisable type.rngis always passed externally, never embedded in config. - REQ-DIST-034:
createSamplerthrowsRandomError("RANDOM.DISTRIBUTION.UNKNOWN_TYPE")for unrecognisedtypevalues (runtime guard for JS consumers without TypeScript).
Quality
- REQ-DIST-040: fast-check: for each distribution, generate 10k samples and verify mean/variance are within 5% of theoretical values at standard parameters.
- REQ-DIST-041:
DistributionConfiground-trips throughJSON.parse(JSON.stringify(config))without loss for all distribution types.