Skip to content

Commit

Permalink
Merge pull request #151 from statrs-dev/fix_laplace_sampling
Browse files Browse the repository at this point in the history
fix laplace distribution sampling
  • Loading branch information
troublescooter authored Aug 24, 2021
2 parents bc5e408 + bde00c1 commit cdc7920
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/distribution/laplace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Laplace {
impl ::rand::distributions::Distribution<f64> for Laplace {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let x: f64 = rng.gen_range(-0.5..0.5);
self.location - self.scale * x.signum() * (1. - 2. * x).ln()
self.location - self.scale * x.signum() * (1. - 2. * x.abs()).ln()
}
}

Expand Down Expand Up @@ -458,4 +458,38 @@ mod tests {
let l = try_create(0.1, 0.5);
l.sample(&mut thread_rng());
}

#[test]
fn test_sample_distribution() {
use ::rand::rngs::StdRng;
use ::rand::SeedableRng;
use rand::distributions::Distribution;

// sanity check sampling
let location = 0.0;
let scale = 1.0;
let n = try_create(location, scale);
let trials = 10_000;
let tolerance = 250;

for seed in 0..10 {
let mut r: StdRng = SeedableRng::seed_from_u64(seed);

let result = (0..trials).map(|_| n.sample(&mut r)).fold(0, |sum, val| {
if val > 0.0 {
sum + 1
} else if val < 0.0 {
sum - 1
} else {
0
}
});
assert!(
result > -tolerance && result < tolerance,
"Balance is {} for seed {}",
result,
seed
);
}
}
}

0 comments on commit cdc7920

Please sign in to comment.