Link Search Menu Expand Document (external link)

How to compute probabilities from a distribution (in Python, using SciPy)

See all solutions.

Task

There are many famous continuous probability distributions, such as the normal and exponential distributions. How can we get access to them in software, to compute the probability of a value/values occurring?

Related tasks:

Solution

You can import many different random variables from SciPy’s stats module. The full list of them is online here.

To compute a probability from a discrete distribution, create a random variable, then use its Probability Mass Function, pmf.

1
2
3
4
5
6
7
8
from scipy import stats

# Create a binomial random variable with 10 trials
# and probability 0.5 of success on each trial
X = stats.binom( 10, 0.5 )

# What is the probability of exactly 3 successes?
X.pmf( 3 )
1
0.1171875

To compute a probability from a continuous distribution, create a random variable, then use its Cumulative Density Function, cdf. You can only compute the probability that a random value will fall in an interval $[a,b]$, not the probability that it will equal a specific value.

1
2
3
4
5
6
7
from scipy import stats

# Create a normal random variable with mean μ=10 and standard deviation σ=5
X = stats.norm( 10, 5 )

# What is the probability of the value lying in the interval [12,13]?
X.cdf( 13 ) - X.cdf( 12 )
1
0.07032514063960227

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Nathan Carter (ncarter@bentley.edu)