function [tau, F] = myHPfilter(y,mu); %This function is an HP filter % Input: % y - a Tx1 data vector % mu - the smoothing parameter % (use: 1600 for quarterly data, 400 for annual?) % Output: % tau - sequence of taus which just are the trend T = size(y,1); coefs = [1 -4 6 -4 1]; % Frows = zeros(1,T); F = zeros(T,T+4); for i = 1:1:T F(i,i:1:i+4) = coefs; end F = F(:,3:T+2); %Do the endpoints by hand F(1,1:3) = [1 -2 1]; F(2,1:4) = [-2 5 -4 1]; F((T-1),(T-3):T) = [1 -4 5 -2]; F(T,(T-2):T) = [1 -2 1]; tau = inv(mu*F + eye(T,T))*y;