## Documentation Center |

This example shows how to apply seasonal filters to deseasonalize a time series (using a multiplicative decomposition). The time series is monthly international airline passenger counts from 1949 to 1960.

Load the airline data set.

load(fullfile(matlabroot,'examples','econ','Data_Airline.mat')) Y = Dataset.PSSG; N = length(Y); figure plot(Y) xlim([0,N]) set(gca,'XTick',[1:12:N]) set(gca,'XTickLabel',datestr(dates(1:12:N),10)) title('Airline Passenger Counts') hold on

The data shows an upward linear trend and a seasonal component with periodicity 12.

**Step 2. Detrend the data using a 13-term moving average.**

Before estimating the seasonal component, estimate and remove the linear trend. Apply a 13-term symmetric moving average, repeating the first and last observations six times to prevent data loss. Use weight 1/24 for the first and last terms in the moving average, and weight 1/12 for all interior terms.

Divide the original series by the smoothed series to detrend the data. Add the moving average trend estimate to the observed time series plot.

sW13 = [1/24;repmat(1/12,11,1);1/24]; Ys = conv(Y,sW13,'same'); Ys(1:6) = Ys(7); Ys(N-5:N) = Ys(N-6); xt = Y./Ys; h = plot(Ys,'r','LineWidth',2); legend(h,'13-Term Moving Average') hold off

**Step 3. Create seasonal indices.**

Create a cell array, `sidx`, to store the indices corresponding to each period. The data is monthly, with periodicity 12, so the first element of `sidx` is a vector with elements 1, 13, 25,...,133 (corresponding to January observations). The second element of `sidx` is a vector with elements 2, 14, 16,...,134 (corresponding to February observations). This is repeated for all 12 months.

s = 12; for i = 1:s sidx{i,1} = i:s:N; end sidx{1:2}

ans = 1 13 25 37 49 61 73 85 97 109 121 133 ans = 2 14 26 38 50 62 74 86 98 110 122 134

Using a cell array to store the indices allows for the possibility that each period does not occur the same number of times within the span of the observed series.

**Step 4. Apply an S(3,3) filter.**

Apply a 5-term
seasonal moving average to the detrended series `xt`. That is, apply a moving average to the January values (at indices 1, 13, 25,...,133), and then apply a moving average to the February series (at indices 2, 14, 26,...,134), and so on for the remaining months.

Use asymmetric weights at the ends of the moving average (using `conv2`). Put the smoothed values back into a single vector.

To center the seasonal component around one, estimate, and then divide by, a 13-term moving average of the estimated seasonal component.

% S3x3 seasonal filter % Symmetric weights sW3 = [1/9;2/9;1/3;2/9;1/9]; % Asymmetric weights for end of series aW3 = [.259 .407;.37 .407;.259 .185;.111 0]; % Apply filter to each month shat = NaN*Y; for i = 1:s Ns = length(sidx{i}); first = 1:4; last = Ns-3:Ns; dat = xt(sidx{i}); sd = conv(dat,sW3,'same'); sd(1:2) = conv2(dat(first),1,rot90(aW3,2),'valid'); sd(Ns-1:Ns) = conv2(dat(last),1,aW3,'valid'); shat(sidx{i}) = sd; end % 13-term moving average of filtered series sW13 = [1/24;repmat(1/12,11,1);1/24]; sb = conv(shat,sW13,'same'); sb(1:6) = sb(s+1:s+6); sb(N-5:N) = sb(N-s-5:N-s); % Center to get final estimate s33 = shat./sb; figure plot(s33) xlim([0,N]) set(gca,'XTick',[1:12:N]) set(gca,'XTickLabel',datestr(dates(1:12:N),10)) title('Estimated Seasonal Component')

Notice that the seasonal level changes over the range of the data. This illustrates the difference between an seasonal filter and a stable seasonal filter. A stable seasonal filter assumes that the seasonal level is constant over the range of the data.

**Step 5. Apply a 13-term Henderson filter.**

To get an improved estimate of the trend component, apply a 13-term Henderson filter to the seasonally adjusted series. The necessary symmetric and asymmetric weights are provided in the following code.

% Deseasonalize series dt = Y./s33; % Henderson filter weights sWH = [-0.019;-0.028;0;.066;.147;.214; .24;.214;.147;.066;0;-0.028;-0.019]; % Asymmetric weights for end of series aWH = [-.034 -.017 .045 .148 .279 .421; -.005 .051 .130 .215 .292 .353; .061 .135 .201 .241 .254 .244; .144 .205 .230 .216 .174 .120; .211 .233 .208 .149 .080 .012; .238 .210 .144 .068 .002 -.058; .213 .146 .066 .003 -.039 -.092; .147 .066 .004 -.025 -.042 0 ; .066 .003 -.020 -.016 0 0 ; .001 -.022 -.008 0 0 0 ; -.026 -.011 0 0 0 0 ; -.016 0 0 0 0 0 ]; % Apply 13-term Henderson filter first = 1:12; last = N-11:N; h13 = conv(dt,sWH,'same'); h13(N-5:end) = conv2(dt(last),1,aWH,'valid'); h13(1:6) = conv2(dt(first),1,rot90(aWH,2),'valid'); % New detrended series xt = Y./h13; figure plot(Y) xlim([0,N]) set(gca,'XTick',[1:12:N]) set(gca,'XTickLabel',datestr(dates(1:12:N),10)) title('Airline Passenger Counts') hold on h = plot(h13,'r','LineWidth',2); legend(h,'13-Term Henderson Filter') hold off

**Step 6. Apply an S(3,5) seasonal filter.**

To get an improved estimate of the seasonal component, apply a 7-term seasonal moving average to the newly detrended series. The symmetric and asymmetric weights are provided in the following code. Center the seasonal estimate to fluctuate around 1.

Deseasonalize the original series by dividing it by the centered seasonal estimate.

% S3x5 seasonal filter % Symmetric weights sW5 = [1/15;2/15;repmat(1/5,3,1);2/15;1/15]; % Asymmetric weights for end of series aW5 = [.150 .250 .293; .217 .250 .283; .217 .250 .283; .217 .183 .150; .133 .067 0; .067 0 0]; % Apply filter to each month shat = NaN*Y; for i = 1:s Ns = length(sidx{i}); first = 1:6; last = Ns-5:Ns; dat = xt(sidx{i}); sd = conv(dat,sW5,'same'); sd(1:3) = conv2(dat(first),1,rot90(aW5,2),'valid'); sd(Ns-2:Ns) = conv2(dat(last),1,aW5,'valid'); shat(sidx{i}) = sd; end % 13-term moving average of filtered series sW13 = [1/24;repmat(1/12,11,1);1/24]; sb = conv(shat,sW13,'same'); sb(1:6) = sb(s+1:s+6); sb(N-5:N) = sb(N-s-5:N-s); % Center to get final estimate s35 = shat./sb; % Deseasonalized series dt = Y./s35; figure plot(dt) xlim([0,N]) set(gca,'XTick',[1:12:N]) set(gca,'XTickLabel',datestr(dates(1:12:N),10)) title('Deseasonalized Airline Passenger Counts')

The deseasonalized series consists of the long-term trend and irregular components. With the seasonal component removed, it is easier to see turning points in the trend.

**Step 7. Plot the components and the original series.**

Compare the original series to a series reconstructed using the component estimates.

figure plot(Y,'Color',[.85,.85,.85],'LineWidth',4) xlim([0,N]) set(gca,'XTick',[1:12:N]) set(gca,'XTickLabel',datestr(dates(1:12:N),10)) title('Airline Passenger Counts') hold on plot(h13,'r','LineWidth',2) plot(h13.*s35,'k--','LineWidth',1.5) legend('Original Series','13-Term Henderson Filter',... 'Trend and Seasonal Components') hold off

**Step 8. Estimate the irregular component.**

Detrend and deseasonalize the original series. Plot the remaining estimate of the irregular component.

Irr = dt./h13; figure plot(Irr) xlim([0,N]) set(gca,'XTick',[1:12:N]) set(gca,'XTickLabel',datestr(dates(1:12:N),10)) title('Airline Passenger Counts Irregular Component')

You can optionally model the detrended and deseasonalized series using a stationary stochastic process model.

- Moving Average Trend Estimation
- Seasonal Adjustment Using a Stable Seasonal Filter
- Parametric Trend Estimation

Was this topic helpful?