% demo of pdf of Gossett's small-sample z-statistic & the t-distribution.
% uses stats toolbox. Barnett 2/21/06

n = 4;                            % sample size
N = 100000;                       % number of such samples for histograms
m = 5; s = 3;                     % mu and sigma to sample from
yi = normrnd(m, s, n, N);         % data: rect array of samples from N(m, s^2)
dz = 0.1;                         % z-values bin width for plotting
z = -8:dz:8;                      % z ordinate list for plotting
ybar = mean(yi);                  % calc length-N row vector of ybars

% PDF on z-statistic with sigma known...
zst = (ybar - m) / (s/sqrt(n));  % calc length-N row vector of z-statistics
figure; hist(zst, z); hold on;
plot(z, N*dz*normpdf(z, 0, 1), '-m', 'linewidth',2); legend(sprintf('n=%d',n));
axis([-5 5 0 N*dz*0.45]); xlabel z; title 'hist of z-stat using true \sigma';

% PDF on z-statistic with sigma unknown...             ...note fatter tails!
S = sqrt(sum((yi - repmat(ybar, [n 1])).^2) / (n-1));
zstu = (ybar - m) ./ (S/sqrt(n));  % calc length-N row vector of z-statistics
figure; hist(zstu, z); hold on;
plot(z, N*dz*normpdf(z, 0, 1), '-m', 'linewidth',2); legend(sprintf('n=%d',n));
axis([-5 5 0 N*dz*0.45]); xlabel z; title 'hist of z-stat using sample S';

% now compare t-distn of n-1 degrees of freedom to sigma-unknown hist...
plot(z, N*dz*tpdf(z, n-1), '-g', 'linewidth',2);       % pretty good, eh!

% do a t-test
y = yi(:,1);                       % use the first set of data
[h, p, ci, stats] = ttest(y, m)    % use null H_0 : m=5 (rejects 5% of time)
figure; plot(y, 0, '.', 'markersize', 20); hold on; plot(ci', [.5 .5], '-');
axis([-2 10 0 1]); xlabel y; legend '95% conf interval on \mu (t-distn)';
plot([mean(y)-1.96*s/sqrt(n), mean(y)+1.96*s/sqrt(n)], [.4 .4], '-');
legend '95% conf interval on \mu (\sigma known)'
