请选择 进入手机版 | 继续访问电脑版
楼主: 徐旸慜
7637 18

[问答] 贝叶斯平均方法 [推广有奖]

  • 0关注
  • 1粉丝

讲师

65%

还不是VIP/贵宾

-

威望
0
论坛币
3949 个
通用积分
1.9908
学术水平
1 点
热心指数
1 点
信用等级
1 点
经验
956 点
帖子
64
精华
0
在线时间
1089 小时
注册时间
2008-12-11
最后登录
2023-6-1

徐旸慜 发表于 2012-11-17 17:44:47 |显示全部楼层 |坛友微信交流群
相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
本人因写轮需要,求助论坛大侠谁会用matlab编写贝叶斯平均计量方法的程序,价格可以联系商量!QQ:314731502 急求!

二维码

扫码加我 拉你入群

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

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

关键词:贝叶斯 MATLAB matla atlab 计量方法 matlab 程序

徐旸慜 发表于 2012-11-17 17:45:27 |显示全部楼层 |坛友微信交流群
自己先顶起来,希望各路高手能帮我解答!

使用道具

tulipsliu 在职认证  发表于 2012-11-17 19:14:48 |显示全部楼层 |坛友微信交流群
你出多少钱?
对这个问得直接了点。
呵呵
不好意思。
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
epoh + 1 + 1 + 1 楼主有福了

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

"Recursive Macroeconomic Theory","Labour Economic".

使用道具

tulipsliu 在职认证  发表于 2012-11-17 21:42:46 |显示全部楼层 |坛友微信交流群
epoh 老兄,你把我将着了;
你都评分了;我估计得免费发代码了;

诶;哈哈,不过也没在论坛有做生意的打算;下周考证书,明年再考有质量的证书,继续做工作,争取从市场上赚到我的 车子、房子钱吧。
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
厝火积薪4 + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

"Recursive Macroeconomic Theory","Labour Economic".

使用道具

tulipsliu 在职认证  发表于 2012-11-17 21:43:26 |显示全部楼层 |坛友微信交流群
有一个老外写的书籍《贝叶斯经济学》,他给出代码的,其中也包括 BMS 模型的 MATLAB代码,我找一下吧。
"Recursive Macroeconomic Theory","Labour Economic".

使用道具

tulipsliu 在职认证  发表于 2012-11-17 21:46:33 |显示全部楼层 |坛友微信交流群
代码文件,他的是一个章节,不太清楚完整不;
复制下面的代码,保存为matlab 的脚本文档,暂且命名为 Demo_BMS
  1. %Program for Exercise 3 in Chapter 16
  2. %Bayesian model averaging program
  3. %Use cross country growth example from Fernandez, Ley and Steel (JAE, 2001)

  4. load growth.dat;
  5. %The data set is arranged with data for each country taking up 6 lines
  6. %The following makes it into an N by K matrix
  7. n=72;
  8. %There probably is a better way of doing this...
  9. rawdat=zeros(n,42);
  10. j=1;
  11. for i=1:n
  12.     rawdat(i,:)= [growth(j,:) growth(j+1,:) growth(j+2,:) ...
  13.             growth(j+3,:) growth(j+4,:) growth(j+5,:)];
  14.     j=j+6;
  15. end

  16. y=rawdat(:,1);
  17. xraw=rawdat(:,2:42);
  18. bigk=size(xraw,2);

  19. %subtract mean from all regressors as in FLS
  20. mxraw=mean(xraw);
  21. for i=1:bigk
  22.     xraw(:,i)=xraw(:,i) - mxraw(1,i);
  23. end


  24. %I am creating a vector of length bigk which is 1 if explanatory
  25. %variable is included, else equals zero
  26. %molddraw is initialized to contain all explanatory variables
  27. %with t-stats greater than .5

  28. molddraw=zeros(bigk,1);
  29. xold=[ones(n,1) xraw];
  30. xtxinv=inv(xold'*xold);
  31. bhat=xtxinv*xold'*y;
  32. e=y-xold*bhat;
  33. sse=e'*e;
  34. s2=sse/(n-bigk-1);
  35. bcov=s2*xtxinv;
  36. bt=zeros(bigk+1,1);
  37. for i=2:bigk+1
  38.     bt(i,1)=bhat(i,1)/sqrt(bcov(i,i));
  39.     if abs(bt(i,1))>.5
  40.         molddraw(i-1,1)=1;
  41.     end
  42. end

  43. %Make up the matrix of explanatory variables for model
  44. xold=ones(n,1);
  45. kold=sum(molddraw)+1;
  46. for i=1:bigk
  47.     if molddraw(i,1)>0
  48.         xold=[xold xraw(:,i)];
  49.     end
  50. end
  51. %specify g0 for the g-prior
  52. if n<=(bigk^2)
  53.     g0=1/(bigk^2);
  54. else
  55.     g0=1/n;
  56. end

  57. yty = (y-mean(y))'*(y-mean(y));
  58. xtxinv=inv(xold'*xold);
  59. ymy = y'*y - y'*xold*xtxinv*xold'*y;
  60. g1=g0/(g0+1);
  61. g2=1/(g0+1);
  62. lprobold = .5*kold*log(g1) -.5*(n-1)*log(g2*ymy + g1*yty);
  63. mstart=molddraw;
  64. lprobstart=lprobold;
  65. inccount=zeros(bigk,1);
  66. msize=0;
  67. %I am keeping records for top 10 drawn models
  68. %Here initialize this to initial model
  69. top10mod=[molddraw molddraw molddraw molddraw molddraw ...
  70.        molddraw molddraw molddraw molddraw molddraw];
  71. lprobtop10=lprobold*ones(10,1);
  72. top10count=zeros(10,1);

  73. %calculate first and second moment of all coefficients
  74. %Initialize them here
  75. b1mo=zeros(bigk,1);
  76. b2mo=zeros(bigk,1);
  77. %Number of burnin and kept draws
  78. nburn=100;
  79. nkeep=1000;

  80. nrep=nburn+nkeep;
  81. for irep=1:nrep
  82.    irep

  83.    %choose at random on of the bigk potential explanatory variables
  84.    %if it is already in the model, delete it else add it
  85.    %Based on this, make up candidate model
  86. indch=round(bigk*rand);
  87. xnew=xold;
  88. mnewdraw=molddraw;
  89. if indch>0
  90. if molddraw(indch,1)==1
  91.      isum=0;
  92.     for i=1:indch
  93.         isum=isum+molddraw(i,1);
  94.     end
  95.     xnew = [xold(:,1:isum) xold(:,isum+2:kold)];
  96.     mnewdraw(indch,1)=0;
  97.    
  98. else
  99.     isum=0;
  100.     for i=1:indch
  101.         isum=isum+molddraw(i,1);
  102.     end
  103.     xnew = [xold(:,1:isum+1) xraw(:,indch) xold(:,isum+2:kold)];
  104.     mnewdraw(indch,1)=1;
  105. end
  106. end

  107. knew=sum(mnewdraw)+1;
  108. xtxinv=inv(xnew'*xnew);
  109. ymy = y'*y - y'*xnew*xtxinv*xnew'*y;
  110. lprobnew = .5*knew*log(g1) -.5*(n-1)*log(g2*ymy + g1*yty);

  111. %Now decide whether to accept candidate draw
  112. if log(rand) < (lprobnew - lprobold)
  113.     xold=xnew;
  114.     lprobold=lprobnew;
  115.     molddraw=mnewdraw;
  116.     kold=knew;
  117. end

  118. if irep>nburn
  119.     %If new drawn model better than current top 10, add it to list
  120.     for i=1:10
  121. if lprobold>=lprobtop10(i,1)
  122.   if sum(abs(molddraw - top10mod(:,i)))<.09
  123.       break
  124.   end
  125.     if i<10
  126.        lprobtop10(i+1:10,1)=lprobtop10(i:9,1);
  127.        top10mod(:,i+1:10) = top10mod(:,i:9);
  128.        top10count(i+1:10,1)=top10count(i:9,1);
  129.     end
  130.     lprobtop10(i,1)=lprobold;
  131.     top10mod(:,i)=molddraw;
  132.     top10count(i,1)=0;
  133.     break
  134.   end

  135. end

  136.    for i=1:10
  137.        temp1=sum(abs(molddraw-top10mod(:,i)));      
  138.        if temp1<.01
  139.            top10count(i,1)=top10count(i,1)+1;
  140.            break
  141.        end
  142.    end

  143.     inccount = inccount + molddraw;
  144.     msize=msize + kold;
  145.     %calculating posterior properties of coefficients means
  146.     %we have to write out full posterior
  147.     Q1inv = (1+g0)*xold'*xold;
  148.     Q0inv=g0*xold'*xold;
  149.         Q1=inv(Q1inv);
  150.         b1= Q1*xold'*y;
  151.         vs2 = (y-xold*b1)'*(y-xold*b1) + b1'*Q0inv*b1;
  152.         bcov = (vs2/(n-2))*Q1;
  153.         %the next bit of this is awkward, needed to find out if variable is
  154.         %included in the model and, if so, to find out where it is
  155.            summer1=1;
  156.         for i=1:bigk
  157.             bc=zeros(1,kold);        
  158.             if molddraw(i,1)==1
  159.               summer1=summer1+1;
  160.               bc(1,summer1)=1;
  161.               bmean=bc*b1;
  162.             bvar =bc*bcov*bc';
  163.          
  164.             b1mo(i,1)=b1mo(i,1) + bmean;
  165.             b2mo(i,1)=b2mo(i,1) + (bvar+bmean^2);
  166.           end
  167.       end
  168.       
  169. end

  170. end

  171. inccount=inccount./nkeep;
  172. 'proportion of models visited containing each variable'
  173. varcount=cumsum(ones(bigk,1));
  174. [varcount inccount]
  175. 'mean number of regressors in models'
  176. msize/nkeep

  177. 'Prob of top 10 models, analytical (after deleting rest of models)'
  178. lt1=lprobtop10 - max(lprobtop10);
  179. exp(lt1)/sum(exp(lt1))

  180. 'Prob of top 10 models, numerical (after deleting rest of models)'
  181. top10count./sum(top10count)

  182. 'Prob of top 10 models out of total number of models, numerical'
  183. sum(top10count)/nkeep

  184. b1mo=b1mo./nkeep;
  185. b2mo=b2mo./nkeep;
  186. bsd=sqrt(b2mo-b1mo.^2);
  187. 'Posterior mean and stand dev of each coefficient'
  188. [b1mo bsd]
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
epoh + 5 + 5 + 5 热心帮助其他会员

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

"Recursive Macroeconomic Theory","Labour Economic".

使用道具

tulipsliu 在职认证  发表于 2012-11-17 21:56:17 |显示全部楼层 |坛友微信交流群
这个是数据文件,和他官方的.dat 文件有区别,他给出的是网页数据了,不好保存为 .dat 的;这个是text 的;
替换上面的第八行;
  1. growth=load growth;
复制代码
如果不行,可以先用右键单击 growth.txt 文档,导入MATLAB 的 workspace ,把这个矩阵名为为 growh
再保存
  1. save growth growth;
复制代码
替换第八行代码为
  1. load growth;
复制代码
就可以了;

再交流; epoh  是论坛里编程很强的;有问题也可以问 epoh,去年我的 mrs_mv_dccgarch 也是问 epoh  的。

growh.txt

42.8 KB

"Recursive Macroeconomic Theory","Labour Economic".

使用道具

tulipsliu 在职认证  发表于 2012-11-17 23:22:01 |显示全部楼层 |坛友微信交流群
proportion of models visited containing each variable
    1.0000    0.2290
    2.0000    0.9790
    3.0000    1.0000
    4.0000    0.8450
    5.0000    0.7240
    6.0000    0.2800
    7.0000    0.2450
    8.0000    0.2150
    9.0000    0.0560
   10.0000    0.8200
   11.0000    0.2520
   12.0000    0.0610
   13.0000    0.0160
   14.0000    0.4710
   15.0000         0
   16.0000    0.4330
   17.0000    0.8900
   18.0000    0.1310
   19.0000    0.0930
   20.0000         0
   21.0000    0.0810
   22.0000    0.0240
   23.0000    0.1420
   24.0000    0.1990
   25.0000    0.1130
   26.0000         0
   27.0000    0.1490
   28.0000    0.1260
   29.0000    1.0000
   30.0000    0.1010
   31.0000    0.0530
   32.0000    0.1410
   33.0000         0
   34.0000    0.5620
   35.0000    0.1300
   36.0000    0.4070
   37.0000    0.6060
   38.0000    0.2440
   39.0000    0.0380
   40.0000    0.0040
   41.0000    0.0760

mean number of regressors in models
   12.9360

Prob of top 10 models, analytical (after deleting rest of models)
    0.1721
    0.1713
    0.1596
    0.0958
    0.0903
    0.0863
    0.0639
    0.0605
    0.0563
    0.0439

Prob of top 10 models, numerical (after deleting rest of models)
    0.0811
    0.0135
    0.3378
    0.1757
    0.0541
    0.1216
    0.0270
    0.0270
    0.0541
    0.1081

Prob of top 10 models out of total number of models, numerical
    0.0740

Posterior mean and stand dev of each coefficient
    0.0051    0.0103
    0.0009    0.0003
   -0.0168    0.0032
    0.0357    0.0206
    0.0016    0.0013
    0.0036    0.0065
   -0.0014    0.0033
    0.0004    0.0016
   -0.0000    0.0000
    0.1272    0.0745
    0.0147    0.0279
   -0.0000    0.0000
   -0.0000    0.0003
   -0.0043    0.0051
         0         0
   -0.0051    0.0067
   -0.0163    0.0080
   -0.0070    0.0221
    0.0044    0.0484
         0         0
   -0.0003    0.0013
   -0.0000    0.0003
   -0.0003    0.0008
   -0.0000    0.0000
   -0.0000    0.0000
         0         0
    0.0017    0.0047
   -0.0006    0.0026
    0.0574    0.0120
    0.0009    0.0029
    0.0001    0.0009
   -0.0072    0.0206
         0         0
    0.0075    0.0078
   -0.0011    0.0034
   -0.0051    0.0070
    0.0087    0.0081
    0.0012    0.0032
    0.0035    0.0408
    0.0000    0.0009
    0.0000    0.0000

Demo_BMS.zip

12.07 KB

本附件包括:

  • Demo_BMS.m
  • growth.mat

"Recursive Macroeconomic Theory","Labour Economic".

使用道具

tulipsliu 在职认证  发表于 2012-11-17 23:23:23 |显示全部楼层 |坛友微信交流群
上面是修改后的代码;
发上来的系数是通过 MATLAB R2012b 运行后的结果;

下载 Demo_BMS 这个 zip 文件解压后用matlab 运行 Demo_BMS 程序就可以了。
"Recursive Macroeconomic Theory","Labour Economic".

使用道具

longddd 发表于 2013-3-19 22:36:04 |显示全部楼层 |坛友微信交流群
tulipsliu
给力 只能顶

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-29 23:03