SoFunction
Updated on 2025-05-14

Python plots exponential probability distribution function

Exponential distribution is a continuous probability distribution widely used in data science and statistics, and is often used to model the time intervals of independent random events. This article will introduce in detail how to quickly draw a probability density function (PDF) graph of exponential distribution in Python and provide some optimization tips. Help readers easily understand and apply exponential distribution with concise and clear explanations and code examples.

1. Theoretical basis of exponential distribution

The probability density function (PDF) of the exponential distribution is:

f(x;λ)=λe−λx

Where λ>0 is a distribution parameter, indicating the average number of occurrences per unit time (i.e. rate), and x≥0 is a random variable, indicating the time interval or waiting time of the event.

The cumulative distribution function (CDF) of the exponential distribution is:

F(x;λ)=1−e−λx

This formula represents the probability of an event occurring in x time or less.

The exponential distribution has the following key characteristics:

Memoryless: No matter what happened in the past, the probability of future events occurring only depends on the length of the time interval and is not related to the start time. This characteristic makes exponential distribution particularly applicable when describing certain random processes with "Markovity".

Monotonous decreasing: The probability density function of the exponential distribution is monotonically decreasing, and when x approaches infinity, the probability density approaches zero. This means that as the interval time between events increases, the probability of the event happening again gradually decreases.

Expectations and Variance: The expected value and variance of the exponential distribution are both 1/λ, a property that allows us to predict the average time and fluctuations of the event through simple calculations.

2. Python draws exponential distribution graph

1. Import the necessary libraries

In Python, we can use the numpy library to process numerical operations, use the matplotlib library to draw graphs, and use the stats module in the scipy library to calculate and draw exponential distribution functions.

import numpy as np
import  as plt
from  import expon

2. Define parameters and generate data points

We need to define the rate parameter λ of the exponential distribution and generate a set of data points for plotting the probability density function.

# define parameters lambdalambda_param = 1.5
# Generate 100 data points between 0 and 5x = (0, 5, 100)

3. Calculate the probability density function (PDF)

Use the formula of exponential distribution to calculate the probability density of each data point.

# Calculate the probability density functionpdf = lambda_param * (-lambda_param * x)

4. Draw a probability density function graph

Use the matplotlib library to plot the calculated probability density graph.

# Create a drawing(figsize=(10, 6))
(x, pdf, label='Exponential PDF', color='blue')
('Exponential Probability Density Function')
('x')
('PDF')
()
(True)
# Show graphics()

5. Calculate and plot exponential distribution functions using scipy library

In addition to manually calculating PDFs, we can also use the expon function in the scipy library to more conveniently calculate and plot exponential distribution functions.

# Create an exponential distribution objectrate = 2
dist = expon(scale=1/rate)
 
# Calculate the probability densityx_value = 1
pdf_value = (x_value)
print(f"PDF at x={x_value}: {pdf_value}")
 
# Calculate the cumulative probabilityx_value = 3
cdf_value = (x_value)
print(f"CDF at x={x_value}: {cdf_value}")
 
# Generate random samplessamples = (size=1000)
 
# Draw histogram(samples, bins=30, density=True, alpha=0.7)
('x')
('Probability')
('Exponential Distribution')
()

3. Optimization skills

1. Adjust the graphics parameters

By adjusting the graphics parameters, the graphics can be made more beautiful and easy to understand. For example, the graphic size, color, label, etc. can be adjusted.

# Create a drawing(figsize=(12, 8))
(x, pdf, label='Exponential PDF', color='darkorange', linewidth=2)
('Exponential Probability Density Function (Optimized)', fontsize=16)
('x', fontsize=14)
('PDF', fontsize=14)
(fontsize=12)
(True, which='both', linestyle='--', linewidth=0.5)
# Show graphics()

2. Add grid and title

Adding grids and titles can make the graphics clearer and easier to interpret.

# Create a drawing(figsize=(10, 6))
(x, pdf, label='Exponential PDF', color='blue')
('Exponential Probability Density Function with Grid')
('x')
('PDF')
()
(True, which='both', linestyle='-', linewidth=0.5)
# Show graphics()

3. Draw a cumulative distribution function (CDF) graph

Drawing the cumulative distribution function graph can further understand the characteristics of exponential distribution.

# Calculate the cumulative distribution function (CDF)cdf = 1 - (-lambda_param * x)
 
# Draw CDF diagram(figsize=(10, 6))
(x, cdf, label='Exponential CDF', color='green')
('Exponential Cumulative Distribution Function')
('x')
('CDF')
()
(True)
# Show graphics()

4. Use different λ values ​​for comparison

By drawing an exponential distribution map of different λ values, the probability of event occurrence at different rates can be intuitively compared.

# Define different λ valueslambda_values = [0.5, 1.0, 1.5, 2.0]
 
# Create a drawing(figsize=(12, 8))
 
# Draw PDF diagrams with different λ valuesfor lambda_param in lambda_values:
    pdf = lambda_param * (-lambda_param * x)
    (x, pdf, label=f'λ={lambda_param}')
 
('Exponential Probability Density Function for Different λ Values')
('x')
('PDF')
()
(True)
# Show graphics()

IV. Practical application cases

1. Reliability Engineering

The exponential distribution is often used to describe the fault time distribution of complex systems such as electronic components and mechanical equipment. For example, the failure time of a certain type of electronic device follows an exponential distribution with a parameter λ=0.01 (that is, the average failure-free time is 100 hours).

# Define parameterslambda_param = 0.01
mean_ttf = 1 / lambda_param  # Average failure-free time (hours) 
# Generate fault time datattf_samples = (scale=mean_ttf, size=1000)
 
# Draw a histogram of fault time distribution(ttf_samples, bins=30, density=True, alpha=0.6, color='blue', edgecolor='black')
 
# Plot the probability density function of exponential distributionx_ttf = (0, 4 * mean_ttf, 1000)
pdf_ttf = lambda_param * (-lambda_param * x_ttf)
(x_ttf, pdf_ttf, label='Exponential PDF', color='red', linewidth=2)
 
('Fault Time Distribution of Electronic Equipment')
('Time (hours)')
('Probability Density')
()
(True)
# Show graphics()

2. Queue theory

The index distribution is used to analyze the distribution of customer arrival time intervals in the service system, such as the arrival of customers in service windows such as banks and hospitals.

# Define parameterslambda_param = 0.3  # Customer arrival rate (person/minute) 
# Generate customer arrival time interval dataarrival_times = (scale=1/lambda_param, size=1000)
 
# Draw a histogram of the time interval distribution of customers(arrival_times, bins=30, density=True, alpha=0.6, color='purple', edgecolor='black')
 
# Plot the probability density function of exponential distributionx_arrival = (0, 20, 1000)
pdf_arrival = lambda_param * (-lambda_param * x_arrival)
(x_arrival,pdf_arrival, label='Exponential PDF', color='green', linewidth=2)
 
('Customer Arrival Time Interval Distribution')
('Time Interval (minutes)')
('Probability Density')
()
(True)
 
Show graphics
()

In the above code, we simulate the situation where the customer arrives at the service window at a rate of 0.3 people per minute, and draws a histogram of the customer arrival time interval and a probability density function graph of the exponential distribution. By comparing the two, we can see that the data distribution of the histogram is very consistent with the probability density function of the exponential distribution, which verifies the applicability of the exponential distribution in the queuing theory.

5. Summary

This article details how to quickly draw the probability density function graph of exponential distribution in Python, and provides some optimization techniques. Through practical cases, we show the application of exponential distribution in reliability engineering and queuing theory. It is hoped that these contents can help readers better understand and apply exponential distribution.

When drawing an exponential distribution graph, we need to pay attention to selecting the appropriate λ value and adjusting the graph parameters to make it more beautiful and easy to understand. In addition, by drawing the cumulative distribution function graph and comparing the exponential distribution graph of different λ values, we can further understand the characteristics of the exponential distribution.

In practical applications, exponential distribution is widely used to describe the time interval distribution of various random events, such as the failure time of electronic components, the arrival time of customers, etc. By simulating and drawing exponential distribution maps, we can make more accurate predictions and analysis of random events.

Finally, it is important to emphasize that while exponential distributions are very useful in many cases, in some complex systems, more complex distribution models may need to be considered to describe the time interval distribution of random events. Therefore, in practical applications, we need to select the appropriate distribution model according to the specific situation and conduct corresponding verification and analysis.

This is the end of this article about Python drawing exponential probability distribution functions. For more related content on Python exponential probability distribution, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!