-
-
Notifications
You must be signed in to change notification settings - Fork 477
Description
Background
Beta distribution is implemented through the Beta struct and samples should give a number between zero and one. It is known that this distribution is numerically delicate when dealing with both parameters (alpha and beta) small.
The implementation of the sample method is though the following characterization.
If X, Y are independent and X follows Gamma(alpha, theta) and Y follows Gamma(beta, theta), then X / (X + Y) follows Beta(alpha, beta).
For more such characterization, see here.
Sampling from a beta distribution with both alpha and beta parameters small returns NAN samples. This is clear from the implementation, but is not expected for the user at all!
By the way, values of 1.0e-3 are already small enough to easily get a NAN result. Just run the following code.
use rand::distributions::Distribution;
fn main() {
let param = 1.0e-3;
let beta = rand_distr::Beta::new(param, param).unwrap();
for x in beta.sample_iter(rand::thread_rng()) {
if (x as f64).is_nan() {
println!("I got a NAN!!");
}
}
}What is your motivation?
I as doing numerical simulations and need to simulation beta samples as part of a rejection sampling algorithm. Running into nan values was unexpected, but could solve the issue by a particular symmetry present in my problem.
What type of application is this? (E.g. cryptography, game, numerical simulation)
Numerical simulation.
Feature request
I would like to contribute to a more robust simulation method of the beta variable that takes into account such cases.