% Hypothesis Testing
% SySc 512, PSU, PD Roberts (May 2007)
% Required statistics package: stixbox 
%  <http://www.maths.lth.se/matstat/stixbox/>
%-------------------------------------------
% Data: body-weight to brain-weight ratio
x=[6000 1000 3290 4150 5290 1807 2239 1257 11500 7757 1285 1961];
% [ Mountain-beaver Cow Gray-wolf  Goat Guinea-pig Asian-elephant Donkey
%   Horse Potar-monkey Cat Giraffe]
figure(1)
plot(x, '*')
%-----------

% 1) Choose null hypothesis (H0): x_n = mu
mu = 1000;

% 2) Choose alternative hypothesis (H1): x_n > mu

% 3) Choose significance level: alpha
alpha=0.01;

% 4) Calculate statistic: Student's t-test:
n     = length(x);          % sample number
x_n = mean(x);              % sample mean
s_n = std(x);               % sample variance
t_n=(x_n-mu)/sqrt(s_n^2/n)  % Student's t-statistic

% 5) Get t-value from t-distribution:
v=n-1;                 % degrees of freedom
t = qt(1-alpha,v)      % t-value

% 6) Regect H0 if t_n > t:
if t_n > t
    1 % Reject H0
else
    0 % Not reject H0
end


figure(2)
range = 4;
value = -range:0.1:range;
cdf = 1 - pt(value, v);
plot(value, cdf)
hold on
plot( [t t], [0 1], '--g')
plot( [t_n t_n], [0 1], '-r')
hold off

% Report p-value
% p=1-pt(t_n,v)  
