Hello. I'm trying to implement the Gallego-Johnson method for building confidence intervals for the HP filter. The methodology is explained in this diagram:



I wrote the following code:

smpl full
genr gdp_d11 = pbip_d11
genr cycle = hpfilt(gdp_d11)
genr trend = gdp_d11-cycle
matrix A = {}
matrix A = A ~ {cycle}
scalar n = 1000
loop for counter=1..n --quiet
    genr auxvar1=trend+resample(cycle,22)
    genr auxvar2=auxvar1-hpfilt(auxvar1)
    genr auxvar3 = hpfilt(auxvar1)
    matrix A = A ~ {auxvar3}
endloop


mubgap=quantile(transp(A),0.95)
mlbgap=quantile(transp(A),0.05)

series ubgap=mubgap
series lbgap=mlbgap

genr ubgap=ubgap+cycle
genr lbgap=cycle-lbgap


But the outputs are wrong. I don't get what I'm doing wrong. I'd be grateful for some help.

Thanks!

PC