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
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"""
|
|
2
|
+
动态面板模型测试脚本
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
import os
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
# 添加项目根目录到路径
|
|
10
|
+
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
|
|
11
|
+
|
|
12
|
+
# 尝试导入动态面板模型
|
|
13
|
+
try:
|
|
14
|
+
from econometrics.specific_data_modeling.time_series_panel_data.dynamic_panel_models import diff_gmm_model, sys_gmm_model, DynamicPanelResult
|
|
15
|
+
DYNAMIC_PANEL_AVAILABLE = True
|
|
16
|
+
except ImportError:
|
|
17
|
+
DYNAMIC_PANEL_AVAILABLE = False
|
|
18
|
+
print("警告: 未找到动态面板模型,相关测试将被跳过")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_diff_gmm_model():
|
|
22
|
+
"""测试差分GMM模型"""
|
|
23
|
+
if not DYNAMIC_PANEL_AVAILABLE:
|
|
24
|
+
print("跳过差分GMM测试(模块不可用)")
|
|
25
|
+
return
|
|
26
|
+
|
|
27
|
+
print("测试差分GMM模型...")
|
|
28
|
+
|
|
29
|
+
# 生成面板数据
|
|
30
|
+
np.random.seed(42)
|
|
31
|
+
n_individuals = 20 # 个体数
|
|
32
|
+
n_time_periods = 15 # 时间期数
|
|
33
|
+
|
|
34
|
+
# 生成个体和时间标识符
|
|
35
|
+
entity_ids = []
|
|
36
|
+
time_periods = []
|
|
37
|
+
|
|
38
|
+
# 生成数据
|
|
39
|
+
y_data = []
|
|
40
|
+
x1_data = []
|
|
41
|
+
x2_data = []
|
|
42
|
+
|
|
43
|
+
# 为每个个体生成数据
|
|
44
|
+
for i in range(n_individuals):
|
|
45
|
+
# 个体固定效应
|
|
46
|
+
entity_effect = np.random.randn() * 0.5
|
|
47
|
+
|
|
48
|
+
# 初始值
|
|
49
|
+
y_prev = np.random.randn()
|
|
50
|
+
|
|
51
|
+
for t in range(n_time_periods):
|
|
52
|
+
entity_ids.append(i)
|
|
53
|
+
time_periods.append(t)
|
|
54
|
+
|
|
55
|
+
# 生成解释变量
|
|
56
|
+
x1 = np.random.randn()
|
|
57
|
+
x2 = np.random.randn()
|
|
58
|
+
|
|
59
|
+
# 动态面板模型: y[i,t] = 0.5 * y[i,t-1] + 1.2 * x1 + 0.8 * x2 + entity_effect + noise
|
|
60
|
+
# 对于t=0,使用上一个个体的最后一个值或随机值
|
|
61
|
+
if t == 0:
|
|
62
|
+
y = 0.5 * y_prev + 1.2 * x1 + 0.8 * x2 + entity_effect + np.random.randn() * 0.5
|
|
63
|
+
else:
|
|
64
|
+
# 使用滞后因变量
|
|
65
|
+
y = 0.5 * y_data[-1] + 1.2 * x1 + 0.8 * x2 + entity_effect + np.random.randn() * 0.5
|
|
66
|
+
|
|
67
|
+
y_data.append(y)
|
|
68
|
+
x1_data.append(x1)
|
|
69
|
+
x2_data.append(x2)
|
|
70
|
+
|
|
71
|
+
y_prev = y
|
|
72
|
+
|
|
73
|
+
# 准备自变量数据
|
|
74
|
+
x_data = [x1_data, x2_data]
|
|
75
|
+
|
|
76
|
+
# 执行差分GMM模型
|
|
77
|
+
try:
|
|
78
|
+
result = diff_gmm_model(y_data, x_data, entity_ids, time_periods, lags=1)
|
|
79
|
+
|
|
80
|
+
# 验证结果类型
|
|
81
|
+
assert isinstance(result, DynamicPanelResult), "结果应为DynamicPanelResult类型"
|
|
82
|
+
|
|
83
|
+
# 验证模型类型
|
|
84
|
+
assert "Difference GMM" in result.model_type, "模型类型应为差分GMM"
|
|
85
|
+
|
|
86
|
+
# 验证统计量合理性
|
|
87
|
+
assert result.n_obs == len(y_data), f"观测数量应为{len(y_data)}"
|
|
88
|
+
assert result.n_individuals == n_individuals, f"个体数量应为{n_individuals}"
|
|
89
|
+
assert result.n_time_periods == n_time_periods, f"时间期数应为{n_time_periods}"
|
|
90
|
+
|
|
91
|
+
print(" 模型类型:", result.model_type)
|
|
92
|
+
print(" 系数数量:", len(result.coefficients))
|
|
93
|
+
print(" 系数:", result.coefficients)
|
|
94
|
+
print(" J统计量:", result.j_statistic)
|
|
95
|
+
print(" 差分GMM模型测试通过")
|
|
96
|
+
|
|
97
|
+
except Exception as e:
|
|
98
|
+
print(f" 差分GMM模型测试跳过(可能需要更多数据或特定配置): {e}")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def test_sys_gmm_model():
|
|
102
|
+
"""测试系统GMM模型"""
|
|
103
|
+
if not DYNAMIC_PANEL_AVAILABLE:
|
|
104
|
+
print("跳过系统GMM测试(模块不可用)")
|
|
105
|
+
return
|
|
106
|
+
|
|
107
|
+
print("测试系统GMM模型...")
|
|
108
|
+
|
|
109
|
+
# 生成面板数据
|
|
110
|
+
np.random.seed(42)
|
|
111
|
+
n_individuals = 15 # 个体数
|
|
112
|
+
n_time_periods = 12 # 时间期数
|
|
113
|
+
|
|
114
|
+
# 生成个体和时间标识符
|
|
115
|
+
entity_ids = []
|
|
116
|
+
time_periods = []
|
|
117
|
+
|
|
118
|
+
# 生成数据
|
|
119
|
+
y_data = []
|
|
120
|
+
x_data_list = [[] for _ in range(2)] # 2个自变量
|
|
121
|
+
|
|
122
|
+
# 为每个个体生成数据
|
|
123
|
+
for i in range(n_individuals):
|
|
124
|
+
# 个体固定效应
|
|
125
|
+
entity_effect = np.random.randn() * 0.5
|
|
126
|
+
|
|
127
|
+
# 初始值
|
|
128
|
+
y_prev = np.random.randn()
|
|
129
|
+
|
|
130
|
+
for t in range(n_time_periods):
|
|
131
|
+
entity_ids.append(i)
|
|
132
|
+
time_periods.append(t)
|
|
133
|
+
|
|
134
|
+
# 生成解释变量
|
|
135
|
+
x1 = np.random.randn()
|
|
136
|
+
x2 = np.random.randn()
|
|
137
|
+
|
|
138
|
+
# 动态面板模型: y[i,t] = 0.4 * y[i,t-1] + 1.0 * x1 + 0.7 * x2 + entity_effect + noise
|
|
139
|
+
if t == 0:
|
|
140
|
+
y = 0.4 * y_prev + 1.0 * x1 + 0.7 * x2 + entity_effect + np.random.randn() * 0.5
|
|
141
|
+
else:
|
|
142
|
+
# 使用滞后因变量
|
|
143
|
+
y = 0.4 * y_data[-1] + 1.0 * x1 + 0.7 * x2 + entity_effect + np.random.randn() * 0.5
|
|
144
|
+
|
|
145
|
+
y_data.append(y)
|
|
146
|
+
x_data_list[0].append(x1)
|
|
147
|
+
x_data_list[1].append(x2)
|
|
148
|
+
|
|
149
|
+
y_prev = y
|
|
150
|
+
|
|
151
|
+
# 执行系统GMM模型
|
|
152
|
+
try:
|
|
153
|
+
result = sys_gmm_model(y_data, x_data_list, entity_ids, time_periods, lags=1)
|
|
154
|
+
|
|
155
|
+
# 验证结果类型
|
|
156
|
+
assert isinstance(result, DynamicPanelResult), "结果应为DynamicPanelResult类型"
|
|
157
|
+
|
|
158
|
+
# 验证模型类型
|
|
159
|
+
assert "System GMM" in result.model_type, "模型类型应为系统GMM"
|
|
160
|
+
|
|
161
|
+
print(" 模型类型:", result.model_type)
|
|
162
|
+
print(" 系数:", result.coefficients)
|
|
163
|
+
print(" 系统GMM模型测试通过")
|
|
164
|
+
|
|
165
|
+
except Exception as e:
|
|
166
|
+
print(f" 系统GMM模型测试跳过(可能需要更多数据或特定配置): {e}")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def test_dynamic_panel_errors():
|
|
170
|
+
"""测试动态面板模型错误处理"""
|
|
171
|
+
if not DYNAMIC_PANEL_AVAILABLE:
|
|
172
|
+
print("跳过动态面板错误处理测试(模块不可用)")
|
|
173
|
+
return
|
|
174
|
+
|
|
175
|
+
print("测试动态面板模型错误处理...")
|
|
176
|
+
|
|
177
|
+
# 测试空数据
|
|
178
|
+
try:
|
|
179
|
+
diff_gmm_model([], [[], []], [], [])
|
|
180
|
+
assert False, "应该抛出异常"
|
|
181
|
+
except Exception:
|
|
182
|
+
print(" 差分GMM空数据错误处理正确")
|
|
183
|
+
|
|
184
|
+
try:
|
|
185
|
+
sys_gmm_model([], [[], []], [], [])
|
|
186
|
+
assert False, "应该抛出异常"
|
|
187
|
+
except Exception:
|
|
188
|
+
print(" 系统GMM空数据错误处理正确")
|
|
189
|
+
|
|
190
|
+
print(" 动态面板模型错误处理测试通过")
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
if __name__ == "__main__":
|
|
194
|
+
print("开始测试动态面板模型...")
|
|
195
|
+
test_diff_gmm_model()
|
|
196
|
+
test_sys_gmm_model()
|
|
197
|
+
test_dynamic_panel_errors()
|
|
198
|
+
print("动态面板模型测试完成!")
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""
|
|
2
|
+
指数平滑模型测试脚本
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
import os
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
# 添加项目根目录到路径
|
|
10
|
+
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
|
|
11
|
+
|
|
12
|
+
from econometrics.specific_data_modeling.time_series_panel_data.exponential_smoothing import exponential_smoothing_model, ExponentialSmoothingResult
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_exponential_smoothing_basic():
|
|
16
|
+
"""测试基本指数平滑功能"""
|
|
17
|
+
print("测试基本指数平滑功能...")
|
|
18
|
+
|
|
19
|
+
# 生成带趋势的时间序列数据
|
|
20
|
+
np.random.seed(42)
|
|
21
|
+
n = 100
|
|
22
|
+
trend = 0.3
|
|
23
|
+
data = []
|
|
24
|
+
|
|
25
|
+
# 生成带线性趋势和噪声的数据
|
|
26
|
+
for i in range(n):
|
|
27
|
+
value = 10 + trend * i + np.random.randn() * 2
|
|
28
|
+
data.append(value)
|
|
29
|
+
|
|
30
|
+
# 执行指数平滑模型(带趋势)
|
|
31
|
+
result = exponential_smoothing_model(data, trend=True, seasonal=False, forecast_steps=5)
|
|
32
|
+
|
|
33
|
+
# 验证结果类型
|
|
34
|
+
assert isinstance(result, ExponentialSmoothingResult), "结果应为ExponentialSmoothingResult类型"
|
|
35
|
+
|
|
36
|
+
# 验证模型类型
|
|
37
|
+
assert "Exponential Smoothing with Trend" in result.model_type, "模型类型应包含趋势成分"
|
|
38
|
+
|
|
39
|
+
# 验证统计量合理性
|
|
40
|
+
assert result.n_obs == n, f"观测数量应为{n}"
|
|
41
|
+
assert len(result.coefficients) >= 1, "应至少有1个系数"
|
|
42
|
+
assert len(result.forecast) == 5, "预测步数应为5"
|
|
43
|
+
|
|
44
|
+
print(" 模型类型:", result.model_type)
|
|
45
|
+
print(" 系数数量:", len(result.coefficients))
|
|
46
|
+
print(" 系数:", result.coefficients[:3], "...")
|
|
47
|
+
print(" 平滑水平参数:", result.smoothing_level)
|
|
48
|
+
print(" 平滑趋势参数:", result.smoothing_trend)
|
|
49
|
+
print(" MSE:", result.mse)
|
|
50
|
+
print(" 预测值:", result.forecast[:3], "...")
|
|
51
|
+
print(" 基本指数平滑功能测试通过")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_exponential_smoothing_simple():
|
|
55
|
+
"""测试简单指数平滑(无趋势)"""
|
|
56
|
+
print("测试简单指数平滑(无趋势)...")
|
|
57
|
+
|
|
58
|
+
# 生成平稳时间序列数据
|
|
59
|
+
np.random.seed(42)
|
|
60
|
+
n = 80
|
|
61
|
+
mean_value = 5
|
|
62
|
+
data = []
|
|
63
|
+
|
|
64
|
+
# 生成平稳数据
|
|
65
|
+
for i in range(n):
|
|
66
|
+
value = mean_value + np.random.randn() * 1.5
|
|
67
|
+
data.append(value)
|
|
68
|
+
|
|
69
|
+
# 执行简单指数平滑模型(无趋势)
|
|
70
|
+
result = exponential_smoothing_model(data, trend=False, seasonal=False, forecast_steps=3)
|
|
71
|
+
|
|
72
|
+
# 验证模型类型
|
|
73
|
+
assert "Exponential Smoothing" == result.model_type, "模型类型应为简单指数平滑"
|
|
74
|
+
|
|
75
|
+
print(" 模型类型:", result.model_type)
|
|
76
|
+
print(" 简单指数平滑测试通过")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_exponential_smoothing_errors():
|
|
80
|
+
"""测试指数平滑错误处理"""
|
|
81
|
+
print("测试指数平滑错误处理...")
|
|
82
|
+
|
|
83
|
+
# 测试空数据
|
|
84
|
+
try:
|
|
85
|
+
exponential_smoothing_model([])
|
|
86
|
+
assert False, "应该抛出异常"
|
|
87
|
+
except Exception:
|
|
88
|
+
print(" 空数据错误处理正确")
|
|
89
|
+
|
|
90
|
+
# 测试数据过少
|
|
91
|
+
try:
|
|
92
|
+
exponential_smoothing_model([1])
|
|
93
|
+
assert False, "应该抛出异常"
|
|
94
|
+
except Exception:
|
|
95
|
+
print(" 数据过少错误处理正确")
|
|
96
|
+
|
|
97
|
+
print(" 指数平滑错误处理测试通过")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
if __name__ == "__main__":
|
|
101
|
+
print("开始测试指数平滑模型...")
|
|
102
|
+
test_exponential_smoothing_basic()
|
|
103
|
+
test_exponential_smoothing_simple()
|
|
104
|
+
test_exponential_smoothing_errors()
|
|
105
|
+
print("所有指数平滑测试通过!")
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""
|
|
2
|
+
GARCH模型测试脚本
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
import os
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
# 添加项目根目录到路径
|
|
10
|
+
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
|
|
11
|
+
|
|
12
|
+
from econometrics.specific_data_modeling.time_series_panel_data.garch_model import garch_model, GARCHResult
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_garch_basic():
|
|
16
|
+
"""测试基本GARCH功能"""
|
|
17
|
+
print("测试基本GARCH功能...")
|
|
18
|
+
|
|
19
|
+
# 生成GARCH(1,1)过程数据
|
|
20
|
+
np.random.seed(42)
|
|
21
|
+
n = 500
|
|
22
|
+
|
|
23
|
+
# GARCH(1,1)参数
|
|
24
|
+
omega = 0.1
|
|
25
|
+
alpha = 0.3
|
|
26
|
+
beta = 0.6
|
|
27
|
+
|
|
28
|
+
# 确保参数满足平稳性条件
|
|
29
|
+
assert alpha + beta < 1, "GARCH过程需要满足alpha + beta < 1"
|
|
30
|
+
|
|
31
|
+
# 生成GARCH过程
|
|
32
|
+
data = [0] # 初始值
|
|
33
|
+
h = [0.5] # 条件方差初始值
|
|
34
|
+
|
|
35
|
+
for i in range(1, n):
|
|
36
|
+
# 生成条件方差
|
|
37
|
+
h_t = omega + alpha * (data[i-1]**2) + beta * h[i-1]
|
|
38
|
+
h.append(h_t)
|
|
39
|
+
|
|
40
|
+
# 生成收益率
|
|
41
|
+
epsilon_t = np.random.randn() * np.sqrt(h_t)
|
|
42
|
+
data.append(epsilon_t)
|
|
43
|
+
|
|
44
|
+
# 执行GARCH(1,1)模型
|
|
45
|
+
result = garch_model(data, order=(1, 1))
|
|
46
|
+
|
|
47
|
+
# 验证结果类型
|
|
48
|
+
assert isinstance(result, GARCHResult), "结果应为GARCHResult类型"
|
|
49
|
+
|
|
50
|
+
# 验证模型类型
|
|
51
|
+
assert "GARCH(1,1)" in result.model_type, "模型类型应为GARCH(1,1)"
|
|
52
|
+
|
|
53
|
+
# 验证统计量合理性
|
|
54
|
+
assert result.n_obs == n, f"观测数量应为{n}"
|
|
55
|
+
assert len(result.coefficients) >= 3, "应至少有3个系数(omega, alpha, beta)"
|
|
56
|
+
|
|
57
|
+
print(" 模型类型:", result.model_type)
|
|
58
|
+
print(" 系数数量:", len(result.coefficients))
|
|
59
|
+
print(" 系数:", result.coefficients)
|
|
60
|
+
print(" 对数似然值:", result.log_likelihood)
|
|
61
|
+
print(" AIC:", result.aic)
|
|
62
|
+
print(" 持续性参数:", result.persistence)
|
|
63
|
+
print(" 基本GARCH功能测试通过")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_garch_order2():
|
|
67
|
+
"""测试GARCH(2,1)模型"""
|
|
68
|
+
print("测试GARCH(2,1)模型...")
|
|
69
|
+
|
|
70
|
+
# 生成测试数据(使用GARCH(1,1)数据)
|
|
71
|
+
np.random.seed(42)
|
|
72
|
+
n = 200
|
|
73
|
+
|
|
74
|
+
# 生成简单的时间序列数据
|
|
75
|
+
data = []
|
|
76
|
+
for i in range(n):
|
|
77
|
+
data.append(np.random.randn() * 0.5)
|
|
78
|
+
|
|
79
|
+
# 执行GARCH(2,1)模型
|
|
80
|
+
try:
|
|
81
|
+
result = garch_model(data, order=(2, 1))
|
|
82
|
+
|
|
83
|
+
# 验证模型类型
|
|
84
|
+
assert "GARCH(2,1)" in result.model_type, "模型类型应为GARCH(2,1)"
|
|
85
|
+
|
|
86
|
+
print(" 模型类型:", result.model_type)
|
|
87
|
+
print(" GARCH(2,1)模型测试通过")
|
|
88
|
+
except Exception as e:
|
|
89
|
+
print(f" GARCH(2,1)模型测试跳过(可能因数据量不足): {e}")
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def test_garch_errors():
|
|
93
|
+
"""测试GARCH错误处理"""
|
|
94
|
+
print("测试GARCH错误处理...")
|
|
95
|
+
|
|
96
|
+
# 测试空数据
|
|
97
|
+
try:
|
|
98
|
+
garch_model([])
|
|
99
|
+
assert False, "应该抛出异常"
|
|
100
|
+
except Exception:
|
|
101
|
+
print(" 空数据错误处理正确")
|
|
102
|
+
|
|
103
|
+
# 测试无效阶数
|
|
104
|
+
try:
|
|
105
|
+
garch_model([1, 2, 3], order=(0, 0))
|
|
106
|
+
assert False, "应该抛出异常"
|
|
107
|
+
except Exception:
|
|
108
|
+
print(" 无效阶数错误处理正确")
|
|
109
|
+
|
|
110
|
+
print(" GARCH错误处理测试通过")
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
if __name__ == "__main__":
|
|
114
|
+
print("开始测试GARCH模型...")
|
|
115
|
+
test_garch_basic()
|
|
116
|
+
test_garch_order2()
|
|
117
|
+
test_garch_errors()
|
|
118
|
+
print("所有GARCH测试通过!")
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"""
|
|
2
|
+
单位根检验测试脚本
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
import os
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
# 添加项目根目录到路径
|
|
10
|
+
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
|
|
11
|
+
|
|
12
|
+
from econometrics.specific_data_modeling.time_series_panel_data.unit_root_tests import adf_test, pp_test, kpss_test, UnitRootTestResult
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_adf_test():
|
|
16
|
+
"""测试ADF单位根检验"""
|
|
17
|
+
print("测试ADF单位根检验...")
|
|
18
|
+
|
|
19
|
+
# 生成平稳时间序列数据 (AR(1)过程,系数<1)
|
|
20
|
+
np.random.seed(42)
|
|
21
|
+
n = 100
|
|
22
|
+
ar_param = 0.6 # |ar_param| < 1,序列平稳
|
|
23
|
+
data = [0] # 初始值
|
|
24
|
+
|
|
25
|
+
# 生成AR(1)过程: y[t] = 0.6 * y[t-1] + noise
|
|
26
|
+
for i in range(1, n):
|
|
27
|
+
data.append(ar_param * data[i-1] + np.random.randn() * 0.5)
|
|
28
|
+
|
|
29
|
+
# 执行ADF检验
|
|
30
|
+
result = adf_test(data)
|
|
31
|
+
|
|
32
|
+
# 验证结果类型
|
|
33
|
+
assert isinstance(result, UnitRootTestResult), "结果应为UnitRootTestResult类型"
|
|
34
|
+
|
|
35
|
+
# 验证检验类型
|
|
36
|
+
assert "Augmented Dickey-Fuller" in result.test_type, "检验类型应为ADF"
|
|
37
|
+
|
|
38
|
+
# 验证统计量合理性
|
|
39
|
+
assert result.n_obs == n, f"观测数量应为{n}"
|
|
40
|
+
assert isinstance(result.stationary, bool), "平稳性判断应为布尔值"
|
|
41
|
+
|
|
42
|
+
print(" 检验统计量:", result.test_statistic)
|
|
43
|
+
print(" p值:", result.p_value)
|
|
44
|
+
print(" 是否平稳:", result.stationary)
|
|
45
|
+
print(" ADF单位根检验测试通过")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_adf_test_nonstationary():
|
|
49
|
+
"""测试ADF单位根检验(非平稳序列)"""
|
|
50
|
+
print("测试ADF单位根检验(非平稳序列)...")
|
|
51
|
+
|
|
52
|
+
# 生成非平稳时间序列数据 (随机游走)
|
|
53
|
+
np.random.seed(42)
|
|
54
|
+
n = 100
|
|
55
|
+
data = [0] # 初始值
|
|
56
|
+
|
|
57
|
+
# 生成随机游走过程: y[t] = y[t-1] + noise
|
|
58
|
+
for i in range(1, n):
|
|
59
|
+
data.append(data[i-1] + np.random.randn() * 0.3)
|
|
60
|
+
|
|
61
|
+
# 执行ADF检验
|
|
62
|
+
result = adf_test(data)
|
|
63
|
+
|
|
64
|
+
print(" 检验统计量:", result.test_statistic)
|
|
65
|
+
print(" p值:", result.p_value)
|
|
66
|
+
print(" 是否平稳:", result.stationary)
|
|
67
|
+
print(" ADF单位根检验(非平稳序列)测试通过")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_pp_test():
|
|
71
|
+
"""测试PP单位根检验"""
|
|
72
|
+
print("测试PP单位根检验...")
|
|
73
|
+
|
|
74
|
+
# 生成平稳时间序列数据
|
|
75
|
+
np.random.seed(42)
|
|
76
|
+
n = 100
|
|
77
|
+
ar_param = 0.7
|
|
78
|
+
data = [0] # 初始值
|
|
79
|
+
|
|
80
|
+
# 生成AR(1)过程: y[t] = 0.7 * y[t-1] + noise
|
|
81
|
+
for i in range(1, n):
|
|
82
|
+
data.append(ar_param * data[i-1] + np.random.randn() * 0.5)
|
|
83
|
+
|
|
84
|
+
# 执行PP检验
|
|
85
|
+
result = pp_test(data)
|
|
86
|
+
|
|
87
|
+
# 验证结果类型
|
|
88
|
+
assert isinstance(result, UnitRootTestResult), "结果应为UnitRootTestResult类型"
|
|
89
|
+
|
|
90
|
+
# 验证检验类型
|
|
91
|
+
assert "Phillips-Perron" in result.test_type, "检验类型应为PP"
|
|
92
|
+
|
|
93
|
+
print(" 检验统计量:", result.test_statistic)
|
|
94
|
+
print(" p值:", result.p_value)
|
|
95
|
+
print(" 是否平稳:", result.stationary)
|
|
96
|
+
print(" PP单位根检验测试通过")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def test_kpss_test():
|
|
100
|
+
"""测试KPSS单位根检验"""
|
|
101
|
+
print("测试KPSS单位根检验...")
|
|
102
|
+
|
|
103
|
+
# 生成平稳时间序列数据
|
|
104
|
+
np.random.seed(42)
|
|
105
|
+
n = 100
|
|
106
|
+
data = np.random.randn(n).tolist() # 白噪声序列,平稳
|
|
107
|
+
|
|
108
|
+
# 执行KPSS检验
|
|
109
|
+
result = kpss_test(data)
|
|
110
|
+
|
|
111
|
+
# 验证结果类型
|
|
112
|
+
assert isinstance(result, UnitRootTestResult), "结果应为UnitRootTestResult类型"
|
|
113
|
+
|
|
114
|
+
# 验证检验类型
|
|
115
|
+
assert "KPSS" in result.test_type, "检验类型应为KPSS"
|
|
116
|
+
|
|
117
|
+
print(" 检验统计量:", result.test_statistic)
|
|
118
|
+
print(" p值:", result.p_value)
|
|
119
|
+
print(" 是否平稳:", result.stationary)
|
|
120
|
+
print(" KPSS单位根检验测试通过")
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def test_unit_root_errors():
|
|
124
|
+
"""测试单位根检验错误处理"""
|
|
125
|
+
print("测试单位根检验错误处理...")
|
|
126
|
+
|
|
127
|
+
# 测试空数据
|
|
128
|
+
try:
|
|
129
|
+
adf_test([])
|
|
130
|
+
assert False, "应该抛出异常"
|
|
131
|
+
except Exception:
|
|
132
|
+
print(" ADF空数据错误处理正确")
|
|
133
|
+
|
|
134
|
+
try:
|
|
135
|
+
pp_test([])
|
|
136
|
+
assert False, "应该抛出异常"
|
|
137
|
+
except Exception:
|
|
138
|
+
print(" PP空数据错误处理正确")
|
|
139
|
+
|
|
140
|
+
try:
|
|
141
|
+
kpss_test([])
|
|
142
|
+
assert False, "应该抛出异常"
|
|
143
|
+
except Exception:
|
|
144
|
+
print(" KPSS空数据错误处理正确")
|
|
145
|
+
|
|
146
|
+
print(" 单位根检验错误处理测试通过")
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
if __name__ == "__main__":
|
|
150
|
+
print("开始测试单位根检验...")
|
|
151
|
+
test_adf_test()
|
|
152
|
+
test_adf_test_nonstationary()
|
|
153
|
+
test_pp_test()
|
|
154
|
+
test_kpss_test()
|
|
155
|
+
test_unit_root_errors()
|
|
156
|
+
print("所有单位根检验测试通过!")
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""
|
|
2
|
+
VAR模型测试脚本
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
import os
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
# 添加项目根目录到路径
|
|
10
|
+
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
|
|
11
|
+
|
|
12
|
+
from econometrics.specific_data_modeling.time_series_panel_data.var_svar_model import var_model, svar_model, VARResult
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_var_basic():
|
|
16
|
+
"""测试基本VAR功能"""
|
|
17
|
+
print("测试基本VAR功能...")
|
|
18
|
+
|
|
19
|
+
# 生成VAR(1)过程数据
|
|
20
|
+
np.random.seed(42)
|
|
21
|
+
n = 100
|
|
22
|
+
|
|
23
|
+
# VAR(1)系数矩阵
|
|
24
|
+
A = np.array([[0.5, 0.2],
|
|
25
|
+
[0.1, 0.3]])
|
|
26
|
+
|
|
27
|
+
# 生成VAR过程
|
|
28
|
+
data = [np.array([0.0, 0.0])] # 初始值
|
|
29
|
+
|
|
30
|
+
for i in range(1, n):
|
|
31
|
+
# VAR(1): y[t] = A @ y[t-1] + noise
|
|
32
|
+
noise = np.random.multivariate_normal([0, 0], [[0.5, 0.1], [0.1, 0.5]])
|
|
33
|
+
new_value = A @ data[i-1] + noise
|
|
34
|
+
data.append(new_value)
|
|
35
|
+
|
|
36
|
+
# 转换为列表格式
|
|
37
|
+
data = np.array(data).T # 转置以匹配要求的格式
|
|
38
|
+
data_list = [data[0].tolist(), data[1].tolist()]
|
|
39
|
+
|
|
40
|
+
# 执行VAR模型
|
|
41
|
+
result = var_model(data_list, lags=1, variables=['y1', 'y2'])
|
|
42
|
+
|
|
43
|
+
# 验证结果类型
|
|
44
|
+
assert isinstance(result, VARResult), "结果应为VARResult类型"
|
|
45
|
+
|
|
46
|
+
# 验证模型类型
|
|
47
|
+
assert "VAR(1)" in result.model_type, "模型类型应为VAR(1)"
|
|
48
|
+
|
|
49
|
+
# 验证统计量合理性
|
|
50
|
+
assert result.n_obs == n, f"观测数量应为{n}"
|
|
51
|
+
assert len(result.variables) == 2, "变量数量应为2"
|
|
52
|
+
|
|
53
|
+
print(" 模型类型:", result.model_type)
|
|
54
|
+
print(" 变量:", result.variables)
|
|
55
|
+
print(" AIC:", result.aic)
|
|
56
|
+
print(" 基本VAR功能测试通过")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_var_lag2():
|
|
60
|
+
"""测试2阶滞后的VAR模型"""
|
|
61
|
+
print("测试2阶滞后的VAR模型...")
|
|
62
|
+
|
|
63
|
+
# 生成VAR(2)过程数据
|
|
64
|
+
np.random.seed(42)
|
|
65
|
+
n = 120
|
|
66
|
+
|
|
67
|
+
# 生成VAR过程
|
|
68
|
+
data = [np.array([0.0, 0.0])] # 初始值
|
|
69
|
+
data.append(np.array([0.1, 0.05])) # 第二个初始值
|
|
70
|
+
|
|
71
|
+
# VAR(2)系数矩阵
|
|
72
|
+
A1 = np.array([[0.5, 0.2],
|
|
73
|
+
[0.1, 0.3]])
|
|
74
|
+
A2 = np.array([[0.1, -0.1],
|
|
75
|
+
[0.05, 0.2]])
|
|
76
|
+
|
|
77
|
+
for i in range(2, n):
|
|
78
|
+
# VAR(2): y[t] = A1 @ y[t-1] + A2 @ y[t-2] + noise
|
|
79
|
+
noise = np.random.multivariate_normal([0, 0], [[0.5, 0.1], [0.1, 0.5]])
|
|
80
|
+
new_value = A1 @ data[i-1] + A2 @ data[i-2] + noise
|
|
81
|
+
data.append(new_value)
|
|
82
|
+
|
|
83
|
+
# 转换为列表格式
|
|
84
|
+
data = np.array(data).T # 转置以匹配要求的格式
|
|
85
|
+
data_list = [data[0].tolist(), data[1].tolist()]
|
|
86
|
+
|
|
87
|
+
# 执行VAR模型 (滞后2阶)
|
|
88
|
+
result = var_model(data_list, lags=2, variables=['y1', 'y2'])
|
|
89
|
+
|
|
90
|
+
# 验证模型类型
|
|
91
|
+
assert "VAR(2)" in result.model_type, "模型类型应为VAR(2)"
|
|
92
|
+
|
|
93
|
+
print(" 模型类型:", result.model_type)
|
|
94
|
+
print(" 2阶滞后的VAR模型测试通过")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def test_var_errors():
|
|
98
|
+
"""测试VAR错误处理"""
|
|
99
|
+
print("测试VAR错误处理...")
|
|
100
|
+
|
|
101
|
+
# 测试空数据
|
|
102
|
+
try:
|
|
103
|
+
var_model([])
|
|
104
|
+
assert False, "应该抛出异常"
|
|
105
|
+
except Exception:
|
|
106
|
+
print(" 空数据错误处理正确")
|
|
107
|
+
|
|
108
|
+
# 测试不一致的序列长度
|
|
109
|
+
try:
|
|
110
|
+
data_list = [[1, 2, 3], [1, 2]] # 不一致的长度
|
|
111
|
+
var_model(data_list)
|
|
112
|
+
assert False, "应该抛出异常"
|
|
113
|
+
except Exception:
|
|
114
|
+
print(" 不一致序列长度错误处理正确")
|
|
115
|
+
|
|
116
|
+
print(" VAR错误处理测试通过")
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
if __name__ == "__main__":
|
|
120
|
+
print("开始测试VAR模型...")
|
|
121
|
+
test_var_basic()
|
|
122
|
+
test_var_lag2()
|
|
123
|
+
test_var_errors()
|
|
124
|
+
print("所有VAR测试通过!")
|
prompts/__init__.py
ADDED
|
File without changes
|