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,128 @@
|
|
|
1
|
+
"""
|
|
2
|
+
GMM模型测试脚本
|
|
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.basic_parametric_estimation.gmm.gmm_model import gmm_estimation, GMMResult
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_gmm_basic():
|
|
16
|
+
"""测试基本GMM功能"""
|
|
17
|
+
print("测试基本GMM功能...")
|
|
18
|
+
|
|
19
|
+
# 生成测试数据
|
|
20
|
+
np.random.seed(42)
|
|
21
|
+
n = 100
|
|
22
|
+
x1 = np.random.randn(n)
|
|
23
|
+
x2 = np.random.randn(n)
|
|
24
|
+
# 真实模型: y = 2 + 3*x1 + 2*x2 + noise
|
|
25
|
+
y = 2 + 3*x1 + 2*x2 + np.random.randn(n) * 0.5
|
|
26
|
+
|
|
27
|
+
# 准备数据
|
|
28
|
+
x_data = [[x1[i], x2[i]] for i in range(n)]
|
|
29
|
+
y_data = y.tolist()
|
|
30
|
+
|
|
31
|
+
# 执行GMM回归(无工具变量,退化为OLS)
|
|
32
|
+
result = gmm_estimation(y_data, x_data, feature_names=['x1', 'x2'])
|
|
33
|
+
|
|
34
|
+
# 验证结果类型
|
|
35
|
+
assert isinstance(result, GMMResult), "结果应为GMMResult类型"
|
|
36
|
+
|
|
37
|
+
# 验证系数数量
|
|
38
|
+
assert len(result.coefficients) == 3, "应该有3个系数(包括常数项)"
|
|
39
|
+
|
|
40
|
+
# 验证统计量合理性
|
|
41
|
+
assert result.n_obs == n, f"观测数量应为{n}"
|
|
42
|
+
assert result.n_moments == 3, "矩条件数量应为3"
|
|
43
|
+
|
|
44
|
+
print(" 系数:", result.coefficients)
|
|
45
|
+
print(" J统计量:", result.j_statistic)
|
|
46
|
+
print(" 基本GMM功能测试通过")
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_gmm_with_instruments():
|
|
50
|
+
"""测试带工具变量的GMM"""
|
|
51
|
+
print("测试带工具变量的GMM...")
|
|
52
|
+
|
|
53
|
+
# 生成测试数据(工具变量)
|
|
54
|
+
np.random.seed(42)
|
|
55
|
+
n = 80
|
|
56
|
+
x1 = np.random.randn(n)
|
|
57
|
+
x2 = np.random.randn(n)
|
|
58
|
+
z1 = np.random.randn(n) # 工具变量
|
|
59
|
+
z2 = np.random.randn(n) # 工具变量
|
|
60
|
+
# 真实模型: y = 2 + 3*x1 + 2*x2 + noise
|
|
61
|
+
y = 2 + 3*x1 + 2*x2 + np.random.randn(n) * 0.5
|
|
62
|
+
|
|
63
|
+
# 准备数据
|
|
64
|
+
x_data = [[x1[i], x2[i]] for i in range(n)]
|
|
65
|
+
instruments = [[z1[i], z2[i]] for i in range(n)]
|
|
66
|
+
y_data = y.tolist()
|
|
67
|
+
|
|
68
|
+
# 执行GMM回归(带工具变量)
|
|
69
|
+
result = gmm_estimation(y_data, x_data, instruments=instruments, feature_names=['x1', 'x2'])
|
|
70
|
+
|
|
71
|
+
# 验证系数数量
|
|
72
|
+
assert len(result.coefficients) == 3, "应该有3个系数(包括常数项)"
|
|
73
|
+
|
|
74
|
+
print(" 系数:", result.coefficients)
|
|
75
|
+
print(" 带工具变量的GMM测试通过")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_gmm_no_constant():
|
|
79
|
+
"""测试不包含常数项的GMM"""
|
|
80
|
+
print("测试不包含常数项的GMM...")
|
|
81
|
+
|
|
82
|
+
# 生成测试数据
|
|
83
|
+
np.random.seed(42)
|
|
84
|
+
n = 50
|
|
85
|
+
x = np.random.randn(n)
|
|
86
|
+
# 真实模型: y = 2*x + noise
|
|
87
|
+
y = 2*x + np.random.randn(n) * 0.3
|
|
88
|
+
|
|
89
|
+
# 执行GMM回归(不包含常数项)
|
|
90
|
+
result = gmm_estimation(y.tolist(), x.tolist(), constant=False, feature_names=['x1'])
|
|
91
|
+
|
|
92
|
+
# 验证系数数量
|
|
93
|
+
assert len(result.coefficients) == 1, "应该有1个系数(不包括常数项)"
|
|
94
|
+
|
|
95
|
+
print(" 系数:", result.coefficients)
|
|
96
|
+
print(" 不包含常数项的GMM测试通过")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def test_gmm_errors():
|
|
100
|
+
"""测试GMM错误处理"""
|
|
101
|
+
print("测试GMM错误处理...")
|
|
102
|
+
|
|
103
|
+
# 测试空数据
|
|
104
|
+
try:
|
|
105
|
+
gmm_estimation([], [])
|
|
106
|
+
assert False, "应该抛出ValueError异常"
|
|
107
|
+
except ValueError:
|
|
108
|
+
print(" 空数据错误处理正确")
|
|
109
|
+
|
|
110
|
+
# 测试不一致的数据维度
|
|
111
|
+
try:
|
|
112
|
+
y_data = [1, 2, 3]
|
|
113
|
+
x_data = [[1, 2], [3, 4]] # 维度不匹配
|
|
114
|
+
gmm_estimation(y_data, x_data)
|
|
115
|
+
assert False, "应该抛出ValueError异常"
|
|
116
|
+
except ValueError:
|
|
117
|
+
print(" 数据维度不一致错误处理正确")
|
|
118
|
+
|
|
119
|
+
print(" GMM错误处理测试通过")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
if __name__ == "__main__":
|
|
123
|
+
print("开始测试GMM模型...")
|
|
124
|
+
test_gmm_basic()
|
|
125
|
+
test_gmm_with_instruments()
|
|
126
|
+
test_gmm_no_constant()
|
|
127
|
+
test_gmm_errors()
|
|
128
|
+
print("所有GMM测试通过!")
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""
|
|
2
|
+
MLE模型测试脚本
|
|
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.basic_parametric_estimation.mle.mle_model import mle_estimation, MLEResult
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_mle_normal():
|
|
16
|
+
"""测试正态分布MLE"""
|
|
17
|
+
print("测试正态分布MLE...")
|
|
18
|
+
|
|
19
|
+
# 生成正态分布测试数据
|
|
20
|
+
np.random.seed(42)
|
|
21
|
+
data = np.random.normal(5, 2, 100).tolist() # 均值5,标准差2
|
|
22
|
+
|
|
23
|
+
# 执行MLE估计
|
|
24
|
+
result = mle_estimation(data, distribution="normal")
|
|
25
|
+
|
|
26
|
+
# 验证结果类型
|
|
27
|
+
assert isinstance(result, MLEResult), "结果应为MLEResult类型"
|
|
28
|
+
|
|
29
|
+
# 验证参数数量
|
|
30
|
+
assert len(result.parameters) == 2, "正态分布应有2个参数(均值和标准差)"
|
|
31
|
+
|
|
32
|
+
# 验证参数名称
|
|
33
|
+
assert result.param_names == ["mu", "sigma"], "参数名称应为['mu', 'sigma']"
|
|
34
|
+
|
|
35
|
+
# 验证统计量合理性
|
|
36
|
+
assert result.n_obs == 100, "观测数量应为100"
|
|
37
|
+
assert result.log_likelihood < 0, "对数似然值应为负数"
|
|
38
|
+
|
|
39
|
+
print(" 估计参数:", result.parameters)
|
|
40
|
+
print(" 对数似然值:", result.log_likelihood)
|
|
41
|
+
print(" AIC:", result.aic)
|
|
42
|
+
print(" 正态分布MLE测试通过")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_mle_poisson():
|
|
46
|
+
"""测试泊松分布MLE"""
|
|
47
|
+
print("测试泊松分布MLE...")
|
|
48
|
+
|
|
49
|
+
# 生成泊松分布测试数据
|
|
50
|
+
np.random.seed(42)
|
|
51
|
+
data = np.random.poisson(3, 100).tolist() # 均值3
|
|
52
|
+
|
|
53
|
+
# 执行MLE估计
|
|
54
|
+
result = mle_estimation(data, distribution="poisson")
|
|
55
|
+
|
|
56
|
+
# 验证结果类型
|
|
57
|
+
assert isinstance(result, MLEResult), "结果应为MLEResult类型"
|
|
58
|
+
|
|
59
|
+
# 验证参数数量
|
|
60
|
+
assert len(result.parameters) == 1, "泊松分布应有1个参数"
|
|
61
|
+
|
|
62
|
+
# 验证参数名称
|
|
63
|
+
assert result.param_names == ["lambda"], "参数名称应为['lambda']"
|
|
64
|
+
|
|
65
|
+
print(" 估计参数:", result.parameters)
|
|
66
|
+
print(" 泊松分布MLE测试通过")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def test_mle_exponential():
|
|
70
|
+
"""测试指数分布MLE"""
|
|
71
|
+
print("测试指数分布MLE...")
|
|
72
|
+
|
|
73
|
+
# 生成指数分布测试数据
|
|
74
|
+
np.random.seed(42)
|
|
75
|
+
data = np.random.exponential(2, 100).tolist() # 均值2
|
|
76
|
+
|
|
77
|
+
# 执行MLE估计
|
|
78
|
+
result = mle_estimation(data, distribution="exponential")
|
|
79
|
+
|
|
80
|
+
# 验证结果类型
|
|
81
|
+
assert isinstance(result, MLEResult), "结果应为MLEResult类型"
|
|
82
|
+
|
|
83
|
+
# 验证参数数量
|
|
84
|
+
assert len(result.parameters) == 1, "指数分布应有1个参数"
|
|
85
|
+
|
|
86
|
+
# 验证参数名称
|
|
87
|
+
assert result.param_names == ["lambda"], "参数名称应为['lambda']"
|
|
88
|
+
|
|
89
|
+
print(" 估计参数:", result.parameters)
|
|
90
|
+
print(" 指数分布MLE测试通过")
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def test_mle_errors():
|
|
94
|
+
"""测试MLE错误处理"""
|
|
95
|
+
print("测试MLE错误处理...")
|
|
96
|
+
|
|
97
|
+
# 测试空数据
|
|
98
|
+
try:
|
|
99
|
+
mle_estimation([])
|
|
100
|
+
assert False, "应该抛出ValueError异常"
|
|
101
|
+
except ValueError:
|
|
102
|
+
print(" 空数据错误处理正确")
|
|
103
|
+
|
|
104
|
+
# 测试不支持的分布类型
|
|
105
|
+
try:
|
|
106
|
+
mle_estimation([1, 2, 3], distribution="unsupported")
|
|
107
|
+
assert False, "应该抛出ValueError异常"
|
|
108
|
+
except ValueError:
|
|
109
|
+
print(" 不支持的分布类型错误处理正确")
|
|
110
|
+
|
|
111
|
+
# 测试负值的指数分布数据
|
|
112
|
+
try:
|
|
113
|
+
mle_estimation([-1, 0, 1], distribution="exponential")
|
|
114
|
+
assert False, "应该抛出ValueError异常"
|
|
115
|
+
except ValueError:
|
|
116
|
+
print(" 负值指数分布数据错误处理正确")
|
|
117
|
+
|
|
118
|
+
print(" MLE错误处理测试通过")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
if __name__ == "__main__":
|
|
122
|
+
print("开始测试MLE模型...")
|
|
123
|
+
test_mle_normal()
|
|
124
|
+
test_mle_poisson()
|
|
125
|
+
test_mle_exponential()
|
|
126
|
+
test_mle_errors()
|
|
127
|
+
print("所有MLE测试通过!")
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""
|
|
2
|
+
OLS模型测试脚本
|
|
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.basic_parametric_estimation.ols.ols_model import ols_regression, OLSResult
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_ols_basic():
|
|
16
|
+
"""测试基本OLS功能"""
|
|
17
|
+
print("测试基本OLS功能...")
|
|
18
|
+
|
|
19
|
+
# 生成测试数据
|
|
20
|
+
np.random.seed(42)
|
|
21
|
+
n = 100
|
|
22
|
+
x1 = np.random.randn(n)
|
|
23
|
+
x2 = np.random.randn(n)
|
|
24
|
+
# 真实模型: y = 2 + 3*x1 + 2*x2 + noise
|
|
25
|
+
y = 2 + 3*x1 + 2*x2 + np.random.randn(n) * 0.5
|
|
26
|
+
|
|
27
|
+
# 准备数据
|
|
28
|
+
x_data = [[x1[i], x2[i]] for i in range(n)]
|
|
29
|
+
y_data = y.tolist()
|
|
30
|
+
|
|
31
|
+
# 执行OLS回归
|
|
32
|
+
result = ols_regression(y_data, x_data, feature_names=['x1', 'x2'])
|
|
33
|
+
|
|
34
|
+
# 验证结果类型
|
|
35
|
+
assert isinstance(result, OLSResult), "结果应为OLSResult类型"
|
|
36
|
+
|
|
37
|
+
# 验证系数数量
|
|
38
|
+
assert len(result.coefficients) == 3, "应该有3个系数(包括常数项)"
|
|
39
|
+
|
|
40
|
+
# 验证统计量合理性
|
|
41
|
+
assert 0 <= result.r_squared <= 1, "R方应在0到1之间"
|
|
42
|
+
assert result.n_obs == n, f"观测数量应为{n}"
|
|
43
|
+
|
|
44
|
+
print(" 系数:", result.coefficients)
|
|
45
|
+
print(" R方:", result.r_squared)
|
|
46
|
+
print(" 调整R方:", result.adj_r_squared)
|
|
47
|
+
print(" F统计量:", result.f_statistic)
|
|
48
|
+
print(" 基本OLS功能测试通过")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_ols_no_constant():
|
|
52
|
+
"""测试不包含常数项的OLS"""
|
|
53
|
+
print("测试不包含常数项的OLS...")
|
|
54
|
+
|
|
55
|
+
# 生成测试数据
|
|
56
|
+
np.random.seed(42)
|
|
57
|
+
n = 50
|
|
58
|
+
x = np.random.randn(n)
|
|
59
|
+
# 真实模型: y = 2*x + noise
|
|
60
|
+
y = 2*x + np.random.randn(n) * 0.3
|
|
61
|
+
|
|
62
|
+
# 执行OLS回归(不包含常数项)
|
|
63
|
+
result = ols_regression(y.tolist(), x.tolist(), constant=False, feature_names=['x1'])
|
|
64
|
+
|
|
65
|
+
# 验证系数数量
|
|
66
|
+
assert len(result.coefficients) == 1, "应该有1个系数(不包括常数项)"
|
|
67
|
+
|
|
68
|
+
print(" 系数:", result.coefficients)
|
|
69
|
+
print(" 不包含常数项的OLS测试通过")
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_ols_errors():
|
|
73
|
+
"""测试OLS错误处理"""
|
|
74
|
+
print("测试OLS错误处理...")
|
|
75
|
+
|
|
76
|
+
# 测试空数据
|
|
77
|
+
try:
|
|
78
|
+
ols_regression([], [])
|
|
79
|
+
assert False, "应该抛出ValueError异常"
|
|
80
|
+
except ValueError:
|
|
81
|
+
print(" 空数据错误处理正确")
|
|
82
|
+
|
|
83
|
+
# 测试不一致的数据维度
|
|
84
|
+
try:
|
|
85
|
+
y_data = [1, 2, 3]
|
|
86
|
+
x_data = [[1, 2], [3, 4]] # 维度不匹配
|
|
87
|
+
ols_regression(y_data, x_data)
|
|
88
|
+
assert False, "应该抛出ValueError异常"
|
|
89
|
+
except ValueError:
|
|
90
|
+
print(" 数据维度不一致错误处理正确")
|
|
91
|
+
|
|
92
|
+
print(" OLS错误处理测试通过")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
if __name__ == "__main__":
|
|
96
|
+
print("开始测试OLS模型...")
|
|
97
|
+
test_ols_basic()
|
|
98
|
+
test_ols_no_constant()
|
|
99
|
+
test_ols_errors()
|
|
100
|
+
print("所有OLS测试通过!")
|
|
@@ -0,0 +1,86 @@
|
|
|
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.model_specification_diagnostics_robust_inference.diagnostic_tests.diagnostic_tests_model import diagnostic_tests, DiagnosticTestsResult
|
|
15
|
+
DIAGNOSTIC_TESTS_AVAILABLE = True
|
|
16
|
+
except ImportError:
|
|
17
|
+
DIAGNOSTIC_TESTS_AVAILABLE = False
|
|
18
|
+
print("警告: 未找到诊断检验模型,相关测试将被跳过")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_diagnostic_tests_basic():
|
|
22
|
+
"""测试基本诊断检验功能"""
|
|
23
|
+
if not DIAGNOSTIC_TESTS_AVAILABLE:
|
|
24
|
+
print("跳过诊断检验测试(模块不可用)")
|
|
25
|
+
return
|
|
26
|
+
|
|
27
|
+
print("测试基本诊断检验功能...")
|
|
28
|
+
|
|
29
|
+
# 生成测试数据
|
|
30
|
+
np.random.seed(42)
|
|
31
|
+
n = 100
|
|
32
|
+
x1 = np.random.randn(n)
|
|
33
|
+
x2 = np.random.randn(n)
|
|
34
|
+
# 真实模型: y = 2 + 3*x1 + 2*x2 + noise
|
|
35
|
+
y = 2 + 3*x1 + 2*x2 + np.random.randn(n) * 0.5
|
|
36
|
+
|
|
37
|
+
# 准备数据
|
|
38
|
+
x_data = [[x1[i], x2[i]] for i in range(n)]
|
|
39
|
+
y_data = y.tolist()
|
|
40
|
+
|
|
41
|
+
# 执行诊断检验
|
|
42
|
+
result = diagnostic_tests(y_data, x_data, feature_names=['x1', 'x2'])
|
|
43
|
+
|
|
44
|
+
# 验证结果类型
|
|
45
|
+
assert isinstance(result, DiagnosticTestsResult), "结果应为DiagnosticTestsResult类型"
|
|
46
|
+
|
|
47
|
+
# 验证统计量合理性
|
|
48
|
+
assert result.n_obs == n, f"观测数量应为{n}"
|
|
49
|
+
|
|
50
|
+
print(" Jarque-Bera检验统计量:", result.jarque_bera_stat)
|
|
51
|
+
print(" Breusch-Pagan检验统计量:", result.breusch_pagan_stat)
|
|
52
|
+
print(" 基本诊断检验功能测试通过")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_diagnostic_tests_serial_correlation():
|
|
56
|
+
"""测试序列相关诊断检验"""
|
|
57
|
+
if not DIAGNOSTIC_TESTS_AVAILABLE:
|
|
58
|
+
print("跳过序列相关诊断检验测试(模块不可用)")
|
|
59
|
+
return
|
|
60
|
+
|
|
61
|
+
print("测试序列相关诊断检验...")
|
|
62
|
+
|
|
63
|
+
# 生成带序列相关的测试数据
|
|
64
|
+
np.random.seed(42)
|
|
65
|
+
n = 100
|
|
66
|
+
# 生成自相关误差项
|
|
67
|
+
errors = [0]
|
|
68
|
+
for i in range(1, n):
|
|
69
|
+
errors.append(0.5 * errors[i-1] + np.random.randn() * 0.3)
|
|
70
|
+
|
|
71
|
+
x = np.random.randn(n)
|
|
72
|
+
# 真实模型: y = 2 + 3*x + errors (带序列相关)
|
|
73
|
+
y = 2 + 3*x + np.array(errors)
|
|
74
|
+
|
|
75
|
+
# 执行诊断检验
|
|
76
|
+
result = diagnostic_tests(y.tolist(), x.tolist(), feature_names=['x1'])
|
|
77
|
+
|
|
78
|
+
print(" Durbin-Watson统计量:", result.durbin_watson_stat)
|
|
79
|
+
print(" 序列相关诊断检验测试通过")
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
if __name__ == "__main__":
|
|
83
|
+
print("开始测试诊断检验模型...")
|
|
84
|
+
test_diagnostic_tests_basic()
|
|
85
|
+
test_diagnostic_tests_serial_correlation()
|
|
86
|
+
print("诊断检验测试完成!")
|
|
@@ -0,0 +1,89 @@
|
|
|
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.model_specification_diagnostics_robust_inference.robust_errors.robust_errors_model import robust_errors_regression, RobustErrorsResult
|
|
15
|
+
ROBUST_ERRORS_AVAILABLE = True
|
|
16
|
+
except ImportError:
|
|
17
|
+
ROBUST_ERRORS_AVAILABLE = False
|
|
18
|
+
print("警告: 未找到稳健标准误模型,相关测试将被跳过")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_robust_errors_basic():
|
|
22
|
+
"""测试基本稳健标准误功能"""
|
|
23
|
+
if not ROBUST_ERRORS_AVAILABLE:
|
|
24
|
+
print("跳过稳健标准误测试(模块不可用)")
|
|
25
|
+
return
|
|
26
|
+
|
|
27
|
+
print("测试基本稳健标准误功能...")
|
|
28
|
+
|
|
29
|
+
# 生成测试数据
|
|
30
|
+
np.random.seed(42)
|
|
31
|
+
n = 100
|
|
32
|
+
x1 = np.random.randn(n)
|
|
33
|
+
x2 = np.random.randn(n)
|
|
34
|
+
# 真实模型: y = 2 + 3*x1 + 2*x2 + noise (异方差)
|
|
35
|
+
noise = np.random.randn(n) * (0.5 + 0.3 * np.abs(x1)) # 异方差噪声
|
|
36
|
+
y = 2 + 3*x1 + 2*x2 + noise
|
|
37
|
+
|
|
38
|
+
# 准备数据
|
|
39
|
+
x_data = [[x1[i], x2[i]] for i in range(n)]
|
|
40
|
+
y_data = y.tolist()
|
|
41
|
+
|
|
42
|
+
# 执行稳健标准误回归
|
|
43
|
+
result = robust_errors_regression(y_data, x_data, feature_names=['x1', 'x2'])
|
|
44
|
+
|
|
45
|
+
# 验证结果类型
|
|
46
|
+
assert isinstance(result, RobustErrorsResult), "结果应为RobustErrorsResult类型"
|
|
47
|
+
|
|
48
|
+
# 验证系数数量
|
|
49
|
+
assert len(result.coefficients) == 3, "应该有3个系数(包括常数项)"
|
|
50
|
+
|
|
51
|
+
# 验证统计量合理性
|
|
52
|
+
assert result.n_obs == n, f"观测数量应为{n}"
|
|
53
|
+
|
|
54
|
+
print(" 系数:", result.coefficients)
|
|
55
|
+
print(" 稳健标准误:", result.robust_std_errors)
|
|
56
|
+
print(" 基本稳健标准误功能测试通过")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_robust_errors_no_constant():
|
|
60
|
+
"""测试不包含常数项的稳健标准误"""
|
|
61
|
+
if not ROBUST_ERRORS_AVAILABLE:
|
|
62
|
+
print("跳过稳健标准误测试(模块不可用)")
|
|
63
|
+
return
|
|
64
|
+
|
|
65
|
+
print("测试不包含常数项的稳健标准误...")
|
|
66
|
+
|
|
67
|
+
# 生成测试数据
|
|
68
|
+
np.random.seed(42)
|
|
69
|
+
n = 50
|
|
70
|
+
x = np.random.randn(n)
|
|
71
|
+
# 真实模型: y = 2*x + noise (异方差)
|
|
72
|
+
noise = np.random.randn(n) * (0.3 + 0.2 * np.abs(x)) # 异方差噪声
|
|
73
|
+
y = 2*x + noise
|
|
74
|
+
|
|
75
|
+
# 执行稳健标准误回归(不包含常数项)
|
|
76
|
+
result = robust_errors_regression(y.tolist(), x.tolist(), constant=False, feature_names=['x1'])
|
|
77
|
+
|
|
78
|
+
# 验证系数数量
|
|
79
|
+
assert len(result.coefficients) == 1, "应该有1个系数(不包括常数项)"
|
|
80
|
+
|
|
81
|
+
print(" 系数:", result.coefficients)
|
|
82
|
+
print(" 不包含常数项的稳健标准误测试通过")
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if __name__ == "__main__":
|
|
86
|
+
print("开始测试稳健标准误模型...")
|
|
87
|
+
test_robust_errors_basic()
|
|
88
|
+
test_robust_errors_no_constant()
|
|
89
|
+
print("稳健标准误测试完成!")
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ARIMA模型测试脚本
|
|
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.arima_model import arima_model, ARIMAResult
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_arima_basic():
|
|
16
|
+
"""测试基本ARIMA功能"""
|
|
17
|
+
print("测试基本ARIMA功能...")
|
|
18
|
+
|
|
19
|
+
# 生成AR(1)过程数据
|
|
20
|
+
np.random.seed(42)
|
|
21
|
+
n = 100
|
|
22
|
+
ar_param = 0.6
|
|
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
|
+
# 执行ARIMA模型 (1,0,0) 即 AR(1)
|
|
30
|
+
result = arima_model(data, order=(1, 0, 0), forecast_steps=5)
|
|
31
|
+
|
|
32
|
+
# 验证结果类型
|
|
33
|
+
assert isinstance(result, ARIMAResult), "结果应为ARIMAResult类型"
|
|
34
|
+
|
|
35
|
+
# 验证模型类型
|
|
36
|
+
assert "ARIMA(1,0,0)" in result.model_type, "模型类型应为ARIMA(1,0,0)"
|
|
37
|
+
|
|
38
|
+
# 验证系数数量
|
|
39
|
+
assert len(result.coefficients) >= 1, "应该至少有1个系数"
|
|
40
|
+
|
|
41
|
+
# 验证统计量合理性
|
|
42
|
+
assert result.n_obs == n, f"观测数量应为{n}"
|
|
43
|
+
assert len(result.forecast) == 5, "预测步数应为5"
|
|
44
|
+
|
|
45
|
+
print(" 模型类型:", result.model_type)
|
|
46
|
+
print(" 系数:", result.coefficients)
|
|
47
|
+
print(" AIC:", result.aic)
|
|
48
|
+
print(" 预测值:", result.forecast[:3], "...")
|
|
49
|
+
print(" 基本ARIMA功能测试通过")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_arima_integrated():
|
|
53
|
+
"""测试带积分的ARIMA模型"""
|
|
54
|
+
print("测试带积分的ARIMA模型...")
|
|
55
|
+
|
|
56
|
+
# 生成带趋势的随机游走数据
|
|
57
|
+
np.random.seed(42)
|
|
58
|
+
n = 80
|
|
59
|
+
data = [0] # 初始值
|
|
60
|
+
|
|
61
|
+
# 生成随机游走过程: y[t] = y[t-1] + noise
|
|
62
|
+
for i in range(1, n):
|
|
63
|
+
data.append(data[i-1] + np.random.randn() * 0.3)
|
|
64
|
+
|
|
65
|
+
# 添加线性趋势
|
|
66
|
+
data = [data[i] + 0.1 * i for i in range(len(data))]
|
|
67
|
+
|
|
68
|
+
# 执行ARIMA模型 (0,1,0) 即随机游走
|
|
69
|
+
result = arima_model(data, order=(0, 1, 0), forecast_steps=3)
|
|
70
|
+
|
|
71
|
+
# 验证模型类型
|
|
72
|
+
assert "ARIMA(0,1,0)" in result.model_type, "模型类型应为ARIMA(0,1,0)"
|
|
73
|
+
|
|
74
|
+
print(" 模型类型:", result.model_type)
|
|
75
|
+
print(" 预测值:", result.forecast)
|
|
76
|
+
print(" 带积分的ARIMA模型测试通过")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_arima_errors():
|
|
80
|
+
"""测试ARIMA错误处理"""
|
|
81
|
+
print("测试ARIMA错误处理...")
|
|
82
|
+
|
|
83
|
+
# 测试空数据
|
|
84
|
+
try:
|
|
85
|
+
arima_model([])
|
|
86
|
+
assert False, "应该抛出异常"
|
|
87
|
+
except Exception:
|
|
88
|
+
print(" 空数据错误处理正确")
|
|
89
|
+
|
|
90
|
+
print(" ARIMA错误处理测试通过")
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
if __name__ == "__main__":
|
|
94
|
+
print("开始测试ARIMA模型...")
|
|
95
|
+
test_arima_basic()
|
|
96
|
+
test_arima_integrated()
|
|
97
|
+
test_arima_errors()
|
|
98
|
+
print("所有ARIMA测试通过!")
|