Skip to content

Unexpected sample values from beta distribution for small parameters #999

@saona-raimundo

Description

@saona-raimundo

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.

I don't have a particular idea in mind. I tried the [scipy module to simulate beta](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.beta.html) and it seemed more robust (it gave some numbers that made sense in the cases I tried).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions