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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2022 The Ferric AI Project Developers
//! A probabilistic programming language in Rust with a declarative syntax for
//! Bayesian models.
//!
//! # Key entry points
//!
//! - [`make_model!`] — declare a probabilistic model; expands into a module
//! with `Model`, `Sample`, `WeightedSample`, and two iterator types.
//! - [`weighted_mean`] / [`weighted_std`] — posterior summaries from
//! self-normalised importance-sampling (SNIS) weights.
//! - [`distributions`] — built-in probability distributions:
//! [`Bernoulli`](distributions::Bernoulli),
//! [`Binomial`](distributions::Binomial),
//! [`Geometric`](distributions::Geometric),
//! [`Poisson`](distributions::Poisson),
//! [`Uniform`](distributions::Uniform),
//! [`Exponential`](distributions::Exponential),
//! [`Normal`](distributions::Normal),
//! [`LogNormal`](distributions::LogNormal),
//! [`Beta`](distributions::Beta),
//! [`Gamma`](distributions::Gamma),
//! [`StudentT`](distributions::StudentT),
//! [`Cauchy`](distributions::Cauchy),
//! [`MultivariateNormal`](distributions::MultivariateNormal),
//! [`MatrixNormal`](distributions::MatrixNormal), and
//! [`Wishart`](distributions::Wishart).
//!
//! See the [README](https://github.com/Ferric-AI/ferric#readme) for a
//! quick-start guide and worked examples.
// re-export make_model from the ferric-macros crate
pub use make_model;
// Public modules
// re-export FeOption and its variants
pub use FeOption;
pub use ;
/// Compute the self-normalised importance-weighted mean of `values`.
///
/// Given a collection of values $x_i$ and their corresponding log importance
/// weights $\tilde{w}_i$ (unnormalised, in log space), this computes the
/// self-normalised importance-sampling (SNIS) estimate of $\mathbb{E}(X)$:
///
/// $$\hat{\mu} = \frac{\sum_i w_i x_i}{\sum_i w_i},
/// \qquad w_i = e^{\tilde{w}_i - \max_j \tilde{w}_j}$$
///
/// The max-subtraction keeps the arithmetic numerically stable without
/// changing the result (it cancels in numerator and denominator).
///
/// # Panics
///
/// Panics if `values` and `log_weights` have different lengths.
///
/// # Examples
///
/// ```
/// use ferric::weighted_mean;
///
/// // Uniform weights — equivalent to a plain mean.
/// let values = vec![1.0_f64, 2.0, 3.0];
/// let log_weights = vec![0.0_f64; 3];
/// let mean = weighted_mean(&values, &log_weights);
/// assert!((mean - 2.0).abs() < 1e-10);
/// ```
/// Compute the self-normalised importance-weighted standard deviation of
/// `values`.
///
/// Uses the same SNIS weights as [`weighted_mean`] to estimate
///
/// $$\hat{\sigma} = \sqrt{\frac{\sum_i w_i (x_i - \hat{\mu})^2}{\sum_i w_i}}$$
///
/// This is the weighted population standard deviation (not the unbiased
/// sample estimate), which is appropriate for summarising an importance-
/// sampling posterior.
///
/// # Panics
///
/// Panics if `values` and `log_weights` have different lengths.
///
/// # Examples
///
/// ```
/// use ferric::weighted_std;
///
/// // Population std of [1, 2, 3] with uniform weights is sqrt(2/3).
/// let values = vec![1.0_f64, 2.0, 3.0];
/// let log_weights = vec![0.0_f64; 3];
/// let std = weighted_std(&values, &log_weights);
/// assert!((std - (2.0_f64 / 3.0).sqrt()).abs() < 1e-10);
/// ```