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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With