Including NaN in function for HP-Filter

조회 수: 9 (최근 30일)
Philipp
Philipp 2013년 5월 24일
Hi there, my question is the following: I have a function file to calculate HP-Filtered series of some series y. The problem is the following: If the series y contains some NaN, then the HP-filter function returns a series only with NaN, i.e. up till now it can only produce HP-filtered series when there are no NaN in y. Can somebody adjust my function file such that it works with NaN?
Below the code:
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;
Thanks very much! Philipp
  댓글 수: 1
David Sanchez
David Sanchez 2013년 5월 24일
I tried with
y=str2array('[1 2 e 3]')
y =
1
2
NaN
3
lambda = .2;
and it seems to work, what's the problem?

댓글을 달려면 로그인하십시오.

채택된 답변

Philipp
Philipp 2013년 5월 24일
편집: Philipp 2013년 5월 24일
Hello David, maybe I have to be more precise: I run this line of command in order to check for my results:
lambda=1600;
h=log(countrydata.GR.WPU./countrydata.GR.GDPDEF);
x=hpfilter(h,lambda);
later I have everything in a loop which looks like this:
for jj=1:lc;
eval(['hpw.' Countries{1,jj} '.WPU=hpfilter(log(countrydata.' Countries{1,jj} '.' Var{1,2} './countrydata.' Countries{1,jj} '.' Var{1,9} '),lambda);']);
eval(['hpw.' Countries{1,jj} '.WPR=hpfilter(log(countrydata.' Countries{1,jj} '.' Var{1,3} './countrydata.' Countries{1,jj} '.' Var{1,9} '),lambda);']);
end
Now h contains some real values and some NaN, but when I execute hpfilter(h,lambda), then x is all NaN?!?
  댓글 수: 2
David Sanchez
David Sanchez 2013년 5월 24일
What's the error message you get?
Philipp
Philipp 2013년 5월 24일
I dont get an error but each observation is NaN. But what I would like to have is that only the ones that are NaN in h are also NaN in x. Therefor, as mentioned in another forum, the interpolation is not an appropriate solution.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by