NumPy Exponential Distribution
The exponential distribution describes the waiting time between independent events that happen at a constant average rate, and NumPy's random.exponential() generates samples from it.
What Is an Exponential Distribution?
The exponential distribution models the time between events in a process where events occur continuously and independently at a constant average rate -- think of the time between customer arrivals at a shop, radioactive decay, or requests hitting a server. It is closely related to the Poisson distribution, which counts how many events occur in a fixed interval.
The random.exponential() Method
NumPy provides this distribution through numpy.random.exponential(scale=1.0, size=None). The scale argument is the inverse of the rate parameter (often written as 1/lambda) and represents the mean waiting time. A larger scale means events are, on average, farther apart.
Example
import numpy as np
# Default scale of 1.0 -- a single sample
wait_time = np.random.exponential()
print(wait_time)
# Mean wait time of 5 minutes between events
wait_time = np.random.exponential(scale=5.0)
print(wait_time)
# 10 samples with a mean of 2.5 seconds
samples = np.random.exponential(scale=2.5, size=10)
print(samples)Shape and Skew
Unlike the uniform distribution, the exponential distribution is not flat -- it is heavily skewed toward zero. Most samples are small, but occasionally a much larger value appears, producing a long right tail. This matches real-world waiting times: most gaps between events are short, but long gaps do happen.
Example
import numpy as np
np.random.seed(42)
samples = np.random.exponential(scale=3.0, size=100000)
print(samples.mean()) # should be close to 3.0
print(samples.std()) # should also be close to 3.0 (mean == std for exponential)
print(samples.min(), samples.max())- Values are always non-negative.
- The mean and the standard deviation of an exponential distribution are both equal to scale.
- Small values are far more common than large ones -- the distribution is right-skewed.
- It is memoryless: the probability of waiting an additional t seconds does not depend on how long you've already waited.
- It is the continuous-time counterpart of the discrete Poisson distribution.
Example
import numpy as np
np.random.seed(1)
# Simulate arrival times over an 8-hour shift where events
# average one every 15 minutes
gaps = np.random.exponential(scale=15, size=50)
arrival_times = np.cumsum(gaps)
# Keep only arrivals within an 8-hour (480 minute) shift
arrivals_in_shift = arrival_times[arrival_times <= 480]
print(arrivals_in_shift)
print(f'Number of arrivals: {len(arrivals_in_shift)}')Exercise: NumPy Exponential Distribution
What real-world scenario does the exponential distribution typically model?