
如何高效爬取全球新闻网站 – 整合Scrapy、Selenium与Mediastack API实现自动化新闻采集
咱们今儿和大家聊的十种最常见的时间序列模型有:
咱们下面详细的聊聊这些方法,大家一定会有一个完整的认识。
ARMA 模型是时间序列分析中的经典模型,结合了自回归 (AR) 和移动平均 (MA) 模型。AR 部分表示时间序列当前值与其过去几个时刻值的线性关系,而 MA 部分表示时间序列当前值与过去几个时刻的误差项的线性组合。
推导:
ARMA 模型通常用于平稳时间序列的建模和预测,如股票价格、经济指标、气象数据的短期预测等。
我们使用 ARMA 模型预测股票市场数据。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.arima.model import ARIMA
# 生成示例数据:股票价格的时间序列
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=100)
data = np.cumsum(np.random.randn(100)) + 100 # 随机漫步序列
# 创建DataFrame
df = pd.DataFrame(data, index=dates, columns=['Stock Price'])
# 拟合ARMA模型 (p=2, q=2)
model = ARIMA(df['Stock Price'], order=(2, 0, 2))
arma_result = model.fit()
# 预测未来20个时间点
forecast = arma_result.get_forecast(steps=20)
forecast_index = pd.date_range(df.index[-1], periods=21, freq='D')[1:]
forecast_values = forecast.predicted_mean
# 可视化
plt.figure(figsize=(12, 6))
plt.plot(df.index, df['Stock Price'], label='Observed', color='blue')
plt.plot(forecast_index, forecast_values, label='Forecast', color='red', linestyle='--')
plt.fill_between(forecast_index,
forecast.conf_int().iloc[:, 0],
forecast.conf_int().iloc[:, 1],
color='pink', alpha=0.3)
plt.title('ARMA Model Forecast of Stock Price')
plt.xlabel('Date')
plt.ylabel('Stock Price')
plt.legend()
plt.grid(True)
plt.show()
整个代码生成一个随机漫步的股票价格序列,使用 ARMA 模型进行拟合并预测未来 20 天的股票价格。图中展示了实际的时间序列数据(蓝色)以及预测的未来值(红色虚线),同时预测区间的置信区间以粉色阴影表示。
ARIMA 模型是 ARMA 模型的扩展,适用于非平稳时间序列。ARIMA 模型通过差分操作使非平稳时间序列转化为平稳时间序列,再对平稳时间序列进行 ARMA 模型拟合。
ARIMA 模型的三个主要参数分别是:
其中,差分次数 是用来消除时间序列中的趋势成分,使其成为平稳序列。
推导:
差分操作:
应用 ARMA 模型:
对差分后的序列应用 ARMA 模型。
ARIMA 模型广泛用于经济、金融等领域的时间序列预测,如 GDP、通货膨胀率、失业率、股票价格等。特别适合处理有趋势但无明显季节性的时间序列。
我们将使用 ARIMA 模型预测一个包含趋势的时间序列数据。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.arima.model import ARIMA
# 生成示例数据:带有趋势的时间序列
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=200)
trend = np.linspace(10, 30, 200) # 线性趋势
data = trend + np.random.randn(200) * 2 # 叠加噪声
# 创建DataFrame
df = pd.DataFrame(data, index=dates, columns=['Value'])
# 拟合ARIMA模型 (p=2, d=1, q=2)
model = ARIMA(df['Value'], order=(2, 1, 2))
arima_result = model.fit()
# 预测未来30个时间点
forecast = arima_result.get_forecast(steps=30)
forecast_index = pd.date_range(df.index[-1], periods=31, freq='D')[1:]
forecast_values = forecast.predicted_mean
# 可视化
plt.figure(figsize=(12, 6))
plt.plot(df.index, df['Value'], label='Observed', color='blue')
plt.plot(forecast_index, forecast_values, label='Forecast', color='green', linestyle='--')
plt.fill_between(forecast_index,
forecast.conf_int().iloc[:, 0],
forecast.conf_int().iloc[:, 1],
color='lightgreen', alpha=0.3)
plt.title('ARIMA Model Forecast')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.grid(True)
plt.show()
使用 ARIMA 模型进行拟合和预测。预测结果用绿色虚线表示,预测的置信区间用浅绿色阴影表示。图中展示了过去的观测值(蓝色)和未来 30 天的预测值,展示了 ARIMA 模型对趋势的预测能力。
SARIMA 模型是 ARIMA 模型的扩展,用于处理具有季节性成分的时间序列。SARIMA 模型引入了季节性成分,通过增加季节性自回归(SAR)、季节性差分(I)和季节性移动平均(SMA)项来建模。
推导:
SARIMA 模型适用于具有季节性波动的时间序列数据,如月度销售数据、季节性气象数据等。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.statespace.sarimax import SARIMAX
# 生成示例数据:季节性时间序列
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=120, freq='M')
seasonal_component = 10 + 10 * np.sin(np.linspace(0, 3 * np.pi, 120))
data = seasonal_component + np.random.randn(120) * 2 # 叠加噪声
# 创建DataFrame
df = pd.DataFrame(data, index=dates, columns=['Value'])
# 拟合SARIMA模型 (p=1, d=1, q=1, P=1, D=1, Q=1, s=12)
model = SARIMAX(df['Value'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
sarima_result = model.fit()
# 预测未来12个月
forecast = sarima_result.get_forecast(steps=12)
forecast_index = pd.date_range(df.index[-1] + pd.DateOffset(months=1), periods=12, freq='M')
forecast_values = forecast.predicted_mean
# 可视化
plt.figure(figsize=(12, 6))
plt.plot(df.index, df['Value'], label='Observed', color='blue')
plt.plot(forecast_index, forecast_values, label='Forecast', color='orange', linestyle='--')
plt.fill_between(forecast_index,
forecast.conf_int().iloc[:, 0],
forecast.conf_int().iloc[:, 1],
color='#FFA07A', alpha=0.3) # 使用有效的颜色代码
plt.title('SARIMA Model Forecast')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.grid(True)
plt.show()
图中展示了一个具有季节性波动的时间序列数据(蓝色)和未来 12 个月的预测值(橙色虚线)。预测区间的置信区间用浅橙色阴影表示。SARIMA 模型能够有效捕捉时间序列中的季节性模式。
VAR 模型用于建模多个时间序列变量之间的相互依赖关系。与 ARMA 模型只对单一时间序列进行建模不同,VAR 模型能够处理多变量时间序列,捕捉它们之间的动态关系。
推导:
VAR 模型适用于多个经济、金融或社会时间序列变量的建模与预测,如宏观经济指标(GDP、通货膨胀率、失业率)之间的关系分析。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.api import VAR
# 生成示例数据:多变量时间序列
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=100)
data1 = np.cumsum(np.random.randn(100)) + 50
data2 = np.cumsum(np.random.randn(100)) + 30
data = pd.DataFrame({'Variable1': data1, 'Variable2': data2}, index=dates)
# 拟合VAR模型 (p=2)
model = VAR(data)
var_result = model.fit(2)
# 预测未来10个时间点
forecast = var_result.forecast(data.values[-2:], steps=10)
forecast_index = pd.date_range(dates[-1] + pd.DateOffset(days=1), periods=10)
forecast_df = pd.DataFrame(forecast, index=forecast_index, columns=data.columns)
# 可视化
plt.figure(figsize=(14, 7))
plt.plot(data.index, data['Variable1'], label='Variable1 (Observed)', color='blue')
plt.plot(data.index, data['Variable2'], label='Variable2 (Observed)', color='green')
plt.plot(forecast_df.index, forecast_df['Variable1'], label='Variable1 (Forecast)', color='orange', linestyle='--')
plt.plot(forecast_df.index, forecast_df['Variable2'], label='Variable2 (Forecast)', color='red', linestyle='--')
plt.title('VAR Model Forecast')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.grid(True)
plt.show()
图中展示了两个时间序列变量的观测数据(蓝色和绿色)以及未来 10 天的预测值(橙色和红色虚线)。VAR 模型能有效捕捉两个变量之间的动态关系。
GARCH 模型用于建模时间序列数据的条件异方差性,特别是金融时间序列数据的波动性。GARCH 模型扩展了 ARCH 模型,通过引入过去的方差来解释当前的方差。
推导:
GARCH 模型广泛用于金融时间序列数据,如股票收益率、汇率等,用于建模和预测波动性。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from arch import arch_model
# 生成示例数据:金融时间序列(收益率)
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=250)
returns = np.random.randn(250) * 0.02 # 生成随机收益率数据
# 创建DataFrame
df = pd.DataFrame(returns, index=dates, columns=['Returns'])
# 拟合GARCH模型 (p=1, q=1)
model = arch_model(df['Returns'], vol='Garch', p=1, q=1)
garch_result = model.fit()
# 预测未来10个时间点的波动性
forecast = garch_result.forecast(horizon=10)
forecast_index = pd.date_range(dates[-1] + pd.DateOffset(days=1), periods=10)
forecast_volatility = forecast.variance.values[-1, :]
# 可视化
plt.figure(figsize=(12, 6))
plt.plot(df.index, df['Returns']**2, label='Observed Variance', color='blue')
plt.plot(forecast_index, forecast_volatility, label='Forecasted Volatility', color='red', linestyle='--')
plt.title('GARCH Model Forecast')
plt.xlabel('Date')
plt.ylabel('Variance')
plt.legend()
plt.grid(True)
plt.show()
图中展示了实际的方差(蓝色)和未来 10 天的预测波动性(红色虚线)。GARCH 模型能有效捕捉时间序列中的波动性特征。
Prophet 是由 Facebook 开发的时间序列预测模型,专为处理具有强季节性、趋势变化以及缺失值和异常值的时间序列数据设计。它的核心思想是将时间序列数据分解为趋势、季节性和假期效应三个部分。
推导:
其中P 是季节周期, K是季节性频率的数量。
3. 假期效应:
Prophet 模型适用于各种具有强季节性和趋势性的数据,例如零售销售、网站流量、生产量等。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from prophet import Prophet # 使用 prophet 替代 fbprophet
# 生成示例数据:带有季节性和趋势的时间序列
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=365)
data = np.linspace(10, 50, 365) + 10 * np.sin(np.linspace(0, 2 * np.pi, 365)) + np.random.randn(365) * 5
# 创建DataFrame
df = pd.DataFrame({'ds': dates, 'y': data})
# 拟合Prophet模型
model = Prophet(yearly_seasonality=True)
model.fit(df)
# 预测未来30天
future = model.make_future_dataframe(periods=30)
forecast = model.predict(future)
# 可视化
fig = model.plot(forecast)
plt.title('Prophet Model Forecast')
plt.xlabel('Date')
plt.ylabel('Value')
plt.show()
图中展示了时间序列数据(黑色点)及其预测结果(蓝色线)。Prophet 模型能有效捕捉时间序列中的趋势和季节性成分,并进行未来的预测。
LSTM 是一种特殊类型的循环神经网络(RNN),用于捕捉时间序列数据中的长期依赖关系。LSTM 网络通过引入门控机制(输入门、遗忘门和输出门)来解决标准 RNN 中的梯度消失和爆炸问题。
LSTM 网络的核心公式如下:
LSTM 模型适用于序列预测任务,如股票价格预测、语音识别、自然语言处理等。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import LSTM, Dense
from sklearn.preprocessing import MinMaxScaler
# 生成示例数据:时间序列
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=100)
data = np.sin(np.linspace(0, 10, 100)) + np.random.randn(100) * 0.1
# 创建DataFrame
df = pd.DataFrame({'Date': dates, 'Value': data})
# 预处理数据
scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(df[['Value']])
X, y = [], []
for i in range(len(scaled_data) - 10):
X.append(scaled_data[i:i+10])
y.append(scaled_data[i+10])
X, y = np.array(X), np.array(y)
# 构建LSTM模型
model = Sequential()
model.add(LSTM(50, input_shape=(X.shape[1], X.shape[2])))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')
# 训练模型
model.fit(X, y, epochs=20, verbose=1)
# 预测
predicted = model.predict(X)
predicted = scaler.inverse_transform(predicted)
actual = scaler.inverse_transform(y.reshape(-1, 1))
# 可视化
plt.figure(figsize=(12, 6))
plt.plot(df['Date'][10:], actual, label='Actual', color='blue')
plt.plot(df['Date'][10:], predicted, label='Predicted', color='red', linestyle='--')
plt.title('LSTM Model Forecast')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.grid(True)
plt.show()
图中展示了 LSTM 模型的预测结果(红色虚线)与实际数据(蓝色)。LSTM 能够捕捉时间序列的长期依赖特征并进行准确预测。
GRU 是另一种改进的 RNN 结构,旨在克服标准 RNN 的梯度消失问题。GRU 相较于 LSTM 具有更简洁的结构,只使用了重置门和更新门来控制信息的流动。
GRU 模型适用于需要捕捉长期依赖关系的时间序列预测任务,如时间序列预测、自然语言处理等。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import GRU, Dense
from sklearn.preprocessing import MinMaxScaler
# 生成示例数据:时间序列
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=100)
data = np.sin(np.linspace(0, 10, 100)) + np.random.randn(100) * 0.1
# 创建DataFrame
df = pd.DataFrame({'Date': dates, 'Value': data})
# 预处理数据
scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(df[['Value']])
X, y = [], []
for i in range(len(scaled_data) - 10):
X.append(scaled_data[i:i+10])
y.append(scaled_data[i+10])
X, y = np.array(X), np.array(y)
# 构建GRU模型
model = Sequential()
model.add(GRU(50, input_shape=(X.shape[1], X.shape[2])))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')
# 训练模型
model.fit(X, y, epochs=20, verbose=1)
# 预测
predicted = model.predict(X)
predicted = scaler.inverse_transform(predicted)
actual = scaler.inverse_transform(y.reshape(-1, 1))
# 可视化
plt.figure(figsize=(12, 6))
plt.plot(df['Date'][10:], actual, label='Actual', color='blue')
plt.plot(df['Date'][10:], predicted, label='Predicted', color='red', linestyle='--')
plt.title('GRU Model Forecast')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.grid(True)
plt.show()
图中展示了 GRU 模型的预测结果(红色虚线)与实际数据(蓝色)。GRU 能够有效处理时间序列数据并进行预测。
BSTS 模型是基于贝叶斯框架的时间序列建模方法,它允许对时间序列数据中的趋势、季节性和假期效应进行建模。BSTS 模型结合了结构时间序列模型和贝叶斯推断方法,以提供灵活的建模能力。
推导:
BSTS 模型适用于具有复杂结构的时间序列数据,如业务销售数据、经济指标预测等。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import numpyro
from bsts import BSTS
import jax
import jax.numpy as jnp
# 确认可用设备数量
print(f"Number of available devices: {jax.local_device_count()}")
# 设置主机设备数量(根据实际情况调整)
numpyro.set_host_device_count(1) # 设置为实际可用的设备数量
# 生成示例数据
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=365)
data = np.linspace(10, 50, 365) + 10 * np.sin(np.linspace(0, 2 * np.pi, 365)) + np.random.randn(365) * 5
df = pd.DataFrame({'Date': dates, 'Value': data})
# 确保数据格式正确
values = np.asarray(df['Value'], dtype=np.float32)
# 初始化 BSTS 模型
model = BSTS(values)
# 拟合模型
model.fit(values)
# 预测未来30天
forecast = model.predict(steps=30)
# 生成未来日期
forecast_index = pd.date_range(dates[-1] + pd.DateOffset(days=1), periods=30)
forecast_values = forecast['mean'] # 根据实际返回值的结构调整
# 可视化
plt.figure(figsize=(12, 6))
plt.plot(df['Date'], df['Value'], label='Observed', color='blue')
plt.plot(forecast_index, forecast_values, label='Forecast', color='red', linestyle='--')
plt.title('BSTS Model Forecast')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.grid(True)
plt.show()
图中展示了时间序列数据(蓝色)及其预测结果(红色虚线)。BSTS 模型能够捕捉时间序列的复杂成分并进行预测。
Seq2Seq 模型是一种深度学习模型,用于处理序列到序列的任务,如机器翻译和时间序列预测。Seq2Seq 模型通常由一个编码器和一个解码器组成,其中编码器处理输入序列,解码器生成输出序列。
Seq2Seq 模型的核心公式包括编码器和解码器:
Seq2Seq 模型适用于需要进行序列转换的任务,如时间序列预测、自然语言处理等。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import LSTM, Dense
from sklearn.preprocessing import MinMaxScaler
# 生成示例数据:时间序列
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=100)
data = np.sin(np.linspace(0, 10, 100)) + np.random.randn(100) * 0.1
# 创建DataFrame
df = pd.DataFrame({'Date': dates, 'Value': data})
# 预处理数据
scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(df[['Value']])
X, y = [], []
for i in range(len(scaled_data) - 10):
X.append(scaled_data[i:i+10])
y.append(scaled_data[i+10])
X, y = np.array(X), np.array(y)
# 构建Seq2Seq模型
model = Sequential()
model.add(LSTM(50, input_shape=(X.shape[1], X.shape[2])))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')
# 训练模型
model.fit(X, y, epochs=20, verbose=1)
# 预测
predicted = model.predict(X)
predicted = scaler.inverse_transform(predicted)
actual = scaler.inverse_transform(y.reshape(-1, 1))
# 可视化
plt.figure(figsize=(12, 6))
plt.plot(df['Date'][10:], actual, label='Actual', color='blue')
plt.plot(df['Date'][10:], predicted, label='Predicted', color='red', linestyle='--')
plt.title('Seq2Seq Model Forecast')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.grid(True)
plt.show()
图中展示了 Seq2Seq 模型的预测结果(红色虚线)与实际数据(蓝色)。Seq2Seq 模型能有效进行时间序列的预测任务。