Dear Statalisters,
I post the code for One-sided HP filter which is particularly easy to implement using the stock tsfilter command.
The main idea is that HP filter is the smoother which employs all information t=1..T, i.e. f(y | T) while one-sided HP filter is filtering up to time t, i.e. f(y | t). I replicate the matlab code by Alexander Meyer-Gohde and see there is an identical result.
We just need to loop from time t=3 to T since we would loss the first two obs due to its lags. Note that, Stock and Watson (1999) defined the one-sided HP filter as follows: y(t) = tau(t) + epsilon(t); (1 - L)^2*tau(t) = eta(t).
There is an equivalent Kalman version of one-sided HP filter in Stata elsewhere in the forum, but it seems that the snippet code below is easy to read and apply. To use it just replace "series..." by your actual series name.
PS: I am not sure the code can produce the exact result of the one-sided HP filter as suggested in Stock and Watson (1999). But you can test with matlab code (serial version) by Alexander Meyer-Gohde and compare two results.
I post the code for One-sided HP filter which is particularly easy to implement using the stock tsfilter command.
The main idea is that HP filter is the smoother which employs all information t=1..T, i.e. f(y | T) while one-sided HP filter is filtering up to time t, i.e. f(y | t). I replicate the matlab code by Alexander Meyer-Gohde and see there is an identical result.
We just need to loop from time t=3 to T since we would loss the first two obs due to its lags. Note that, Stock and Watson (1999) defined the one-sided HP filter as follows: y(t) = tau(t) + epsilon(t); (1 - L)^2*tau(t) = eta(t).
There is an equivalent Kalman version of one-sided HP filter in Stata elsewhere in the forum, but it seems that the snippet code below is easy to read and apply. To use it just replace "series..." by your actual series name.
Code:
capture drop series_1scycle local start 3 local end _N forvalues time = `start'/`end' { qui tsfilter series_1scycle = series_original in 1/`time', smooth(`lambda') qui replace series_1scycle = series_original in `time' } generate series_1strend = series_original - series_1scycle
Comment