XiaoMaColtAI/math-modeling-skill / dsh-plugin/math-modeling-agent/skills/math-modeling
math-modeling
Verified当用户要求数学建模、建模竞赛、建模分析、代码求解、结果可视化或生成数学建模论文时使用。支持完整三阶段流程、单独执行任一角色,以及在关键节点使用独立 Subagent 进行阶段内质检;默认只启用质检 Subagent,用户可明确选择额外协作。
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 10.5 KB |
| assets/01-优化算法说明.md | markdown | 79.1 KB |
| assets/02-预测类算法说明.md | markdown | 67.4 KB |
| assets/03-评价类算法说明.md | markdown | 81.6 KB |
| assets/04-图论与网络分析算法说明.md | markdown | 38.8 KB |
| assets/05-统计分析与数据处理算法说明.md | markdown | 60.7 KB |
| assets/06-综合类算法说明.md | markdown | 35.6 KB |
| assets/07-机器学习算法说明.md | markdown | 51.9 KB |
| assets/README.md | markdown | 9.1 KB |
| references/README.md | markdown | 2.1 KB |
| references/Subagent调度.md | markdown | 7.4 KB |
| references/交付与截止时间协议.md | markdown | 4.4 KB |
| references/算法索引.md | markdown | 1021 B |
| 使用指南.md | markdown | 3.4 KB |
assets/02-预测类算法说明.md
View on GitHub# 预测类算法说明文档
## 概述
预测类算法是数学建模中的重要算法类型,用于根据历史数据对未来趋势进行预测。预测问题广泛存在于经济预测、人口预测、需求预测、时间序列分析等领域。
---
## 1. 灰色预测模型 (Grey Prediction Model, GM)
### 算法介绍
灰色预测模型用于处理"小样本、贫信息"的不确定系统。最常用的是GM(1,1)模型,通过对原始数据进行累加生成(AGO)来弱化随机性。
**GM(1,1)模型**:
原始序列:$X^{(0)} = \{x^{(0)}(1), x^{(0)}(2), \ldots, x^{(0)}(n)\}$
一次累加生成:$X^{(1)} = \{x^{(1)}(1), x^{(1)}(2), \ldots, x^{(1)}(n)\}$
其中 $x^{(1)}(k) = \sum_{i=1}^{k} x^{(0)}(i)$
白化微分方程:$\frac{dx^{(1)}}{dt} + ax^{(1)} = b$
**参数估计**:
$$
\begin{bmatrix} a \\ b \end{bmatrix} = (B^T B)^{-1} B^T Y
$$
其中:
$$
B = \begin{bmatrix} -\frac{1}{2}[x^{(1)}(1) + x^{(1)}(2)] & 1 \\ -\frac{1}{2}[x^{(1)}(2) + x^{(1)}(3)] & 1 \\ \vdots & \vdots \\ -\frac{1}{2}[x^{(1)}(n-1) + x^{(1)}(n)] & 1 \end{bmatrix}, \quad Y = \begin{bmatrix} x^{(0)}(2) \\ x^{(0)}(3) \\ \vdots \\ x^{(0)}(n) \end{bmatrix}
$$
**预测公式**:
$$
\hat{x}^{(0)}(k+1) = (1 - e^{a})[x^{(0)}(1) - \frac{b}{a}]e^{-ak}, \quad k = 1, 2, \ldots
$$
### 适用范围
| 题型类型 | 典型问题 | 特征 |
|---------|---------|------|
| 经济预测 | GDP预测、财政收入预测 | 短序列、增长趋势 |
| 人口预测 | 人口增长预测 | 单调变化 |
| 能源预测 | 电力负荷预测 | 非负数据 |
| 环境预测 | 污染物浓度预测 | 小样本 |
| 交通预测 | 交通流量预测 | 趋势明显 |
### 可视化图表类型
- **拟合曲线图**:实际值与预测值对比
- **残差图**:预测误差分布
- **相对误差图**:各点预测相对误差
- **置信区间图**:预测的置信区间
### 模型检验
| 检验类型 | 判定标准 | 说明 |
|---------|---------|------|
| 残差检验 | 平均相对误差 < 5% | 精度检验 |
| 关联度检验 | 关联度 > 0.6 | 趋势一致性 |
| 后验差检验 | C < 0.35, P > 0.95 | 方差比与小误差概率 |
### 关键文献
| 论文名称 | 作者 | 年份 | 来源 |
|---------|------|------|------|
| 灰色系统理论教程 | 邓聚龙 | 1990 | 华中理工大学出版社 |
| Grey Prediction Theory and Its Applications | 刘思峰 | 2010 | Springer |
### 代码实现要点
```python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import warnings
warnings.filterwarnings('ignore')
# 设置中文字体 - 多字体备选防止渲染问题
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans', 'Arial Unicode MS', 'Microsoft YaHei', 'Arial']
plt.rcParams['axes.unicode_minus'] = False
class GreyPredictionModel:
"""
GM(1,1)灰色预测模型实现
"""
def __init__(self, data):
self.data = np.array(data, dtype=float)
self.n = len(data)
self.a = None # 发展系数
self.b = None # 灰色作用量
self.fitted_values = None
self.residuals = None
def fit(self):
"""拟合GM(1,1)模型"""
# 一次累加生成(AGO)
x1 = np.cumsum(self.data)
# 构造数据矩阵B和向量Y
B = np.zeros((self.n - 1, 2))
Y = self.data[1:].reshape(-1, 1)
for i in range(self.n - 1):
B[i, 0] = -(x1[i] + x1[i + 1]) / 2 # 紧邻均值生成
B[i, 1] = 1
# 最小二乘估计参数 [a, b]^T
params = np.linalg.inv(B.T @ B) @ B.T @ Y
self.a = params[0, 0]
self.b = params[1, 0]
# 计算拟合值
self.fitted_values = self.predict(self.n)
self.residuals = self.data - self.fitted_values[:self.n]
return self
def predict(self, steps):
"""
预测未来steps个值
:param steps: 预测步数(包含历史数据的拟合)
:return: 预测序列
"""
if self.a is None:
raise ValueError("请先调用fit()方法拟合模型")
predictions = []
for k in range(steps):
if k == 0:
pred = self.data[0]
else:
# GM(1,1)预测公式
pred = (self.data[0] - self.b / self.a) * (1 - np.exp(self.a)) * np.exp(-self.a * k)
predictions.append(pred)
return np.array(predictions)
def calculate_metrics(self):
"""计算模型评估指标"""
if self.residuals is None:
raise ValueError("请先调用fit()方法拟合模型")
# 残差检验
relative_errors = np.abs(self.residuals) / self.data * 100
mean_relative_error = np.mean(relative_errors)
# 后验差检验
std_original = np.std(self.data, ddof=1)
std_residual = np.std(self.residuals, ddof=1)
C = std_residual / std_original # 方差比
# 小误差概率
S0 = 0.6745 * std_original
small_error_count = np.sum(np.abs(self.residuals - np.mean(self.residuals)) < S0)
P = small_error_count / self.n
# 关联度检验
rho = 0.5 # 分辨系数
min_diff = np.min(np.abs(self.data - self.fitted_values[:self.n]))
max_diff = np.max(np.abs(self.data - self.fitted_values[:self.n]))
xi = (min_diff + rho * max_diff) / (np.abs(self.data - self.fitted_values[:self.n]) + rho * max_diff)
R = np.mean(xi)
return {
'mean_relative_error': mean_relative_error,
'C': C,
'P': P,
'R': R,
'grade': self._grade_model(C, P)
}
def _grade_model(self, C, P):
"""根据后验差检验评定模型等级"""
if C < 0.35 and P > 0.95:
return '优秀'
elif C < 0.5 and P > 0.80:
return '良好'
elif C < 0.65 and P > 0.70:
return '合格'
else:
return '不合格'
def visualize_gm11_results(model, predict_steps=5):
"""可视化GM(1,1)模型结果"""
fig = plt.figure(figsize=(16, 12))
gs = fig.add_gridspec(3, 2, height_ratios=[1, 1, 1])
# 原始数据和拟合/预测数据
n = model.n
x_original = np.arange(n)
x_predict = np.arange(n + predict_steps)
fitted_and_predicted = model.predict(n + predict_steps)
# 图1:原始数据与预测对比
ax1 = fig.add_subplot(gs[0, :])
ax1.plot(x_original, model.data, 'bo-', linewidth=2, markersize=8, label='原始数据')
ax1.plot(x_predict, fitted_and_predicted, 'r--', linewidth=2, markersize=6, label='拟合/预测值')
# 标记预测区域
if predict_steps > 0:
ax1.axvline(x=n-1, color='gray', linestyle=':', alpha=0.7, label='预测起点')
ax1.fill_between(x_predict[n-1:], fitted_and_predicted[n-1:], alpha=0.2, color='red', label='预测区间')
ax1.set_xlabel('时间', fontsize=12)
ax1.set_ylabel('数值', fontsize=12)
ax1.set_title('GM(1,1)灰色预测结果', fontsize=14)
ax1.legend()
# 图2:残差分析
ax2 = fig.add_subplot(gs[1, 0])
residuals = model.residuals
relative_errors = np.abs(residuals) / model.data * 100
ax2.bar(x_original, residuals, color='lightblue', edgecolor='black', alpha=0.7)
ax2.axhline(y=0, color='r', linestyle='--', linewidth=1)
ax2.set_xlabel('时间', fontsize=12)
ax2.set_ylabel('残差', fontsize=12)
ax2.set_title('残差分布', fontsize=14)
# 图3:相对误差
ax3 = fig.add_subplot(gs[1, 1])
ax3.bar(x_original, relative_errors, color='lightcoral', edgecolor='black', alpha=0.7)
ax3.axhline(y=5, color='r', linestyle='--', linewidth=1, label='5%阈值')
ax3.set_xlabel('时间', fontsize=12)
ax3.set_ylabel('相对误差 (%)', fontsize=12)
ax3.set_title('相对误差分析', fontsize=14)
ax3.legend()
# 图4:累加生成序列
ax4 = fig.add_subplot(gs[2, :])
x1 = np.cumsum(model.data)
# 计算GM(1,1)的解曲线
k_values = np.linspace(0, n + predict_steps - 1, 200)
x1_fitted = (model.data[0] - model.b / model.a) * np.exp(-model.a * k_values) + model.b / model.a
ax4.scatter(range(len(x1)), x1, c='blue', s=80, label='一次累加生成序列', zorder=5)
ax4.plot(k_values, x1_fitted, 'r-', linewidth=2, label='GM(1,1)模型曲线')
# 标记紧邻均值生成点
z1 = [(x1[i] + x1[i+1]) / 2 for i in range(len(x1) - 1)]
ax4.scatter(range(0.5, len(x1) - 0.5), z1, c='green', s=60, marker='s',
label='紧邻均值生成', zorder=4)
ax4.set_xlabel('时间', fontsize=12)
ax4.set_ylabel('累加值', fontsize=12)
ax4.set_title('一次累加生成序列与模型拟合', fontsize=14)
ax4.legend()
plt.tight_layout()
plt.savefig('gm11_prediction_results.png', dpi=150, bbox_inches='tight')
plt.show()
print("\n图表已保存为 'gm11_prediction_results.png'")
# 运行示例
if __name__ == "__main__":
print("=" * 60)
print("GM(1,1)灰色预测模型示例")
print("=" * 60)
# 示例数据:某产品销售额(单位:万元)
data = [2.874, 3.278, 3.337, 3.390, 3.679]
predict_steps = 3
print(f"\n原始数据: {data}")
print(f"数据长度: {len(data)}")
print(f"预测步数: {predict_steps}")
# 创建并拟合模型
model = GreyPredictionModel(data)
model.fit()
print(f"\n模型参数:")
print(f" 发展系数 a = {model.a:.6f}")
print(f" 灰色作用量 b = {model.b:.6f}")
# 计算评估指标
metrics = model.calculate_metrics()
print(f"\n模型评估:")
print(f" 平均相对误差: {metrics['mean_relative_error']:.4f}%")
print(f" 方差比 C = {metrics['C']:.4f}")
print(f" 小误差概率 P = {metrics['P']:.4f}")
print(f" 关联度 R = {metrics['R']:.4f}")
print(f" 模型等级: {metrics['grade']}")
# 预测
predictions = model.predict(len(data) + predict_steps)
print(f"\n预测结果:")
for i in range(len(data)):
print(f" 第{i+1}期 (拟合): {predictions[i]:.4f} (原始: {data[i]:.4f})")
for i in range(predict_steps):
print(f" 第{len(data)+i+1}期 (预测): {predictions[len(data)+i]:.4f}")
# 可视化
visualize_gm11_results(model, predict_steps)
```
---
## 2. 插值与拟合 (Interpolation and Fitting)
### 算法介绍
插值与拟合是数据处理的基础方法,用于构造近似函数来逼近给定数据点。
#### 2.1 多项式插值
**拉格朗日插值**:
$$
L_n(x) = \sum_{i=0}^{n} y_i \prod_{j \neq i} \frac{x - x_j}{x_i - x_j}
$$
**牛顿插值**:
$$
N_n(x) = f[x_0] + f[x_0, x_1](x-x_0) + \cdots + f[x_0, \ldots, x_n](x-x_0)\cdots(x-x_{n-1})
$$
#### 2.2 样条插值
**三次样条函数**:
在每个区间 $[x_i, x_{i+1}]$ 上:
$$
S_i(x) = a_i + b_i(x-x_i) + c_i(x-x_i)^2 + d_i(x-x_i)^3
$$
满足条件:
- 插值条件:$S_i(x_i) = y_i$, $S_i(x_{i+1}) = y_{i+1}$
- 连续性条件:一阶、二阶导数连续
- 边界条件:自然边界或固定边界
#### 2.3 最小二乘拟合
**线性最小二乘**:
$$
\min_{a,b} \sum_{i=1}^{n} [y_i - (ax_i + b)]^2
$$
**多项式拟合**:
$$
\min_{a_0,\ldots,a_m} \sum_{i=1}^{n} [y_i - \sum_{j=0}^{m} a_j x_i^j]^2
$$
**正则化拟合(岭回归)**:
$$
\min_{\beta} \|y - X\beta\|^2 + \lambda \|\beta\|^2
$$
### 适用范围
| 题型类型 | 典型问题 | 特征 |
|---------|---------|------|
| 数据补全 | 缺失值填充 | 已知部分点 |
| 曲线拟合 | 趋势分析 | 带噪声数据 |
| 函数逼近 | 数值计算 | 近似计算 |
| 数据平滑 | 信号处理 | 去除噪声 |
| 图像处理 | 图像缩放 | 像素插值 |
### 可视化图表类型
- **插值曲线图**:插值函数与原始数据点
- **误差分布图**:拟合误差的分布
- **残差图**:残差与自变量关系
- **置信带图**:拟合的置信区间
### 关键文献
| 论文名称 | 作者 | 年份 | 来源 |
|---------|------|------|------|
| A Practical Guide to Splines | Carl de Boor | 1978 | Springer |
| Numerical Analysis | Burden & Faires | 2010 | Brooks/Cole |
### 代码实现要点
```python
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d, CubicSpline, lagrange
from scipy.optimize import curve_fit
from sklearn.metrics import r2_score, mean_squared_error
import warnings
warnings.filterwarnings('ignore')
# 设置中文字体 - 多字体备选防止渲染问题
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans', 'Arial Unicode MS', 'Microsoft YaHei', 'Arial']
plt.rcParams['axes.unicode_minus'] = False
class InterpolationFitting:
"""
插值与拟合方法集合
"""
@staticmethod
def lagrange_interpolation(x, y, x_new):
"""拉格朗日插值"""
n = len(x)
y_new = np.zeros_like(x_new, dtype=float)
for i in range(n):
# 计算拉格朗日基函数
Li = np.ones_like(x_new, dtype=float)
for j in range(n):
if i != j:
Li *= (x_new - x[j]) / (x[i] - x[j])
y_new += y[i] * Li
return y_new
@staticmethod
def newton_interpolation(x, y, x_new):
"""牛顿插值"""
n = len(x)
# 计算差商表
divided_diff = np.zeros((n, n))
divided_diff[:, 0] = y
for j in range(1, n):
for i in range(n - j):
divided_diff[i, j] = (divided_diff[i + 1, j - 1] - divided_diff[i, j - 1]) / (x[i + j] - x[i])
# 计算插值
y_new = np.zeros_like(x_new, dtype=float)
for k in range(len(x_new)):
result = divided_diff[0, 0]
product = 1.0
for i in range(1, n):
product *= (x_new[k] - x[i - 1])
result += divided_diff[0, i] * product
y_new[k] = result
return y_new
@staticmethod
def cubic_spline_interpolation(x, y, x_new, bc_type='natural'):
"""三次样条插值"""
cs = CubicSpline(x, y, bc_type=bc_type)
return cs(x_new), cs
@staticmethod
def polynomial_fit(x, y, degree):
"""多项式最小二乘拟合"""
coeffs = np.polyfit(x, y, degree)
poly = np.poly1d(coeffs)
y_fit = poly(x)
# 计算拟合优度
r2 = r2_score(y, y_fit)
rmse = np.sqrt(mean_squared_error(y, y_fit))
return poly, coeffs, r2, rmse
@staticmethod
def nonlinear_fit(x, y, func, p0=None):
"""
非线性曲线拟合
:param func: 拟合函数,形式为 func(x, *params)
:param p0: 参数初始值
"""
params, covariance = curve_fit(func, x, y, p0=p0)
y_fit = func(x, *params)
r2 = r2_score(y, y_fit)
rmse = np.sqrt(mean_squared_error(y, y_fit))
return params, covariance, r2, rmse
def visualize_interpolation_fitting():
"""可视化插值与拟合结果"""
fig = plt.figure(figsize=(16, 12))
gs = fig.add_gridspec(2, 2)
methods = InterpolationFitting()
# ========== 示例1:插值方法对比 ==========
# 原始数据点(Runge函数)
x_data = np.linspace(-1, 1, 11)
y_data = 1 / (1 + 25 * x_data**2) # Runge函数
# 插值点
x_fine = np.linspace(-1, 1, 200)
y_true = 1 / (1 + 25 * x_fine**2)
ax1 = fig.add_subplot(gs[0, 0])
# 拉格朗日插值(高次多项式,会出现Runge现象)
try:
y_lagrange = methods.lagrange_interpolation(x_data, y_data, x_fine)
ax1.plot(x_fine, y_lagrange, 'b--', linewidth=1.5, label='拉格朗日插值 (10次)', alpha=0.7)
except:
pass
# 分段线性插值
f_linear = interp1d(x_data, y_data, kind='linear')
y_linear = f_linear(x_fine)
ax1.plot(x_fine, y_linear, 'g-', linewidth=2, label='分段线性插值')
# 三次样条插值
y_spline, _ = methods.cubic_spline_interpolation(x_data, y_data, x_fine)
ax1.plot(x_fine, y_spline, 'r-', linewidth=2, label='三次样条插值')
# 真实函数
ax1.plot(x_fine, y_true, 'k:', linewidth=2, label='真实函数')
# 数据点
ax1.scatter(x_data, y_data, c='black', s=80, zorder=5, label='数据点')
ax1.set_xlabel('x', fontsize=12)
ax1.set_ylabel('y', fontsize=12)
ax1.set_title('插值方法对比(Runge函数)', fontsize=14)
ax1.legend()
ax1.set_ylim(-0.5, 1.5)
# ========== 示例2:拟合方法对比 ==========
# 生成带噪声的数据
np.random.seed(42)
x_fit = np.linspace(0, 10, 50)
y_true_func = 2 * np.sin(x_fit) + 0.5 * x_fit
y_noisy = y_true_func + np.random.normal(0, 0.5, len(x_fit))
ax2 = fig.add_subplot(gs[0, 1])
# 线性拟合
poly1, _, r2_1, rmse_1 = methods.polynomial_fit(x_fit, y_noisy, 1)
y_poly1 = poly1(x_fit)
ax2.plot(x_fit, y_poly1, 'b-', linewidth=2, label=f'线性拟合 (R²={r2_1:.3f})')
# 二次拟合
poly2, _, r2_2, rmse_2 = methods.polynomial_fit(x_fit, y_noisy, 2)
y_poly2 = poly2(x_fit)
ax2.plot(x_fit, y_poly2, 'g-', linewidth=2, label=f'二次拟合 (R²={r2_2:.3f})')
# 五次拟合
poly5, _, r2_5, rmse_5 = methods.polynomial_fit(x_fit, y_noisy, 5)
y_poly5 = poly5(x_fit)
ax2.plot(x_fit, y_poly5, 'r-', linewidth=2, label=f'五次拟合 (R²={r2_5:.3f})')
# 数据点
ax2.scatter(x_fit, y_noisy, c='gray', alpha=0.5, s=30, label='数据点')
ax2.plot(x_fit, y_true_func, 'k:', linewidth=2, label='真实趋势')
ax2.set_xlabel('x', fontsize=12)
ax2.set_ylabel('y', fontsize=12)
ax2.set_title('多项式拟合对比', fontsize=14)
ax2.legend()
# ========== 示例3:非线性拟合 ==========
ax3 = fig.add_subplot(gs[1, 0])
# 指数增长数据
x_exp = np.linspace(0, 5, 30)
y_exp = 2 * np.exp(0.5 * x_exp) + np.random.normal(0, 0.5, len(x_exp))
# 定义指数函数
def exp_func(x, a, b):
return a * np.exp(b * x)
params, _, r2_exp, rmse_exp = methods.nonlinear_fit(x_exp, y_exp, exp_func, p0=[1, 1])
y_exp_fit = exp_func(x_exp, *params)
ax3.scatter(x_exp, y_exp, c='blue', s=50, alpha=0.6, label='数据点')
ax3.plot(x_exp, y_exp_fit, 'r-', linewidth=2,
label=f'指数拟合: y = {params[0]:.3f} * e^({params[1]:.3f}x)\nR² = {r2_exp:.4f}')
ax3.set_xlabel('x', fontsize=12)
ax3.set_ylabel('y', fontsize=12)
ax3.set_title('非线性拟合(指数函数)', fontsize=14)
ax3.legend()
# ========== 示例4:残差分析 ==========
ax4 = fig.add_subplot(gs[1, 1])
# 使用三次样条拟合的数据进行残差分析
y_spline_fit, cs = methods.cubic_spline_interpolation(x_data, y_data, x_data)
residuals = y_data - y_spline_fit
# 残差图
ax4.bar(range(len(residuals)), residuals, color='lightblue', edgecolor='black', alpha=0.7)
ax4.axhline(y=0, color='r', linestyle='--', linewidth=1)
ax4.set_xlabel('数据点索引', fontsize=12)
ax4.set_ylabel('残差', fontsize=12)
ax4.set_title('三次样条插值残差分析', fontsize=14)
# 添加统计信息
rmse = np.sqrt(np.mean(residuals**2))
max_error = np.max(np.abs(residuals))
ax4.text(0.95, 0.95, f'RMSE: {rmse:.6f}\nMax Error: {max_error:.6f}',
transform=ax4.transAxes, ha='right', va='top',
bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5),
fontsize=10)
plt.tight_layout()
plt.savefig('interpolation_fitting_results.png', dpi=150, bbox_inches='tight')
plt.show()
print("\n图表已保存为 'interpolation_fitting_results.png'")
# 运行示例
if __name__ == "__main__":
print("=" * 60)
print("插值与拟合方法示例")
print("=" * 60)
methods = InterpolationFitting()
# 示例1:插值
print("\n【示例1】插值方法")
x_data = np.array([0, 1, 2, 3, 4, 5])
y_data = np.array([0, 0.8, 0.9, 0.1, -0.8, -1.0])
x_new = np.array([2.5, 3.5])
print(f"原始数据点: x = {x_data}")
print(f" y = {y_data}")
print(f"插值点: x = {x_new}")
y_lagrange = methods.lagrange_interpolation(x_data, y_data, x_new)
print(f"拉格朗日插值结果: y = {y_lagrange}")
y_newton = methods.newton_interpolation(x_data, y_data, x_new)
print(f"牛顿插值结果: y = {y_newton}")
y_spline, _ = methods.cubic_spline_interpolation(x_data, y_data, x_new)
print(f"三次样条插值结果: y = {y_spline}")
# 示例2:拟合
print("\n【示例2】多项式拟合")
x_fit = np.linspace(0, 10, 20)
y_fit = 2 * x_fit**2 + 3 * x_fit + 1 + np.random.normal(0, 5, len(x_fit))
for degree in [1, 2, 3]:
poly, coeffs, r2, rmse = methods.polynomial_fit(x_fit, y_fit, degree)
print(f"\n{degree}次多项式拟合:")
print(f" 系数: {coeffs}")
print(f" R² = {r2:.4f}")
print(f" RMSE = {rmse:.4f}")
# 可视化
visualize_interpolation_fitting()
```
---
## 3. 线性回归 (Linear Regression)
### 算法介绍
线性回归用于建立自变量与因变量之间的线性关系模型。
#### 3.1 一元线性回归
**模型**:
$$
y = \beta_0 + \beta_1 x + \varepsilon
$$
**参数估计**(最小二乘):
$$
\hat{\beta}_1 = \frac{\sum_{i=1}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{n}(x_i - \bar{x})^2}
$$
$$
\hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x}
$$
#### 3.2 多元线性回归
**模型**:
$$
y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \cdots + \beta_p x_p + \varepsilon
$$
**矩阵形式**:
$$
Y = X\beta + \varepsilon
$$
**参数估计**:
$$
\hat{\beta} = (X^T X)^{-1} X^T Y
$$
#### 3.3 逐步回归
**前进法**:逐步引入重要变量
**后退法**:逐步剔除不重要变量
**逐步法**:结合前进和后退
**变量选择准则**:
- AIC准则:$AIC = n \ln(RSS/n) + 2p$
- BIC准则:$BIC = n \ln(RSS/n) + p \ln n$
- 调整$R^2$:$R^2_{adj} = 1 - \frac{(1-R^2)(n-1)}{n-p-1}$
### 适用范围
| 题型类型 | 典型问题 | 特征 |
|---------|---------|------|
| 经济预测 | GDP与投资关系 | 线性关系 |
| 市场分析 | 销量与广告投入 | 多因素影响 |
| 房价预测 | 房价与特征关系 | 多变量 |
| 医学研究 | 病情与指标关系 | 因果分析 |
| 生产预测 | 产量与要素投入 | 生产函数 |
### 可视化图表类型
- **散点图+回归线**:一元回归可视化
- **残差图**:残差与预测值关系
- **QQ图**:残差正态性检验
- **偏回归图**:各自变量的影响
- **置信区间图**:预测的置信区间
### 模型评估
| 评估指标 | 公式 | 说明 |
|---------|------|------|
| $R^2$ | $1 - \frac{RSS}{TSS}$ | 拟合优度 |
| 调整$R^2$ | $1 - \frac{(1-R^2)(n-1)}{n-p-1}$ | 考虑变量数 |
| RMSE | $\sqrt{\frac{1}{n}\sum_{i=1}^{n}(y_i - \hat{y}_i)^2}$ | 均方根误差 |
| MAE | $\frac{1}{n}\sum_{i=1}^{n}\|y_i - \hat{y}_i\|$ | 平均绝对误差 |
| MAPE | $\frac{100\%}{n}\sum_{i=1}^{n}\|\frac{y_i - \hat{y}_i}{y_i}\|$ | 平均绝对百分比误差 |
### 关键文献
| 论文名称 | 作者 | 年份 | 来源 |
|---------|------|------|------|
| Applied Linear Statistical Models | Kutner et al. | 2005 | McGraw-Hill |
| Regression Modeling Strategies | Harrell | 2015 | Springer |
### 代码实现要点
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.metrics import r2_score, mean_squared_error
from sklearn.model_selection import train_test_split
import statsmodels.api as sm
from scipy import stats
import warnings
warnings.filterwarnings('ignore')
# 设置中文字体 - 多字体备选防止渲染问题
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans', 'Arial Unicode MS', 'Microsoft YaHei', 'Arial']
plt.rcParams['axes.unicode_minus'] = False
class LinearRegressionAnalysis:
"""
线性回归分析类
"""
@staticmethod
def simple_linear_regression(x, y):
"""一元线性回归"""
x = np.array(x).reshape(-1, 1)
y = np.array(y)
model = LinearRegression()
model.fit(x, y)
y_pred = model.predict(x)
# 计算统计量
n = len(y)
r2 = r2_score(y, y_pred)
mse = mean_squared_error(y, y_pred)
rmse = np.sqrt(mse)
mae = np.mean(np.abs(y - y_pred))
# 计算调整R²
adj_r2 = 1 - (1 - r2) * (n - 1) / (n - 2)
return {
'model': model,
'intercept': model.intercept_,
'slope': model.coef_[0],
'r2': r2,
'adj_r2': adj_r2,
'mse': mse,
'rmse': rmse,
'mae': mae,
'predictions': y_pred
}
@staticmethod
def multiple_linear_regression(X, y, feature_names=None):
"""多元线性回归(使用statsmodels提供详细统计信息)"""
X = np.array(X)
y = np.array(y)
if X.ndim == 1:
X = X.reshape(-1, 1)
# 添加常数项
X_with_const = sm.add_constant(X)
# 拟合模型
model = sm.OLS(y, X_with_const).fit()
# 创建结果字典
results = {
'model': model,
'params': model.params,
'pvalues': model.pvalues,
'rsquared': model.rsquared,
'rsquared_adj': model.rsquared_adj,
'fvalue': model.fvalue,
'f_pvalue': model.f_pvalue,
'aic': model.aic,
'bic': model.bic,
'summary': model.summary(),
'residuals': model.resid,
'fittedvalues': model.fittedvalues
}
if feature_names is not None:
results['feature_names'] = feature_names
return results
@staticmethod
def ridge_regression(X, y, alpha=1.0):
"""岭回归"""
model = Ridge(alpha=alpha)
model.fit(X, y)
y_pred = model.predict(X)
return {
'model': model,
'coef': model.coef_,
'intercept': model.intercept_,
'r2': r2_score(y, y_pred),
'rmse': np.sqrt(mean_squared_error(y, y_pred))
}
@staticmethod
def lasso_regression(X, y, alpha=1.0):
"""Lasso回归"""
model = Lasso(alpha=alpha)
model.fit(X, y)
y_pred = model.predict(X)
return {
'model': model,
'coef': model.coef_,
'intercept': model.intercept_,
'r2': r2_score(y, y_pred),
'rmse': np.sqrt(mean_squared_error(y, y_pred))
}
def visualize_regression_analysis():
"""可视化回归分析结果"""
fig = plt.figure(figsize=(16, 12))
gs = fig.add_gridspec(3, 2, height_ratios=[1, 1, 1])
lr = LinearRegressionAnalysis()
# ========== 示例1:一元线性回归 ==========
np.random.seed(42)
x1 = np.linspace(0, 10, 50)
y1 = 2.5 * x1 + 5 + np.random.normal(0, 2, len(x1))
result1 = lr.simple_linear_regression(x1, y1)
y_pred1 = result1['predictions']
ax1 = fig.add_subplot(gs[0, 0])
ax1.scatter(x1, y1, c='blue', alpha=0.6, s=50, label='数据点')
ax1.plot(x1, y_pred1, 'r-', linewidth=2, label=f'回归线: y = {result1["slope"]:.3f}x + {result1["intercept"]:.3f}')
# 添加置信区间
residuals = y1 - y_pred1
std_error = np.std(residuals)
ax1.fill_between(x1, y_pred1 - 1.96*std_error, y_pred1 + 1.96*std_error,
alpha=0.2, color='red', label='95%置信区间')
ax1.set_xlabel('X', fontsize=12)
ax1.set_ylabel('Y', fontsize=12)
ax1.set_title(f'一元线性回归 (R² = {result1["r2"]:.4f})', fontsize=14)
ax1.legend()
# 添加统计信息
textstr = f'斜率: {result1["slope"]:.4f}\n截距: {result1["intercept"]:.4f}\nR²: {result1["r2"]:.4f}\nRMSE: {result1["rmse"]:.4f}'
ax1.text(0.05, 0.95, textstr, transform=ax1.transAxes, fontsize=10,
verticalalignment='top', bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))
# ========== 示例2:残差分析 ==========
ax2 = fig.add_subplot(gs[0, 1])
# 残差图
ax2.scatter(y_pred1, residuals, c='blue', alpha=0.6, s=50)
ax2.axhline(y=0, color='r', linestyle='--', linewidth=1)
ax2.set_xlabel('预测值', fontsize=12)
ax2.set_ylabel('残差', fontsize=12)
ax2.set_title('残差图', fontsize=14)
# ========== 示例3:多元线性回归 ==========
np.random.seed(42)
n_samples = 100
X_multi = np.random.randn(n_samples, 3)
y_multi = (3 * X_multi[:, 0] + 2 * X_multi[:, 1] - 1.5 * X_multi[:, 2] +
5 + np.random.normal(0, 1, n_samples))
feature_names = ['X₁', 'X₂', 'X₃']
result_multi = lr.multiple_linear_regression(X_multi, y_multi, feature_names)
ax3 = fig.add_subplot(gs[1, 0])
# 实际值 vs 预测值
ax3.scatter(result_multi['fittedvalues'], y_multi, c='green', alpha=0.6, s=50)
# 添加对角线
min_val = min(min(result_multi['fittedvalues']), min(y_multi))
max_val = max(max(result_multi['fittedvalues']), max(y_multi))
ax3.plot([min_val, max_val], [min_val, max_val], 'r--', linewidth=2, label='理想拟合线')
ax3.set_xlabel('预测值', fontsize=12)
ax3.set_ylabel('实际值', fontsize=12)
ax3.set_title(f'多元线性回归: 实际值 vs 预测值 (R² = {result_multi["rsquared"]:.4f})', fontsize=14)
ax3.legend()
# ========== 示例4:回归系数 ==========
ax4 = fig.add_subplot(gs[1, 1])
coefs = result_multi['params'][1:] # 排除截距
pvalues = result_multi['pvalues'][1:]
colors = ['green' if p < 0.05 else 'red' for p in pvalues]
bars = ax4.barh(feature_names, coefs, color=colors, edgecolor='black', alpha=0.7)
ax4.axvline(x=0, color='black', linestyle='-', linewidth=0.5)
ax4.set_xlabel('回归系数', fontsize=12)
ax4.set_ylabel('特征', fontsize=12)
ax4.set_title('多元线性回归系数(绿色=p<0.05)', fontsize=14)
# 添加数值标签
for bar, coef in zip(bars, coefs):
width = bar.get_width()
ax4.text(width, bar.get_y() + bar.get_height()/2,
f'{coef:.3f}', ha='left' if width > 0 else 'right',
va='center', fontsize=10)
# ========== 示例5:正则化对比 ==========
ax5 = fig.add_subplot(gs[2, :])
# 生成高维数据
np.random.seed(42)
n_samples, n_features = 50, 20
X_high = np.random.randn(n_samples, n_features)
true_coef = np.zeros(n_features)
true_coef[:5] = np.array([5, -3, 2, -1, 4]) # 只有前5个特征重要
y_high = X_high @ true_coef + np.random.normal(0, 1, n_samples)
# 普通线性回归
result_ols = lr.multiple_linear_regression(X_high, y_high)
coef_ols = result_ols['params'][1:]
# 岭回归
result_ridge = lr.ridge_regression(X_high, y_high, alpha=10.0)
coef_ridge = result_ridge['coef']
# Lasso回归
result_lasso = lr.lasso_regression(X_high, y_high, alpha=1.0)
coef_lasso = result_lasso['coef']
x_pos = np.arange(n_features)
width = 0.25
ax5.bar(x_pos - width, true_coef, width, label='真实系数', color='black', alpha=0.7)
ax5.bar(x_pos, coef_ols, width, label='普通最小二乘', color='blue', alpha=0.7)
ax5.bar(x_pos + width, coef_ridge, width, label='岭回归', color='green', alpha=0.7)
ax5.bar(x_pos + 2*width, coef_lasso, width, label='Lasso', color='red', alpha=0.7)
ax5.set_xlabel('特征索引', fontsize=12)
ax5.set_ylabel('系数值', fontsize=12)
ax5.set_title('正则化方法对比(OLS vs Ridge vs Lasso)', fontsize=14)
ax5.set_xticks(x_pos)
ax5.set_xticklabels([f'X{i+1}' for i in range(n_features)])
ax5.legend()
plt.tight_layout()
plt.savefig('linear_regression_analysis.png', dpi=150, bbox_inches='tight')
plt.show()
print("\n图表已保存为 'linear_regression_analysis.png'")
# 运行示例
if __name__ == "__main__":
print("=" * 60)
print("线性回归分析示例")
print("=" * 60)
lr = LinearRegressionAnalysis()
# 示例1:一元线性回归
print("\n【示例1】一元线性回归")
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([2.1, 4.0, 6.2, 7.8, 10.5, 12.1, 14.0, 16.2, 18.1, 20.0])
result = lr.simple_linear_regression(x, y)
print(f"回归方程: y = {result['slope']:.4f}x + {result['intercept']:.4f}")
print(f"R² = {result['r2']:.4f}")
print(f"调整R² = {result['adj_r2']:.4f}")
print(f"RMSE = {result['rmse']:.4f}")
# 示例2:多元线性回归
print("\n【示例2】多元线性回归")
np.random.seed(42)
X = np.random.randn(100, 3)
y = 3*X[:, 0] + 2*X[:, 1] - X[:, 2] + 5 + np.random.normal(0, 0.5, 100)
result_multi = lr.multiple_linear_regression(X, y, ['X1', 'X2', 'X3'])
print(f"R² = {result_multi['rsquared']:.4f}")
print(f"调整R² = {result_multi['rsquared_adj']:.4f}")
print(f"F统计量 = {result_multi['fvalue']:.4f}")
print(f"F检验p值 = {result_multi['f_pvalue']:.6f}")
print("\n回归系数:")
for i, (name, coef, pval) in enumerate(zip(['截距', 'X1', 'X2', 'X3'],
result_multi['params'],
result_multi['pvalues'])):
significance = "***" if pval < 0.001 else "**" if pval < 0.01 else "*" if pval < 0.05 else ""
print(f" {name}: {coef:.4f} (p={pval:.4f}) {significance}")
# 可视化
visualize_regression_analysis()
```
---
## 4. 神经网络 (Neural Networks)
### 算法介绍
神经网络是一种模仿生物神经系统的计算模型,通过多层神经元进行非线性映射。
#### 4.1 前馈神经网络(BP网络)
**前向传播**:
$$
h_j = f(\sum_{i} w_{ij} x_i + b_j)
$$
$$
y_k = g(\sum_{j} v_{jk} h_j + c_k)
$$
**反向传播(BP算法)**:
输出层误差:$\delta_k = (y_k - t_k) \cdot g'(a_k)$
隐层误差:$\delta_j = \sum_{k} \delta_k v_{jk} \cdot f'(a_j)$
**权重更新**:
$$
\Delta w_{ij} = -\eta \frac{\partial E}{\partial w_{ij}} = \eta \delta_j x_i
$$
#### 4.2 RBF神经网络
**径向基函数**(高斯核):
$$
\phi_j(x) = \exp\left(-\frac{\|x - c_j\|^2}{2\sigma_j^2}\right)
$$
**输出**:
$$
y = \sum_{j=1}^{m} w_j \phi_j(x)
$$
#### 4.3 LSTM网络(时间序列预测)
**遗忘门**:$f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + b_f)$
**输入门**:$i_t = \sigma(W_i \cdot [h_{t-1}, x_t] + b_i)$
**候选值**:$\tilde{C}_t = \tanh(W_C \cdot [h_{t-1}, x_t] + b_C)$
**更新状态**:$C_t = f_t \odot C_{t-1} + i_t \odot \tilde{C}_t$
**输出门**:$o_t = \sigma(W_o \cdot [h_{t-1}, x_t] + b_o)$
$$h_t = o_t \odot \tanh(C_t)$$
### 适用范围
| 题型类型 | 典型问题 | 特征 |
|---------|---------|------|
| 非线性预测 | 股票价格预测 | 复杂非线性 |
| 时间序列 | 电力负荷预测 | 时序依赖 |
| 图像预测 | 图像生成 | 高维数据 |
| 语音预测 | 语音合成 | 序列数据 |
| 多变量预测 | 多维时间序列 | 多输入多输出 |
### 可视化图表类型
- **网络结构图**:神经网络层次结构
- **损失曲线**:训练过程损失变化
- **预测对比图**:实际值与预测值
- **误差热力图**:预测误差分布
- **注意力权重图**:注意力机制的权重
### 关键文献
| 论文名称 | 作者 | 年份 | 来源 |
|---------|------|------|------|
| Learning representations by back-propagating errors | Rumelhart, Hinton & Williams | 1986 | Nature |
| Long Short-Term Memory | Hochreiter & Schmidhuber | 1997 | Neural Computation |
| Adam: A Method for Stochastic Optimization | Kingma & Ba | 2014 | ICLR |
### 代码实现要点
```python
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
# 设置中文字体 - 多字体备选防止渲染问题
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans', 'Arial Unicode MS', 'Microsoft YaHei', 'Arial']
plt.rcParams['axes.unicode_minus'] = False
# BP神经网络
def create_bpnn(input_dim, hidden_layers, output_dim):
"""创建BP神经网络"""
model = keras.Sequential()
# 输入层
model.add(layers.Input(shape=(input_dim,)))
# 隐藏层
for units in hidden_layers:
model.add(layers.Dense(units, activation='relu'))
model.add(layers.Dropout(0.2))
# 输出层
model.add(layers.Dense(output_dim, activation='linear'))
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
return model
# LSTM网络
def create_lstm(input_shape, lstm_units, output_dim):
"""创建LSTM网络"""
model = keras.Sequential([
layers.Input(shape=input_shape),
layers.LSTM(lstm_units, return_sequences=True),
layers.Dropout(0.2),
layers.LSTM(lstm_units // 2),
layers.Dropout(0.2),
layers.Dense(output_dim)
])
model.compile(optimizer='adam', loss='mse')
return model
# 训练和预测
def train_and_predict(model, X_train, y_train, X_test, epochs=100, batch_size=32):
"""训练和预测"""
history = model.fit(X_train, y_train,
epochs=epochs,
batch_size=batch_size,
validation_split=0.2,
verbose=0)
predictions = model.predict(X_test)
return history, predictions
```
---
## 5. 支持向量机 (Support Vector Machine, SVM)
### 算法介绍
支持向量机是一种强大的监督学习算法,可用于分类和回归。SVR(支持向量回归)用于预测问题。
#### 5.1 线性SVR
**原始问题**:
$$
\min_{w,b,\xi,\xi^*} \frac{1}{2}\|w\|^2 + C\sum_{i=1}^{n}(\xi_i + \xi_i^*)
$$
**约束条件**:
$$
\begin{cases}
y_i - w^T x_i - b \leq \varepsilon + \xi_i \\
w^T x_i + b - y_i \leq \varepsilon + \xi_i^* \\
\xi_i, \xi_i^* \geq 0
\end{cases}
$$
#### 5.2 非线性SVR(核方法)
**对偶问题**:
$$
\max_{\alpha, \alpha^*} -\frac{1}{2}\sum_{i,j} (\alpha_i - \alpha_i^*)(\alpha_j - \alpha_j^*)K(x_i, x_j) - \varepsilon\sum_{i}(\alpha_i + \alpha_i^*) + \sum_{i} y_i(\alpha_i - \alpha_i^*)
$$
**决策函数**:
$$
f(x) = \sum_{i=1}^{n} (\alpha_i - \alpha_i^*) K(x_i, x) + b
$$
**常用核函数**:
- 线性核:$K(x, x') = x^T x'$
- 多项式核:$K(x, x') = (x^T x' + c)^d$
- RBF核:$K(x, x') = \exp(-\gamma \|x - x'\|^2)$
- Sigmoid核:$K(x, x') = \tanh(\gamma x^T x' + r)$
### 适用范围
| 题型类型 | 典型问题 | 特征 |
|---------|---------|------|
| 回归预测 | 股价预测、房价预测 | 非线性关系 |
| 时间序列 | 负荷预测、流量预测 | 小样本 |
| 函数逼近 | 复杂函数拟合 | 高维空间 |
| 异常检测 | 故障诊断 | 单类分类 |
### 可视化图表类型
- **支持向量图**:显示支持向量点
- **ε-带图**:ε不敏感带
- **决策边界图**:分类边界(分类问题)
- **核函数效果图**:不同核函数效果对比
- **参数敏感性图**:C和ε参数影响
### 关键文献
| 论文名称 | 作者 | 年份 | 来源 |
|---------|------|------|------|
| Support Vector Networks | Cortes & Vapnik | 1995 | Machine Learning |
| A Tutorial on Support Vector Regression | Smola & Schölkopf | 2004 | Statistics and Computing |
### 代码实现要点
```python
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
import numpy as np
# SVR回归
def svr_predict(X_train, y_train, X_test, kernel='rbf', C=1.0, epsilon=0.1):
"""支持向量回归预测"""
# 数据标准化
scaler_X = StandardScaler()
scaler_y = StandardScaler()
X_train_scaled = scaler_X.fit_transform(X_train)
y_train_scaled = scaler_y.fit_transform(y_train.reshape(-1, 1)).ravel()
# 创建SVR模型
model = SVR(kernel=kernel, C=C, epsilon=epsilon, gamma='scale')
model.fit(X_train_scaled, y_train_scaled)
# 预测
X_test_scaled = scaler_X.transform(X_test)
y_pred_scaled = model.predict(X_test_scaled)
y_pred = scaler_y.inverse_transform(y_pred_scaled.reshape(-1, 1)).ravel()
return y_pred, model
# 参数调优
def svr_grid_search(X_train, y_train):
"""SVR参数网格搜索"""
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
param_grid = {
'kernel': ['rbf', 'linear', 'poly'],
'C': [0.1, 1, 10, 100],
'epsilon': [0.01, 0.1, 0.2],
'gamma': ['scale', 'auto', 0.001, 0.01, 0.1]
}
svr = SVR()
grid_search = GridSearchCV(svr, param_grid, cv=5, scoring='neg_mean_squared_error')
grid_search.fit(X_train_scaled, y_train)
return grid_search.best_params_, grid_search.best_score_
```
---
## 6. ARIMA模型
### 算法介绍
ARIMA(自回归差分移动平均)模型是经典的时间序列预测模型。
**ARIMA(p, d, q)模型**:
**AR(p)部分**:$X_t = c + \sum_{i=1}^{p} \phi_i X_{t-i} + \varepsilon_t$
**I(d)部分**:差分$d$次实现平稳
**MA(q)部分**:$X_t = \mu + \sum_{i=1}^{q} \theta_i \varepsilon_{t-i} + \varepsilon_t$
**完整形式**(差分后):
$$
(1 - \sum_{i=1}^{p} \phi_i L^i)(1 - L)^d X_t = (1 + \sum_{i=1}^{q} \theta_i L^i) \varepsilon_t
$$
其中$L$是滞后算子,$L X_t = X_{t-1}$。
### 适用范围
| 题型类型 | 典型问题 | 特征 |
|---------|---------|------|
| 时间序列 | 经济指标预测 | 单变量时序 |
| 销售预测 | 产品销量预测 | 趋势+季节性 |
| 金融预测 | 股价、利率预测 | 波动性 |
| 气象预测 | 气温、降雨量预测 | 周期性 |
### 可视化图表类型
- **时序图**:原始数据和预测值
- **ACF/PACF图**:自相关和偏自相关
- **残差图**:模型残差检验
- **预测区间图**:置信区间
### 关键文献
| 论文名称 | 作者 | 年份 | 来源 |
|---------|------|------|------|
| Time Series Analysis | Box & Jenkins | 1970 | Holden-Day |
### 代码实现要点
```python
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.tsa.stattools import adfuller
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
# 平稳性检验
def test_stationarity(timeseries):
"""ADF平稳性检验"""
result = adfuller(timeseries)
return {'ADF Statistic': result[0], 'p-value': result[1]}
# ARIMA建模
def arima_forecast(train_data, test_steps, order=(1,1,1)):
"""ARIMA预测"""
model = ARIMA(train_data, order=order)
fitted_model = model.fit()
forecast = fitted_model.forecast(steps=test_steps)
return forecast, fitted_model
# 自动选择阶数
def auto_arima(train_data, max_p=5, max_d=2, max_q=5):
"""自动ARIMA阶数选择"""
import itertools
import warnings
best_aic = np.inf
best_order = None
warnings.filterwarnings('ignore')
for p in range(max_p + 1):
for d in range(max_d + 1):
for q in range(max_q + 1):
try:
model = ARIMA(train_data, order=(p, d, q))
results = model.fit()
if results.aic < best_aic:
best_aic = results.aic
best_order = (p, d, q)
except:
continue
return best_order
```
---
## 7. 指数平滑法 (Exponential Smoothing)
### 算法介绍
指数平滑法通过对历史数据加权平均进行预测,近期数据权重更大。
#### 7.1 简单指数平滑
适用于无趋势、无季节性的数据。
$$
F_{t+1} = \alpha y_t + (1 - \alpha) F_t
$$
其中$\alpha \in (0, 1)$是平滑系数。
#### 7.2 Holt线性趋势法
适用于有趋势、无季节性的数据。
**水平方程**:$L_t = \alpha y_t + (1 - \alpha)(L_{t-1} + T_{t-1})$
**趋势方程**:$T_t = \beta(L_t - L_{t-1}) + (1 - \beta)T_{t-1}$
**预测**:$F_{t+m} = L_t + m T_t$
#### 7.3 Holt-Winters季节性方法
适用于有趋势和季节性的数据。
**水平方程**:$L_t = \alpha \frac{y_t}{S_{t-s}} + (1 - \alpha)(L_{t-1} + T_{t-1})$
**趋势方程**:$T_t = \beta(L_t - L_{t-1}) + (1 - \beta)T_{t-1}$
**季节方程**:$S_t = \gamma \frac{y_t}{L_t} + (1 - \gamma)S_{t-s}$
**预测**:$F_{t+m} = (L_t + m T_t) S_{t-s+m}$
### 适用范围
| 题型类型 | 典型问题 | 特征 |
|---------|---------|------|
| 短期预测 | 销售预测 | 短期趋势 |
| 库存预测 | 需求预测 | 平稳变化 |
| 季节性预测 | 季节性商品销售 | 季节波动 |
| 平稳序列 | 指标监测 | 无明显趋势 |
### 可视化图表类型
- **时序预测图**:历史数据+预测值
- **分解图**:趋势、季节、残差分解
- **误差分析图**:预测误差分析
### 关键文献
| 论文名称 | 作者 | 年份 | 来源 |
|---------|------|------|------|
| Forecasting: principles and practice | Hyndman & Athanasopoulos | 2018 | OTexts |
### 代码实现要点
```python
from statsmodels.tsa.holtwinters import SimpleExpSmoothing, Holt, ExponentialSmoothing
# 简单指数平滑
def simple_exponential_smoothing(data, alpha=0.2):
"""简单指数平滑"""
model = SimpleExpSmoothing(data)
fit = model.fit(smoothing_level=alpha, optimized=False)
forecast = fit.forecast(steps=1)
return forecast, fit
# Holt线性趋势法
def holt_linear_trend(data, alpha=0.3, beta=0.1):
"""Holt线性趋势法"""
model = Holt(data)
fit = model.fit(smoothing_level=alpha, smoothing_trend=beta, optimized=False)
forecast = fit.forecast(steps=5)
return forecast, fit
# Holt-Winters季节性方法
def holt_winters(data, seasonal_periods=12, trend='add', seasonal='add'):
"""Holt-Winters季节性方法"""
model = ExponentialSmoothing(data, trend=trend, seasonal=seasonal,
seasonal_periods=seasonal_periods)
fit = model.fit()
forecast = fit.forecast(steps=seasonal_periods)
return forecast, fit
```
---
## 8. Prophet预测模型
### 算法介绍
Prophet是Facebook开发的时间序列预测模型,适合具有季节性和节假日效应的商务时间序列。
> ⚠️ **维护状态提示**:Prophet 官方已停止积极维护,新项目建议优先使用 `statsmodels`(STL/ETS/ARIMA,本章 §6/§7)或 `chronos` 等现代时序模型;仅在已有 Prophet 依赖或题目明确要求时使用本节方案。模型公式与思想仍可参考学习。
**模型公式**:
$$
y(t) = g(t) + s(t) + h(t) + \varepsilon_t
$$
其中:
- $g(t)$:趋势项(线性或逻辑斯谛)
- $s(t)$:季节项(傅里叶级数)
- $h(t)$:节假日效应
- $\varepsilon_t$:误差项
**趋势项**(线性):$g(t) = (k + a_k \delta(t) \cdot t + (m + a_m \delta(t))$
**季节项**:$s(t) = \sum_{n=1}^{N} [a_n \cos(\frac{2\pi n t}{P}) + b_n \sin(\frac{2\pi n t}{P})]$
### 适用范围
| 题型类型 | 典型问题 | 特征 |
|---------|---------|------|
| 商务预测 | 销售量预测 | 季节性强 |
| 流量预测 | 网站访问量 | 节假日效应 |
| 用户增长 | 用户数预测 | 趋势+周期 |
| 库存预测 | 商品需求 | 多种影响因素 |
### 可视化图表类型
- **预测分解图**:趋势、季节、节假日分解
- **预测区间图**:不确定性区间
- **组件影响图**:各组件贡献
### 关键文献
| 论文名称 | 作者 | 年份 | 来源 |
|---------|------|------|------|
| Forecasting at scale | Taylor & Letham | 2018 | arXiv |
### 代码实现要点
```python
from prophet import Prophet
import pandas as pd
def prophet_forecast(df, periods=30, yearly_seasonality=True, weekly_seasonality=True):
"""
Prophet时间序列预测
:param df: DataFrame with columns 'ds' (datetime) and 'y' (value)
:param periods: 预测步数
:return: 预测结果
"""
# 创建模型
model = Prophet(
yearly_seasonality=yearly_seasonality,
weekly_seasonality=weekly_seasonality,
daily_seasonality=False,
seasonality_mode='additive'
)
# 添加自定义季节性
model.add_seasonality(name='monthly', period=30.5, fourier_order=5)
# 拟合模型
model.fit(df)
# 创建未来日期
future = model.make_future_dataframe(periods=periods)
# 预测
forecast = model.predict(future)
return forecast, model
```
---
## 9. XGBoost/LightGBM预测模型
### 算法介绍
XGBoost和LightGBM是两种高效的梯度提升决策树算法,广泛应用于各类预测问题。
#### 9.1 XGBoost (eXtreme Gradient Boosting)
**核心思想**:通过迭代添加弱学习器(决策树)来最小化目标函数。
**目标函数**:
$$
Obj(\Theta) = \sum_{i=1}^{n} L(y_i, \hat{y}_i) + \sum_{k=1}^{K} \Omega(f_k)
$$
其中:
- $L$:损失函数(如平方损失、逻辑损失)
- $\Omega$:正则化项(控制模型复杂度)
**二阶泰勒展开**:
$$
Obj^{(t)} \approx \sum_{i=1}^{n} [L(y_i, \hat{y}_i^{(t-1)}) + g_i f_t(x_i) + \frac{1}{2} h_i f_t^2(x_i)] + \Omega(f_t)
$$
其中 $g_i = \partial_{\hat{y}^{(t-1)}} L(y_i, \hat{y}^{(t-1)})$,$h_i = \partial_{\hat{y}^{(t-1)}}^2 L(y_i, \hat{y}^{(t-1)})$
**最优目标函数值**:
$$
\tilde{Obj}^{(t)} = -\frac{1}{2} \sum_{i=1}^{n} \frac{g_i^2}{h_i + \lambda} + \gamma T
$$
#### 9.2 LightGBM (Light Gradient Boosting Machine)
**核心改进**:
- **基于直方图的决策树**:将连续特征离散化为bins,加速训练
- **带深度限制的叶子增长(Leaf-wise)**:选择增益最大的叶子分裂
- **特征捆绑**:将互斥特征捆绑降低维度
- **单边梯度采样(GOSS)**:保留大梯度样本,随机采样小梯度样本
**目标函数**:
$$
Obj(\Theta) = \sum_{i=1}^{n} g_i f_t(x_i) + \frac{1}{2} \sum_{i=1}^{n} h_i f_t^2(x_i) + \Omega(f_t)
$$
### 适用范围
| 题型类型 | 典型问题 | 特征 |
|---------|---------|------|
| 表格数据预测 | 销量预测、用户行为 | 结构化数据 |
| 时间序列预测 | 股价、负荷预测 | 序列特征 |
| 分类预测 | 违约预测、疾病诊断 | 二分类/多分类 |
| 排序预测 | 搜索排名、推荐 | 排序任务 |
| 异常检测 | 欺诈检测 | 不平衡数据 |
### 可视化图表类型
- **特征重要性图**:特征贡献排序
- **SHAP值图**:特征对预测的影响
- **学习曲线**:训练/验证误差
- **残差图**:预测误差分析
- **树可视化**:决策树结构
### 关键文献
| 论文名称 | 作者 | 年份 | 来源 |
|---------|------|------|------|
| XGBoost: A scalable tree boosting system | Chen & Guestrin | 2016 | KDD |
| LightGBM: A highly efficient gradient boosting decision tree | Ke et al. | 2017 | NeurIPS |
### 代码实现要点
```python
import xgboost as xgb
import lightgbm as lgb
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
# 设置中文字体 - 多字体备选防止渲染问题
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans', 'Arial Unicode MS', 'Microsoft YaHei', 'Arial']
plt.rcParams['axes.unicode_minus'] = False
# XGBoost回归
def xgboost_regression(X_train, y_train, X_test, y_test=None,
params=None, n_estimators=100, early_stopping_rounds=10):
"""
XGBoost回归预测
:param X_train: 训练特征
:param y_train: 训练目标
:param X_test: 测试特征
:param y_test: 测试目标(用于早停)
:param params: 模型参数
:return: 模型, 预测值
"""
if params is None:
params = {
'objective': 'reg:squarederror',
'max_depth': 6,
'learning_rate': 0.1,
'subsample': 0.8,
'colsample_bytree': 0.8,
'random_state': 42
}
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test)
# 训练模型
evals_result = {}
model = xgb.train(params, dtrain, num_boost_round=n_estimators,
evals=[(dtrain, 'train')],
early_stopping_rounds=early_stopping_rounds,
evals_result=evals_result,
verbose_eval=False)
# 预测
y_pred = model.predict(dtest)
return model, y_pred, evals_result
# LightGBM回归
def lightgbm_regression(X_train, y_train, X_test, y_test=None,
params=None, n_estimators=100, early_stopping_rounds=10):
"""
LightGBM回归预测
:param X_train: 训练特征
:param y_train: 训练目标
:param X_test: 测试特征
:param y_test: 测试目标(用于早停)
:param params: 模型参数
:return: 模型, 预测值
"""
if params is None:
params = {
'objective': 'regression',
'max_depth': -1, # 不限制深度
'learning_rate': 0.1,
'feature_fraction': 0.8,
'bagging_fraction': 0.8,
'bagging_freq': 5,
'verbose': -1
}
# 创建数据集
train_data = lgb.Dataset(X_train, label=y_train)
valid_data = lgb.Dataset(X_test, label=y_test, reference=train_data) if y_test is not None else None
# 训练模型
evals_result = {}
model = lgb.train(params, train_data, num_boost_round=n_estimators,
valid_sets=[train_data] if valid_data is None else [train_data, valid_data],
valid_names=['train'] if valid_data is None else ['train', 'valid'],
callbacks=[lgb.early_stopping(stopping_rounds=early_stopping_rounds, verbose=False),
lgb.record_evaluation(evals_result)])
# 预测
y_pred = model.predict(X_test, num_iteration=model.best_iteration)
return model, y_pred, evals_result
# 绘制特征重要性
def plot_feature_importance(model, feature_names, model_type='xgboost', top_n=20):
"""绘制特征重要性"""
if model_type == 'xgboost':
importance = model.get_score(importance_type='weight')
importance = pd.DataFrame({'feature': list(importance.keys()),
'importance': list(importance.values())})
else: # lightgbm
importance = pd.DataFrame({'feature': feature_names,
'importance': model.feature_importance(importance_type='split')})
importance = importance.sort_values('importance', ascending=False).head(top_n)
plt.figure(figsize=(10, 6))
plt.barh(importance['feature'][::-1], importance['importance'][::-1])
plt.xlabel('Importance')
plt.ylabel('Feature')
plt.title(f'Feature Importance ({model_type})')
plt.tight_layout()
plt.show()
# 绘制学习曲线
def plot_learning_curve(evals_result, model_type='xgboost'):
"""绘制学习曲线"""
plt.figure(figsize=(10, 6))
if model_type == 'xgboost':
train_metric = evals_result['train']['rmse']
else: # lightgbm
train_metric = evals_result['train']['l2']
if 'valid' in evals_result:
valid_metric = evals_result['valid']['l2']
plt.plot(valid_metric, label='Validation')
plt.plot(train_metric, label='Training')
plt.xlabel('Iteration')
plt.ylabel('RMSE')
plt.title('Learning Curve')
plt.legend()
plt.show()
# 超参数调优示例
def xgboost_grid_search(X_train, y_train, X_val, y_val):
"""XGBoost网格搜索"""
from sklearn.model_selection import GridSearchCV
params = {
'max_depth': [3, 5, 7],
'learning_rate': [0.01, 0.1, 0.2],
'n_estimators': [100, 200, 500],
'subsample': [0.8, 0.9, 1.0]
}
xgb_model = xgb.XGBRegressor(objective='reg:squarederror', random_state=42)
grid_search = GridSearchCV(xgb_model, params, cv=3, scoring='neg_mean_squared_error', n_jobs=-1)
grid_search.fit(X_train, y_train, eval_set=[(X_val, y_val)], early_stopping_rounds=10, verbose=False)
return grid_search.best_params_, grid_search.best_score_
```
---
## 10. 时空预测模型
### 算法介绍
时空预测模型用于同时考虑时间和空间相关性的预测问题,如交通流量预测、空气质量预测、疫情传播等。
#### 10.1 时空数据特点
**时空相关性**:
- **时间依赖**:当前状态受历史状态影响
- **空间依赖**:当前位置受邻近位置影响
- **时空交互**:时间和空间相互影响
**时空数据表示**:
$$
X \in \mathbb{R}^{N \times T \times F}
$$
其中:$N$是空间节点数,$T$是时间步长,$F$是特征维度。
#### 10.2 经典时空预测模型
**ARIMA时空扩展**:STARIMA
$$
X_t = \sum_{k=1}^{p} \Phi_k X_{t-k} + \sum_{l=1}^{q} \Theta_l \epsilon_{t-l} + \epsilon_t
$$
**时空图卷积网络**:STGCN
结合图卷积网络(GCN)和时序卷积网络(TCN)。
**图卷积层**:
$$
H^{(l+1)} = \sigma(\tilde{D}^{-\frac{1}{2}}\tilde{A}\tilde{D}^{-\frac{1}{2}} H^{(l)} W^{(l)})
$$
其中$\tilde{A} = A + I$(加入自连接),$\tilde{D}_{ii} = \sum_j \tilde{A}_{ij}$
**时间卷积层**:使用1D卷积捕获时序模式。
#### 10.3 交通流量预测示例
**基于图神经网络的交通预测**:
1. **构建道路网络图**:$G = (V, E, W)$
- $V$:道路节点
- $E$:道路连接
- $W$:连接权重(距离、相似度等)
2. **时空注意力机制**:
$$
\alpha_{ij}^t = \frac{\exp(score(s_i^t, s_j^t))}{\sum_{k \in \mathcal{N}(i)} \exp(score(s_i^t, s_k^t))}
$$
3. **预测输出**:
$$
\hat{X}_{t+1:t+H} = \text{Decoder}(\text{Encoder}(X_{t-T:t}, G))
$$
#### 10.4 评价指标
**RMSE**(均方根误差):
$$
RMSE = \sqrt{\frac{1}{NTH} \sum_{n,t,h} (X_{n,t+h} - \hat{X}_{n,t+h})^2}
$$
**MAE**(平均绝对误差):
$$
MAE = \frac{1}{NTH} \sum_{n,t,h} |X_{n,t+h} - \hat{X}_{n,t+h}|
$$
**MAPE**(平均绝对百分比误差):
$$
MAPE = \frac{100\%}{NTH} \sum_{n,t,h} \left|\frac{X_{n,t+h} - \hat{X}_{n,t+h}}{X_{n,t+h}}\right|
$$
### 适用范围
| 题型类型 | 典型问题 | 特征 |
|---------|---------|------|
| 交通预测 | 道路流量、地铁客流 | 路网结构 |
| 环境预测 | 空气质量、温度场 | 空间扩散 |
| 疫情预测 | 疾病传播 | 区域传播 |
| 能源预测 | 区域电力负荷 | 电网结构 |
| 经济预测 | 区域经济指标 | 空间溢出 |
### 可视化图表类型
- **时空热力图**:预测值在时空上的分布
- **路网流量图**:道路网络上的预测流量
- **等值线图**:连续空间上的预测值
- **时间序列对比**:多位置预测对比
- **误差空间分布**:预测误差的空间分布
### 关键文献
| 论文名称 | 作者 | 年份 | 来源 |
|---------|------|------|------|
| Spatio-Temporal Graph Convolutional Networks | Yu et al. | 2018 | IJCAI |
| Diffusion Convolutional Recurrent Neural Network | Li et al. | 2018 | AAAI |
| Attention Based Spatial-Temporal Graph Convolutional Networks | Guo et al. | 2019 | CIKM |
### 代码实现要点
```python
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import networkx as nx
import matplotlib.pyplot as plt
# 构建时空图
def build_spatial_graph(adj_matrix, threshold=0.1):
"""
构建空间图
:param adj_matrix: 邻接矩阵或距离矩阵
:param threshold: 连接阈值
:return: NetworkX图
"""
G = nx.Graph()
n = adj_matrix.shape[0]
for i in range(n):
for j in range(i+1, n):
if adj_matrix[i, j] > threshold:
G.add_edge(i, j, weight=adj_matrix[i, j])
return G
def compute_normalized_laplacian(adj_matrix):
"""
计算归一化拉普拉斯矩阵
:param adj_matrix: 邻接矩阵
:return: 归一化拉普拉斯矩阵
"""
A = adj_matrix + np.eye(adj_matrix.shape[0]) # 自连接
D = np.diag(A.sum(axis=1))
D_inv_sqrt = np.linalg.inv(np.sqrt(D))
L = np.eye(A.shape[0]) - D_inv_sqrt @ A @ D_inv_sqrt
return torch.FloatTensor(L)
# 时空图卷积层
class STGCNBlock(nn.Module):
"""时空图卷积块"""
def __init__(self, in_channels, out_channels, num_nodes):
super().__init__()
self.temporal_conv = nn.Conv2d(in_channels, out_channels, kernel_size=(1, 3), padding=(0, 1))
self.graph_conv = GraphConv(out_channels, out_channels, num_nodes)
self.bn = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU()
def forward(self, x, laplacian):
# x: (batch, channels, time, nodes)
x = self.temporal_conv(x)
x = self.graph_conv(x, laplacian)
x = self.bn(x)
x = self.relu(x)
return x
class GraphConv(nn.Module):
"""图卷积层"""
def __init__(self, in_channels, out_channels, num_nodes):
super().__init__()
self.theta = nn.Parameter(torch.FloatTensor(in_channels, out_channels))
nn.init.xavier_uniform_(self.theta)
def forward(self, x, laplacian):
# x: (batch, channels, time, nodes)
batch, channels, time, nodes = x.shape
x = x.permute(0, 2, 3, 1) # (batch, time, nodes, channels)
x = torch.matmul(x, self.theta) # (batch, time, nodes, out_channels)
# 图卷积: x = L @ x
x = torch.matmul(laplacian, x) # (batch, time, nodes, out_channels)
x = x.permute(0, 3, 1, 2) # (batch, out_channels, time, nodes)
return x
# 简化的时空预测模型
class STGCNPredictor(nn.Module):
"""时空图卷积预测模型"""
def __init__(self, input_dim, hidden_dim, output_dim, num_nodes, seq_len, pred_len):
super().__init__()
self.seq_len = seq_len
self.pred_len = pred_len
self.stgcn1 = STGCNBlock(input_dim, hidden_dim, num_nodes)
self.stgcn2 = STGCNBlock(hidden_dim, hidden_dim, num_nodes)
self.stgcn3 = STGCNBlock(hidden_dim, output_dim, num_nodes)
# 输出层
self.fc = nn.Linear(seq_len, pred_len)
def forward(self, x, laplacian):
# x: (batch, features, time, nodes)
x = self.stgcn1(x, laplacian)
x = self.stgcn2(x, laplacian)
x = self.stgcn3(x, laplacian)
# 时间维度映射
batch, channels, time, nodes = x.shape
x = x.permute(0, 3, 2, 1) # (batch, nodes, time, channels)
x = x.reshape(batch * nodes, time, channels)
x = x.permute(0, 2, 1) # (batch*nodes, channels, time)
x = self.fc(x) # (batch*nodes, channels, pred_len)
x = x.permute(0, 2, 1) # (batch*nodes, pred_len, channels)
x = x.reshape(batch, nodes, self.pred_len, -1)
x = x.permute(0, 3, 2, 1) # (batch, channels, pred_len, nodes)
return x
def train_stgcn_model(X_train, y_train, adj_matrix, epochs=100, lr=0.001):
"""
训练时空图卷积网络
:param X_train: (samples, features, time, nodes)
:param y_train: (samples, features, pred_time, nodes)
:param adj_matrix: 邻接矩阵
:return: 训练好的模型
"""
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# 计算拉普拉斯矩阵
laplacian = compute_normalized_laplacian(adj_matrix).to(device)
# 转换为张量
X_train = torch.FloatTensor(X_train).to(device)
y_train = torch.FloatTensor(y_train).to(device)
# 初始化模型
model = STGCNPredictor(
input_dim=X_train.shape[1],
hidden_dim=64,
output_dim=y_train.shape[1],
num_nodes=X_train.shape[3],
seq_len=X_train.shape[2],
pred_len=y_train.shape[2]
).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
criterion = nn.MSELoss()
losses = []
for epoch in range(epochs):
model.train()
optimizer.zero_grad()
outputs = model(X_train, laplacian)
loss = criterion(outputs, y_train)
loss.backward()
optimizer.step()
losses.append(loss.item())
if (epoch + 1) % 10 == 0:
print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}')
return model, losses
# 绘制时空预测结果
def plot_spatiotemporal_prediction(true_values, pred_values, node_ids, times, title="时空预测结果"):
"""
绘制时空预测结果
:param true_values: 真实值 (time, nodes)
:param pred_values: 预测值 (time, nodes)
:param node_ids: 节点ID列表
:param times: 时间点列表
:param title: 图标题
"""
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
# 真实值热力图
im1 = axes[0].imshow(true_values.T, aspect='auto', cmap='YlOrRd', origin='lower')
axes[0].set_xlabel('Time Step')
axes[0].set_ylabel('Node')
axes[0].set_title('True Values')
axes[0].set_yticks(range(len(node_ids)))
axes[0].set_yticklabels(node_ids)
plt.colorbar(im1, ax=axes[0])
# 预测值热力图
im2 = axes[1].imshow(pred_values.T, aspect='auto', cmap='YlOrRd', origin='lower')
axes[1].set_xlabel('Time Step')
axes[1].set_ylabel('Node')
axes[1].set_title('Predicted Values')
axes[1].set_yticks(range(len(node_ids)))
axes[1].set_yticklabels(node_ids)
plt.colorbar(im2, ax=axes[1])
plt.suptitle(title)
plt.tight_layout()
plt.show()
def evaluate_st_model(y_true, y_pred):
"""
评估时空预测模型
:param y_true: 真实值 (samples, features, time, nodes)
:param y_pred: 预测值 (samples, features, time, nodes)
:return: 评估指标字典
"""
y_true = y_true.reshape(-1)
y_pred = y_pred.reshape(-1)
rmse = np.sqrt(np.mean((y_true - y_pred) ** 2))
mae = np.mean(np.abs(y_true - y_pred))
mape = np.mean(np.abs((y_true - y_pred) / (y_true + 1e-8))) * 100
return {
'RMSE': rmse,
'MAE': mae,
'MAPE': mape
}
```
---
## 预测算法选择指南
### 按数据规模选择
| 数据规模 | 推荐算法 | 说明 |
|---------|---------|------|
| 极小样本(<20) | 灰色预测、多项式拟合 | 小样本专用 |
| 小样本(20-100) | 灰色预测、SVR | 避免过拟合 |
| 中等样本(100-10000) | ARIMA、Prophet、回归 | 经典方法 |
| 大样本(>10000) | 神经网络、深度学习 | 充分学习 |
### 按数据特征选择
| 数据特征 | 推荐算法 |
|---------|---------|
| 单调增长/下降 | 灰色预测、趋势回归 |
| 季节性 + 趋势 | Holt-Winters、Prophet、SARIMA |
| 平稳时间序列 | ARIMA、指数平滑 |
| 非线性关系 | SVR、神经网络 |
| 多变量预测 | 多元回归、VAR、LSTM |
| 小样本 + 线性 | 线性回归 |
| 小样本 + 非线性 | SVR、RBF网络 |
### 按预测目标选择
| 预测目标 | 推荐算法 |
|---------|---------|
| 短期预测(<1周) | 指数平滑、ARIMA |
| 中期预测(1周-3月) | ARIMA、Prophet |
| 长期预测(>3月) | 灰色预测、神经网络 |
| 点预测 | 回归、SVR |
| 区间预测 | ARIMA、Prophet、贝叶斯方法 |
| 概率预测 | 贝叶斯方法、分位数回归 |
---
## 参考文献
1. 邓聚龙. (1990). *灰色系统理论教程*. 华中理工大学出版社.
2. Box, G. E., & Jenkins, G. M. (1970). *Time Series Analysis: Forecasting and Control*. Holden-Day.
3. Cortes, C., & Vapnik, V. (1995). Support-vector networks. *Machine Learning*, 20(3), 273-297.
4. Hochreiter, S., & Schmidhuber, J. (1997). Long short-term memory. *Neural Computation*, 9(8), 1735-1780.
5. Taylor, S. J., & Letham, B. (2018). Forecasting at scale. *The American Statistician*, 72(1), 37-45.
6. Hyndman, R. J., & Athanasopoulos, G. (2018). *Forecasting: principles and practice* (2nd ed.). OTexts.
7. Smola, A. J., & Schölkopf, B. (2004). A tutorial on support vector regression. *Statistics and Computing*, 14(3), 199-222.
8. Chen, T., & Guestrin, C. (2016). XGBoost: A scalable tree boosting system. *Proceedings of the 22nd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining*, 785-794.
9. Ke, G., Meng, Q., Finley, T., Wang, T., Chen, W., Ma, W., ... & Liu, T. Y. (2017). LightGBM: A highly efficient gradient boosting decision tree. *Advances in Neural Information Processing Systems*, 30.
10. Yu, B., Yin, H., & Zhu, Z. (2018). Spatio-temporal graph convolutional networks: A deep learning framework for traffic forecasting. *Proceedings of the 27th International Joint Conference on Artificial Intelligence*, 3634-3640.
11. Burden, R. L., & Faires, J. D. (2010). *Numerical Analysis* (9th ed.). Brooks/Cole.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |