楼主: tulipsliu
895 2

[程序分享] DSGE动态经济学建模Band-Pass filter 程序分享 [推广有奖]

经济学论述自由撰稿人!

学科带头人

44%

还不是VIP/贵宾

-

威望
0
论坛币
386064 个
通用积分
469.5402
学术水平
127 点
热心指数
140 点
信用等级
103 点
经验
46970 点
帖子
1770
精华
0
在线时间
2488 小时
注册时间
2007-11-5
最后登录
2024-5-4

初级热心勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
这个代码我调试过,添加了缺少的函数文件 ols.m ,但是依然还有10% 的错误,本来用markdown 发帖,但是贴了主程序1100行的代码,太大导致无法发出去。 所以就算了。

这里插入一个函数计算 band-pass filter的。

  1. function yf=BPF(y,up,dn,K)

  2. % bpf.m  
  3. % Program to compute band-pass filtered series
  4. % Inputs are
  5. %  y:   data (rows = observations, columns=series)
  6. %  up:  period corresponding to highest frequency (e.g., 6)
  7. %  dn:  period corresponding to lowest frequency (e.g., 32)
  8. %  K:   number of terms in approximating moving average
  9. %  [calls filtk.m (filter with symmetric weights) as subroutine]
  10. x=[up dn];
  11. disp(' ')
  12. disp('bpf(y,up,dn,K): band-pass filtering of series y with symmetric MA(2K+1)')
  13. disp(' ')
  14. disp('                for additional information see: ')
  15. disp(' ')
  16. disp('                   M. Baxter and R.G. King      ')
  17. disp(' ')
  18. disp('                   Measuring Business Cycles:   ')
  19. disp('                   Approximate Band-Pass Filters')
  20. disp('                   for Macroeconomic Time Series')
  21. disp(' ')
  22. disp('Filter extracts components between periods of: ')
  23. disp('    up    dn')
  24. disp(x)
  25. % pause(2)

  26. if (up>dn)
  27. disp('Periods reversed: switching indices up & dn')
  28. disp(' ')
  29. dn=x(1); up=x(2);
  30. end

  31. if (up<2)
  32. up=2;
  33. disp('Higher periodicity > max: Setting up=2')
  34. disp(' ')
  35. end
  36.   
  37. % convert to column vector
  38. [r c]=size(y);
  39. if (r<c)
  40. y=y';
  41. disp('There are more columns than rows: Transposing data matrix')
  42. disp(' ')
  43. end
  44.   
  45. % Implied Frequencies
  46. omubar=2*pi/up;
  47. omlbar=2*pi/dn;

  48. % An approximate low pass filter, with a cutoff frequency of "ombar",
  49. % has a frequency response function
  50. %
  51. %  alpha(om) = a0 + 2*a1 cos(om) + ... 2*aK cos(K om)
  52. %
  53. % and the ak's are given by:
  54. %  
  55. % a0 = ombar/(pi)             ak = sin(k ombar)/(k pi)
  56. %
  57. % where ombar is the cutoff frequency.

  58. % A band-pass filter is the difference between two
  59. % low-pass filters,
  60. %   bp(L) = bu(L) - bl(L)
  61. % with bu(L) being the filter with the high cutoff point and bl(L) being
  62. % that with the low cutoff point.  Thus, the weights are differences
  63. % of weights for two low-pass filters.

  64. % Construct filter weights for bandpass filter (a(0)....a(K)).

  65. akvec=zeros(1,K+1);

  66. akvec(1)=(omubar-omlbar)/(pi);  % weight at k=0

  67. for k=1:K;
  68.   akvec(k+1)=(sin(k*omubar)-sin(k*omlbar))/(k*pi); % weights at k=1,2,...K
  69. end

  70. % Impose constraint on frequency response at om = 0
  71. % (If high pass filter, this amounts to requiring that weights sum to zero).
  72. % (If low pass filter, this amounts to requiring that weights sum to one).

  73. if (dn>1000)
  74. disp('dn > 1000: assuming low pass filter')
  75. phi=1;
  76. else
  77. phi=0;
  78. end

  79. % sum of weights without constraint
  80. theta=akvec(1)+2*sum(akvec(2:K+1));
  81. % amount to add to each nonzero lag/lead to get sum = phi
  82. theta=(phi-theta)/(2*K+1);
  83. % adjustment of weights
  84. akvec=akvec+theta;

  85. % filter the time series
  86. yf=filtk(y,akvec);

  87. if (r<c)
  88. yf=yf';
  89. end
  90. 
复制代码



插入附件,特别大也许会消耗论坛流量,因为有143M 这样大。 文件夹里有数据、主程序脚本文件、函数文件。 希望对大家的学习有帮助,未来三年推动国内的动态宏观经济学建模向国际一流接近。

因为太大所以我用了网盘,发链接吧。

链接:https://pan.baidu.com/s/1XyKiuoi5g5r_8L2Qregjhw
提取码:取消密码公布


二维码

扫码加我 拉你入群

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

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

关键词:filter 动态经济学 动态经济 Pass DSGE DSGE band-pass-filter DSGE band-pass-filter

劳动经济学
沙发
sunstar868 发表于 2020-7-10 09:57:01 |只看作者 |坛友微信交流群
赞一赞热心的楼主!

使用道具

藤椅
tulipsliu 在职认证  发表于 2020-7-11 08:07:50 |只看作者 |坛友微信交流群
sunstar868 发表于 2020-7-10 09:57
赞一赞热心的楼主!
习惯了,这么多年我都喜欢在论坛分享代码。

使用道具

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

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

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

GMT+8, 2024-5-7 19:35