MATLAB 实现基于季节性趋势分解(STL)的时间序列预测
本文介绍在 MATLAB 中实现基于季节性趋势分解(STL, Seasonal and Trend decomposition using Loess)进行时间序列预测的项目实例。该方法适用于具有明显季节性和趋势成分的时间序列(如电力负荷、气温、销售数据等),通过 STL 分解后对各成分分别建模,再组合实现预测。
项目目标
对时间序列进行 STL 分解: $$y_t = T_t + S_t + R_t$$ 其中:$T_t$ = 趋势项,$S_t$ = 季节项,$R_t$ = 余项(残差)
- 对趋势项和余项分别建立预测模型(如 ARIMA、线性回归、MLP 等)
- 利用已知的季节模式(周期重复)外推季节项
- 重构预测结果:$$\hat{y}{t+h} = \hat{T}{t+h} + \hat{S}{t+h} + \hat{R}{t+h}$$
注意:MATLAB 没有内置 STL 函数,但可通过 movmean + 自定义 Loess 或使用替代方案。为保持纯 MATLAB 实现,我们采用近似 STL 的经典三步滤波法(Cleveland et al. 方法简化版)。
推荐方案:使用 MATLAB 官方 stl 替代函数(自定义实现)
由于 R2023a 起 MATLAB 仍未提供 stl,我们将使用一个高效、开源的 MATLAB STL 实现。
步骤 1:定义 STL 函数
以下是一个简化版 STL(基于滑动平均 + 季节周期对齐),适合教学或快速原型:
function [trend, seasonal, remainder] = simple_stl(y, period, nIter)
% SIMPLE_STL: 简化版 STL 分解(非 Loess,但保留核心思想)
% 输入:
% y - 时间序列 (列向量)
% period - 季节周期(如 12 表示月度数据年周期)
% nIter - 内部迭代次数(默认 2)
% 输出:
% trend, seasonal, remainder
if nargin < 3, nIter = 2; end
n = length(y);
seasonal = zeros(n, 1);
trend = zeros(n, 1);
% 初始化
detrended = y;
for iter = 1:nIter
% Step 1: 提取季节项(按周期分组取中位数/均值)
seasonal = zeros(n, 1);
for i = 1:period
idx = i:period:n;
if ~isempty(idx)
val = detrended(idx);
seasonal(idx) = mean(val); % 可改用 median 更鲁棒
end
end
% 去除季节项
deseason = y - seasonal;
% Step 2: 提取趋势项(用移动平均平滑)
win = min(2*floor(period/2)+1, floor(n/5)); % 窗口大小
if mod(win,2)==0, win = win+1; end
trend = smoothdata(deseason, 'movmean', win);
% 更新去趋势序列用于下一轮季节提取
detrended = y - trend;
end
remainder = y - trend - seasonal;
end
对于高精度需求,建议使用完整 Loess 版本。
步骤 2:加载或生成时间序列数据
% 示例:合成带趋势 + 季节 + 噪声的数据
t = (1:200)';
season = 10 * sin(2*pi*t/12); % 周期=12(月度)
trend = 0.1 * t; % 线性增长趋势
noise = 2 * randn(size(t)); % 随机噪声
y = trend + season + noise + 50; % 基准值 50
% 可视化
figure;
plot(t, y, 'k'); xlabel('Time'); ylabel('Value');
title('Original Time Series with Trend + Seasonality');
步骤 3:执行 STL 分解
period = 12; % 假设年度季节性(月度数据)
[trend, seasonal, remainder] = simple_stl(y, period);
% 可视化分解结果
figure;
subplot(4,1,1); plot(y); title('Original'); grid on;
subplot(4,1,2); plot(trend); title('Trend'); grid on;
subplot(4,1,3); plot(seasonal); title('Seasonal'); grid on;
subplot(4,1,4); plot(remainder); title('Remainder'); grid on;

