clear;
addpath('functions');
T=500;
B1=0.2;
B2=0.9;
C1=1;
C2=-1;
S1=3;
S2=1;
P=[0.9 0.1;0.1 0.9];
strue=zeros(T,2);
strue(1,1)=1;
strue=simS(strue,P);
e=randn(T,1);
Y=zeros(T,1);
X=zeros(T,1);
for i=2:T;
X(i,:)=Y(i-1,:);
if strue(i,1)==1
Y(i)=[X(i,:) 1]*[B1 C1]'+e(i)*sqrt(S1);
else
Y(i)=[X(i,:) 1]*[B2 C2]'+e(i)*sqrt(S2);
end
end
y=Y;
x=[X ones(T,1)];
phi1=[0.5;1];
phi2=[0.8;-1];
sig1=3;
sig2=1;
p=0.95;
q=0.95;
pmat=[p 1-q;1-p q];
ncrit=10;
B0=zeros(2,1);
Sigma0=eye(2);
d0=0.1;
v0=1;
u00=25;
u01=5;
u11=25;
u10=5;
out1=[];
out2=[];
out3=[];
out4=[];
REPS=10000;
BURN=5000;
igibbs=1;
count=1;
while count<REPS-BURN
A = [(eye(2)-pmat);ones(1,2)];
EN=[0;0;1];
ett11= pinv(A'*A)*A'*EN;
iS1=1/sig1;
iS2=1/sig2;
lik=0;
filter=zeros(T,2);
for j=1:T
em1=Y(j)-[X(j,:) 1]*phi1;
em2=Y(j)-[X(j,:) 1]*phi2;
neta1=(1/sqrt(sig1))*exp(-0.5*(em1*iS1*em1'));
neta2=(1/sqrt(sig2))*exp(-0.5*(em2*iS2*em2'));
ett10=pmat*ett11;
ett11=ett10.*[neta1;neta2];
fit=sum(ett11);
ett11=(ett11)/fit;
filter(j,1:2)=ett11';
lik=lik+log(fit);
end
check=-1;
while check<0
S=zeros(T,1);
p1=filter(T,1);
p2=filter(T,2);
p=p1/(p1+p2);
u=rand(1,1);
S(T,1)=(u>=p);
for t=T-1:-1:1
if S(t+1)==0
p00=pmat(1,1)*filter(t,1);
p01=pmat(1,2)*filter(t,2);
elseif S(t+1)==1
p00=pmat(2,1)*filter(t,1);
p01=pmat(2,2)*filter(t,2);
end
u=rand(1,1);
p=p00/(p00+p01);
if u<p
S(t)=0;
else
S(t)=1;
end
end
if sum(S==0)>=ncrit
check=1;
end
end
tranmat=switchg(S+1,[1;2]);
N00=tranmat(1,1);
N01=tranmat(1,2);
N10=tranmat(2,1);
N11=tranmat(2,2);
p0=drchrnd([N00+u00;N01+u01]);
p=p0(1,1);
p0=drchrnd([N10+u10;N11+u11]);
q=p0(2,1);
pmat=[p 1-q;1-p q];
id=find(S==0);
y1=y(id);
x1=x(id,:);
M=inv(inv(Sigma0)+(1/sig1)*(x1'*x1))*(inv(Sigma0)*B0+(1/sig1)*x1'*y1);
V=inv(inv(Sigma0)+(1/sig1)*(x1'*x1));
phi1=M+(randn(1,2)*chol(V))';
id=find(S==1);
y2=y(id);
x2=x(id,:);
M=inv(inv(Sigma0)+(1/sig2)*(x2'*x2))*(inv(Sigma0)*B0+(1/sig2)*x2'*y2);
V=inv(inv(Sigma0)+(1/sig2)*(x2'*x2));
phi2=M+(randn(1,2)*chol(V))';
e1=y1-x1*phi1;
T1=v0+rows(e1);
D1=d0+e1'*e1;
z0=randn(T1,1);
z0z0=z0'*z0;
sig1=D1/z0z0;
e2=y2-x2*phi2;
T2=v0+rows(e2);
D2=d0+e2'*e2;
z0=randn(T2,1);
z0z0=z0'*z0;
sig2=D2/z0z0;
if igibbs>BURN
chck=phi1(2,1)>phi2(2,1);
if chck
out1=[out1;([phi1' phi2'])];
out2=[out2;([sig1 sig2 ])];
out3=[out3;S'];
out4=[out4;[p q]];
count=count+1;
end
end
igibbs=igibbs+1;
disp(sprintf(' Replication %s , %s Saved Draws %s. ', ...
num2str(igibbs), num2str(count) ));
end
figure(1)
subplot(5,2,1);
hist(out1(:,1),50);
vline(B1)
title('Coefficient regime 1');
axis tight
subplot(5,2,2);
hist(out1(:,3),50);
title('Coefficient regime 2');
vline(B2)
axis tight
subplot(5,2,3);
hist(out1(:,2),50);
vline(C1)
title('Intercept regime 1');
axis tight
subplot(5,2,4);
hist(out1(:,4),50);
title('Intercept regime 2');
vline(C2)
axis tight
subplot(5,2,5);
hist(out2(:,1),50);
vline(S1)
title('\sigma_{1}');
axis tight
subplot(5,2,6);
hist(out2(:,2),50);
vline(S2)
title('\sigma_{2}');
axis tight
subplot(5,2,7);
hist(out4(:,1),50);
title('P_{00}');
vline(P(1,1))
axis tight
subplot(5,2,8);
hist(out4(:,2),50);
title('p_{11}');
vline(P(2,2))
axis tight
subplot(5,2,[9 10])
temp=mean(out3,1);
plot(temp,'c','LineWidth',2);
hold on
plot(strue(:,2),'k','LineWidth',2)
title('Probability of Regime 2');
legend('Estimate','True')
axis tight