import torch
import torch.distributions as dist
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
= dist.Bernoulli(probs=0.5)
X
# Sample N samples (X1, X2, ..., XN) from the Bernoulli distribution
# and store them in a tensor
= 500
N
# Num of samples from population
= 1000
K = X.sample((K, N))
samples
0, :] samples[
tensor([1., 1., 1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 0., 1., 1., 0., 0., 1.,
1., 1., 1., 0., 1., 1., 0., 0., 0., 0., 1., 1., 1., 1., 0., 0., 1., 1.,
1., 1., 0., 1., 0., 1., 0., 1., 1., 1., 0., 0., 0., 0., 1., 1., 1., 1.,
0., 0., 1., 1., 1., 1., 0., 0., 0., 1., 1., 1., 0., 0., 1., 0., 1., 1.,
0., 0., 1., 0., 0., 1., 0., 0., 1., 1., 0., 1., 0., 1., 1., 0., 1., 1.,
1., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 1., 0., 1., 0., 0., 0., 1.,
1., 0., 0., 1., 1., 0., 0., 1., 1., 0., 1., 1., 0., 1., 1., 0., 0., 1.,
1., 0., 0., 0., 0., 1., 1., 1., 0., 1., 1., 0., 0., 1., 0., 0., 1., 0.,
1., 0., 0., 1., 0., 1., 0., 0., 1., 1., 1., 0., 0., 1., 0., 0., 1., 1.,
0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 1., 1., 0., 0., 0., 1., 1.,
1., 1., 1., 1., 0., 0., 1., 0., 1., 1., 0., 0., 0., 0., 1., 1., 0., 0.,
1., 0., 0., 1., 1., 0., 0., 0., 1., 0., 1., 1., 1., 1., 1., 1., 0., 0.,
0., 0., 0., 0., 1., 0., 1., 1., 1., 1., 0., 1., 1., 0., 1., 1., 1., 0.,
0., 0., 0., 0., 1., 1., 1., 0., 0., 1., 0., 1., 1., 1., 1., 1., 1., 1.,
1., 0., 0., 0., 1., 1., 1., 0., 1., 0., 1., 0., 0., 1., 1., 1., 1., 0.,
1., 0., 0., 1., 0., 1., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 0., 0.,
0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 1., 0., 0., 0., 0., 0., 1.,
0., 1., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 1., 1., 0., 1.,
1., 0., 1., 1., 0., 1., 0., 0., 1., 0., 1., 1., 0., 1., 1., 0., 0., 0.,
1., 1., 0., 0., 1., 1., 1., 1., 0., 1., 1., 0., 1., 1., 0., 1., 0., 1.,
1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 0., 1., 1., 0., 1., 1., 0., 1.,
0., 0., 1., 1., 0., 0., 0., 1., 1., 0., 1., 1., 0., 0., 1., 0., 0., 1.,
1., 0., 1., 0., 0., 0., 0., 1., 1., 1., 1., 1., 0., 1., 0., 1., 1., 1.,
0., 1., 1., 1., 1., 0., 0., 1., 1., 1., 0., 0., 0., 1., 0., 0., 1., 1.,
1., 1., 0., 0., 1., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 1.,
1., 1., 1., 1., 0., 1., 0., 0., 1., 1., 1., 0., 1., 1., 0., 0., 0., 1.,
1., 0., 0., 1., 1., 0., 1., 0., 0., 0., 1., 1., 0., 1., 0., 0., 0., 0.,
0., 1., 0., 0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 1.])
0, :], dim=0)/torch.arange(1, N+1).float())
plt.plot(torch.cumsum(samples[0, 1) plt.ylim(
1, :], dim=0)/torch.arange(1, N+1).float())
plt.plot(torch.cumsum(samples[0, 1) plt.ylim(
# Plot K draws
= torch.cumsum(samples, dim=1) / torch.arange(1, N+1).float()
running_means for i in range(K):
=0.02, color='gray')
plt.plot(running_means[i], alpha
0, 1)
plt.ylim(0.5, color='red', linestyle='--', label=r'E[X]')
plt.axhline( plt.legend()
import torch
import matplotlib.pyplot as plt
import seaborn as sns
# Parameters
= 1000 # number of repetitions (sample paths)
K = 100 # samples per repetition
N = 0.5 # Bernoulli parameter
p
# Generate samples
= torch.distributions.Bernoulli(probs=p)
X = X.sample((K, N))
samples
# Running averages (LLN)
= torch.cumsum(samples, dim=1) / torch.arange(1, N + 1).float()
running_means
# Final sample means (for CLT)
= running_means[:, -1].numpy()
final_means
# --- Plotting ---
= plt.subplots(1, 2, figsize=(10, 6), gridspec_kw={'width_ratios': [3, 1]}, sharey=True)
fig, (ax_lln, ax_clt)
# LLN plot (running means)
for i in range(K):
='gray', alpha=0.02)
ax_lln.plot(running_means[i], color
='red', linestyle='--', label=r'$\mathbb{E}[X]$')
ax_lln.axhline(p, color"n")
ax_lln.set_xlabel(r"$\bar{X}_n$")
ax_lln.set_ylabel("LLN: Running Averages")
ax_lln.set_title(0, 1)
ax_lln.set_ylim(
ax_lln.legend()
# CLT plot (rotated histogram + KDE)
#sns.histplot(final_means, bins=30, stat='density', orientation='horizontal',
# ax=ax_clt, color='skyblue', edgecolor='white', alpha=0.6)
=ax_clt, color='black', linewidth=2, vertical=True, bw_adjust=4)
sns.kdeplot(final_means, ax
"Density")
ax_clt.set_xlabel("") # shared y-axis
ax_clt.set_ylabel("CLT: Distribution of Sample Means")
ax_clt.set_title(0, 1)
ax_clt.set_ylim(False)
ax_clt.grid(
plt.tight_layout()
/var/folders/1x/wmgn24mn1bbd2vgbqlk98tbc0000gn/T/ipykernel_28285/2045725416.py:37: UserWarning:
The `vertical` parameter is deprecated; assigning data to `y`.
This will become an error in seaborn v0.14.0; please update your code.
sns.kdeplot(final_means, ax=ax_clt, color='black', linewidth=2, vertical=True)