%% central limit theorem test - sample variance

% choose one:
dist = 'norm'; p1 = 0; p2 = 1; rng = [0 5];
%%
SN = [2 5 10 100 1000]; % no of samples
MN = 10000; % no of means
H = 51; % hist bins
HX = linspace(rng(1),rng(2),H);
leg = cell(length(SN),1);
HNX = zeros(length(SN),H);
for i = 1:length(SN)
    N = SN(i);
    % sample
    if isempty(p2)
        X=random(dist,p1,N,MN);
    else
        X=random(dist,p1,p2,N,MN);
    end
    % sample 
    SX = mean(X);
    DX = var(X);
    % save hist
    HNX(i,:) = hist(DX,HX)/MN;
    leg{i} = sprintf('N=%d',N);
end
%% plot
figure('Name',dist); 
plot(repmat(HX,length(SN),1)', HNX'); legend(leg);
xlabel x; ylabel p(x);
