I'm trying to estimate the sampling distribution of a variance from a single sample to do a certain statistical test.
To test the proof of concept, I take a normal distribution $N(200,5)$ with mean 200 and variance 25.
I then pick random samples of size sample_size from the normal distribution and calculate the variance of those samples to construct a sampling distribution of the variance.
My understanding is that the sampling distribution of the variance should follow a $\chi^2(\mathrm{sample~size} -1)$ distribution.
However, when I plot a PDF of the $\chi^2(\mathrm{sample~size} -1)$ distribution over my histogram of sample variances, the results do not agree.
The fit improves with increasing sample size but never truly "fits".
Where am I going wrong? Python MWE below.
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#Setup population with known variance to test
mu = 200
variance = 25
sigma = np.sqrt(variance)
N = 1000000
d = np.random.normal(mu, sigma, N)
n_samples = 100000
def sample_variance(sample_size):
#Take many random samples of sample_size from the distribution d above.
sample_vars = []
for i in range(n_samples):
sample = np.random.choice(d, sample_size)
sample_var = np.var(sample,ddof=1)
sample_vars.append(sample_var)
sample_vars = np.array(sample_vars)
std_mean = np.mean(sample_vars) #The mean of the sample variances correctly reproduces the population variance
std_std = np.std(sample_vars) #Calculated just for plotting purposes.
plt.hist((sample_vars), bins=30, density=True)
xs = np.linspace(std_mean-4*std_std,std_mean+4*std_std,200) #Just for x-scale plotting
#Plot chi^2 distribution with degrees of freedom = sample_size - 1
plt.plot(xs,stats.chi2.pdf(xs,df=sample_size-1))
plt.plot((std_mean,std_mean),(0,0.30),'--')
plt.xlim(0,50)
plt.xlabel('Variance of sample')
plt.ylabel('Density')
plt.show()
sample_variance(30)
sample_variance(7)
EDIT: For clarity, assume I have no knowledge of the population variance since this is what I'm trying to estimate after I get this test case working.