Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calculation of HP Filtered-Series with NaN observations

4 views
Skip to first unread message

Philipp

unread,
May 23, 2013, 12:06:23 PM5/23/13
to
My problem is the following:
I want to get the hp-filtered series using this function below. Yet my y-series contains some NaN observations and then the function returns only NaN. How to tackle this issue?

Thanks for responding!
Philipp

The function reads:

function [cycle,trend]=hpfilter(y,lambda)
long = size(y,1);
HP=[1+lambda -2*lambda lambda zeros(1,long-3);...
-2*lambda 1+5*lambda -4*lambda lambda zeros(1,long-4);...
zeros(long-4,long);...
zeros(1,long-4) lambda -4*lambda 1+5*lambda -2*lambda;...
zeros(1,long-3) lambda -2*lambda 1+lambda];
for i=3:long-2;
HP(i,i-2)=lambda;
HP(i,i-1)=-4*lambda;
HP(i,i)=1+6*lambda;
HP(i,i+1)=-4*lambda;
HP(i,i+2)=lambda;
end;
trend = HP\y;
cycle = y-HP\y;

dpb

unread,
May 23, 2013, 2:52:54 PM5/23/13
to
On 5/23/2013 11:06 AM, Philipp wrote:
> My problem is the following:
> I want to get the hp-filtered series using this function below. Yet my
> y-series contains some NaN observations and then the function returns
> only NaN. How to tackle this issue?
>
...

Probably need to interpolate to fill in missing values first, then
filter. Otherwise, what is supposed to happen there????

--

TideMan

unread,
May 23, 2013, 3:51:25 PM5/23/13
to
Below is an elegant little routine to interpolate over NaN's. It's from Rich Pawlowicz's t_tide package.
You may wish to note the location of the NaNs so that you can re-insert them later.
ibad=isnan(y);


function y=fixgaps(x);
% FIXGAPS Linearly interpolates gaps in a time series
% YOUT=FIXGAPS(YIN) linearly interpolates over NaN
% in the input time series (may be complex), but ignores
% trailing and leading NaN.
%

% R. Pawlowicz 6/Nov/99

y=x;

bd=isnan(x);
gd=find(~bd);

bd([1:(min(gd)-1) (max(gd)+1):end])=0;


y(bd)=interp1(gd,x(gd),find(bd));
0 new messages