楼主: stanleyjunjun
2048 0

Matlab code for Markov Switching Model - SWARCHLIKE_6 [推广有奖]

  • 4关注
  • 10粉丝

已卖:1484份资源

副教授

19%

还不是VIP/贵宾

-

威望
0
论坛币
3580 个
通用积分
195.4370
学术水平
22 点
热心指数
32 点
信用等级
4 点
经验
18439 点
帖子
378
精华
0
在线时间
1016 小时
注册时间
2007-6-5
最后登录
2026-1-26

楼主
stanleyjunjun 发表于 2009-7-30 01:21:22 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
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;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:matlab code switching MATLAB SWARCH switch

本版微信群
扫码
拉您进交流群
GMT+8, 2026-2-1 11:57