aigroup-econ-mcp 0.4.2__py3-none-any.whl → 1.4.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- .gitignore +253 -0
- PKG-INFO +710 -0
- README.md +672 -0
- __init__.py +14 -0
- aigroup_econ_mcp-1.4.3.dist-info/METADATA +710 -0
- aigroup_econ_mcp-1.4.3.dist-info/RECORD +92 -0
- aigroup_econ_mcp-1.4.3.dist-info/entry_points.txt +2 -0
- aigroup_econ_mcp-1.4.3.dist-info/licenses/LICENSE +21 -0
- cli.py +28 -0
- econometrics/README.md +18 -0
- econometrics/__init__.py +191 -0
- econometrics/advanced_methods/modern_computing_machine_learning/__init__.py +0 -0
- econometrics/basic_parametric_estimation/__init__.py +31 -0
- econometrics/basic_parametric_estimation/gmm/__init__.py +13 -0
- econometrics/basic_parametric_estimation/gmm/gmm_model.py +256 -0
- econometrics/basic_parametric_estimation/mle/__init__.py +13 -0
- econometrics/basic_parametric_estimation/mle/mle_model.py +241 -0
- econometrics/basic_parametric_estimation/ols/__init__.py +13 -0
- econometrics/basic_parametric_estimation/ols/ols_model.py +141 -0
- econometrics/causal_inference/causal_identification_strategy/__init__.py +0 -0
- econometrics/missing_data/missing_data_measurement_error/__init__.py +0 -0
- econometrics/model_specification_diagnostics_robust_inference/README.md +173 -0
- econometrics/model_specification_diagnostics_robust_inference/__init__.py +78 -0
- econometrics/model_specification_diagnostics_robust_inference/diagnostic_tests/__init__.py +20 -0
- econometrics/model_specification_diagnostics_robust_inference/diagnostic_tests/diagnostic_tests_model.py +149 -0
- econometrics/model_specification_diagnostics_robust_inference/generalized_least_squares/__init__.py +15 -0
- econometrics/model_specification_diagnostics_robust_inference/generalized_least_squares/gls_model.py +130 -0
- econometrics/model_specification_diagnostics_robust_inference/model_selection/__init__.py +18 -0
- econometrics/model_specification_diagnostics_robust_inference/model_selection/model_selection_model.py +286 -0
- econometrics/model_specification_diagnostics_robust_inference/regularization/__init__.py +15 -0
- econometrics/model_specification_diagnostics_robust_inference/regularization/regularization_model.py +177 -0
- econometrics/model_specification_diagnostics_robust_inference/robust_errors/__init__.py +15 -0
- econometrics/model_specification_diagnostics_robust_inference/robust_errors/robust_errors_model.py +122 -0
- econometrics/model_specification_diagnostics_robust_inference/simultaneous_equations/__init__.py +15 -0
- econometrics/model_specification_diagnostics_robust_inference/simultaneous_equations/simultaneous_equations_model.py +246 -0
- econometrics/model_specification_diagnostics_robust_inference/weighted_least_squares/__init__.py +15 -0
- econometrics/model_specification_diagnostics_robust_inference/weighted_least_squares/wls_model.py +127 -0
- econometrics/nonparametric/nonparametric_semiparametric_methods/__init__.py +0 -0
- econometrics/spatial_econometrics/spatial_econometrics_new/__init__.py +0 -0
- econometrics/specific_data_modeling/micro_discrete_limited_data/__init__.py +0 -0
- econometrics/specific_data_modeling/survival_duration_data/__init__.py +0 -0
- econometrics/specific_data_modeling/time_series_panel_data/__init__.py +143 -0
- econometrics/specific_data_modeling/time_series_panel_data/arima_model.py +104 -0
- econometrics/specific_data_modeling/time_series_panel_data/cointegration_vecm.py +334 -0
- econometrics/specific_data_modeling/time_series_panel_data/dynamic_panel_models.py +653 -0
- econometrics/specific_data_modeling/time_series_panel_data/exponential_smoothing.py +176 -0
- econometrics/specific_data_modeling/time_series_panel_data/garch_model.py +198 -0
- econometrics/specific_data_modeling/time_series_panel_data/panel_diagnostics.py +125 -0
- econometrics/specific_data_modeling/time_series_panel_data/panel_var.py +60 -0
- econometrics/specific_data_modeling/time_series_panel_data/structural_break_tests.py +87 -0
- econometrics/specific_data_modeling/time_series_panel_data/time_varying_parameter_models.py +106 -0
- econometrics/specific_data_modeling/time_series_panel_data/unit_root_tests.py +204 -0
- econometrics/specific_data_modeling/time_series_panel_data/var_svar_model.py +372 -0
- econometrics/statistical_inference/statistical_inference_techniques/__init__.py +0 -0
- econometrics/statistics/distribution_decomposition_methods/__init__.py +0 -0
- econometrics/tests/basic_parametric_estimation_tests/__init__.py +3 -0
- econometrics/tests/basic_parametric_estimation_tests/test_gmm.py +128 -0
- econometrics/tests/basic_parametric_estimation_tests/test_mle.py +127 -0
- econometrics/tests/basic_parametric_estimation_tests/test_ols.py +100 -0
- econometrics/tests/model_specification_diagnostics_tests/__init__.py +3 -0
- econometrics/tests/model_specification_diagnostics_tests/test_diagnostic_tests.py +86 -0
- econometrics/tests/model_specification_diagnostics_tests/test_robust_errors.py +89 -0
- econometrics/tests/specific_data_modeling_tests/__init__.py +3 -0
- econometrics/tests/specific_data_modeling_tests/test_arima.py +98 -0
- econometrics/tests/specific_data_modeling_tests/test_dynamic_panel.py +198 -0
- econometrics/tests/specific_data_modeling_tests/test_exponential_smoothing.py +105 -0
- econometrics/tests/specific_data_modeling_tests/test_garch.py +118 -0
- econometrics/tests/specific_data_modeling_tests/test_unit_root.py +156 -0
- econometrics/tests/specific_data_modeling_tests/test_var.py +124 -0
- prompts/__init__.py +0 -0
- prompts/analysis_guides.py +43 -0
- pyproject.toml +78 -0
- resources/MCP_MASTER_GUIDE.md +422 -0
- resources/MCP_TOOLS_DATA_FORMAT_GUIDE.md +185 -0
- resources/__init__.py +0 -0
- server.py +83 -0
- tools/README.md +88 -0
- tools/__init__.py +45 -0
- tools/data_loader.py +213 -0
- tools/decorators.py +38 -0
- tools/econometrics_adapter.py +286 -0
- tools/mcp_tool_groups/__init__.py +1 -0
- tools/mcp_tool_groups/basic_parametric_tools.py +173 -0
- tools/mcp_tool_groups/model_specification_tools.py +402 -0
- tools/mcp_tool_groups/time_series_tools.py +494 -0
- tools/mcp_tools_registry.py +114 -0
- tools/model_specification_adapter.py +369 -0
- tools/output_formatter.py +563 -0
- tools/time_series_panel_data_adapter.py +858 -0
- tools/time_series_panel_data_tools.py +65 -0
- aigroup_econ_mcp/__init__.py +0 -19
- aigroup_econ_mcp/cli.py +0 -82
- aigroup_econ_mcp/config.py +0 -561
- aigroup_econ_mcp/server.py +0 -452
- aigroup_econ_mcp/tools/__init__.py +0 -18
- aigroup_econ_mcp/tools/base.py +0 -470
- aigroup_econ_mcp/tools/cache.py +0 -533
- aigroup_econ_mcp/tools/data_loader.py +0 -171
- aigroup_econ_mcp/tools/file_parser.py +0 -829
- aigroup_econ_mcp/tools/machine_learning.py +0 -60
- aigroup_econ_mcp/tools/ml_ensemble.py +0 -210
- aigroup_econ_mcp/tools/ml_evaluation.py +0 -272
- aigroup_econ_mcp/tools/ml_models.py +0 -54
- aigroup_econ_mcp/tools/ml_regularization.py +0 -172
- aigroup_econ_mcp/tools/monitoring.py +0 -555
- aigroup_econ_mcp/tools/optimized_example.py +0 -229
- aigroup_econ_mcp/tools/panel_data.py +0 -553
- aigroup_econ_mcp/tools/regression.py +0 -214
- aigroup_econ_mcp/tools/statistics.py +0 -154
- aigroup_econ_mcp/tools/time_series.py +0 -667
- aigroup_econ_mcp/tools/timeout.py +0 -283
- aigroup_econ_mcp/tools/tool_handlers.py +0 -378
- aigroup_econ_mcp/tools/tool_registry.py +0 -170
- aigroup_econ_mcp/tools/validation.py +0 -482
- aigroup_econ_mcp-0.4.2.dist-info/METADATA +0 -360
- aigroup_econ_mcp-0.4.2.dist-info/RECORD +0 -29
- aigroup_econ_mcp-0.4.2.dist-info/entry_points.txt +0 -2
- /aigroup_econ_mcp-0.4.2.dist-info/licenses/LICENSE → /LICENSE +0 -0
- {aigroup_econ_mcp-0.4.2.dist-info → aigroup_econ_mcp-1.4.3.dist-info}/WHEEL +0 -0
econometrics/model_specification_diagnostics_robust_inference/weighted_least_squares/wls_model.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""
|
|
2
|
+
加权最小二乘法 (Weighted Least Squares, WLS) 模型实现
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import List, Dict, Any, Optional
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from pydantic import BaseModel, Field
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pandas as pd
|
|
10
|
+
from scipy import stats
|
|
11
|
+
import statsmodels.api as sm
|
|
12
|
+
|
|
13
|
+
from tools.decorators import with_file_support_decorator as econometric_tool, validate_input
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class WLSResult(BaseModel):
|
|
17
|
+
"""WLS回归结果"""
|
|
18
|
+
coefficients: List[float] = Field(..., description="回归系数")
|
|
19
|
+
std_errors: List[float] = Field(..., description="系数标准误")
|
|
20
|
+
t_values: List[float] = Field(..., description="t统计量")
|
|
21
|
+
p_values: List[float] = Field(..., description="p值")
|
|
22
|
+
conf_int_lower: List[float] = Field(..., description="置信区间下界")
|
|
23
|
+
conf_int_upper: List[float] = Field(..., description="置信区间上界")
|
|
24
|
+
r_squared: float = Field(..., description="R方")
|
|
25
|
+
adj_r_squared: float = Field(..., description="调整R方")
|
|
26
|
+
f_statistic: float = Field(..., description="F统计量")
|
|
27
|
+
f_p_value: float = Field(..., description="F统计量p值")
|
|
28
|
+
n_obs: int = Field(..., description="观测数量")
|
|
29
|
+
feature_names: List[str] = Field(..., description="特征名称")
|
|
30
|
+
weights: List[float] = Field(..., description="使用的权重")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@econometric_tool("wls_regression")
|
|
34
|
+
@validate_input(data_type="econometric")
|
|
35
|
+
def wls_regression(
|
|
36
|
+
y_data: List[float],
|
|
37
|
+
x_data: List[List[float]],
|
|
38
|
+
weights: List[float],
|
|
39
|
+
feature_names: Optional[List[str]] = None,
|
|
40
|
+
constant: bool = True,
|
|
41
|
+
confidence_level: float = 0.95
|
|
42
|
+
) -> WLSResult:
|
|
43
|
+
"""
|
|
44
|
+
加权最小二乘法回归
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
y_data: 因变量数据
|
|
48
|
+
x_data: 自变量数据
|
|
49
|
+
weights: 权重列表(与观测值一一对应)
|
|
50
|
+
feature_names: 特征名称
|
|
51
|
+
constant: 是否包含常数项
|
|
52
|
+
confidence_level: 置信水平
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
WLSResult: WLS回归结果
|
|
56
|
+
"""
|
|
57
|
+
# 转换为numpy数组
|
|
58
|
+
y = np.asarray(y_data, dtype=np.float64)
|
|
59
|
+
X = np.asarray(x_data, dtype=np.float64)
|
|
60
|
+
w = np.asarray(weights, dtype=np.float64)
|
|
61
|
+
|
|
62
|
+
# 检查数据维度
|
|
63
|
+
if len(w) != len(y):
|
|
64
|
+
raise ValueError("权重数量必须与观测值数量相同")
|
|
65
|
+
|
|
66
|
+
# 检查权重是否为正数
|
|
67
|
+
if np.any(w <= 0):
|
|
68
|
+
raise ValueError("所有权重必须为正数")
|
|
69
|
+
|
|
70
|
+
# 添加常数项
|
|
71
|
+
if constant:
|
|
72
|
+
X = sm.add_constant(X)
|
|
73
|
+
if feature_names:
|
|
74
|
+
feature_names = ["const"] + feature_names
|
|
75
|
+
else:
|
|
76
|
+
feature_names = [f"x{i}" for i in range(X.shape[1])]
|
|
77
|
+
else:
|
|
78
|
+
if not feature_names:
|
|
79
|
+
feature_names = [f"x{i}" for i in range(X.shape[1])]
|
|
80
|
+
|
|
81
|
+
# 检查数据维度
|
|
82
|
+
n, k = X.shape
|
|
83
|
+
if n <= k:
|
|
84
|
+
raise ValueError(f"观测数量({n})必须大于变量数量({k})")
|
|
85
|
+
|
|
86
|
+
# 使用statsmodels执行WLS回归
|
|
87
|
+
try:
|
|
88
|
+
model = sm.WLS(y, X, weights=w)
|
|
89
|
+
results = model.fit()
|
|
90
|
+
except Exception as e:
|
|
91
|
+
raise ValueError(f"无法拟合WLS模型: {str(e)}")
|
|
92
|
+
|
|
93
|
+
# 提取结果
|
|
94
|
+
coefficients = results.params.tolist()
|
|
95
|
+
std_errors = results.bse.tolist()
|
|
96
|
+
t_values = results.tvalues.tolist()
|
|
97
|
+
p_values = results.pvalues.tolist()
|
|
98
|
+
|
|
99
|
+
# 计算置信区间
|
|
100
|
+
alpha = 1 - confidence_level
|
|
101
|
+
conf_int = results.conf_int(alpha=alpha)
|
|
102
|
+
conf_int_lower = conf_int[:, 0].tolist()
|
|
103
|
+
conf_int_upper = conf_int[:, 1].tolist()
|
|
104
|
+
|
|
105
|
+
# 其他统计量
|
|
106
|
+
r_squared = float(results.rsquared)
|
|
107
|
+
adj_r_squared = float(results.rsquared_adj)
|
|
108
|
+
|
|
109
|
+
# F统计量
|
|
110
|
+
f_statistic = float(results.fvalue) if not np.isnan(results.fvalue) else 0.0
|
|
111
|
+
f_p_value = float(results.f_pvalue) if not np.isnan(results.f_pvalue) else 1.0
|
|
112
|
+
|
|
113
|
+
return WLSResult(
|
|
114
|
+
coefficients=coefficients,
|
|
115
|
+
std_errors=std_errors,
|
|
116
|
+
t_values=t_values,
|
|
117
|
+
p_values=p_values,
|
|
118
|
+
conf_int_lower=conf_int_lower,
|
|
119
|
+
conf_int_upper=conf_int_upper,
|
|
120
|
+
r_squared=r_squared,
|
|
121
|
+
adj_r_squared=adj_r_squared,
|
|
122
|
+
f_statistic=f_statistic,
|
|
123
|
+
f_p_value=f_p_value,
|
|
124
|
+
n_obs=int(results.nobs),
|
|
125
|
+
feature_names=feature_names,
|
|
126
|
+
weights=weights
|
|
127
|
+
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""
|
|
2
|
+
时间序列与面板数据模块
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
# ARIMA模型
|
|
6
|
+
from .arima_model import (
|
|
7
|
+
ARIMAResult,
|
|
8
|
+
arima_model
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
# 指数平滑法
|
|
12
|
+
from .exponential_smoothing import (
|
|
13
|
+
ExponentialSmoothingResult,
|
|
14
|
+
exponential_smoothing_model
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
# VAR/SVAR模型
|
|
18
|
+
from .var_svar_model import (
|
|
19
|
+
VARResult,
|
|
20
|
+
var_model,
|
|
21
|
+
svar_model
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
# GARCH模型
|
|
25
|
+
from .garch_model import (
|
|
26
|
+
GARCHResult,
|
|
27
|
+
garch_model
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
# 协整分析/VECM
|
|
31
|
+
from .cointegration_vecm import (
|
|
32
|
+
CointegrationResult,
|
|
33
|
+
VECMResult,
|
|
34
|
+
engle_granger_cointegration_test,
|
|
35
|
+
johansen_cointegration_test,
|
|
36
|
+
vecm_model
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
# 面板VAR
|
|
40
|
+
from .panel_var import (
|
|
41
|
+
PanelVARResult,
|
|
42
|
+
panel_var_model
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
# 单位根检验
|
|
46
|
+
from .unit_root_tests import (
|
|
47
|
+
UnitRootTestResult,
|
|
48
|
+
adf_test,
|
|
49
|
+
pp_test,
|
|
50
|
+
kpss_test
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# 动态面板模型
|
|
54
|
+
from .dynamic_panel_models import (
|
|
55
|
+
DynamicPanelResult,
|
|
56
|
+
diff_gmm_model,
|
|
57
|
+
sys_gmm_model
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
# 结构突变检验
|
|
61
|
+
from .structural_break_tests import (
|
|
62
|
+
StructuralBreakResult,
|
|
63
|
+
chow_test,
|
|
64
|
+
quandt_andrews_test,
|
|
65
|
+
bai_perron_test
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# 面板数据诊断
|
|
69
|
+
from .panel_diagnostics import (
|
|
70
|
+
PanelDiagnosticResult,
|
|
71
|
+
hausman_test,
|
|
72
|
+
pooling_f_test,
|
|
73
|
+
lm_test,
|
|
74
|
+
within_correlation_test
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
# 时变参数模型
|
|
78
|
+
from .time_varying_parameter_models import (
|
|
79
|
+
TimeVaryingParameterResult,
|
|
80
|
+
tar_model,
|
|
81
|
+
star_model,
|
|
82
|
+
markov_switching_model
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
__all__ = [
|
|
86
|
+
# ARIMA模型
|
|
87
|
+
"ARIMAResult",
|
|
88
|
+
"arima_model",
|
|
89
|
+
|
|
90
|
+
# 指数平滑法
|
|
91
|
+
"ExponentialSmoothingResult",
|
|
92
|
+
"exponential_smoothing_model",
|
|
93
|
+
|
|
94
|
+
# VAR/SVAR模型
|
|
95
|
+
"VARResult",
|
|
96
|
+
"var_model",
|
|
97
|
+
"svar_model",
|
|
98
|
+
|
|
99
|
+
# GARCH模型
|
|
100
|
+
"GARCHResult",
|
|
101
|
+
"garch_model",
|
|
102
|
+
|
|
103
|
+
# 协整分析/VECM
|
|
104
|
+
"CointegrationResult",
|
|
105
|
+
"VECMResult",
|
|
106
|
+
"engle_granger_cointegration_test",
|
|
107
|
+
"johansen_cointegration_test",
|
|
108
|
+
"vecm_model",
|
|
109
|
+
|
|
110
|
+
# 面板VAR
|
|
111
|
+
"PanelVARResult",
|
|
112
|
+
"panel_var_model",
|
|
113
|
+
|
|
114
|
+
# 单位根检验
|
|
115
|
+
"UnitRootTestResult",
|
|
116
|
+
"adf_test",
|
|
117
|
+
"pp_test",
|
|
118
|
+
"kpss_test",
|
|
119
|
+
|
|
120
|
+
# 动态面板模型
|
|
121
|
+
"DynamicPanelResult",
|
|
122
|
+
"diff_gmm_model",
|
|
123
|
+
"sys_gmm_model",
|
|
124
|
+
|
|
125
|
+
# 结构突变检验
|
|
126
|
+
"StructuralBreakResult",
|
|
127
|
+
"chow_test",
|
|
128
|
+
"quandt_andrews_test",
|
|
129
|
+
"bai_perron_test",
|
|
130
|
+
|
|
131
|
+
# 面板数据诊断
|
|
132
|
+
"PanelDiagnosticResult",
|
|
133
|
+
"hausman_test",
|
|
134
|
+
"pooling_f_test",
|
|
135
|
+
"lm_test",
|
|
136
|
+
"within_correlation_test",
|
|
137
|
+
|
|
138
|
+
# 时变参数模型
|
|
139
|
+
"TimeVaryingParameterResult",
|
|
140
|
+
"tar_model",
|
|
141
|
+
"star_model",
|
|
142
|
+
"markov_switching_model"
|
|
143
|
+
]
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ARIMA模型实现
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import List, Optional
|
|
6
|
+
from pydantic import BaseModel, Field
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ARIMAResult(BaseModel):
|
|
11
|
+
"""ARIMA模型结果"""
|
|
12
|
+
model_type: str = Field(..., description="模型类型")
|
|
13
|
+
order: tuple = Field(..., description="模型阶数(p, d, q)")
|
|
14
|
+
coefficients: List[float] = Field(..., description="回归系数")
|
|
15
|
+
std_errors: Optional[List[float]] = Field(None, description="系数标准误")
|
|
16
|
+
t_values: Optional[List[float]] = Field(None, description="t统计量")
|
|
17
|
+
p_values: Optional[List[float]] = Field(None, description="p值")
|
|
18
|
+
conf_int_lower: Optional[List[float]] = Field(None, description="置信区间下界")
|
|
19
|
+
conf_int_upper: Optional[List[float]] = Field(None, description="置信区间上界")
|
|
20
|
+
aic: Optional[float] = Field(None, description="赤池信息准则")
|
|
21
|
+
bic: Optional[float] = Field(None, description="贝叶斯信息准则")
|
|
22
|
+
hqic: Optional[float] = Field(None, description="汉南-奎因信息准则")
|
|
23
|
+
r_squared: Optional[float] = Field(None, description="R方")
|
|
24
|
+
adj_r_squared: Optional[float] = Field(None, description="调整R方")
|
|
25
|
+
n_obs: int = Field(..., description="观测数量")
|
|
26
|
+
forecast: Optional[List[float]] = Field(None, description="预测值")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def arima_model(
|
|
30
|
+
data: List[float],
|
|
31
|
+
order: tuple = (1, 1, 1),
|
|
32
|
+
forecast_steps: int = 1
|
|
33
|
+
) -> ARIMAResult:
|
|
34
|
+
"""
|
|
35
|
+
ARIMA模型实现
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
data: 时间序列数据
|
|
39
|
+
order: (p,d,q) 参数设置
|
|
40
|
+
forecast_steps: 预测步数
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
ARIMAResult: ARIMA模型结果
|
|
44
|
+
"""
|
|
45
|
+
try:
|
|
46
|
+
# 导入statsmodels ARIMA模型
|
|
47
|
+
from statsmodels.tsa.arima.model import ARIMA as StatsARIMA
|
|
48
|
+
from statsmodels.tsa.stattools import arma_order_select_ic
|
|
49
|
+
|
|
50
|
+
# 拟合ARIMA模型
|
|
51
|
+
model = StatsARIMA(data, order=order)
|
|
52
|
+
fitted_model = model.fit()
|
|
53
|
+
|
|
54
|
+
# 提取模型参数
|
|
55
|
+
params = fitted_model.params.tolist()
|
|
56
|
+
std_errors = fitted_model.bse.tolist() if fitted_model.bse is not None else None
|
|
57
|
+
t_values = fitted_model.tvalues.tolist() if fitted_model.tvalues is not None else None
|
|
58
|
+
p_values = fitted_model.pvalues.tolist() if fitted_model.pvalues is not None else None
|
|
59
|
+
|
|
60
|
+
# 计算置信区间
|
|
61
|
+
if fitted_model.conf_int() is not None:
|
|
62
|
+
conf_int = fitted_model.conf_int()
|
|
63
|
+
conf_int_lower = conf_int[:, 0].tolist()
|
|
64
|
+
conf_int_upper = conf_int[:, 1].tolist()
|
|
65
|
+
else:
|
|
66
|
+
conf_int_lower = None
|
|
67
|
+
conf_int_upper = None
|
|
68
|
+
|
|
69
|
+
# 进行预测
|
|
70
|
+
forecast_result = fitted_model.forecast(steps=forecast_steps)
|
|
71
|
+
forecast = forecast_result.tolist()
|
|
72
|
+
|
|
73
|
+
# 获取模型统计信息
|
|
74
|
+
aic = float(fitted_model.aic) if hasattr(fitted_model, 'aic') else None
|
|
75
|
+
bic = float(fitted_model.bic) if hasattr(fitted_model, 'bic') else None
|
|
76
|
+
hqic = float(fitted_model.hqic) if hasattr(fitted_model, 'hqic') else None
|
|
77
|
+
|
|
78
|
+
# 对于ARIMA模型,通常不计算R方,因为它是基于预测误差而不是解释方差
|
|
79
|
+
# 但我们仍可以尝试获取,如果没有就设为None
|
|
80
|
+
r_squared = float(getattr(fitted_model, 'rsquared', None)) if hasattr(fitted_model, 'rsquared') else None
|
|
81
|
+
adj_r_squared = float(getattr(fitted_model, 'rsquared_adj', None)) if hasattr(fitted_model, 'rsquared_adj') else None
|
|
82
|
+
|
|
83
|
+
p, d, q = order
|
|
84
|
+
|
|
85
|
+
return ARIMAResult(
|
|
86
|
+
model_type=f"ARIMA({p},{d},{q})",
|
|
87
|
+
order=order,
|
|
88
|
+
coefficients=params,
|
|
89
|
+
std_errors=std_errors,
|
|
90
|
+
t_values=t_values,
|
|
91
|
+
p_values=p_values,
|
|
92
|
+
conf_int_lower=conf_int_lower,
|
|
93
|
+
conf_int_upper=conf_int_upper,
|
|
94
|
+
aic=aic,
|
|
95
|
+
bic=bic,
|
|
96
|
+
hqic=hqic,
|
|
97
|
+
r_squared=r_squared,
|
|
98
|
+
adj_r_squared=adj_r_squared,
|
|
99
|
+
n_obs=len(data),
|
|
100
|
+
forecast=forecast
|
|
101
|
+
)
|
|
102
|
+
except Exception as e:
|
|
103
|
+
# 出现错误时抛出异常
|
|
104
|
+
raise ValueError(f"ARIMA模型拟合失败: {str(e)}")
|