7

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?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
J.D.
  • 139
  • 4
  • 14

1 Answers1

7

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.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • @Warren. I don't think there's much ambiguity. A distribution is sampled according to its PDF, sort of by definition. I'd be happy to add the CDF as well. It's useful information, and there's a chance you may be right. – Mad Physicist Jul 26 '19 at 06:56
  • how to calculate PDF between two probabilities? I've tried dist.pdf(x2) - dist.pdf(x1) however it doesn't seem correct... – haneulkim Jul 19 '21 at 05:56
  • @haneulkim. I have no idea what that means. Consider asking a question with an MCVE. You can ping me here when you do if you want me to take a look. – Mad Physicist Jul 19 '21 at 12:52