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>>
where F: Fn(f64) -> f64 + Copy,
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 sampling
  • m - Number of pieces
  • eps - Epsilon for max pooling
  • rng - 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(())
}