% Let's take a closer look at the prior and posterior distributions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load c:\klaus\AAEC6564\mlab\worksp\mod2s1a betamat sig2mat;

rand('state',37); % set arbitrary seed for uniform draws
randn('state',37); % set arbitrary seed for normal draws

R=10000; %number of retained draws from S5_main

%repeat prior settings
mu0=zeros(k,1); %diffuse prior for mean of betas
V0=eye(k)*100; % diffuse prior for varcov of beta
% elements for sig2
v0=1/2;
tau0=1/2; % diffuse prior shape and scale

%draw from priors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sig2prior=1./gamrnd(v0,1/tau0,R,1);
%focus on last beta
b3prior=mu0(end)+sqrt(V0(end,end)).*randn(R,1);

%posteriors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
b3post=betamat(end,:)';
sig2post=sig2mat;

[f1,x1]=ksdensity(sig2prior,'kernel','epanechnikov','npoints',200);
[f2,x2]=ksdensity(b3prior,'kernel','epanechnikov','npoints',200);

[f3,x3]=ksdensity(sig2post,'kernel','epanechnikov','npoints',200);
[f4,x4]=ksdensity(b3post,'kernel','epanechnikov','npoints',200);


figure(1);

% compare prior and posterior for sig2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(2,1,1);
plot(x1,f1,':k',x3,f3,'-k');
title('prior and posterior for $\sigma^2$','interpreter','latex','fontsize',14');
set(gca,'Xlim',[1 2]);
set(gca,'Ylim',[0 20]);
legend('prior','posterior');

% compare prior and posterior for beta3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(2,1,2);
plot(x2,f2,':k',x4,f4,'-k');
title('prior and posterior for $\beta_3$','interpreter','latex','fontsize',14');
set(gca,'Xlim',[0.5 1]);
set(gca,'Ylim',[0 70]);
legend('prior','posterior');

