Generating Random Variables from a Uniform Distribution#
A common and general technique for generating random variables with a specified distribution starts with a uniformly distributed random variable
To transform
where
Example: Generating an Exponential Random Variable from a Uniform Distribution#
To generate an exponential random variable from a uniform distribution, we follow these steps:
Start with a Uniform Random Variable
Let
Define the PDF of the Target Distribution
Assume we want to generate a random variable
This PDF describes the likelihood of different values of
Determine the CDF of the Target Distribution
The cumulative distribution function (CDF)
This function represents the cumulative probability up to a point
Use the Inverse CDF Transformation
To generate a value of
When using the inverse CDF transformation, we equate the cumulative distribution function (CDF) of the desired random variable
In mathematical terms:
where
For this example, we set the CDF of
Solving this equation for
Rearranging the equation to solve for
Thus, the transformation
Generate the Exponential Random Variable
To generate a sample
First, we generate a sample
from the uniform distribution .We apply the transformation
to obtain the corresponding value from the exponential distribution.
This process can be repeated to generate multiple samples from the exponential distribution, with each sample derived from a new independent uniform random variable
Simulation#
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import expon
# Set the seed for reproducibility
np.random.seed(0)
# Number of samples to generate
n_samples = 10000
# Method 1: Generate from uniform distribution and transform using inverse CDF
u = np.random.uniform(0, 1, n_samples)
x_empirical = -np.log(1 - u)
# Method 2: Using built-in exponential distribution generator
x_builtin = np.random.exponential(scale=1.0, size=n_samples)
# Plot the histograms
plt.figure(figsize=(12, 6))
# Histogram of empirical x
plt.hist(x_empirical, bins=50, density=True, alpha=0.3, label='Empirical $x$ from $u$')
# Histogram of built-in x
plt.hist(x_builtin, bins=100, density=True, alpha=0.3, label='Built-in $x$ from numpy')
# Generate the x values
x_vals = np.linspace(0, 10, 1000)
# Theoretical PDF using scipy
pdf_scipy = expon.pdf(x_vals)
# Theoretical PDF using the expression p_x(x) = e^{-x}
pdf_expr = np.exp(-x_vals)
# Plot the scipy.stats.expon PDF
plt.plot(x_vals, pdf_scipy, 'b-', lw=2, label='Theoretical PDF (scipy.stats.expon)')
# Plot the PDF using the expression p_x(x) = e^{-x}
plt.plot(x_vals, pdf_expr, 'r--', lw=2, label='$f_{X}(x) = e^{-x}$')
# Labels and title
plt.xlabel('x')
plt.ylabel('Density')
plt.title('Comparison of Exponential Distribution Generation Methods')
plt.legend()
plt.grid(True)
plt.show()

Schonhoff’s Method#
import numpy as np
def exprv(num):
"""
Generate exponentially distributed random variables.
Parameters:
num (int): Number of random variables to generate.
Returns:
numpy.ndarray: Array of exponentially distributed random variables.
"""
# Set the random seed for reproducibility
np.random.seed(0)
# Generate uniformly distributed random variables in the range (0, 1)
u = np.random.rand(num)
# Transform to exponentially distributed random variables using -log(u)
ex = -np.log(u)
return ex
# Example usage
num = 5 # Number of random variables to generate
exponential_rvs = exprv(num)
print("Exponentially distributed random variables:", exponential_rvs)
Exponentially distributed random variables: [0.5999966 0.33520792 0.50623057 0.60718385 0.85883631]
import numpy as np
import matplotlib.pyplot as plt
def ete(num):
"""
Theoretical and Empirical PDFs of Exponentially distributed random numbers.
Parameters:
num (int): Number of random variables (RVs) to generate.
Outputs:
Displays the theoretical PDF and the empirical PDF.
"""
# Generate uniformly distributed random variables
u = np.random.rand(num)
# Convert to exponentially distributed random variables
z = -np.log(u)
# Create a range for x values
x = np.arange(0, 10.1, 0.1)
# Compute the empirical PDF using histogram
et, bin_edges = np.histogram(z, bins=x, density=False)
# Normalize the empirical PDF
n10 = num / 10
nznorm = et / n10
# Plot the empirical PDF
plt.plot(bin_edges[:-1], nznorm, 'b', label='Empirical PDF')
# Compute and plot the theoretical PDF
y = np.exp(-x)
plt.plot(x, y, 'r', label='Theoretical PDF')
# Add labels, grid, and legend
plt.xlabel('x')
plt.ylabel('PDF')
plt.grid(True)
plt.legend()
# Display the plot
plt.show()
# Example usage
ete(1000) # Generate and plot the PDFs using 1000 random variables

References#
T. Schonhoff and A. Giordano, Detection and Estimation Theory and its Applications. Prentice Hall, 2006.