import torch
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
Uniform Distribution Transformation
- \(X \sim \text{Uniform}(a, b)\)
- Transformation: \(Y = X^2\)
- Goal: Find the PDF of \(Y\)
= 1, 3
a, b = 100000
num_samples
= torch.distributions.Uniform(a, b)
uniform_dist = uniform_dist.sample((num_samples,))
X_samples
= X_samples ** 2
Y_samples
= torch.linspace(a**2, b**2, 500)
y_values = 1 / (2 * torch.sqrt(y_values) * (b - a))
f_Y
= plt.subplots(1, 2, figsize=(12, 6), sharey=True)
fig, axes
0].hist(X_samples.numpy(), bins=50, density=True, alpha=0.6, label="Empirical PDF")
axes[0].axhline(1 / (b - a), color='r', linestyle='--', label="Theoretical PDF")
axes[0].set_xlabel("X ~ Uniform(a, b)")
axes[0].set_ylabel("Density")
axes[0].set_title("PDF of Uniform(a, b)")
axes[0].legend()
axes[0].grid()
axes[
1].hist(Y_samples.numpy(), bins=50, density=True, alpha=0.6, label="Empirical PDF")
axes[1].plot(y_values.numpy(), f_Y.numpy(), 'r-', label="Theoretical PDF")
axes[1].set_xlabel("Y = X^2")
axes[1].set_ylabel("Density")
axes[1].set_title("Transformation of Uniform(a, b) via Y = X^2")
axes[1].legend()
axes[1].grid()
axes[
plt.tight_layout() plt.show()
\(Y = X^2 \Rightarrow X = \sqrt{Y}\) or \(X = -\sqrt{Y}\)
Since \(X \in [a, b]\) and \(a > 0\), only the positive root is valid.
\[ f_Y(y) = f_X(x(y)) \cdot \left|\frac{dx}{dy}\right| = f_X(\sqrt{y}) \cdot \frac{1}{2\sqrt{y}} \] And since \(f_X(x) = \frac{1}{b - a}\) (for uniform),
\[ f_Y(y) = \frac{1}{b - a} \cdot \frac{1}{2\sqrt{y}}, \quad \text{for } y \in [a^2, b^2] \]
\[ f_Y(y) = \frac{1}{2(b - a)\sqrt{y}}, \quad y \in [a^2, b^2] \]
Gaussian Transformation
- Sample from a standard Gaussian \(X \sim \mathcal{N}(0, 1)\)
- Transform it via \(Y = \mu + \sigma X \Rightarrow Y \sim \mathcal{N}(\mu, \sigma^2)\)
- Plot histogram of \(Y\)
= 2.0
mu = 1.5
sigma = 100_000
num_samples
= torch.distributions.Normal(0.0, 1.0)
standard_normal = standard_normal.sample((num_samples,))
X
= mu + sigma * X
Y
= torch.linspace(mu - 4*sigma, mu + 4*sigma, 500)
y_vals = (1 / (sigma * torch.sqrt(torch.tensor(2 * torch.pi)))) * \
pdf_y -((y_vals - mu)**2) / (2 * sigma**2))
torch.exp(
= plt.subplots(2, 1, figsize=(6, 8), sharex=True)
fig, axes
0].hist(X.numpy(), bins=100, density=True, alpha=0.6, label="Empirical PDF")
axes[= torch.linspace(-4, 4, 500)
x_vals = (1 / torch.sqrt(torch.tensor(2 * torch.pi))) * torch.exp(-0.5 * x_vals**2)
pdf_x 0].plot(x_vals.numpy(), pdf_x.numpy(), 'r-', linewidth=2, label="Theoretical PDF")
axes[0].set_xlabel("X ~ N(0,1)")
axes[0].set_ylabel("Density")
axes[0].set_title("PDF of Standard Normal Distribution")
axes[0].legend()
axes[0].grid()
axes[
1].hist(Y.numpy(), bins=100, density=True, alpha=0.6, label="Empirical PDF")
axes[1].plot(y_vals.numpy(), pdf_y.numpy(), 'r-', linewidth=2, label="Theoretical PDF")
axes[1].set_xlabel("Y = μ + σX")
axes[1].set_ylabel("Density")
axes[1].set_title(f"PDF of Y = {mu} + {sigma}X where X ~ N(0,1)")
axes[1].legend()
axes[1].grid()
axes[
plt.tight_layout() plt.show()
Exponentional RV
\[ f_X(x) = \lambda e^{-\lambda x}, \quad x \ge 0 \]
\[ F_X(x) = 1 - e^{-\lambda x} \]
\[ F_X^{-1}(u) = -\frac{\ln(1 - u)}{\lambda} \]
= 1.0
lambda_ = 10000
n_samples
# Sample from Uniform(0,1)
= torch.rand(n_samples)
u
# Apply inverse CDF of exponential
= -torch.log(1 - u) / lambda_
samples
# True distribution using torch.distributions
= torch.distributions.Exponential(rate=lambda_)
exp_dist
= torch.linspace(0, 6, 100)
x_vals = exp_dist.log_prob(x_vals).exp() # f_X(x) = λ e^(-λx)
pdf_vals
=(8, 5))
plt.figure(figsize=50, density=True, alpha=0.6, label="Inverse CDF Samples")
plt.hist(samples.numpy(), bins'r-', label="True Exponential PDF")
plt.plot(x_vals.numpy(), pdf_vals.numpy(), "Inverse Transform Sampling using PyTorch")
plt.title("x")
plt.xlabel("Density")
plt.ylabel(
plt.legend()True)
plt.grid( plt.show()