function [sumlike, output, logLikVec] = SWARCHLIKE_6(y, param)
%%%%% model: Switching Variance model; to check why SWARCH failed
%%%%% y = mean(y) + c(s) + e
%%%%% e = u*h(s)^1/2; u - iid.N(0,1)
%%%%%
%%%%% reference Cai 1992
nr=length(y);
% param0 = [0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5];
coeff.c(1,1:2) = param(1:2); % constant in mean equation, state switching
coeff.h(1,1:2)=param(3:4); % variance, state switching
coeff.pro(1,1:2)=param(5:6); % initial value set to be 0.5 each
coeff.pro(2,1:2)=param(7:8); % initial value set to be 0.5 each
% Prealocation of large matrices
% cond_mean=zeros(nr,k);
n=zeros(nr,2); % likehood density
e=zeros(nr,2); % residual error
% h=zeros(nr,2); % conditional variance
for t = 1:nr;
for i = 1:2;
e(t,i) = y(t) - mean(y) - coeff.c(1,i);
end
end
for t = 2:nr;
for j = 1:2;
n(t,j)=(1/((coeff.h(j)^0.5)*sqrt(2*pi)))*exp(-(e(t,j)^2)/(2*coeff.h(j))); % Normal prob density at each state
end
end
f=zeros(nr,1); % conditional density of y on prior infomation
E=zeros(nr,2); % conditional density of state
E(1:2,1:2)=1/2; % with dimension 1*2
%%%%%% (coeff.pro*E(i-1,:)' = sum(i)[pro(ij)*E(i,t-1)],
%%%%%% thus pro11 = p, pro12 =q, pro21 = 1-p, pro22 = 1-q
%%%%%%
%%%%%%
for i=2:nr % here from 4 to end
f(i,1)=ones(2,1)'*(coeff.pro*E(i-1,:)'.*n(i,:)'); % MS Filter equation (1*2) (2*2)(2*nr).(2*nr) attention dot product
E(i,:)=((coeff.pro*E(i-1,:)'.*n(i,:)')/f(i,1)); % MS Filter equation for probabilities
end
sumlike=-sum(log(sum(f(2:end,:),2)));
if isnan(sumlike)||isreal(sumlike)==0||isinf(sumlike)
sumlike=inf;
end
logLikVec = log(f);
output.loglikvec =log(f);
output.coeff=coeff;
output.filtprob=E;
output.LL=-sumlike;
% Output.ar=ar;
% Output.k=k;
output.number_parameters=length(param);
output.param=param;
% Output.condMean=sum(Cond_mean.*E,2);
% Output.condStd=sum(repmat(Coeff.Std,nr,1).*E,2);
% output.h = h;
output.e = e;


雷达卡


京公网安备 11010802022788号







