知识点回顾:
- 假设检验基础知识
- 原假设与备择假设
- P值、统计量、显著水平、置信区间
- 白噪声
- 白噪声的定义
- 自相关性检验:ACF检验和Ljung-Box 检验
- 偏自相关性检验:PACF检验
- 平稳性
- 平稳性的定义
- 单位根ADF检验: 越小越平稳
- 季节性检验
- ACF检验
- 序列分解:趋势+季节性+残差
记忆口诀:p越小,落在置信区间外,越拒绝原假设。
时序部分需要铺垫的知识非常多,相信这次应该说清楚了假设检验相关的基础知识。
作业:自行构造数据集,来检查是否符合这个要求。
时序检验作业实现方案
1. 新建数据生成模块
import numpy as np
import pandas as pddef generate_time_series():# 生成白噪声序列white_noise = np.random.normal(size=300)# 生成非平稳序列(带趋势)trend = 0.1 * np.arange(300)nonstationary = trend + np.random.randn(300)# 生成季节性序列seasonal = 5 * np.sin(np.arange(300)*(2*np.pi/12))return pd.DataFrame({'white_noise': white_noise,'nonstationary': nonstationary,'seasonal': seasonal})
2.新增检验模块
from statsmodels.tsa.stattools import adfuller, acf, pacf
from statsmodels.tsa.seasonal import seasonal_decomposedef run_hypothesis_tests(series):# ADF平稳性检验adf_result = adfuller(series)print(f'ADF统计量: {adf_result[0]:.3f}, p值: {adf_result[1]:.3f}')# Ljung-Box白噪声检验lb_test = acf(series, nlags=20, qstat=True)print(f'Ljung-Box p值: {lb_test[2][-1]:.3f}')# 季节性分解decomposition = seasonal_decompose(series, period=12)return decomposition
3.可视化模块增强
import matplotlib.pyplot as pltdef plot_acf_pacf(series, lags=20):fig, (ax1, ax2) = plt.subplots(2,1)plot_acf(series, lags=lags, ax=ax1)plot_pacf(series, lags=lags, ax=ax2)plt.show()
执行命令
python -m src.models.train --task hypothesis_test
预期结果