Skip to content

Python exponential distribution

  • by

The exponential distribution is a continuous probability distribution that models the time between events in a Poisson process, where events occur continuously and independently at a constant average rate.

In Python, you can work with the exponential distribution using the scipy.stats module. Here’s an example of how to generate random numbers from an exponential distribution and calculate some statistics:

import numpy as np
from scipy.stats import expon

# Generate random numbers from exponential distribution
scale = 2.0  # The scale parameter (mean = 1 / scale)
size = 1000  # Number of random numbers to generate
random_numbers = expon.rvs(scale=scale, size=size)

# Calculate statistics
mean = expon.mean(scale=scale)
variance = expon.var(scale=scale)
standard_deviation = expon.std(scale=scale)
median = expon.median(scale=scale)

print("Mean:", mean)
print("Variance:", variance)
print("Standard Deviation:", standard_deviation)
print("Median:", median)

You can also use the exponential distribution to calculate probabilities and percentiles. For example, to calculate the cumulative distribution function (CDF) at a given value or the inverse CDF (quantile), you can use the expon.cdf and expon.ppf functions, respectively.

# Calculate CDF at a given value
x = 3.0
cdf = expon.cdf(x, scale=scale)
print("CDF at", x, ":", cdf)

# Calculate quantile at a given probability
p = 0.75
quantile = expon.ppf(p, scale=scale)
print("Quantile at", p, ":", quantile)

These examples demonstrate the basic usage of the exponential distribution in Python using the scipy.stats module. You can explore more functions and capabilities of the module in the SciPy documentation: https://docs.scipy.org/doc/scipy/reference/stats.html

Python exponential distribution example

Here’s an example that demonstrates how to work with the exponential distribution in Python:

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

# Parameters for the exponential distribution
scale = 2.0

# Create an exponential distribution object
dist = expon(scale=scale)

# Generate random numbers from the exponential distribution
random_numbers = dist.rvs(size=1000)

# Calculate the probability density function (PDF)
x = np.linspace(0, 10, 100)
pdf = dist.pdf(x)

# Calculate the cumulative distribution function (CDF)
cdf = dist.cdf(x)

# Plot the PDF and CDF
plt.figure(figsize=(10, 4))

plt.subplot(1, 2, 1)
plt.plot(x, pdf, label='PDF')
plt.title('Exponential Distribution (PDF)')
plt.xlabel('x')
plt.ylabel('Probability Density')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(x, cdf, label='CDF')
plt.title('Exponential Distribution (CDF)')
plt.xlabel('x')
plt.ylabel('Cumulative Probability')
plt.legend()

plt.tight_layout()
plt.show()

Output:

Python exponential distribution

To visualize the distribution, we calculate the probability density function (PDF) and cumulative distribution function (CDF) at different values of x. We use np.linspace to create an array of x values from 0 to 10.

You can run this code to see the plots representing the exponential distribution’s PDF and CDF based on the specified scale parameter. Feel free to adjust the scale or experiment with different parameters to explore the behavior of the exponential distribution.

Additional Methods and Attributes:

# Calculate the mean of the distribution
mean = dist.mean()

# Calculate the variance of the distribution
variance = dist.var()

# Calculate the standard deviation of the distribution
std_dev = dist.std()

# Calculate the median of the distribution
median = dist.median()

# Calculate the moment of a specific order
moment = dist.moment(n)

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading