Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sampling from uniform and categorical distributions #483

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

balping
Copy link
Contributor

@balping balping commented Dec 1, 2024

Now you can sample from uniform and categorical discrete distributions by calling rand().

Examples:

$uniform = new Uniform(10, 20);
$random = $uniform->rand();


$categorical   = new Categorical(3, ['a' => 0.2, 'b' => 0.5, 'c' => 0.3]);
$random  = $categorical->rand(); // $random is 'a' or 'b' or 'c'

PS. I'm not sure why the static analysis is failing

@coveralls
Copy link

Coverage Status

coverage: 99.912% (-0.009%) from 99.921%
when pulling 80d51f9 on balping:master
into e587bfe on markrogoyski:master.

@balping balping changed the base branch from master to develop December 1, 2024 00:37
@markrogoyski
Copy link
Owner

Hi @balping,

Thank you for your interest in MathPHP, and thank you for the PR to add random functions to the Uniform and Categorical distributions.

The Uniform distribution random function looks good.

For the Categorical distribution, how about first defining a simple public cdf function on the distribution that returns the sum up to the point of the category selected. For order, how about just using the order of the categories as provided in the input array when constructing the distribution? The user can decide the order upon construction. Then, then rand function can make use of cdf internally. The cdf function itself could set a private parameter for storing all the cumulative sums.

How does that sound?

Thank you again for your PR!
Mark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants