请选择 进入手机版 | 继续访问电脑版
爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 4912|回复: 1

[源程序] 自己寻找的hp滤波程序

[复制链接]

新浪微博达人勋

发表于 2018-12-6 12:14:31 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
function [s,desvabs] = hpfilter(y,w,~)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Author: Wilmer Henao    wi-henao@uniandes.edu.co
%   Department of Mathematics
%   Universidad de los Andes
%   Colombia
%
%   Hodrick-Prescott filter extracts the trend of a time series, the output
%   is not a formula but a new filtered time series.  This trend can be
%   adjusted with parameter w; values for w lie usually in the interval
%   [100,20000], and it is up to you to use the one you like, As w approaches infty,
%   H-P will approach a line.  If the series doesn't have a trend p.e.White Noise,
%   doing H-P is meaningles
%
%    = hpfilter(y,w)
%   w = Smoothing parameter (Economists advice: "Use w = 1600 for quarterly data")
%   y = Original series
%   s = Filtered series
%   This program can work with several series at a 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
%   
%    = 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 a mesure of the standardized differences in absolute values
%   between the original and the filtered series.  A big desvabs means
%   that the series implies a large relative volatility.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
tic
if nargin < 2
    error('Requires at least two arguments.');
end

[m,n] = size (y);
if m < n
    y = y';     m = n;
end
d = 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;
B = spdiags(d, -2:0, m, m);    %I use a sparse version of B, because when m is large, B will have many zeros     
B = B+B';
s = By;

if nargin == 3
    t = size(y,2);
    for i = 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
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2023-4-18 16:37:47 | 显示全部楼层
感谢分享!!!
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表