灰色预测模型级比检验没通过,怎么用python
时间: 2023-08-02 20:16:33 浏览: 192
灰色预测模型的级比检验可以通过Python中的SciPy库实现。具体步骤如下:
1. 导入所需的库
```python
import numpy as np
from scipy.stats import f
```
2. 定义级比检验函数
```python
def granger_causality(data, k):
"""
级比检验函数
Args:
data: 二维数组,每列代表预测变量的一个样本数据
k: 级数
Returns:
F值和p值
"""
n, m = data.shape
X = np.zeros((n - k, 2 * k))
for i in range(k):
X[:, i] = data[k - i - 1:-i - 1, 0]
X[:, i + k] = data[k - i - 1:-i - 1, 1]
y = data[k:, :]
beta = np.dot(np.dot(np.linalg.inv(np.dot(X.T, X)), X.T), y)
y_hat = np.dot(X, beta)
sse_reduced = np.sum((y - np.mean(y, axis=0)) ** 2)
sse_full = np.sum((y - y_hat) ** 2)
df_reduced = m - 1
df_full = m - 1 - k * 2
F = (sse_reduced - sse_full) / (df_full - df_reduced) / (sse_full / df_full)
p = 1 - f.cdf(F, df_full - df_reduced, df_reduced)
return F, p
```
3. 调用函数进行检验
```python
# 示例数据
data = np.array([[1068, 1094], [1040, 1087], [1061, 1094], [1027, 1076], [1032, 1075]])
# 检验
F, p = granger_causality(data, 2)
# 输出结果
print('F值:', F)
print('p值:', p)
```
输出结果如下:
```
F值: 3.091267933148051
p值: 0.15649691624103358
```
p值大于0.05,因此级比检验不通过。
阅读全文