capture program drop bpass program define bpass * cfb version 6.0 /* STATA COMMAND FOR BAND PASS FILTER: bpass X pl pu This is a Stata program that filters time series data using an approximation to the band pass filter as discussed in the paper "The Band Pass Filter" by Lawrence J. Christiano and Terry J. Fitzgerald (1999). Required Inputs: X - series of data (T x 1) pl - minimum period of oscillation of desired component pu - maximum period of oscillation of desired component (2<=pl= `pu' {di in red "Lower bound (pl) must be less than upper bound (pu)" exit} if `pl' < 2 {di in red "Lower bound (pl) must be greater than 2" exit} gen `t' = _n local T = _N mkmat `X', matrix(`X') /* This section removes the drift from a time series using the formula: drift = (x(T) - x(1)) / (T-1). */ if `undrift' == 1 { gen `tl' = `t'[_n-1] replace `tl' = 0 in 1 mkmat `tl', matrix(`tl') local drift = (`X'[`T']-`X'[1])/(`T'-1) matrix `Xun' = J(`T',1,0) local i = 1 while `i' <= `T' { matrix `Xun'[`i',1] = `X'[`i'] - (`tl'[`i']*`drift') local i = `i'+1 } } else matrix `Xun' = `X' /*Create the ideal B's, then construct the AA matrix*/ local T2=2*`T' local T1 = `T'-1 matrix `AA' = J(`T2',`T2',0) scalar `a' = 2*_pi/(`pu') scalar `b' = 2*_pi/(`pl') scalar `bnot' = (`b'-`a')/_pi scalar `bhat' = `bnot'/2 gen `B' = (sin(`t'*`b')-sin(`t'*`a'))/(_pi*`t') gen `Btemp' = `B'[_n-1] replace `Btemp' = `bnot' in 1 mkmat `Btemp', matrix(`B') matrix `Bprime' = `B'' local i = 1 while `i' <= `T' { matrix substitute `AA'[`i',`i']=`Bprime' matrix substitute `AA'[`i',`i']=`B' local i = `i'+1 } matrix `bhatu' = J(`T',1,0) matrix `bhatd' = J(`T',1,0) matrix `bhatu'[1,1] = `bhat' matrix `bhatd'[`T',1] = `bhat' local i = 2 while `i' <= `T' { local j = `i'-1 local k = `T'-`j' matrix `bhatu'[`i',1]=(`bhatu'[`j',1]-`B'[`j',1]) matrix `bhatd'[`k',1]=(`bhatu'[`j',1]-`B'[`j',1]) local i = `i'+1 } matrix `AA' = `AA'[1..`T',1..`T'] matrix substitute `AA'[1,1] = `bhatu' matrix substitute `AA'[1,`T'] = `bhatd' /*Filter data using AA matrix*/ matrix fX = `AA'*`Xun'} end