Python SciPy Exponential

Recently, I was working on a data analysis project where I needed to model the time between customer arrivals at a store. The exponential distribution was perfect for this scenario, as it’s commonly used to model the time between independent events. SciPy, one of Python’s most powerful scientific libraries, offers excellent tools for working with exponential distributions.

In this article, I’ll show you how to use SciPy’s exponential distribution functions for various statistical tasks. Whether you’re analyzing customer behavior, equipment failures, or network traffic patterns, these techniques will help you extract valuable insights from your data.

So let’s get in!

What is the Exponential Distribution?

The exponential distribution is a continuous probability distribution used to model the time between events that occur independently at a constant average rate. Some real-world examples include:

  • Time between customer arrivals at a store
  • Duration between equipment failures
  • Time between emails arriving in your inbox
  • Radioactive decay processes

The exponential distribution has a single parameter, λ (lambda), which represents the rate parameter. The mean of the distribution is 1/λ, and its probability density function is:

f(x; λ) = λe^(-λx) for x ≥ 0

Read Python SciPy Stats Skew

Work with Exponential Distribution in SciPy

Let me explain the methods to work with the exponential distribution in SciPy.

Method 1: Use scipy.stats.expon for Random Sampling

SciPy provides a expon class in its stats module for working with exponential distributions. Let’s start by generating random samples:

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

# Create an exponential distribution with scale=1/lambda
# Here we're using scale=2 (which means lambda=0.5)
scale = 2
exponential = stats.expon(scale=scale)

# Generate 1000 random samples from this distribution
samples = exponential.rvs(size=1000, random_state=42)

# Plot histogram of the samples
plt.hist(samples, bins=30, density=True, alpha=0.7, color='skyblue')

# Plot the PDF
x = np.linspace(0, 15, 1000)
plt.plot(x, exponential.pdf(x), 'r-', lw=2, label=f'PDF (scale={scale})')

plt.xlabel('x')
plt.ylabel('Probability Density')
plt.title('Exponential Distribution (λ=0.5)')
plt.legend()
plt.show()

print(f"Mean of samples: {np.mean(samples):.4f}")
print(f"Theoretical mean: {scale:.4f}")

I executed the above example code and added the screenshot below.

exponential regression python

In this example, we created an exponential distribution with scale=2 (which corresponds to λ=0.5), generated 1000 random samples, and visualized them with a histogram alongside the theoretical probability density function.

Check out Python SciPy Stats Poisson

Method 2: Calculate Probabilities

We can use SciPy’s exponential distribution to calculate various probabilities:

# Probability that x is less than 3
prob_less_than_3 = exponential.cdf(3)
print(f"P(X < 3): {prob_less_than_3:.4f}")

# Probability that x is greater than 4
prob_greater_than_4 = 1 - exponential.cdf(4)
print(f"P(X > 4): {prob_greater_than_4:.4f}")

# Probability that x is between 1 and 5
prob_between_1_and_5 = exponential.cdf(5) - exponential.cdf(1)
print(f"P(1 < X < 5): {prob_between_1_and_5:.4f}")

I executed the above example code and added the screenshot below.

scipy stats exponential

The cdf method gives us the cumulative distribution function, which is the probability that a random variable takes a value less than or equal to x.

Read Python SciPy Stats Norm

Method 3: Find Percentiles with PPF

The percent point function (PPF) is the inverse of the CDF. It lets us find the value below which a certain percentage of observations fall:

# Find the 90th percentile
percentile_90 = exponential.ppf(0.9)
print(f"90th percentile: {percentile_90:.4f}")

# Find the median (50th percentile)
median = exponential.ppf(0.5)
print(f"Median: {median:.4f}")

I executed the above example code and added the screenshot below.

scipy exponential distribution

This is particularly useful when we need to estimate wait times or durations with a certain level of confidence.

Read Python SciPy Gamma

Method 4: Parameter Estimation with Fit

Often, we have data and need to estimate the exponential distribution parameters that best fit it:

# Generate some sample data
true_scale = 3
sample_data = stats.expon.rvs(scale=true_scale, size=500, random_state=42)

# Estimate the parameters
params = stats.expon.fit(sample_data)
fitted_scale = params[1]  # params[0] is loc, params[1] is scale

print(f"True scale parameter: {true_scale}")
print(f"Estimated scale parameter: {fitted_scale:.4f}")

# Plot the original data histogram
plt.hist(sample_data, bins=30, density=True, alpha=0.7, color='skyblue')

# Plot the fitted PDF
x = np.linspace(0, 20, 1000)
fitted_pdf = stats.expon.pdf(x, *params)
plt.plot(x, fitted_pdf, 'r-', lw=2, label=f'Fitted PDF (scale={fitted_scale:.4f})')

plt.xlabel('x')
plt.ylabel('Probability Density')
plt.title('Fitting Exponential Distribution to Data')
plt.legend()
plt.show()

In this example, we generated sample data with a known scale parameter, then used fit to estimate the parameter from the data. This technique is invaluable for real-world applications where the true parameters are unknown.

Method 5: Practical Example – Customer Service Wait Time

Let’s apply the exponential distribution to a real-world scenario: analyzing customer service call wait times for a company based in New York:

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

# Average wait time is 2.5 minutes
avg_wait_time = 2.5
lambda_rate = 1 / avg_wait_time

# Create exponential distribution
wait_time_dist = stats.expon(scale=avg_wait_time)

# Calculate probability of waiting less than 1 minute
prob_less_than_1 = wait_time_dist.cdf(1)
print(f"Probability of waiting less than 1 minute: {prob_less_than_1:.2%}")

# Calculate probability of waiting more than 5 minutes
prob_more_than_5 = 1 - wait_time_dist.cdf(5)
print(f"Probability of waiting more than 5 minutes: {prob_more_than_5:.2%}")

# Calculate the 95th percentile wait time
percentile_95 = wait_time_dist.ppf(0.95)
print(f"95% of customers wait less than {percentile_95:.2f} minutes")

# Simulate 1000 customer wait times
simulated_wait_times = wait_time_dist.rvs(size=1000, random_state=42)

# Plot the distribution of wait times
plt.figure(figsize=(10, 6))
plt.hist(simulated_wait_times, bins=30, density=True, alpha=0.7, color='skyblue')
x = np.linspace(0, 15, 1000)
plt.plot(x, wait_time_dist.pdf(x), 'r-', lw=2, label='Probability Density Function')
plt.axvline(x=percentile_95, color='g', linestyle='--', label=f'95th percentile ({percentile_95:.2f} min)')
plt.xlabel('Wait Time (minutes)')
plt.ylabel('Probability Density')
plt.title('Distribution of Customer Service Wait Times')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

This example shows how to analyze customer service wait times, calculate important probabilities, and visualize the distribution, all valuable insights for resource planning and customer satisfaction metrics.

Check out Python SciPy ttest_ind

Combine Exponential with Other SciPy Features

SciPy’s exponential functions work seamlessly with other SciPy and NumPy features:

# Generate 1000 random values from an exponential distribution
samples = stats.expon.rvs(scale=2, size=1000, random_state=42)

# Basic statistics
print(f"Mean: {np.mean(samples):.4f}")
print(f"Variance: {np.var(samples):.4f}")
print(f"Skewness: {stats.skew(samples):.4f}")
print(f"Kurtosis: {stats.kurtosis(samples):.4f}")

# Confidence interval for the mean (using bootstrap)
from scipy import stats
bootstrap_means = [np.mean(np.random.choice(samples, size=len(samples))) 
                  for _ in range(1000)]
ci_lower = np.percentile(bootstrap_means, 2.5)
ci_upper = np.percentile(bootstrap_means, 97.5)
print(f"95% confidence interval for the mean: ({ci_lower:.4f}, {ci_upper:.4f})")

This integration makes SciPy extremely powerful for comprehensive statistical analysis.

I hope you found this guide to SciPy’s exponential distribution helpful! Whether you’re modeling customer arrivals, equipment failures, or other time-between-events scenarios, SciPy provides all the tools you need for sophisticated analysis.

If you have any questions or suggestions, please let me know in the comments below.

Other Python articles you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.