NumPy - Exponential Distribution



Exponential Distribution in NumPy

The Exponential Distribution is a continuous probability distribution used to model the time between independent events that occur at a constant average rate.

It is defined by a single parameter (lambda), which is the rate parameter, where the mean time between events is 1/. This distribution is often used in scenarios such as the time between arrivals in a queue.

For example, if events occur on average every 5 minutes (=1/5), the exponential distribution models the time between these events.

The probability density function (PDF) of the exponential distribution is defined as −

f(x; ) =  * exp(-x) for x  0, 0 otherwise

Where,

  • : Rate parameter (inverse of the mean).
  • x: Time between events.
  • exp: Exponential function.

Exponential Distributions in NumPy

NumPy provides the numpy.random.exponential() function to generate samples from an exponential distribution. You can specify the scale parameter (1/) and the size of the generated samples.

Example

In this example, we generate 10 random samples from an exponential distribution with a rate parameter =2 (scale=0.5) −

import numpy as np

# Generate 10 random samples from an exponential distribution with =2 (scale=0.5)
samples = np.random.exponential(scale=0.5, size=10)
print("Random samples from exponential distribution:", samples)

Following is the output obtained −

Random samples from exponential distribution: [0.49251018 0.14301367 0.48682864 0.09396999 0.11923932 0.28674142
 0.14285472 0.11916798 1.59846326 0.27175065]

Visualizing Exponential Distributions

Visualizing exponential distributions helps to understand their properties better. We can use libraries such as Matplotlib to create histograms that display the distribution of generated samples.

Example

In the following example, we are first generating 1000 random samples from an exponential distribution with =2 (scale=0.5). We are then creating a histogram to visualize this distribution −

import numpy as np
import matplotlib.pyplot as plt

# Generate 1000 random samples from an exponential distribution with =2 (scale=0.5)
samples = np.random.exponential(scale=0.5, size=1000)

# Create a histogram to visualize the distribution
plt.hist(samples, bins=30, edgecolor='black', density=True)
plt.title('Exponential Distribution')
plt.xlabel('Time between events')
plt.ylabel('Frequency')
plt.show()

The histogram shows the frequency of the time between events in the exponential trials. The bars represent the probability of each possible outcome, which forms the characteristic shape of an exponential distribution −

Exponential Distribution

Applications of Exponential Distributions

Exponential distributions are used in various fields to model the time between events in a Poisson process. Here are a few practical applications −

  • Reliability Analysis: Modeling the time between failures of mechanical systems.
  • Queuing Theory: Modeling the time between arrivals of customers in a queue.
  • Survival Analysis: Modeling the time until an event, such as death or failure, occurs.

Generating Cumulative Exponential Distributions

Sometimes, we are interested in the cumulative distribution function (CDF) of an exponential distribution, which gives the probability of getting up to and including x events in the interval.

NumPy does not have a built-in function for the CDF of an exponential distribution, but we can calculate it using a loop and the scipy.stats.expon.cdf() function from the SciPy library.

Example

Following is an example to generate cumulative exponential distribution in NumPy −

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

# Define the rate parameter
lam = 2

# Generate the cumulative distribution function (CDF) values
x = np.linspace(0, 3, 100)
cdf = expon.cdf(x, scale=1/lam)

# Plot the CDF
plt.plot(x, cdf, marker='o', linestyle='-', color='b')
plt.title('Cumulative Exponential Distribution')
plt.xlabel('Time between events')
plt.ylabel('Cumulative probability')
plt.grid(True)
plt.show()

The plot shows the cumulative probability of getting up to and including each time between events in the exponential trials. The CDF is a smooth curve that increases to 1 as the time between events increases −

Cumulative Exponential Distribution

Properties of Exponential Distributions

Exponential distributions have several key properties, they are −

  • Memoryless Property: The probability of an event occurring in the future is independent of the time that has already passed.
  • Mean and Variance: The mean of an exponential distribution is 1/, and the variance is 1/2.
  • Skewness: The distribution is skewed to the right, with a long tail.

Exponential Distribution for Hypothesis Testing

Exponential distributions are often used in hypothesis testing, particularly in tests for the time between events.

One common test is the exponential test, which is used to determine if the observed time between events is significantly different from the expected time. Here is an example using the scipy.stats.expon() function:

Example

In this example, we perform an exponential test to determine if the observed time between events (0.8) is significantly different from the expected rate (=2). The p-value indicates the probability of obtaining a result at least as extreme as the observed result, assuming the null hypothesis is true −

from scipy.stats import expon

# Observed time between events
observed_time = 0.8

# Expected rate parameter ()
expected_rate = 2

# Perform the exponential test
p_value = expon.sf(observed_time, scale=1/expected_rate)
print("P-value from exponential test:", p_value)

The output obtained is as shown below −

P-value from exponential test: 0.20189651799465538

Seeding for Reproducibility

To ensure reproducibility, you can set a specific seed before generating exponential distributions. This ensures that the same sequence of random numbers is generated each time you run the code.

Example

By setting the seed, you ensure that the random generation produces the same result every time the code is executed as shown in the example below −

import numpy as np

# Set the seed for reproducibility
np.random.seed(42)

# Generate 10 random samples from an exponential distribution with =2 (scale=0.5)
samples = np.random.exponential(scale=0.5, size=10)
print("Random samples with seed 42:", samples)

The result produced is as follows −

Random samples with seed 42: [0.23463404 1.50506072 0.65837285 0.45647128 0.08481244 0.08479815
 0.02991938 1.00561543 0.45954108 0.61562503]
Advertisements