English: Distributions of sample means under the null (θ=0) and alternative hypotheses are shown. The shaded red area represents Type I error (α), held constant at 0.05, while the shaded green area represents statistical power (1-β). As the sample size increases, the distributions narrow, leading to clearer separation between the hypotheses and higher power. Similarly, a larger effect size increases the distance between the distributions, resulting in greater power.
```python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
- Parameters
alpha = 0.05 # Significance level
- Create subplots
xmin, xmax = -0.4, 0.6
ns = [10, 30, 100]
thetas = [0.1, 0.3, 0.5]
fig, axes = plt.subplots(len(ns), len(thetas), figsize=(12, 10))
- Loop through different scenarios
for i, n in enumerate(ns): # Sample sizes
for j, theta in enumerate(thetas): # Effect sizes
# Calculate parameters
sigma = 1 / np.sqrt(n)
z_alpha = norm.ppf(1 - alpha)
threshold = z_alpha * sigma
power = 1 - norm.cdf(z_alpha - theta / sigma)
# Generate distributions
x = np.linspace(xmin, xmax, 1000)
null_dist = norm.pdf(x, 0, sigma)
alt_dist = norm.pdf(x, theta, sigma)
# Plot on the corresponding subplot
ax = axes[i, j]
ax.plot(x, null_dist, label=rf'$H_0\; (\theta = 0)
)
ax.plot(x, alt_dist, label=rf'$H_1\; (\theta = {theta})