Function peroxide::statistics::rand::prs_with_rng
source · pub fn prs_with_rng<F, R: Rng + Clone>(
f: F,
n: usize,
(a, b): (f64, f64),
m: usize,
eps: f64,
rng: &mut R,
) -> Result<Vec<f64>>
Expand description
Piecewise Rejection Sampling with specific Rng
§Arguments
f
- Function to sample (unnormalized function is allowed)n
- Number of samples(a, b)
- Range of samplingm
- Number of pieceseps
- Epsilon for max poolingrng
- Random number generator
§Examples
use peroxide::fuga::*;
fn main() -> Result<(), Box<dyn Error>> {
let mut rng = smallrng_from_seed(42);
let f = |x: f64| {
if (0f64..=2f64).contains(&x) {
-(x - 1f64).powi(2) + 1f64
} else {
0f64
}
};
let samples = prs_with_rng(f, 1000, (-1f64, 3f64), 200, 1e-4, &mut rng)?;
assert!((samples.mean() - 1f64).abs() < 1e-1);
Ok(())
}