1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! Symbolic probability and statistics (SymPy's `stats`): random variables
//! with named distributions, exact moments, probabilities of events,
//! densities and distribution functions — all as expressions — plus
//! conditioning, transformations and mixtures of distributions.
//!
//! A [`RandomVariable`] is a symbol together with a [`Distribution`]. The
//! queries are exact where the distribution's parameters are exact:
//!
//! ```
//! use symplex::prelude::*;
//! use symplex::stats::{Distribution, RandomVariable};
//!
//! let ctx = Context::new();
//! let x = RandomVariable::new(&ctx, "X", Distribution::normal(ctx.int(0), ctx.int(1)));
//! assert_eq!(x.mean(), ctx.int(0));
//! assert_eq!(x.variance(), ctx.int(1));
//! // E[X² + 3X] for a standard normal.
//! assert_eq!(x.expectation(&(x.symbol().powi(2) + 3 * x.symbol())).simplify(), ctx.int(1));
//!
//! let y = RandomVariable::new(&ctx, "Y", Distribution::binomial(ctx.int(5), ctx.rational(1, 3)));
//! assert_eq!(y.mean(), ctx.rational(5, 3));
//! assert_eq!(y.probability(&y.symbol().gt(&ctx.int(2)))?, ctx.rational(17, 81));
//! // Conditioning, and a transformed variable.
//! let half = x.given(&x.symbol().gt(&ctx.int(0)))?; // X | X > 0
//! assert_eq!(half.mean().equals(&(ctx.int(2) / ctx.pi()).sqrt()), Some(true));
//! let w = x.transform("W", &(2 * x.symbol() + 1))?; // 2X + 1 ~ Normal(1, 2)
//! assert_eq!(w.variance(), ctx.int(4));
//! # Ok::<(), SymplexError>(())
//! ```
//!
//! # Design
//!
//! * A distribution is a [`Family`]: its **support** ([`Support`], a
//! finite union of intervals and points, continuous or on the integer
//! lattice), its **density** (or probability mass function) as an
//! expression in a free variable, and the **closed forms** it happens to
//! have (moments, CDF, MGF, quantile, entropy), each optional. The
//! generic machinery on [`Distribution`] answers every query from those:
//! it clips an event's region to the support and measures each piece
//! through the CDF, or by exact
//! [`integrate_definite`](crate::api::expr::Ex::integrate_definite) /
//! [`summation`](crate::api::expr::Ex::summation) of the density, and it
//! takes `E[g(X)]` through the raw moments when `g` is a polynomial.
//! Implement [`Family`] to add a distribution of your own.
//! * Families **compose**: [`Truncated`] (conditioning), [`Affine`]
//! (`aX + b`), [`Transformed`] (`g(X)` by the change-of-variables
//! formula) and [`Mixture`] wrap other distributions and transport their
//! closed forms exactly where the transport is exact.
//! * Parameters are expressions: rational parameters give exact rational
//! answers, symbolic parameters give symbolic answers (`E[X] = μ`).
//! Parameter *validity* (`σ > 0`, `0 ≤ p ≤ 1`) is the caller's promise
//! for symbolic parameters and is checked for numeric ones by the
//! `try_` constructors.
//! * Nothing here is numerical by default: [`Distribution::sample`] is the
//! only place a random number generator appears, and it is a plain
//! deterministic-seed generator so tests are reproducible. Every
//! built-in family samples exactly in distribution: by inverse transform
//! through a closed-form quantile (`Normal`, `Uniform`, `Exponential`,
//! `Cauchy`, `Laplace`, `Logistic`, `LogNormal`, `Weibull`, `Pareto`,
//! `Triangular`), by cumulative sums over a finite lattice or table
//! (`Bernoulli`, `Binomial`, `Hypergeometric`, `DiscreteUniform`,
//! `Finite`), or by a route of its own — Marsaglia–Tsang gamma variates
//! behind `Gamma`, `ChiSquared`, `Beta`, `StudentT` and `FDistribution`;
//! Knuth's method and Hörmann's PTRS behind `Poisson`; closed-form
//! inversion for `Geometric`; the Poisson–Gamma mixture for
//! `NegativeBinomial`. The wrappers sample through their inner family.
//!
//! Distribution families: the structs re-exported below
//! ([`Normal`], [`Binomial`], …); each has a `Distribution::name(…)`
//! constructor and a `try_name` twin.
//!
//! # Statistics on data: where things live and what they take
//!
//! The data modules follow one placement rule each (stated at the top of
//! every module):
//!
//! | Module | Contains |
//! |---|---|
//! | [`data`] | descriptive statistics, including measures of association |
//! | [`estimation`] | point and interval estimates — every confidence / credible interval for a parameter |
//! | [`hypothesis`] | tests, effect sizes, multiplicity, resampling, power, contingency-table tools |
//! | [`anova`] | every analysis of variance and post-hoc procedure |
//! | [`agreement`] | every inter-rater statistic, including its inference |
//! | [`reliability`] | scale reliability and item analysis only |
//! | [`cox`] | Cox proportional-hazards regression (Efron / Breslow ties) |
//! | [`numdist`] | the `f64` reference distributions (`cdf`/`sf`/`ppf`/`isf`) behind every numeric quantile and critical value |
//! | [`regression`], [`survival`], [`sequential`], [`information`], [`multivariate`], [`markov`], [`aggregation`], [`order`] | as named |
//!
//! and one set of type conventions:
//!
//! * observations are `&[Q]` (exact rationals); counts are `usize`;
//! * a distribution or hypothesis *parameter* (`p0`, `mu0`, `sigma`, a
//! prior shape) is a `&Q`; a probability *level* (`confidence`,
//! `alpha`, `power`) is an `f64` in `(0, 1)`, checked with one wording;
//! * **`ctx: &Context` is a parameter iff the result contains an
//! [`Ex`](crate::api::expr::Ex)** — a function returning `f64`
//! intervals or plain `Q`s takes none, and a function given an `Ex`
//! uses that expression's context;
//! * every error names the function that raised it.
pub
pub use *;
pub use *;
pub use ;
pub use PValue;
pub use *;
pub use ;
pub use RandomVariable;
pub use Rng;
pub use ;
pub use ;