加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

[转载]HP滤波Matlab代码

(2017-01-26 23:03:35)
标签:

转载

分类: matlab
原文地址:HP滤波Matlab代码作者:中克ZK
 Matlab Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function [s,desvabs] hpfilter(y,w,plotter)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  Author: Wilmer Henao    wi-henao@uniandes.edu.co
  Department of Mathematics
  Universidad de los Andes
  Colombia
%
  Hodrick-Prescott filter extracts the trend of time series, the output
  is not formula but new filtered time series.  This trend can be
  adjusted with parameter w; values for lie usually in the interval
  [100,20000], and it is up to you to use the one you like, As approaches infty, 
  H-P will approach line.  If the series doesn't have trend p.e.White Noise, 
  doing H-P is meaningles
%
  [s] hpfilter(y,w)
  Smoothing parameter (Economists advice: "Use 1600 for quarterly data")
  Original series
  Filtered series
  This program can work with several series at time, as long as the
  number of series you are working with doesn't exceed the number of
  elements in the series it uses sparse matrices which improves speed
  and performance in the longest series
  
  [s] hpfilter(y,w,'makeplot')
  'makeplot' in the input, plots the graphics of the original series
  against the filtered series, if more than one series is being
  considered the program will plot all of them in different axes
%
  [s,desvabs] hpfilter(y,w)
  Gives you mesure of the standardized differences in absolute values
  between the original and the filtered series.  big desvabs means
  that the series implies large relative volatility.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
tic
if nargin 2
    error('Requires at least two arguments.');
end

[m,n] size (y);
if n
    y';     n;
end
repmat([w -4*w ((6*w+1)/2)], m, 1);
d(1,2) -2*w;      d(m-1,2) -2*w;
d(1,3) (1+w)/2;   d(m,3) (1+w)/2;
d(2,3) (5*w+1)/2; d(m-1,3) (5*w+1)/2;
spdiags(d, -2:0, m, m);    %I use sparse version of B, because when is large, will have many zeros     
B+B';
By;

if nargin == 3
    size(y,2);
    for 1:t
        figure(i)
        plot(s(:,i),'r');   grid on;   hold on;   plot(y(:,i));   title(['Series #',num2str(i)]);
    end
end
if nargout == 2
    desvabs mean(abs(y-s)./s);
end
toc

0

  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有