Exponential Distribution

📈 Exponential Distribution in Python

The Exponential Distribution models the time between independent events that occur at a constant rate.
It is closely related to the Poisson Distribution, which models the number of events in a fixed interval.


✅ 1. Characteristics of Exponential Distribution

  • Continuous distribution

  • Parameter: scale = 1/λ (λ = event rate)

  • Probability Density Function (PDF):

f(x)=1scalee−x/scalefor x≥0f(x) = \frac{1}{\text{scale}} e^{-x/\text{scale}} \quad \text{for } x \ge 0

  • Mean = scale

  • Standard Deviation = scale

Applications:

  • Time between customer arrivals

  • Lifespan of electronic components

  • Waiting times in queues


✅ 2. Generate Exponential Data Using NumPy

import numpy as np

# Parameters
scale = 2.0 # mean time between events
size = 1000 # number of samples

# Generate exponential random numbers
data = np.random.exponential(scale=scale, size=size)

print(data[:10])

Output (example):

[0.34, 1.12, 3.45, 0.56, 4.23, 0.89, 2.34, 1.67, 0.45, 0.78]
  • Values ≥ 0

  • Skewed distribution


✅ 3. Visualize Exponential Distribution

import matplotlib.pyplot as plt
import seaborn as sns

sns.histplot(data, bins=30, kde=True, color='purple')
plt.title("Exponential Distribution (scale=2.0)")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

  • Histogram is right-skewed

  • KDE shows the smooth probability density


✅ 4. Change Scale Parameter

data_small = np.random.exponential(scale=1.0, size=1000)
data_large = np.random.exponential(scale=5.0, size=1000)

sns.histplot(data_small, bins=30, color='red', label='scale=1', kde=True, alpha=0.5)
sns.histplot(data_large, bins=30, color='blue', label='scale=5', kde=True, alpha=0.5)
plt.title("Exponential Distribution Comparison")
plt.legend()
plt.show()

  • Smaller scale → more concentrated near 0

  • Larger scale → more spread out


✅ 5. Mean and Standard Deviation

mean = np.mean(data)
std = np.std(data)
print("Mean:", mean)
print("Standard Deviation:", std)
  • Both should approximately equal the scale parameter


🧠 Summary Table

Function Parameters Description
np.random.exponential() scale, size Generate exponential random numbers
scale Mean / 1/λ Average time between events
size Number of samples Output array size

🎯 Practice Exercises

  1. Simulate waiting times between calls with scale=3 for 1000 samples.

  2. Compare distributions with scale=1, 2, 5 in one plot.

  3. Compute mean and standard deviation of generated data and compare with scale.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *