format short
format compact
clc
echo on
% Artificial data for two groups 
FF = zeros(1000,1);
NN = [10 15]; % number per group
NCases = sum(NN); NTreatments = 2;
MM = [5 5]; % means
SD = [1 1]; % SDs
% Now do 20 runs:
echo off
for(run=1:1000)
Sample = [MM(1)+SD(1)*randn(NN(1),1); MM(2)+SD(2)*randn(NN(2),1)];
%
% Now calculate the various sums of squares:
%
% Individual -- 
II = Sample'*Sample;
%
% Treatment --
T1 = Sample(1:NN(1)); T2 = Sample((NN(1)+1):NCases);
TT = (sum(T1)^2)/NN(1) + (sum(T2)^2)/NN(2);
%
% Combination --
CC = (sum(Sample)^2)/length(Sample);
%
% Now calculate the partition of variance...
% Total square deviations: II - CC with NCases-1 degrees of freedom
Total = II - CC;   % df = 34
%
% Treatment: TT - CC with NTreatments-1 degrees of freedom
Treatment = TT - CC;   % df = 1
%
% Residual: II - TT with  NCases - NTreatments degrees of freedom
Residual = II - TT ;  % df = NCases - NTreatments = 33
% Do they add up?
Treatment + Residual;
% Now calculate the F ratio:
MSTR = Treatment/(NTreatments-1);  % df =1
MSE = Residual/(NCases-NTreatments); % df = 34
FF(run) = MSTR/MSE;  % Should be small, since there is really no difference
end
echo on
% Now compare against expectations for F(1,34)
mean(FF)
finv(1-[.05 .01 .005 .001],1,34) % significance thresholds for .05, .01, etc.
sum(FF>finv(.95,1,23))


