趋近智
平稳性是时间序列分析中的一项基本特性。验证此条件非常重要,因为许多统计模型,包括ARIMA,都假定底层时间序列数据表现出平稳性。评估平稳性的一种主要且直观的方法是可视化检查。虽然单独的可视化检查不具决定性,但绘制数据及其滚动统计量图通常能显示出明显的趋势或方差变化,这些都表明数据不平稳。
最基本的检查只是简单地绘制时间序列值随时间变化的图。请查找以下图示特征:
我们来看一个使用Python的例子。假设您的时间序列数据已加载到以时间为索引的Pandas Series或DataFrame中。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.graph_objects as go # 使用 Plotly 绘制交互式图表
# 示例数据:创建带有趋势的非平稳序列
dates = pd.date_range(start='2023-01-01', periods=50, freq='D')
data_nonstationary = np.linspace(10, 25, 50) + np.random.normal(0, 2, 50)
ts_nonstationary = pd.Series(data_nonstationary, index=dates)
# 示例数据:创建可能平稳的序列
data_stationary = np.random.normal(15, 3, 50)
ts_stationary = pd.Series(data_stationary, index=dates)
# --- 绘制非平稳数据 ---
fig_nonstationary = go.Figure()
fig_nonstationary.add_trace(go.Scatter(x=ts_nonstationary.index, y=ts_nonstationary.values,
mode='lines', name='时间序列', line=dict(color='#339af0'))) # blue
fig_nonstationary.update_layout(
title='具有上升趋势的时间序列',
xaxis_title='日期',
yaxis_title='值',
template='plotly_white',
height=350,
margin=dict(l=20, r=20, t=40, b=20)
)
# fig_nonstationary.show() # 在Jupyter Notebook中使用
# --- 绘制平稳数据 ---
fig_stationary = go.Figure()
fig_stationary.add_trace(go.Scatter(x=ts_stationary.index, y=ts_stationary.values,
mode='lines', name='时间序列', line=dict(color='#20c997'))) # teal
fig_stationary.update_layout(
title='可能平稳的时间序列',
xaxis_title='日期',
yaxis_title='值',
template='plotly_white',
height=350,
margin=dict(l=20, r=20, t=40, b=20)
)
# fig_stationary.show() # 在Jupyter Notebook中使用
运行以上代码(最好在能显示Plotly图表的环境中)会生成两张图。
此图中的明显上升斜率是由于趋势引起的不平稳性的一个强烈信号。
该序列似乎在一个固定水平(约15)附近波动,方差相对一致。它看起来更可能平稳,尽管还需要更多检查。
一种更具量化特性的可视化方法是绘制滚动均值和滚动标准差(或方差)。如果序列是平稳的,那么滚动均值和滚动标准差应随时间大致保持不变。这些滚动统计量出现显著的上升或下降趋势,或大幅波动,都强烈表明不平稳性。
Pandas提供了rolling()方法以方便地计算这些统计量。
# 定义滚动统计量的窗口大小(例如,7天)
window_size = 7
# 计算非平稳序列的滚动均值和标准差
rolling_mean_ns = ts_nonstationary.rolling(window=window_size).mean()
rolling_std_ns = ts_nonstationary.rolling(window=window_size).std()
# --- 绘制带有滚动统计量的非平稳数据 ---
fig_roll_ns = go.Figure()
fig_roll_ns.add_trace(go.Scatter(x=ts_nonstationary.index, y=ts_nonstationary.values,
mode='lines', name='原始序列', line=dict(color='#adb5bd', width=1))) # gray
fig_roll_ns.add_trace(go.Scatter(x=rolling_mean_ns.index, y=rolling_mean_ns.values,
mode='lines', name='滚动均值', line=dict(color='#f03e3e', width=2))) # red
fig_roll_ns.add_trace(go.Scatter(x=rolling_std_ns.index, y=rolling_std_ns.values,
mode='lines', name='滚动标准差', line=dict(color='#fd7e14', width=2))) # orange
fig_roll_ns.update_layout(
title='带有滚动统计量的非平稳序列(窗口=7)',
xaxis_title='日期',
yaxis_title='值',
template='plotly_white',
height=400,
legend_title_text='度量',
margin=dict(l=20, r=20, t=50, b=20)
)
# fig_roll_ns.show()
我们将其可视化给非平稳数据。
滚动均值明显呈现上升趋势,这证实了第一张图的视觉判断。滚动标准差在这里看起来相对恒定,但均值漂移足以将该序列归类为非平稳。对于平稳示例的类似图表,将显示滚动均值和滚动标准差都围绕着水平线波动。
可视化检查是一个有价值的起点,提供对数据行为的快速认识。然而,它也有局限性:
因此,尽管可视化检查有助于识别明显的非平稳性,但应始终辅以更严格的统计检验,例如增广迪基-福勒(ADF)检验,这些检验提供平稳性的量化衡量。我们将在下一节中介绍这些检验。
这部分内容有帮助吗?
© 2026 ApX Machine Learning用心打造