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
//! Percentile confidence intervals and the bootstrap-statistic helper they
//! consume.
//!
//! [`percentile_ci`] is validated for reference equivalence against
//! `scipy.stats.bootstrap` golden fixtures downstream.
use floor_rank;
use bootstrap_indices;
use crateNormalDistribution;
use crateSample;
use crate;
use crateSplitMix64;
/// Computes the two-sided percentile confidence interval of `samples`.
///
/// Sorts a copy of `samples` by total order (NaN-safe) and returns the
/// `alpha/2` and `1 - alpha/2` percentile values by nearest-rank: the lower bound
/// is the element at rank `floor((alpha/2) * n)` and the upper bound the element
/// at rank `floor((1 - alpha/2) * n)`, both clamped to `0..n`. This is the
/// percentile-method interval used for the bootstrap distribution of a statistic.
///
/// # Arguments
///
/// * `samples` — the (e.g. bootstrap) statistic values; order does not matter.
/// * `alpha` — the total tail mass; e.g. `0.05` for a 95% interval. Expected in
/// `[0, 1]`.
///
/// # Returns
///
/// `(lower, upper)` percentile bounds drawn from `samples`.
///
/// # Errors
///
/// Returns [`Error::EmptyInput`] when `samples` is empty (no interval is defined).
///
/// # Examples
///
/// ```
/// use stats_claw::resampling::percentile_ci;
///
/// let xs: Vec<f64> = (0..1000).map(f64::from).collect();
/// let (lo, hi) = percentile_ci(&xs, 0.05)?;
/// assert!(lo < 500.0 && hi > 500.0);
/// # Ok::<(), stats_claw::error::Error>(())
/// ```
/// Computes the bootstrap distribution of a statistic over `data`.
///
/// Draws `b` with-replacement resamples of `data` (via [`bootstrap_indices`]),
/// evaluates `statistic` on each resampled view, and returns the `b` results.
/// Pairing this with [`percentile_ci`] yields a percentile bootstrap interval.
///
/// # Arguments
///
/// * `data` — the observed sample.
/// * `b` — number of bootstrap resamples.
/// * `rng` — the deterministic generator driving the resampling.
/// * `statistic` — maps a resampled view of the data to a scalar (e.g. the
/// median).
///
/// # Returns
///
/// A vector of `b` statistic values, one per resample.
///
/// # Errors
///
/// Returns [`Error::EmptyInput`] when `data` is empty.
/// Estimates the empirical coverage of a percentile interval for the mean of a
/// known normal, over seeded Monte-Carlo replications.
///
/// Each replication draws `n` variates from `Normal(mean, std_dev)`, forms the
/// `1 − alpha` percentile interval of the sample, and checks whether it brackets
/// the true `mean`. The returned rate should approach the nominal `1 − alpha` as
/// `replications` grows.
///
/// # Arguments
///
/// * `mean`, `std_dev` — the true sampling distribution (`std_dev > 0`).
/// * `n` — observations per replication.
/// * `replications` — number of seeded replications.
/// * `alpha` — total tail mass of each interval (e.g. `0.05`).
/// * `rng` — the deterministic generator driving every draw.
///
/// # Returns
///
/// The fraction of replications whose interval contained `mean`, in `[0, 1]`.