作业:自行构造数据集,来检查是否符合这个要求。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import acf, pacf, adfuller, lbtest
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.seasonal import seasonal_decompose
# 设置中文显示
plt.rcParams["font.family"] = ["SimHei", "WenQuanYi Micro Hei", "Heiti TC"]
plt.rcParams["axes.unicode_minus"] = False # 解决负号显示问题
def generate_and_test_white_noise():
"""生成白噪声序列并进行检验"""
# 生成白噪声序列(均值0,方差1的正态分布)
np.random.seed(42)
white_noise = np.random.normal(loc=0, scale=1, size=1000)
white_noise_series = pd.Series(white_noise, name='白噪声序列')
# 白噪声检验:Ljung-Box检验
lb_test_result = lbtest(white_noise_series, nlags=10)
print("===== 白噪声序列检验 =====")
print(f"Ljung-Box检验P值列表:{lb_test_result.pvalues.round(4)}")
print(f"是否全部P值>0.05:{np.all(lb_test_result.pvalues > 0.05)}")
# 可视化ACF和PACF
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
plot_acf(white_noise_series, lags=30, ax=axes[0])
axes[0].set_title('白噪声ACF图')
plot_pacf(white_noise_series, lags=30, ax=axes[1])
axes[1].set_title('白噪声PACF图')
plt.tight_layout()
plt.savefig('white_noise_acf_pacf.png')
plt.close()
return white_noise_series
def generate_and_test_non_stationary():
"""生成非平稳序列并进行平稳性检验"""
# 生成随机游走序列(非平稳)
np.random.seed(42)
random_walk = np.cumsum(np.random.normal(loc=0, scale=1, size=1000))
random_walk_series = pd.Series(random_walk, name='随机游走序列')
# 平稳性检验:ADF单位根检验
adf_result = adfuller(random_walk_series)
print("\n===== 随机游走序列平稳性检验 =====")
print(f"ADF统计量:{adf_result[0]:.4f}")
print(f"P值:{adf_result[1]:.4f}")
print(f"1%临界值:{adf_result[4]['1%']:.4f}")
print(f"是否拒绝原假设(非平稳):{adf_result[1] < 0.05}")
# 可视化序列
plt.figure(figsize=(10, 4))
plt.plot(random_walk_series)
plt.title('随机游走序列(非平稳)')
plt.xlabel('时间')
plt.ylabel('值')
plt.tight_layout()
plt.savefig('random_walk_series.png')
plt.close()
return random_walk_series
def generate_and_test_seasonal():
"""生成季节性序列并进行季节性检验"""
# 生成带季节性的序列:趋势+季节性+白噪声
np.random.seed(42)
time = np.arange(0, 1000)
trend = 0.01 * time # 线性趋势
seasonality = 5 * np.sin(2 * np.pi * time / 12) # 周期为12的季节性
noise = np.random.normal(loc=0, scale=1, size=1000)
seasonal_series = pd.Series(trend + seasonality + noise, name='季节性序列')
# 季节性检验:ACF检验
acf_values = acf(seasonal_series, nlags=30)
print("\n===== 季节性序列检验 =====")
print(f"滞后12阶ACF值:{acf_values[12]:.4f}")
# 可视化ACF和序列
fig, axes = plt.subplots(2, 1, figsize=(10, 8))
plot_acf(seasonal_series, lags=30, ax=axes[0])
axes[0].set_title('季节性序列ACF图')
axes[1].plot(seasonal_series)
axes[1].set_title('季节性序列(趋势+周期+噪声)')
plt.tight_layout()
plt.savefig('seasonal_series.png')
plt.close()
# 序列分解
decomposition = seasonal_decompose(seasonal_series, model='additive', period=12)
trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid
# 可视化分解结果
fig, axes = plt.subplots(4, 1, figsize=(10, 10))
axes[0].plot(seasonal_series)
axes[0].set_title('原始序列')
axes[1].plot(trend)
axes[1].set_title('趋势成分')
axes[2].plot(seasonal)
axes[2].set_title('季节性成分')
axes[3].plot(residual)
axes[3].set_title('残差成分')
plt.tight_layout()
plt.savefig('seasonal_decomposition.png')
plt.close()
return seasonal_series
def main():
"""主函数:执行所有检验并汇总结果"""
print("===== 时间序列假设检验演示 =====")
# 1. 检验白噪声
white_noise = generate_and_test_white_noise()
# 2. 检验非平稳序列
non_stationary = generate_and_test_non_stationary()
# 3. 检验季节性序列
seasonal = generate_and_test_seasonal()
print("\n===== 假设检验结论 =====")
print("1. 白噪声序列:Ljung-Box检验P值全部>0.05,不拒绝‘白噪声’原假设")
print("2. 随机游走序列:ADF检验P值>0.05,不拒绝‘非平稳’原假设")
print("3. 季节性序列:ACF滞后12阶显著(接近1),存在季节性")
print("\n可视化图表已保存至当前目录:")
print("- white_noise_acf_pacf.png")
print("- random_walk_series.png")
print("- seasonal_series.png")
print("- seasonal_decomposition.png")
if __name__ == "__main__":
main()
737

被折叠的 条评论
为什么被折叠?



