Function peroxide::statistics::rand::prs
source · pub fn prs<F>(
f: F,
n: usize,
(a, b): (f64, f64),
m: usize,
eps: f64,
) -> Result<Vec<f64>>
Expand description
Piecewise Rejection Sampling
§Arguments
f
- Function to sample (unnormalized function is allowed)n
- Number of samples(a, b)
- Range of samplingm
- Number of pieceseps
- Epsilon for max pooling
§Examples
use peroxide::fuga::*;
fn main() -> Result<(), Box<dyn Error>> {
let f = |x: f64| {
if (0f64..=2f64).contains(&x) {
-(x - 1f64).powi(2) + 1f64
} else {
0f64
}
};
let samples = prs(f, 1000, (-1f64, 3f64), 200, 1e-4)?;
samples.mean().print(); // near 1
Ok(())
}