%% central limit theorem test - sample means

% choose one:
%dist = 'unif'; p1 = 0; p2 = 1; rng = [0 1];
dist = 'norm'; p1 = 0; p2 = 1; rng = [-1 1];
%dist = 't'; p1 = 1; p2 = []; rng = [-10 10]; % ==> Cauchy, no mean
%dist = 'unid'; p1 = 10; p2 = []; rng = [0 10];
%dist = 'poiss'; p1 = 10; p2 = []; rng = [0 20];
%%
SN = [10 100 1000 10000]; % no of samples
MN = 1000; % no of means
H = 301; % 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 mean
    SX = sum(X)/N;
    % save hist
    HNX(i,:) = hist(SX,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);

%% normal
% N = 10;
% A=random('norm',0,1,N,N);
% plot(sort(sum(A))/size(A,1));
% 
% %% cauchy
% N = 100;
% A=random('t',1,N,N);
% plot(sort(sum(A))/size(A,1)');