Getting quantiles from a beta distribution using python
Asked Answered
R

2

6

I need to get the Nth quantile of a beta distribution, or equivalently, the 95% or 99% percentile. This is so much easier in Maple, which allows symbolic input -- but how is this done in Python?

I've searched stackoverflow, and it seems that people are often concerned with the normal distribution only.

Roundlet answered 26/10, 2015 at 1:41 Comment(2)
I am confused. Are you asking for a way to tell, for example, whether x=1 is in the 95% percentile? You could use scipy.stats.beta for this.Enceladus
Thanks @Julien, I did just that.Roundlet
R
7

I ended up with the ppf instead:

scipy.stats.beta.ppf(prob,2,N-2)
Roundlet answered 27/10, 2015 at 3:5 Comment(1)
Much better. 123Ithyphallic
K
1

You can compute the quantile of a beta distribution with the following function:

from scipy.stats import beta
import numpy as np
a, b = 2.31, 0.627
x = np.linspace(beta.ppf(0.01, a, b), beta.ppf(0.99, a, b), 100)
distribution=beta.pdf(x, a, b)
def quantile(x,quantiles):
    xsorted = sorted(x)
    qvalues = [xsorted[int(q * len(xsorted))] for q in quantiles]
    return zip(quantiles,qvalues)
quantiles = quantile(distribution,[0.05,0.16,.5,.84, 0.95])
Knurl answered 26/10, 2015 at 2:14 Comment(1)
Thanks a lot! I ended up using the ppf but this is really helpful.Roundlet

© 2022 - 2024 — McMap. All rights reserved.