楼主: tulipsliu
11810 14

[问答] Markov regime switching Model [推广有奖]

经济学论述自由撰稿人!

已卖:2751份资源

学科带头人

45%

还不是VIP/贵宾

-

威望
0
论坛币
386043 个
通用积分
526.9898
学术水平
127 点
热心指数
140 点
信用等级
103 点
经验
46986 点
帖子
1773
精华
0
在线时间
2509 小时
注册时间
2007-11-5
最后登录
2024-8-16

初级热心勋章

楼主
tulipsliu 在职认证  发表于 2011-5-16 15:24:11 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
最近朋友找我帮忙做一个计量模型的 Markov regime swithing 的模型,我想了一周,也没改造成功,看到论坛那么多有才的人,比如epoh,我这里发一个贴,期待各位的回应。看看能解决这个问题不。这里发一个arch_3_state的状态空间模型,期待各位的回复。
  1. %% coed_MS_ARCH_3S.txt
  2. %-------------------------------------------
  3. % Written by Zhu, Junjun,
  4. % Ph.D candidate in School of Economics
  5. % Fudan University, Shanghai 200433
  6. % Nov. 2009

  7. % please notice, we can not guarantee that these codes are without mistakes.
  8. % You may use these at your own risks.
  9. %-------------------------------------------

  10. clc;
  11. clear;
  12. load data_200910weekex;
  13. y = data_200910weekex;
  14. n = size(y,1);
  15. x = ones(n,1);
  16. sn = 3; % number of state
  17. nk = 1; % regressor number in mean equation
  18. np = 3; % p-term in GARCH
  19. y_p = y(np+1:n);
  20. x_p = x(np+1:n);
  21. pi_pri = [18 2 2; 2 28 2; 2 2 16];  % prior for pi
  22. bols = inv(x'*x)*x'*y;
  23. s2ols = (y-x*bols)'*(y-x*bols)/(n-1);
  24. xsquare=x'*x;
  25. indexSwitch =0;
  26. switch_1 = 0;
  27. switch_2 = 0;
  28. switch_3 = 0;
  29. P=[0.8 0.1 0.1;0.1 0.8 0.1; 0.1 0.1 0.8];
  30. %spec = garchset('distribution', 'T','p',0,'q',3);
  31. %[coeff, errors,LLF,innovations,sigmas,summary] = garchfit(spec,y);
  32. %garchdisp(coeff,errors)
  33. % coeff.C = [0.0005;]; coeff.K = 0.00015; coeff.ARCH = [0.197,0.26,0.292;]
  34. % default model: y(t) = C +e(t); h(t) = K + GARCH*h(t-1) + ARCH*e(t-1)^2
  35. %   beta = [0.0016; 0.0016; 0.0016];  
  36. %   a_1 = [0.00074; 0.2; 0.26; 0.29]; % ARCH coeff. a0, a1, a2, a3 for state 1
  37. %   a_2 = [0.00074; 0.2; 0.26; 0.29];
  38. %   a_3 = [0.00074; 0.2; 0.26; 0.29];
  39. beta = [-0.0011; 0.02; -0.0061];  
  40. a_1 = [0.0005; 0.11; 0.15; 0.08];
  41. a_2 = [0.0014; 0.22; 0.17; 0.13];
  42. a_3 = [0.0022; 0.14; 0.10; 0.15];
  43. var_err = (y-x*beta(1)).^2;

  44. beta_ = [];
  45. a1_ = [];
  46. a2_ = [];
  47. a3_ = [];
  48. P_ = [];
  49. nburn=0; %number of burnin replications
  50. nrep=30000; %number of retained replications
  51. ntotal=nburn+nrep;


  52. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
  53. %-----------------------------------------------------------------------
  54. %starting value for S, P
  55. %-----------------------------------------------------------------------
  56.     S=zeros(n,1);
  57.    
  58.     var_err_mean = mean(var_err);
  59.     for t=1:n
  60.         if var_err(t)<=var_err_mean-0.00005              % state 1 as low volatility state
  61.             S(t)=1;
  62.         else
  63.             if var_err(t)<=var_err_mean+0.00015
  64.             S(t)=2;
  65.             else
  66.             S(t) = 3;
  67.             end
  68.         end
  69.     end
  70.    
  71. %----------------------------------------------------------------------
  72. % starting value for P
  73. %----------------------------------------------------------------------
  74.     T = n;
  75.     sm=[S(1:T-1) S(2:T)];  % switching number, sm: (T-1)*2
  76.     tm=zeros(sn,sn);   % total number
  77.     for im=1:sn
  78.        for jm=1:sn
  79.         tm(im,jm)=size(sm(sm(:,1)==im & sm(:,2)==jm,:),1); % total number of each transition from i state to j state              
  80.        end
  81.     rr = pi_pri(im,:)+tm(im,:);
  82.     P_draw = chi2rnd(2*rr);
  83.     P(im,:)=P_draw./repmat(sum(P_draw,2),1,sn);
  84.     % P(im,:)=draw_dirichlet(1,sn,pi_pri(im,:)+tm(im,:),0); % rm: prior for number of state transition (ij)  
  85.     end
  86. % 第一部分结束,
复制代码
二维码

扫码加我 拉你入群

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

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

关键词:switching Markov regime switch Ching model Markov regime switching

回帖推荐

gssdzc 发表于14楼  查看完整内容

这个好像是单元的,有木有人,有二元或者是多元的MS-GARCH程序。欢迎多讨论,一起多交流。我的扣扣:529820052

本帖被以下文库推荐

劳动经济学

沙发
tulipsliu 在职认证  发表于 2011-5-16 15:28:06
  1. [code]%紧接第一部分代码

  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Now start Gibbs loop%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. for i = 1:ntotal
  4. completion_process=i/ntotal
  5. %-------------------------------------------------------
  6. % caculate draw probability
  7. %---------------S1---------------------------------------
  8. %log likelihood for the regression model with ARCH errors -- old draw
  9. y_1 = y(S==1); x_1 = x(S==1);
  10. err_1 = y_1 - x_1*beta(1);
  11. n1 = length(err_1);
  12. h1 =a_1(1)*ones(n1-np,1); % vols: 524*1
  13. for j=1:np
  14. h1 = h1 + a_1(j+1,1)*err_1(np+1-j:n1-j,1).^2; % conditional variance h
  15. end
  16. % alternative: vols = vols + adraw(2,1)*errs(p:t-1,1) + adraw(3,1)*errs(p-1:t-2) + ...
  17. % adraw(4,1)*errs(p-2:t-3,1) + adraw(5,1)*errs(p-3:t-4,1)
  18. lterm=(err_1(np+1:n1,1).^2)./h1;
  19. llike1 = -.5*sum(log(h1)) -.5*sum(lterm);

  20. %---------------S2---------------------------------------
  21. y_2 = y(S==2); x_2 = x(S==2);
  22. err_2 = y_2 - x_2*beta(2);
  23. n2 = length(err_2);
  24. h2 =a_2(1)*ones(n2-np,1);% vols: 524*1
  25. for j=1:np
  26. h2 = h2 + a_2(j+1,1)*err_2(np+1-j:n2-j,1).^2; % conditional variance h
  27. end
  28. lterm=(err_2(np+1:n2,1).^2)./h2;
  29. llike2 = -.5*sum(log(h2)) -.5*sum(lterm);

  30. %---------------S3---------------------------------------
  31. y_3 = y(S==3); x_3 = x(S==3);
  32. err_3 = y_3 - x_3*beta(3);
  33. n3 = length(err_3);
  34. h3 =a_3(1)*ones(n3-np,1);% vols: 524*1
  35. for j=1:np
  36. h3 = h3 + a_3(j+1,1)*err_3(np+1-j:n3-j,1).^2; % conditional variance h
  37. end
  38. lterm=(err_3(np+1:n3,1).^2)./h3;
  39. llike3 = -.5*sum(log(h3)) -.5*sum(lterm);

  40. %---------------------------------------------------------
  41. % take candidate draw, caculate candidate probability
  42. %---------------------------------------------------------
  43. %take candidate draw
  44. beta_sig = [(0.003/4)^2 0 0; 0 (0.013/4)^2 0; 0 0 (0.018/4)^2 ];
  45. beta_can = beta + norm_rnd(beta_sig);
  46. % 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];
  47. 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];
  48. a_1_can = a_1 + norm_rnd(a_1_sig);
  49. % 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];
  50. 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];
  51. a_2_can = a_2 + norm_rnd(a_2_sig);
  52. % 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];
  53. 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];
  54. a_3_can = a_3 + norm_rnd(a_3_sig);
  55. %if this draw violates positivity or stationarity restrictions do not accept it
  56. violat_1 =0; violat_2 =0; violat_3 =0;
  57. %first check positivity of arch coefficients
  58. for j=1:np+1
  59. if a_1_can(j,1)<=0
  60. violat_1=1;
  61. end
  62. if a_2_can(j,1)<=0
  63. violat_2=1;
  64. end
  65. if a_3_can(j,1)<=0
  66. violat_3=1;
  67. end
  68. end
  69. if a_1_can(2) + a_1_can(3) +a_1_can(4) >1; violat_1=1; end
  70. if a_2_can(2) + a_2_can(3) +a_2_can(4) >1; violat_2=1; end
  71. if a_3_can(2) + a_3_can(3) +a_3_can(4) >1; violat_3=1; end

  72. %-------------------------------------------------------
  73. % Accept the candidate with probability
  74. %-------------------------------------------------------
  75. if violat_1 ==0
  76. %log likelihood for the regression model with ARCH errors -- candidate draw
  77. err_1_can = y_1 - x_1*beta_can(1);
  78. h1_can=a_1_can(1,1)*ones(n1-np,1);
  79. for j=1:np
  80. h1_can = h1_can + a_1_can(j+1,1)*err_1_can(np+1-j:n1-j,1).^2;
  81. end
  82. lterm=(err_1_can(np+1:n1,1).^2)./h1_can;
  83. llike1_p = -.5*sum(log(h1_can)) -.5*sum(lterm);
  84. %log of acceptance probability
  85. laccprob_1 = llike1_p - llike1;
  86. %now accept candidate with appropriate probability
  87. if laccprob_1 > log(rand)
  88. beta(1)=beta_can(1);
  89. a_1 = a_1_can;
  90. switch_1 = switch_1+1;
  91. end
  92. end
  93. %-------------------------- S2--------------------------------------------
  94. if violat_2 ==0
  95. %log likelihood for the regression model with ARCH errors -- candidate draw
  96. err_2 = y_2 - x_2*beta_can(2);
  97. h2=a_2_can(1,1)*ones(n2-np,1);
  98. for j=1:np
  99. h2 = h2 + a_2_can(j+1,1)*err_2(np+1-j:n2-j,1).^2;
  100. end
  101. lterm=(err_2(np+1:n2,1).^2)./h2;
  102. llike2_p = -.5*sum(log(h2)) -.5*sum(lterm);
  103. %log of acceptance probability
  104. laccprob_2 = llike2_p - llike2;
  105. %now accept candidate with appropriate probability
  106. if laccprob_2 > log(rand)
  107. beta(2)=beta_can(2);
  108. a_2 = a_2_can;
  109. switch_2 = switch_2+1;
  110. end
  111. end

  112. %-------------------------- S2--------------------------------------------
  113. if violat_3 ==0
  114. %log likelihood for the regression model with ARCH errors -- candidate draw
  115. err_3 = y_3 - x_3*beta_can(3);
  116. h3=a_3_can(1,1)*ones(n3-np,1);
  117. for j=1:np
  118. h3 = h3 + a_3_can(j+1,1)*err_3(np+1-j:n3-j,1).^2;
  119. end
  120. lterm=(err_3(np+1:n3,1).^2)./h3;
  121. llike3_p = -.5*sum(log(h3)) -.5*sum(lterm);
  122. %log of acceptance probability
  123. laccprob_3 = llike3_p - llike3;
  124. %now accept candidate with appropriate probability
  125. if laccprob_3 > log(rand)
  126. beta(3)=beta_can(3);
  127. a_3 = a_3_can;
  128. switch_3 = switch_3+1;
  129. end
  130. end

  131. %--------------------------------------------------------------------
  132. %draw S conditional on [lnl,filprob,simstate] = MSLinRegDrawState(T,k1,k2,nm,indSigma,p,P,yv,X1,X2,Beta,hv);
  133. %--------------------------------------------------------------------
  134. err_S = zeros(n,sn);
  135. h_S= zeros(n,sn);
  136. for j=1:sn
  137. for i = 1:n
  138. err_S(i,j) = y(i) - x(i)*beta(j);
  139. end
  140. end
  141. h_S(1,1) = a_1(1)/(1-a_1(2)-a_1(3)-a_1(4));
  142. h_S(2,1) = a_1(1) + a_1(2)*err_S(1,1)^2;
  143. h_S(3,1) = a_1(1) + a_1(2)*err_S(2,1)^2+a_1(3)*err_S(1,1)^2;
  144. h_S(1,2) = a_2(1)/(1-a_2(2)-a_2(3)-a_2(4));
  145. h_S(2,2) = a_2(1) + a_2(2)*err_S(1,2)^2;
  146. h_S(3,2) = a_2(1) + a_2(2)*err_S(2,2)^2+a_2(3)*err_S(1,2)^2;
  147. h_S(1,3) = a_3(1)/(1-a_3(2)-a_3(3)-a_3(4));
  148. h_S(2,3) = a_3(1) + a_3(2)*err_S(1,3)^2;
  149. h_S(3,3) = a_3(1) + a_3(2)*err_S(2,3)^2+a_3(3)*err_S(1,3)^2;
  150. for j = 4:n
  151. 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;
  152. 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;
  153. 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;
  154. end
  155. % (normpdf) lnlike = exp(-0.5 * ((x - mu)./sigma).^2) ./ (sqrt(2*pi) .* sigma);
  156. lnpdat(:,1) = -0.5*log(2*pi*h_S(:,1))-0.5*(err_S(:,1).^2)./h_S(:,1);
  157. lnpdat(:,2) = -0.5*log(2*pi*h_S(:,2))-0.5*(err_S(:,2).^2)./h_S(:,2);
  158. lnpdat(:,3) = -0.5*log(2*pi*h_S(:,3))-0.5*(err_S(:,3).^2)./h_S(:,3);
  159. lnpdat(:,1) = exp(lnpdat(:,1));
  160. lnpdat(:,2) = exp(lnpdat(:,2));
  161. lnpdat(:,3) = exp(lnpdat(:,3));
  162. filprob=zeros(n,3);
  163. k=S(2);
  164. 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));
  165. 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));
  166. 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));
  167. random_S = rand;
  168. if random_S<=piSti1(1)
  169. S(1) = 1;
  170. else
  171. if random_S <=piSti1(1)+piSti2(1)
  172. S(1)=2;
  173. else
  174. S(1) = 3;
  175. end
  176. end

  177. filprob(1,1)=piSti1(1);
  178. filprob(1,2)=piSti2(1);
  179. filprob(1,3)=piSti3(1);

  180. for t =2:n-1
  181. l=S(t-1);
  182. k=S(t+1);
  183. 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));
  184. 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));
  185. 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));
  186. random_St = rand;
  187. if random_St<=piSti1(t)
  188. S(t) = 1;
  189. else
  190. if random_St<=piSti1(t)+piSti2(t)
  191. S(t)=2;
  192. else
  193. S(t)=3;
  194. end
  195. end
  196. filprob(t,1)=piSti1(t);
  197. filprob(t,2)=piSti2(t);
  198. filprob(t,3)=piSti3(t);
  199. end

  200. l=S(n-1);
  201. 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));
  202. 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));
  203. 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));
  204. random_Sn = rand;
  205. if random_Sn<=piSti1(n)
  206. S(n) = 1;
  207. else
  208. if random_Sn<=piSti1(n)+piSti2(n)
  209. S(n)=2;
  210. else
  211. S(n) =3;
  212. end
  213. end

  214. filprob(n,1)=piSti1(n);
  215. filprob(n,2)=piSti2(n);
  216. filprob(n,3)=piSti3(n);
  217. %--------------------------------------------------------------------
  218. %draw P conditional on S
  219. %--------------------------------------------------------------------
  220. sm=[S(1:n-1) S(2:n)]; % switching number, sm: (T-1)*2
  221. tm=zeros(sn,sn); % total number
  222. for im=1:sn
  223. for jm=1:sn
  224. tm(im,jm)=size(sm(sm(:,1)==im & sm(:,2)==jm,:),1); % total number of each transition from i state to j state
  225. end
  226. rr = pi_pri(im,:)+tm(im,:);
  227. P_draw = chi2rnd(2*rr);
  228. P(im,:)=P_draw./repmat(sum(P_draw,2),1,sn);
  229. % P(im,:)=draw_dirichlet(1,sn,pi_pri(im,:)+tm(im,:),0); % rm: prior for number of state transition (ij)
  230. end


  231. %-------------------------------------------------------
  232. %identification constraint
  233. %-------------------------------------------------------
  234. indexconstraint1=a_1(1)>a_2(1);
  235. %if indexconstraint1 & abs(beta(1)-beta(2))<0.002
  236. if a_1(1)-a_2(1)>0.0003 && beta(1)-beta(2)>0.005
  237. indexSwitch=indexSwitch+1;
  238. ndxv = [2 1 3];
  239. ABSETZ = a_2;
  240. a_2 = a_1;
  241. a_1 = ABSETZ;
  242. beta=beta(ndxv);
  243. P=P(:,ndxv);
  244. P=P(ndxv,:);
  245. filprob=filprob(:,ndxv);
  246. end

  247. % indexconstraint2=beta(1)>beta(3)>beta(2);
  248. indexconstraint2=a_1(1)>a_3(1);
  249. %if indexconstraint2 & abs(beta(1)-beta(3))<0.002
  250. if a_1(1)-a_3(1)>0.0003 && abs(beta(1)-beta(3))>0.002
  251. indexSwitch=indexSwitch+1;
  252. ndxv = [3 2 1];
  253. ABSETZ = a_3;
  254. a_3 = a_1;

  255. a_1 = ABSETZ;
  256. beta=beta(ndxv);
  257. P=P(:,ndxv);
  258. P=P(ndxv,:);
  259. filprob=filprob(:,ndxv);
  260. end

  261. indexconstraint3=a_2(1)>a_3(1);
  262. if indexconstraint3 && beta(3)-beta(2)>0.003
  263. indexSwitch=indexSwitch+1;
  264. ndxv = [1 3 2];
  265. ABSETZ = a_3;
  266. a_3 = a_2;
  267. a_2 = ABSETZ;
  268. beta=beta(ndxv);
  269. P=P(:,ndxv);
  270. P=P(ndxv,:);
  271. filprob=filprob(:,ndxv);
  272. end

  273. %-----------------------------------------------------
  274. % save accepted draw after burn-in replications
  275. %-----------------------------------------------------

  276. if i>nburn
  277. beta_ = [beta_ beta];
  278. a1_ = [a1_ a_1];
  279. a2_ = [a2_ a_2];
  280. a3_ = [a3_ a_3];
  281. Pvec = [P(1,:)' ; P(2,:)'; P(3,:)'];
  282. P_ = [P_ Pvec];
  283. end
  284. end
复制代码
劳动经济学

藤椅
tulipsliu 在职认证  发表于 2011-5-16 15:29:34
  1. MS3.P=P;
  2. MS3.P_=P_;
  3. MS3.S=S;
  4. MS3.beta_=beta_;
  5. MS3.a1_=a1_;
  6. MS3.a2_=a2_;
  7. MS3.a3_=a3_;
  8. MS3.err_S=err_S;
  9. MS3.h_S=h_S;
  10. MS3.filprob=filprob;

  11. switch_1/ntotal
  12. switch_2/ntotal
  13. switch_3/ntotal
  14. indexSwitch

  15. figure(1)
  16. subplot (3,2,1); plot(beta_')
  17. title ('convergence of omega');
  18. subplot(3,2,2); plot ([a1_(1,:)' a2_(1,:)' a3_(1,:)']);
  19. title('convergence of alpha');
  20. subplot(323); plot([a1_(2,:)' a2_(2,:)' a3_(2,:)'])
  21. title('convergence of beta1')
  22. subplot(324); plot([a1_(3,:)' a2_(3,:)'  a3_(3,:)'])
  23. title('convergence of beta2')
  24. subplot(325); plot([a1_(4,:)' a2_(4,:)'  a3_(4,:)'])
  25. title('convergence of beta3')

  26. %%%% calculate the conditional variance

  27. ha = h_S.*[(S==1) (S==2) (S==3)];
  28. ha = ha(:,1) + ha(:,2)+ ha(:,3);
  29.    
  30. figure(2)
  31. plot(ha)



  32. %------------------FUNCTION-------------------------------------
  33. function y = norm_rnd(sig);
  34. % PURPOSE: random multivariate random vector based on
  35. %          var-cov matrix sig
  36. %---------------------------------------------------
  37. % USAGE:   y = norm_rnd(sig)
  38. % where:   sig = a square-symmetric covariance matrix
  39. % NOTE: for mean b, var-cov sig use: b +  norm_rnd(sig)
  40. %---------------------------------------------------      
  41. % RETURNS: y = random vector normal draw mean 0, var-cov(sig)
  42. %---------------------------------------------------

  43. % written by:
  44. % James P. LeSage, Dept of Economics
  45. % University of Toledo
  46. % 2801 W. Bancroft St,
  47. % Toledo, OH 43606
  48. % jlesage@spatial-econometrics.com

  49. if nargin ~= 1
  50. error('Wrong # of arguments to norm_rnd');
  51. end;

  52. h = chol(sig);
  53. [nrow, ncol] = size(sig);
  54. x = randn(nrow,1);
  55. y = h'*x;
复制代码
已有 1 人评分经验 收起 理由
hanyuning + 20 鼓励积极发帖讨论

总评分: 经验 + 20   查看全部评分

劳动经济学

板凳
yishions 发表于 2011-12-19 19:52:47
不会也要顶一下

报纸
xx_xx 发表于 2011-12-20 13:27:55
太深奥了

地板
741panjiali 发表于 2012-2-23 07:40:11
糊里糊涂

7
syasd 发表于 2012-5-30 20:32:15
先mark一下  最近正在学MRS-GARCH模型  现在似然函数编出来的,却不知道该如何做参数估计,还好有楼主的帖子做个参考,实在感谢
我要一步一步往上爬,在最高点乘着叶片往前飞,任风吹干流过的泪和汗,总有一天我有属于我的天。

8
syasd 发表于 2012-6-9 15:13:02
你好,又要打扰楼主了,你发的程序我现在看了1/4多吧,但是又几个疑惑,是在等不急,就先问你一下吧:
首先是帖子第二楼的“start gibbs toop中,求了三个似然函数(09行——42行),ARCH效应中本期的条件方差是用前几期(这程序里是滞后阶数是3)的残差平方表示的,但是在程序中却是先把残差按波动的大小分为3个状态,而在3个状态中分别求条件方差h,那没在时间上,显然t期的h不是用t-1,t-2,t-3期的残差平方表示了。所以这里求h的方法让我无法理解,楼主能不能把原理讲一下,谢谢。
另一个问题仍是在GIBBS TOOP中,第49行有一个norm_rnd函数,这个是不是从正态分布中抽取随机数?因为我的matlab中现实没有这个函数,所以我就改成了normrnd(),但是又显示
Error using normrnd (line 28)
Requires at least two input arguments.

Error in code_ms_3s (line 147)
   beta_can = beta + normrnd(beta_sig);
我不知道问题出在哪里,还是希望楼主能指点一下,谢谢
我要一步一步往上爬,在最高点乘着叶片往前飞,任风吹干流过的泪和汗,总有一天我有属于我的天。

9
syasd 发表于 2012-6-9 15:53:10
49行可不可以改成
beta_can=beta+[normrnd(0,(0.003/4)^2);normrnd(0,(0.013/4)^2);normrnd(0,0.018/4)^2)]
我要一步一步往上爬,在最高点乘着叶片往前飞,任风吹干流过的泪和汗,总有一天我有属于我的天。

10
tulipsliu 在职认证  发表于 2012-6-16 14:28:31
syasd 发表于 2012-6-9 15:13
你好,又要打扰楼主了,你发的程序我现在看了1/4多吧,但是又几个疑惑,是在等不急,就先问你一下吧:
首先 ...
不好意思,过去几个月在工作。很少来论坛交流,也有7个月的时间做客户营销,没时间运行和修改matlab程序;
你问的问你很深入,也比较难以解决。毕竟MR-GARCH不是简单的。Griddy gibbs 抽样技术,也许你们自己有书,我从论坛找到国外的一个作者的贝叶斯金融统计建模,里面也提到过这个。不过很多我还没理解。
关于你提到的 normrnd()函数,这个是出自 lesage 的 JPL7 计量经济学工具箱里,是他本人自己写的一个多元正态分布随机函数,你可以下载JPL7,将这里的代码放进去,就可以正常运行。
如果你改用系统的多元正态分布随机数函数,因为输入参数和输出,也就是我们说的函数调用不匹配问题,导致出错。

明天要考试。所以不深入聊下去。我很高兴你这样深入的看这个程序和提这么多问题。有时间的时候多交流。
我的Q:280201722 潇湘品茗 30岁,大学毕业5年了。
劳动经济学

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-3 20:08