How to code One-sided HP filter in Matlab

Dear All

I got my time series data y, c, g in excel file *.xlsx
I just want to use One-sided HP filter to filter the data from Matlab.
Can somebody guide me how to write the code in Matlab?

thank you very much

Hi yenyen,

there is a Matlab code by Alex Meyer-Gohde: Matlab code for one-sided HP-filters


Is there any built-in function available in matlab for this procedure?

thanks very much

so, in order to activate this hp filter function, we need to extract the file & place this file one_sided_hp_filter_kalman.m into the matlab folder.
am I right?

Yes, exactly. And then you can use it like any other Matlab function you wrote.

thank you so much

sorry to ask you again. this is regarding one-sided hp filter file.
i downloaded one_sided_hp_filter_kalman.m and one_sided_hp_filter_serial.m
I paste them into the matlab toolbox hdlfilter folder.
It is the right way to do it?
Then I try out this filter on variable gdp data series in matlab
my code is:

but there was an error and didn’t work at all.
I don’t know how to do coding for this. Can you guide me?

Another question I want to ask what is the right procedure to hpfilter a time series data? Say i got gdp data (in level form).
First I seasonally adjusted it, then I perform hdfilter (1-sided), lastly I do log-difference. Is this the correct procedure to do data transformation?

thanks a lot

  1. What is the error message?

  2. You should

    1. seasonally adjust the data
    2. take the log of the data
    3. apply the one-sided HP-filter

    You should not take first difference. Either one uses growth rates or a filter, but not both.

Dear Prof.
Here are attached files together with error message.

Thank you very much for your time
MATLAB _error.pdf (8.8 KB)
gdp1.m (47 Bytes)
gdp1.mat (1.1 KB)

and the hpfilter one-sided files
one_sided_hp_filter_kalman.m (5.4 KB)
one_sided_hp_filter_serial.m (7.3 KB)

Dear Prof,

After the 3 steps (SA, log, hpfilter), we stop at the final step.
should we proceed to take log difference? then we demean the data series in the final step. I am not so sure after read so many posts in this forum regarding the data transformation.

thanks so much

Hi yenyen,

I do not really understand the problem at hand. The way you use these functions is to put them in your directory where you are working for easy use and then follow the steps that Johannes gave you. As GDP is probably already seasonally adjusted what you do is:


If you have a representative household you further could divide it by the population to get something like:


Then you are done, no further log-difference required. This of course depends on what kind of variable you have in your model that you want to match with the data. Hope this helps :slight_smile:

1 Like

Thank you so much. I got it done