function [Trend,Cyclical] = hpfilter(Y,w)
%HPFILTER Hodrick-Prescott filter for trend and cyclical components
% Syntax:
% [Trend,Cyclical] = hpfilter(Y)
% [Trend,Cyclical] = hpfilter(Y,w)
% Description:
% Separate one or more time series into trend and cyclical components
% with the Hodrick-Prescott filter.
% Input Arguments:
% Y - Time series data. Y may be a vector or a matrix. If Y is a vector,
% it represents a single series. If Y is a T-by-numSeries matrix,
% it represents T observations of numSeries series, with
% observations across any row assumed to occur at the same time. The
% last observation of any series is assumed to be the most recent.
% Optional Input Argument:
% w - Smoothing parameter. The default is 1600, which is suggested
% in [1] for quarterly data. If w is 0, no smoothing occurs.
% As the smoothing parameter increases, the smoothed series
% approaches a straight line. If w is Inf, the series is detrended.
% Output Arguments:
% Trend - Trend component of Y, the same size as Y.
% Cyclical - Cyclical component of Y, the same size as Y.
% Notes:
% o The Hodrick-Prescott filter separates a time series into a trend
% component and a cyclical component such that
% Y = Trend + Cyclical
% The filter is equivalent to a cubic spline smoother, where the
% smoothed portion is in Trend.
% o [1] suggests values for the smoothing parameter that depend upon
% the periodicity of the data:
% Periodicity smoothing
% Yearly 100
% Quarterly 1600
% Monthly 14400
% Reference:
% [1] Hodrick, R. J., and E. C. Prescott. "Postwar U.S. Business Cycles:
% An Empirical Investigation." Journal of Money, Credit, and Banking.
% Vol. 29, 1997, pp. 1-16.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Iskander Karibzhanov
% Department of Economics
% University of Minnesota
% karib003@umn.edu
T = size(Y,1);
if nargin < 2; w = 1600; end
e = repmat([w -4*w 1+6*w -4*w w],T,1);
e([T+1 2*T-1 3*T+2 4*T]) = -2*w;
e([2*T+1 3*T]) = 1+w;
e([2*T+2 3*T-1]) = 1+5*w;
A = spdiags(e,-2:2,T,T);
Trend = A\Y;
Cyclical = Y-Trend;