Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluating (not Sampling from) Beta Distribution in Numpy

I am trying to calculate Beta(x; a, b) for many specific values of x, taking a = 1, b = 25. There is a beta function in numpy, namely numpy.random.beta(a, b). But it samples from the beta distribution, rather than evaluating for specific values of x.

Any quick way around this, rather than having to code in the formula for beta?

like image 801
J.D. Avatar asked Jul 26 '19 06:07

J.D.


1 Answers

You are most likely looking for the scipy.stats.beta class. This lets you create a distribution for a particular pair of a, b values:

dist = scipy.stats.beta(a, b)

You can then get the PDF and evaluate at any x:

dist.pdf(x)

x can be any numpy array, not just a scalar. You can evaluate the cumulative distribution in a similar manner:

dist.cdf(x)

You don't even need to create an instance object. This is especially useful if you want to evaluate the function once. In your particular use-case, it would probably be better to create an instance:

scipy.stats.beta.pdf(x, a, b)

The same applies to the CDF. The linked documentation shows lots of other things you can do with the distribution object.

scipy.stats contains many of other common continuous distributions that all follow a similar interface. Any time you have a question related to basic statistics, scipy is the place to start.

like image 80
Mad Physicist Avatar answered Oct 07 '22 12:10

Mad Physicist