tl;dr; why is does my monte carlo simulations differ so much with the confidence interval?
So I was checking the confidence interval for the sample standard deviation for a normal distribution: https://en.wikipedia.org/wiki/Standard_deviation#Confidence_interval_of_a_sampled_standard_deviation
Now I created a plot using Matlab where the confidence interval is displayed as function of the sample size. At first I thought I implemented it correctly.
Then, I also added some Monte Carlo simulations and plotted them. However, that kind of makes we wonder if my confidence interval is correctly implemented.
What goes wrong here?
The figure;
My Matlab code;
clear all;
close all;
clc;
% Number of samples
N = 2:50;
% Mean and standard deviation
mu = 0;
sigma = 0.2;
% Confidence level
alpha = 0.975;
% Generate confidence interval
for i = 1:length(N)
% Degrees of freedom
k = N(i) - 1;
% Calculate confidence interval
lower(i) = k*sigma^2 / chi2inv(1 - alpha,k);
upper(i) = k*sigma^2 / chi2inv( alpha,k);
end
% Create figure
figure;
hold all;
% Plot confidence interval (standard deviation, not variance)
plot(N,sqrt(lower));
plot(N,sqrt(upper));
% Plot standard deviation
plot([N(1),N(end)],[sigma,sigma]);
% Monte carlo simulation
NN = [];
XX = [];
% For each sample size
for i = 1:length(N)
% Do 500 simulations
for j = 1:500
NN = [NN N(i)];
XX = [XX std(normrnd(mu,sigma,1,N(i)))];
end
end
scatter(NN,XX,15,'filled');
grid on; box on;
xlabel('Sample size N');
ylabel('Standard deviation [-]');
legend({'Upper confidence bound','Lower confidence bound','Standard deviation','Monte carlo simulation'});
ylim([0 0.6]);
