阅读权限 255 威望 0 级论坛币 386043 个 通用积分 526.9898 学术水平 127 点 热心指数 140 点 信用等级 103 点 经验 46986 点 帖子 1773 精华 0 在线时间 2509 小时 注册时间 2007-11-5 最后登录 2024-8-16
[code]%紧接第一部分代码
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Now start Gibbs loop%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:ntotal
completion_process=i/ntotal
%-------------------------------------------------------
% caculate draw probability
%---------------S1---------------------------------------
%log likelihood for the regression model with ARCH errors -- old draw
y_1 = y(S==1); x_1 = x(S==1);
err_1 = y_1 - x_1*beta(1);
n1 = length(err_1);
h1 =a_1(1)*ones(n1-np,1); % vols: 524*1
for j=1:np
h1 = h1 + a_1(j+1,1)*err_1(np+1-j:n1-j,1).^2; % conditional variance h
end
% alternative: vols = vols + adraw(2,1)*errs(p:t-1,1) + adraw(3,1)*errs(p-1:t-2) + ...
% adraw(4,1)*errs(p-2:t-3,1) + adraw(5,1)*errs(p-3:t-4,1)
lterm=(err_1(np+1:n1,1).^2)./h1;
llike1 = -.5*sum(log(h1)) -.5*sum(lterm);
%---------------S2---------------------------------------
y_2 = y(S==2); x_2 = x(S==2);
err_2 = y_2 - x_2*beta(2);
n2 = length(err_2);
h2 =a_2(1)*ones(n2-np,1);% vols: 524*1
for j=1:np
h2 = h2 + a_2(j+1,1)*err_2(np+1-j:n2-j,1).^2; % conditional variance h
end
lterm=(err_2(np+1:n2,1).^2)./h2;
llike2 = -.5*sum(log(h2)) -.5*sum(lterm);
%---------------S3---------------------------------------
y_3 = y(S==3); x_3 = x(S==3);
err_3 = y_3 - x_3*beta(3);
n3 = length(err_3);
h3 =a_3(1)*ones(n3-np,1);% vols: 524*1
for j=1:np
h3 = h3 + a_3(j+1,1)*err_3(np+1-j:n3-j,1).^2; % conditional variance h
end
lterm=(err_3(np+1:n3,1).^2)./h3;
llike3 = -.5*sum(log(h3)) -.5*sum(lterm);
%---------------------------------------------------------
% take candidate draw, caculate candidate probability
%---------------------------------------------------------
%take candidate draw
beta_sig = [(0.003/4)^2 0 0; 0 (0.013/4)^2 0; 0 0 (0.018/4)^2 ];
beta_can = beta + norm_rnd(beta_sig);
% a_1_sig = [(0.00074/6)^2 0 0 0;0 (0.1/6)^2 0 0; 0 0 (0.1/6)^2 0; 0 0 0 (0.2/6)^2];
a_1_sig = [(0.0001/4)^2 0 0 0;0 (0.075/4)^2 0 0; 0 0 (0.07/4)^2 0; 0 0 0 (0.647/4)^2];
a_1_can = a_1 + norm_rnd(a_1_sig);
% a_2_sig = [(0.00074/4)^2 0 0 0;0 (0.1/4)^2 0 0; 0 0 (0.1/4)^2 0; 0 0 0 (0.2/4)^2];
a_2_sig = [(0.0007/4)^2 0 0 0;0 (0.1406/4)^2 0 0; 0 0 (0.13/4)^2 0; 0 0 0 (0.12/4)^2];
a_2_can = a_2 + norm_rnd(a_2_sig);
% a_3_sig = [(0.00074/4)^2 0 0 0;0 (0.1/4)^2 0 0; 0 0 (0.1/4)^2 0; 0 0 0 (0.2/4)^2];
a_3_sig = [(0.0011/4)^2 0 0 0;0 (0.1473/4)^2 0 0; 0 0 (0.0855/4)^2 0; 0 0 0 (0.1824/4)^2];
a_3_can = a_3 + norm_rnd(a_3_sig);
%if this draw violates positivity or stationarity restrictions do not accept it
violat_1 =0; violat_2 =0; violat_3 =0;
%first check positivity of arch coefficients
for j=1:np+1
if a_1_can(j,1)<=0
violat_1=1;
end
if a_2_can(j,1)<=0
violat_2=1;
end
if a_3_can(j,1)<=0
violat_3=1;
end
end
if a_1_can(2) + a_1_can(3) +a_1_can(4) >1; violat_1=1; end
if a_2_can(2) + a_2_can(3) +a_2_can(4) >1; violat_2=1; end
if a_3_can(2) + a_3_can(3) +a_3_can(4) >1; violat_3=1; end
%-------------------------------------------------------
% Accept the candidate with probability
%-------------------------------------------------------
if violat_1 ==0
%log likelihood for the regression model with ARCH errors -- candidate draw
err_1_can = y_1 - x_1*beta_can(1);
h1_can=a_1_can(1,1)*ones(n1-np,1);
for j=1:np
h1_can = h1_can + a_1_can(j+1,1)*err_1_can(np+1-j:n1-j,1).^2;
end
lterm=(err_1_can(np+1:n1,1).^2)./h1_can;
llike1_p = -.5*sum(log(h1_can)) -.5*sum(lterm);
%log of acceptance probability
laccprob_1 = llike1_p - llike1;
%now accept candidate with appropriate probability
if laccprob_1 > log(rand)
beta(1)=beta_can(1);
a_1 = a_1_can;
switch_1 = switch_1+1;
end
end
%-------------------------- S2--------------------------------------------
if violat_2 ==0
%log likelihood for the regression model with ARCH errors -- candidate draw
err_2 = y_2 - x_2*beta_can(2);
h2=a_2_can(1,1)*ones(n2-np,1);
for j=1:np
h2 = h2 + a_2_can(j+1,1)*err_2(np+1-j:n2-j,1).^2;
end
lterm=(err_2(np+1:n2,1).^2)./h2;
llike2_p = -.5*sum(log(h2)) -.5*sum(lterm);
%log of acceptance probability
laccprob_2 = llike2_p - llike2;
%now accept candidate with appropriate probability
if laccprob_2 > log(rand)
beta(2)=beta_can(2);
a_2 = a_2_can;
switch_2 = switch_2+1;
end
end
%-------------------------- S2--------------------------------------------
if violat_3 ==0
%log likelihood for the regression model with ARCH errors -- candidate draw
err_3 = y_3 - x_3*beta_can(3);
h3=a_3_can(1,1)*ones(n3-np,1);
for j=1:np
h3 = h3 + a_3_can(j+1,1)*err_3(np+1-j:n3-j,1).^2;
end
lterm=(err_3(np+1:n3,1).^2)./h3;
llike3_p = -.5*sum(log(h3)) -.5*sum(lterm);
%log of acceptance probability
laccprob_3 = llike3_p - llike3;
%now accept candidate with appropriate probability
if laccprob_3 > log(rand)
beta(3)=beta_can(3);
a_3 = a_3_can;
switch_3 = switch_3+1;
end
end
%--------------------------------------------------------------------
%draw S conditional on [lnl,filprob,simstate] = MSLinRegDrawState(T,k1,k2,nm,indSigma,p,P,yv,X1,X2,Beta,hv);
%--------------------------------------------------------------------
err_S = zeros(n,sn);
h_S= zeros(n,sn);
for j=1:sn
for i = 1:n
err_S(i,j) = y(i) - x(i)*beta(j);
end
end
h_S(1,1) = a_1(1)/(1-a_1(2)-a_1(3)-a_1(4));
h_S(2,1) = a_1(1) + a_1(2)*err_S(1,1)^2;
h_S(3,1) = a_1(1) + a_1(2)*err_S(2,1)^2+a_1(3)*err_S(1,1)^2;
h_S(1,2) = a_2(1)/(1-a_2(2)-a_2(3)-a_2(4));
h_S(2,2) = a_2(1) + a_2(2)*err_S(1,2)^2;
h_S(3,2) = a_2(1) + a_2(2)*err_S(2,2)^2+a_2(3)*err_S(1,2)^2;
h_S(1,3) = a_3(1)/(1-a_3(2)-a_3(3)-a_3(4));
h_S(2,3) = a_3(1) + a_3(2)*err_S(1,3)^2;
h_S(3,3) = a_3(1) + a_3(2)*err_S(2,3)^2+a_3(3)*err_S(1,3)^2;
for j = 4:n
h_S(j,1) = a_1(1) + a_1(2)*err_S(j-1,1)^2 + a_1(3)*err_S(j-2,1)^2 + a_1(4)*err_S(j-3,1)^2;
h_S(j,2) = a_2(1) + a_2(2)*err_S(j-1,2)^2 + a_2(3)*err_S(j-2,2)^2 + a_2(4)*err_S(j-3,2)^2;
h_S(j,3) = a_3(1) + a_3(2)*err_S(j-1,3)^2 + a_3(3)*err_S(j-2,3)^2 + a_3(4)*err_S(j-3,3)^2;
end
% (normpdf) lnlike = exp(-0.5 * ((x - mu)./sigma).^2) ./ (sqrt(2*pi) .* sigma);
lnpdat(:,1) = -0.5*log(2*pi*h_S(:,1))-0.5*(err_S(:,1).^2)./h_S(:,1);
lnpdat(:,2) = -0.5*log(2*pi*h_S(:,2))-0.5*(err_S(:,2).^2)./h_S(:,2);
lnpdat(:,3) = -0.5*log(2*pi*h_S(:,3))-0.5*(err_S(:,3).^2)./h_S(:,3);
lnpdat(:,1) = exp(lnpdat(:,1));
lnpdat(:,2) = exp(lnpdat(:,2));
lnpdat(:,3) = exp(lnpdat(:,3));
filprob=zeros(n,3);
k=S(2);
piSti1(1) = P(1,k)*lnpdat(1,1)/(P(1,k)*lnpdat(1,1)+P(2,k)*lnpdat(1,2)+P(3,k)*lnpdat(1,3));
piSti2(1) = P(2,k)*lnpdat(1,2)/(P(1,k)*lnpdat(1,1)+P(2,k)*lnpdat(1,2)+P(3,k)*lnpdat(1,3));
piSti3(1) = P(3,k)*lnpdat(1,3)/(P(1,k)*lnpdat(1,1)+P(2,k)*lnpdat(1,2)+P(3,k)*lnpdat(1,3));
random_S = rand;
if random_S<=piSti1(1)
S(1) = 1;
else
if random_S <=piSti1(1)+piSti2(1)
S(1)=2;
else
S(1) = 3;
end
end
filprob(1,1)=piSti1(1);
filprob(1,2)=piSti2(1);
filprob(1,3)=piSti3(1);
for t =2:n-1
l=S(t-1);
k=S(t+1);
piSti1(t) = P(l,1)*P(1,k)*lnpdat(t,1)/(P(l,1)*P(1,k)*lnpdat(t,1)+P(l,2)*P(2,k)*lnpdat(t,2)+P(l,3)*P(3,k)*lnpdat(t,3));
piSti2(t) = P(l,2)*P(2,k)*lnpdat(t,2)/(P(l,1)*P(1,k)*lnpdat(t,1)+P(l,2)*P(2,k)*lnpdat(t,2)+P(l,3)*P(3,k)*lnpdat(t,3));
piSti3(t) = P(l,3)*P(3,k)*lnpdat(t,3)/(P(l,1)*P(1,k)*lnpdat(t,1)+P(l,2)*P(2,k)*lnpdat(t,2)+P(l,3)*P(3,k)*lnpdat(t,3));
random_St = rand;
if random_St<=piSti1(t)
S(t) = 1;
else
if random_St<=piSti1(t)+piSti2(t)
S(t)=2;
else
S(t)=3;
end
end
filprob(t,1)=piSti1(t);
filprob(t,2)=piSti2(t);
filprob(t,3)=piSti3(t);
end
l=S(n-1);
piSti1(n) = P(l,1)*lnpdat(n,1)/(P(l,1)*lnpdat(n,1)+P(l,2)*lnpdat(n,2)+P(l,3)*lnpdat(n,3));
piSti2(n) = P(l,2)*lnpdat(n,2)/(P(l,1)*lnpdat(n,1)+P(l,2)*lnpdat(n,2)+P(l,3)*lnpdat(n,3));
piSti3(n) = P(l,3)*lnpdat(n,3)/(P(l,1)*lnpdat(n,1)+P(l,2)*lnpdat(n,2)+P(l,3)*lnpdat(n,3));
random_Sn = rand;
if random_Sn<=piSti1(n)
S(n) = 1;
else
if random_Sn<=piSti1(n)+piSti2(n)
S(n)=2;
else
S(n) =3;
end
end
filprob(n,1)=piSti1(n);
filprob(n,2)=piSti2(n);
filprob(n,3)=piSti3(n);
%--------------------------------------------------------------------
%draw P conditional on S
%--------------------------------------------------------------------
sm=[S(1:n-1) S(2:n)]; % switching number, sm: (T-1)*2
tm=zeros(sn,sn); % total number
for im=1:sn
for jm=1:sn
tm(im,jm)=size(sm(sm(:,1)==im & sm(:,2)==jm,:),1); % total number of each transition from i state to j state
end
rr = pi_pri(im,:)+tm(im,:);
P_draw = chi2rnd(2*rr);
P(im,:)=P_draw./repmat(sum(P_draw,2),1,sn);
% P(im,:)=draw_dirichlet(1,sn,pi_pri(im,:)+tm(im,:),0); % rm: prior for number of state transition (ij)
end
%-------------------------------------------------------
%identification constraint
%-------------------------------------------------------
indexconstraint1=a_1(1)>a_2(1);
%if indexconstraint1 & abs(beta(1)-beta(2))<0.002
if a_1(1)-a_2(1)>0.0003 && beta(1)-beta(2)>0.005
indexSwitch=indexSwitch+1;
ndxv = [2 1 3];
ABSETZ = a_2;
a_2 = a_1;
a_1 = ABSETZ;
beta=beta(ndxv);
P=P(:,ndxv);
P=P(ndxv,:);
filprob=filprob(:,ndxv);
end
% indexconstraint2=beta(1)>beta(3)>beta(2);
indexconstraint2=a_1(1)>a_3(1);
%if indexconstraint2 & abs(beta(1)-beta(3))<0.002
if a_1(1)-a_3(1)>0.0003 && abs(beta(1)-beta(3))>0.002
indexSwitch=indexSwitch+1;
ndxv = [3 2 1];
ABSETZ = a_3;
a_3 = a_1;
a_1 = ABSETZ;
beta=beta(ndxv);
P=P(:,ndxv);
P=P(ndxv,:);
filprob=filprob(:,ndxv);
end
indexconstraint3=a_2(1)>a_3(1);
if indexconstraint3 && beta(3)-beta(2)>0.003
indexSwitch=indexSwitch+1;
ndxv = [1 3 2];
ABSETZ = a_3;
a_3 = a_2;
a_2 = ABSETZ;
beta=beta(ndxv);
P=P(:,ndxv);
P=P(ndxv,:);
filprob=filprob(:,ndxv);
end
%-----------------------------------------------------
% save accepted draw after burn-in replications
%-----------------------------------------------------
if i>nburn
beta_ = [beta_ beta];
a1_ = [a1_ a_1];
a2_ = [a2_ a_2];
a3_ = [a3_ a_3];
Pvec = [P(1,:)' ; P(2,:)'; P(3,:)'];
P_ = [P_ Pvec];
end
end 复制代码