% eda05_01
% Illustrate the application of Bayes theorem to least squares

% set up vectors m1 and m2
L=40;
Dm = 1.0;
m1 = Dm*[0:L-1]';
m2 = Dm*[0:L-1]';

% make normal distribution pp(m)
m1bar=10;
m2bar=20;
Cpm=[[6,0]',[0,10]'];
CpmI=inv(Cpm);
norm=1/(2*pi*sqrt(det(Cpm)));
Ppm=zeros(L,L);
for i = [1:L]
for j = [1:L]
   dm = [m1(i)-m1bar, m2(j)-m2bar]';
   Pp(i,j)=norm*exp( -0.5 * dm' * CpmI * dm );
end
end

% search grid for maximum 
[maxtemp, k] = max(Pp);
[pmax, j] = max(maxtemp);
i=k(j);
m1bar = m1(i);
m2bar = m2(j);
m1bar
m2bar

% make normal distribution pdgm(m)
% with a one-row data kernel that the
% difference of two model parameters is d1
G = [1, -1];
d1 = 0;
Cd=3;
CdI=1/Cd;
norm=1/sqrt(2*pi*Cd);
Pdgm=zeros(L,L);
for i = [1:L]
for j = [1:L]
   m = [m1(i), m2(j)]';
   dd = d1-G*m;
   Pdgm(i,j)=norm*exp( -0.5 * dd' * CdI * dd );
end
end

% multiply the two to get Pmgd
Pmgd = Pp .* Pdgm;

% search grid for maximum 
[maxtemp, k] = max(Pmgd);
[pmax, j] = max(maxtemp);
i=k(j);
m1est = m1(i);
m2est = m2(j);
m1est
m2est

% use simple drawing function that encapsulates all the graphics
eda_draw(' ',Pp,'caption pp(m)',Pdgm,'caption p(d|m)',Pmgd,'caption p(m|d)');

