% regression demo, Barnett 3/3/06, requires stats toolbox

% create `fake' data, using noisy linear model...
n = 10;
x = rand(1,n);
ao = 1.5;  bo = 0.7; so = 0.3;
y = ao*x + bo + normrnd(0, so, size(x));
figure; plot(x, y, '+', 'markersize', 10); xlabel x; ylabel y; title 'data'

% sample correlation coefficient...
Rmat = corrcoef(x', y');
R = Rmat(1,2)               % extract offdiagonal element

% likelihood plot for linear model Y = aX + b
a = 0:0.1:3; b = -1:0.05:2;
[aa,bb] = meshgrid(a, b);
L = ones(size(aa));
s = so;                % assume noise sigma known
for i=1:n
  L = L .* exp(-(y(i) - aa*x(i) - bb).^2/(2*s^2)) / s;
end
figure; contourf(a, b, L); xlabel a; ylabel b; title 'L(a,b), noise \sigma known'

