irene
irene

Reputation: 2243

Getting quantiles from a beta distribution using python

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.

Upvotes: 6

Views: 4798

Answers (2)

irene
irene

Reputation: 2243

I ended up with the ppf instead:

scipy.stats.beta.ppf(prob,2,N-2)

Upvotes: 8

Dalek
Dalek

Reputation: 4318

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])

Upvotes: 1

Related Questions