% Regression lineaire simple
% Examen 2002

% Entree des effectifs

clear
n=20;
% Entree des donnees

x=[30 54 29 51 36 41 40 23 49 30 32 22 22 32 44 34 32 20 46 45]';
y=[175 165 169 172 170 170 167 170 166 167 177 169 172 173 168 169 170 172 175 168]';
mx=mean(x);
vx=var(x,1);
my=mean(y);
vy=var(y,1);
mxy=mean(x.*y);
covxy=mxy-mx*my;
X=[ones(n,1) x];

% Estimation
%%%%%%%%%%%%%%%%%%
XX=X'*X;
m=inv(XX)*X'*y;
a=m(1);
b=m(2);
bb=covxy/vx;
aa=my-bb*mx;
ychap=X*m;
sigma2=((norm(y-ychap))^2)/(n-2);

% Intervalles de confiance
%%%%%%%%%%%%%%%%%%%%%%%%%%%
t=2.101;
Ia=[a-t*sqrt(sigma2)*sqrt(mean(x.*x)/(n*vx)) a+t*sqrt(sigma2)*sqrt(mean(x.*x)/(n*vx))];
Ib=[b-t*sqrt(sigma2)*sqrt(1/(n*vx)) b+t*sqrt(sigma2)*sqrt(1/(n*vx))];

% Significativite
%%%%%%%%%%%%%%%%%%%%%%%%%%%
T=(sqrt(n*vx))*b/sqrt(sigma2);

% Prediction
%%%%%%%%%%%%%%%%%%%%%%%%%%%
xs=65;ychaps=a+b*xs;
ps=(1+(((xs-mx)^2)/vx))/n;
Is=[ychaps-t*sqrt(sigma2)*sqrt(1+ps) ychaps+t*sqrt(sigma2)*sqrt(1+ps)];


