How do you find the exponential distribution in python?


The exponential distribution is a probability distribution that is used to model the time we must wait until a certain event occurs.

If a random variable X follows an exponential distribution, then the cumulative distribution function of X can be written as:

F(x; λ) = 1 – e-λx

where:

  • λ: the rate parameter (calculated as λ = 1/μ)
  • e: A constant roughly equal to 2.718

This tutorial explains how to use the exponential distribution in Python.

How to Generate an Exponential Distribution

You can use the expon.rvs(scale, size) function from the SciPy library in Python to generate random values from an exponential distribution with a specific rate parameter and sample size:

from scipy.stats import expon

#generate random values from exponential distribution with rate=40 and sample size=10
expon.rvs(scale=40, size=10)

array([116.5368323 ,  67.23514699,  12.00399043,  40.74580584,
        34.60922432,   2.68266663,  22.70459831,  97.66661811,
         6.64272914,  46.15547298])

Note: You can find the complete documentation for the SciPy library here.

How to Calculate Probabilities Using an Exponential Distribution

Suppose the mean number of minutes between eruptions for a certain geyser is 40 minutes. What is the probability that we’ll have to wait less than 50 minutes for an eruption?

To solve this, we need to first calculate the rate parameter:

  • λ = 1/μ
  • λ = 1/40
  • λ = .025

We can plug in λ = .025 and x = 50 to the formula for the CDF:

  • P(X ≤ x) = 1 – e-λx
  • P(X ≤ 50) = 1 – e-.025(50)
  • P(X ≤ 50) = 0.7135

The probability that we’ll have to wait less than 50 minutes for the next eruption is 0.7135.

We can use the expon.cdf() function from SciPy to solve this problem in Python:

from scipy.stats import expon

#calculate probability that x is less than 50 when mean rate is 40
expon.cdf(x=50, scale=40)

0.7134952031398099

The probability that we’ll have to wait less than 50 minutes for the next eruption is 0.7135.

This matches the value that we calculated by hand.

How to Plot an Exponential Distribution

You can use the following syntax to plot an exponential distribution with a given rate parameter:

from scipy.stats import expon
import matplotlib.pyplot as plt

#generate exponential distribution with sample size 10000
x = expon.rvs(scale=40, size=10000)

#create plot of exponential distribution
plt.hist(x, density=True, edgecolor='black')

How do you find the exponential distribution in python?

Additional Resources

The following tutorials explain how to use other common distributions in Python:

How to Use the Poisson Distribution in Python
How to Use the t Distribution in Python
How to Use the Uniform Distribution in Python


Exponential Distribution

Exponential distribution is used for describing time till next event e.g. failure/success etc.

It has two parameters:

scale - inverse of rate ( see lam in poisson distribution ) defaults to 1.0.

size - The shape of the returned array.

Example

Draw out a sample for exponential distribution with 2.0 scale with 2x3 size:

from numpy import random

x = random.exponential(scale=2, size=(2, 3))

print(x)

Try it Yourself »


Visualization of Exponential Distribution

Example

from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot(random.exponential(size=1000), hist=False)

plt.show()

Result

How do you find the exponential distribution in python?

Try it Yourself »


Relation Between Poisson and Exponential Distribution

Poisson distribution deals with number of occurences of an event in a time period whereas exponential distribution deals with the time between these events.



How do you write an exponential distribution in Python?

The exponential distribution is a probability distribution that is used to model the time we must wait until a certain event occurs. where: λ: the rate parameter (calculated as λ = 1/μ).
P(X ≤ x) = 1 – e. -λx.
P(X ≤ 50) = 1 – e. -.025(50).
P(X ≤ 50) = 0.7135..

How do you find the exponential distribution?

The formula for the exponential distribution: P ( X = x ) = m e - m x = 1 μ e - 1 μ x P ( X = x ) = m e - m x = 1 μ e - 1 μ x Where m = the rate parameter, or μ = average time between occurrences.

How do you plot a CDF of an exponential distribution in Python?

Plotting exponential distribution.
import matplotlib. pyplot as plt import numpy as np #fixing the seed for reproducibility #of the result np. ... .
import numpy as np import matplotlib. pyplot as plt import seaborn as sns #fixing the seed for reproducibility #of the result np. ... .
import numpy as np import matplotlib..

What does NP random exponential do?

exponential() in Python. With the help of numpy. random. exponential() method, we can get the random samples from exponential distribution and returns the numpy array of random samples by using this method.