aigroup-econ-mcp 1.3.3__py3-none-any.whl → 2.0.1__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.
Files changed (198) hide show
  1. .gitignore +253 -0
  2. PKG-INFO +732 -0
  3. README.md +687 -0
  4. __init__.py +14 -0
  5. aigroup_econ_mcp-2.0.1.dist-info/METADATA +732 -0
  6. aigroup_econ_mcp-2.0.1.dist-info/RECORD +170 -0
  7. aigroup_econ_mcp-2.0.1.dist-info/entry_points.txt +2 -0
  8. aigroup_econ_mcp-2.0.1.dist-info/licenses/LICENSE +21 -0
  9. cli.py +32 -0
  10. econometrics/README.md +18 -0
  11. econometrics/__init__.py +191 -0
  12. econometrics/advanced_methods/modern_computing_machine_learning/__init__.py +30 -0
  13. econometrics/advanced_methods/modern_computing_machine_learning/causal_forest.py +253 -0
  14. econometrics/advanced_methods/modern_computing_machine_learning/double_ml.py +268 -0
  15. econometrics/advanced_methods/modern_computing_machine_learning/gradient_boosting.py +249 -0
  16. econometrics/advanced_methods/modern_computing_machine_learning/hierarchical_clustering.py +243 -0
  17. econometrics/advanced_methods/modern_computing_machine_learning/kmeans_clustering.py +293 -0
  18. econometrics/advanced_methods/modern_computing_machine_learning/neural_network.py +264 -0
  19. econometrics/advanced_methods/modern_computing_machine_learning/random_forest.py +195 -0
  20. econometrics/advanced_methods/modern_computing_machine_learning/support_vector_machine.py +226 -0
  21. econometrics/advanced_methods/modern_computing_machine_learning/test_all_modules.py +329 -0
  22. econometrics/advanced_methods/modern_computing_machine_learning/test_report.md +107 -0
  23. econometrics/basic_parametric_estimation/__init__.py +31 -0
  24. econometrics/basic_parametric_estimation/gmm/__init__.py +13 -0
  25. econometrics/basic_parametric_estimation/gmm/gmm_model.py +256 -0
  26. econometrics/basic_parametric_estimation/mle/__init__.py +13 -0
  27. econometrics/basic_parametric_estimation/mle/mle_model.py +241 -0
  28. econometrics/basic_parametric_estimation/ols/__init__.py +13 -0
  29. econometrics/basic_parametric_estimation/ols/ols_model.py +141 -0
  30. econometrics/causal_inference/__init__.py +66 -0
  31. econometrics/causal_inference/causal_identification_strategy/__init__.py +104 -0
  32. econometrics/causal_inference/causal_identification_strategy/control_function.py +112 -0
  33. econometrics/causal_inference/causal_identification_strategy/difference_in_differences.py +107 -0
  34. econometrics/causal_inference/causal_identification_strategy/event_study.py +119 -0
  35. econometrics/causal_inference/causal_identification_strategy/first_difference.py +89 -0
  36. econometrics/causal_inference/causal_identification_strategy/fixed_effects.py +103 -0
  37. econometrics/causal_inference/causal_identification_strategy/hausman_test.py +69 -0
  38. econometrics/causal_inference/causal_identification_strategy/instrumental_variables.py +145 -0
  39. econometrics/causal_inference/causal_identification_strategy/mediation_analysis.py +121 -0
  40. econometrics/causal_inference/causal_identification_strategy/moderation_analysis.py +109 -0
  41. econometrics/causal_inference/causal_identification_strategy/propensity_score_matching.py +140 -0
  42. econometrics/causal_inference/causal_identification_strategy/random_effects.py +100 -0
  43. econometrics/causal_inference/causal_identification_strategy/regression_discontinuity.py +98 -0
  44. econometrics/causal_inference/causal_identification_strategy/synthetic_control.py +111 -0
  45. econometrics/causal_inference/causal_identification_strategy/triple_difference.py +86 -0
  46. econometrics/distribution_analysis/__init__.py +28 -0
  47. econometrics/distribution_analysis/oaxaca_blinder.py +184 -0
  48. econometrics/distribution_analysis/time_series_decomposition.py +152 -0
  49. econometrics/distribution_analysis/variance_decomposition.py +179 -0
  50. econometrics/missing_data/__init__.py +18 -0
  51. econometrics/missing_data/imputation_methods.py +219 -0
  52. econometrics/missing_data/missing_data_measurement_error/__init__.py +0 -0
  53. econometrics/model_specification_diagnostics_robust_inference/README.md +173 -0
  54. econometrics/model_specification_diagnostics_robust_inference/__init__.py +78 -0
  55. econometrics/model_specification_diagnostics_robust_inference/diagnostic_tests/__init__.py +20 -0
  56. econometrics/model_specification_diagnostics_robust_inference/diagnostic_tests/diagnostic_tests_model.py +149 -0
  57. econometrics/model_specification_diagnostics_robust_inference/generalized_least_squares/__init__.py +15 -0
  58. econometrics/model_specification_diagnostics_robust_inference/generalized_least_squares/gls_model.py +130 -0
  59. econometrics/model_specification_diagnostics_robust_inference/model_selection/__init__.py +18 -0
  60. econometrics/model_specification_diagnostics_robust_inference/model_selection/model_selection_model.py +286 -0
  61. econometrics/model_specification_diagnostics_robust_inference/regularization/__init__.py +15 -0
  62. econometrics/model_specification_diagnostics_robust_inference/regularization/regularization_model.py +177 -0
  63. econometrics/model_specification_diagnostics_robust_inference/robust_errors/__init__.py +15 -0
  64. econometrics/model_specification_diagnostics_robust_inference/robust_errors/robust_errors_model.py +122 -0
  65. econometrics/model_specification_diagnostics_robust_inference/simultaneous_equations/__init__.py +15 -0
  66. econometrics/model_specification_diagnostics_robust_inference/simultaneous_equations/simultaneous_equations_model.py +246 -0
  67. econometrics/model_specification_diagnostics_robust_inference/weighted_least_squares/__init__.py +15 -0
  68. econometrics/model_specification_diagnostics_robust_inference/weighted_least_squares/wls_model.py +127 -0
  69. econometrics/nonparametric/__init__.py +35 -0
  70. econometrics/nonparametric/gam_model.py +117 -0
  71. econometrics/nonparametric/kernel_regression.py +161 -0
  72. econometrics/nonparametric/nonparametric_semiparametric_methods/__init__.py +0 -0
  73. econometrics/nonparametric/quantile_regression.py +249 -0
  74. econometrics/nonparametric/spline_regression.py +100 -0
  75. econometrics/spatial_econometrics/__init__.py +68 -0
  76. econometrics/spatial_econometrics/geographically_weighted_regression.py +211 -0
  77. econometrics/spatial_econometrics/gwr_simple.py +154 -0
  78. econometrics/spatial_econometrics/spatial_autocorrelation.py +356 -0
  79. econometrics/spatial_econometrics/spatial_durbin_model.py +177 -0
  80. econometrics/spatial_econometrics/spatial_econometrics_new/__init__.py +0 -0
  81. econometrics/spatial_econometrics/spatial_regression.py +315 -0
  82. econometrics/spatial_econometrics/spatial_weights.py +226 -0
  83. econometrics/specific_data_modeling/micro_discrete_limited_data/README.md +164 -0
  84. econometrics/specific_data_modeling/micro_discrete_limited_data/__init__.py +40 -0
  85. econometrics/specific_data_modeling/micro_discrete_limited_data/count_data_models.py +311 -0
  86. econometrics/specific_data_modeling/micro_discrete_limited_data/discrete_choice_models.py +294 -0
  87. econometrics/specific_data_modeling/micro_discrete_limited_data/limited_dependent_variable_models.py +282 -0
  88. econometrics/specific_data_modeling/survival_duration_data/__init__.py +0 -0
  89. econometrics/specific_data_modeling/time_series_panel_data/__init__.py +143 -0
  90. econometrics/specific_data_modeling/time_series_panel_data/arima_model.py +104 -0
  91. econometrics/specific_data_modeling/time_series_panel_data/cointegration_vecm.py +334 -0
  92. econometrics/specific_data_modeling/time_series_panel_data/dynamic_panel_models.py +653 -0
  93. econometrics/specific_data_modeling/time_series_panel_data/exponential_smoothing.py +176 -0
  94. econometrics/specific_data_modeling/time_series_panel_data/garch_model.py +198 -0
  95. econometrics/specific_data_modeling/time_series_panel_data/panel_diagnostics.py +125 -0
  96. econometrics/specific_data_modeling/time_series_panel_data/panel_var.py +60 -0
  97. econometrics/specific_data_modeling/time_series_panel_data/structural_break_tests.py +87 -0
  98. econometrics/specific_data_modeling/time_series_panel_data/time_varying_parameter_models.py +106 -0
  99. econometrics/specific_data_modeling/time_series_panel_data/unit_root_tests.py +204 -0
  100. econometrics/specific_data_modeling/time_series_panel_data/var_svar_model.py +372 -0
  101. econometrics/statistical_inference/__init__.py +21 -0
  102. econometrics/statistical_inference/bootstrap_methods.py +162 -0
  103. econometrics/statistical_inference/permutation_test.py +177 -0
  104. econometrics/statistical_inference/statistical_inference_techniques/__init__.py +0 -0
  105. econometrics/statistics/distribution_decomposition_methods/__init__.py +0 -0
  106. econometrics/survival_analysis/__init__.py +18 -0
  107. econometrics/survival_analysis/survival_models.py +259 -0
  108. econometrics/tests/basic_parametric_estimation_tests/__init__.py +3 -0
  109. econometrics/tests/basic_parametric_estimation_tests/test_gmm.py +128 -0
  110. econometrics/tests/basic_parametric_estimation_tests/test_mle.py +127 -0
  111. econometrics/tests/basic_parametric_estimation_tests/test_ols.py +100 -0
  112. econometrics/tests/causal_inference_tests/__init__.py +3 -0
  113. econometrics/tests/causal_inference_tests/detailed_test.py +441 -0
  114. econometrics/tests/causal_inference_tests/test_all_methods.py +418 -0
  115. econometrics/tests/causal_inference_tests/test_causal_identification_strategy.py +202 -0
  116. econometrics/tests/causal_inference_tests/test_difference_in_differences.py +53 -0
  117. econometrics/tests/causal_inference_tests/test_instrumental_variables.py +44 -0
  118. econometrics/tests/model_specification_diagnostics_tests/__init__.py +3 -0
  119. econometrics/tests/model_specification_diagnostics_tests/test_diagnostic_tests.py +86 -0
  120. econometrics/tests/model_specification_diagnostics_tests/test_robust_errors.py +89 -0
  121. econometrics/tests/specific_data_modeling_tests/__init__.py +3 -0
  122. econometrics/tests/specific_data_modeling_tests/test_arima.py +98 -0
  123. econometrics/tests/specific_data_modeling_tests/test_dynamic_panel.py +198 -0
  124. econometrics/tests/specific_data_modeling_tests/test_exponential_smoothing.py +105 -0
  125. econometrics/tests/specific_data_modeling_tests/test_garch.py +118 -0
  126. econometrics/tests/specific_data_modeling_tests/test_micro_discrete_limited_data.py +189 -0
  127. econometrics/tests/specific_data_modeling_tests/test_unit_root.py +156 -0
  128. econometrics/tests/specific_data_modeling_tests/test_var.py +124 -0
  129. econometrics//321/206/320/254/320/272/321/205/342/225/235/320/220/321/205/320/237/320/241/321/205/320/264/320/267/321/207/342/226/222/342/225/227/321/204/342/225/235/320/250/321/205/320/225/320/230/321/207/342/225/221/320/267/321/205/320/230/320/226/321/206/320/256/320/240.md +544 -0
  130. prompts/__init__.py +0 -0
  131. prompts/analysis_guides.py +43 -0
  132. pyproject.toml +85 -0
  133. resources/MCP_MASTER_GUIDE.md +422 -0
  134. resources/MCP_TOOLS_DATA_FORMAT_GUIDE.md +185 -0
  135. resources/__init__.py +0 -0
  136. server.py +97 -0
  137. tools/README.md +88 -0
  138. tools/__init__.py +119 -0
  139. tools/causal_inference_adapter.py +658 -0
  140. tools/data_loader.py +213 -0
  141. tools/decorators.py +38 -0
  142. tools/distribution_analysis_adapter.py +121 -0
  143. tools/econometrics_adapter.py +286 -0
  144. tools/gwr_simple_adapter.py +54 -0
  145. tools/machine_learning_adapter.py +567 -0
  146. tools/mcp_tool_groups/__init__.py +15 -0
  147. tools/mcp_tool_groups/basic_parametric_tools.py +173 -0
  148. tools/mcp_tool_groups/causal_inference_tools.py +643 -0
  149. tools/mcp_tool_groups/distribution_analysis_tools.py +169 -0
  150. tools/mcp_tool_groups/machine_learning_tools.py +422 -0
  151. tools/mcp_tool_groups/microecon_tools.py +325 -0
  152. tools/mcp_tool_groups/missing_data_tools.py +117 -0
  153. tools/mcp_tool_groups/model_specification_tools.py +402 -0
  154. tools/mcp_tool_groups/nonparametric_tools.py +225 -0
  155. tools/mcp_tool_groups/spatial_econometrics_tools.py +323 -0
  156. tools/mcp_tool_groups/statistical_inference_tools.py +131 -0
  157. tools/mcp_tool_groups/time_series_tools.py +494 -0
  158. tools/mcp_tools_registry.py +124 -0
  159. tools/microecon_adapter.py +412 -0
  160. tools/missing_data_adapter.py +73 -0
  161. tools/model_specification_adapter.py +369 -0
  162. tools/nonparametric_adapter.py +190 -0
  163. tools/output_formatter.py +563 -0
  164. tools/spatial_econometrics_adapter.py +318 -0
  165. tools/statistical_inference_adapter.py +90 -0
  166. tools/survival_analysis_adapter.py +46 -0
  167. tools/time_series_panel_data_adapter.py +858 -0
  168. tools/time_series_panel_data_tools.py +65 -0
  169. aigroup_econ_mcp/__init__.py +0 -19
  170. aigroup_econ_mcp/cli.py +0 -82
  171. aigroup_econ_mcp/config.py +0 -561
  172. aigroup_econ_mcp/server.py +0 -452
  173. aigroup_econ_mcp/tools/__init__.py +0 -19
  174. aigroup_econ_mcp/tools/base.py +0 -470
  175. aigroup_econ_mcp/tools/cache.py +0 -533
  176. aigroup_econ_mcp/tools/data_loader.py +0 -195
  177. aigroup_econ_mcp/tools/file_parser.py +0 -1027
  178. aigroup_econ_mcp/tools/machine_learning.py +0 -60
  179. aigroup_econ_mcp/tools/ml_ensemble.py +0 -210
  180. aigroup_econ_mcp/tools/ml_evaluation.py +0 -272
  181. aigroup_econ_mcp/tools/ml_models.py +0 -54
  182. aigroup_econ_mcp/tools/ml_regularization.py +0 -186
  183. aigroup_econ_mcp/tools/monitoring.py +0 -555
  184. aigroup_econ_mcp/tools/optimized_example.py +0 -229
  185. aigroup_econ_mcp/tools/panel_data.py +0 -619
  186. aigroup_econ_mcp/tools/regression.py +0 -214
  187. aigroup_econ_mcp/tools/statistics.py +0 -154
  188. aigroup_econ_mcp/tools/time_series.py +0 -698
  189. aigroup_econ_mcp/tools/timeout.py +0 -283
  190. aigroup_econ_mcp/tools/tool_descriptions.py +0 -410
  191. aigroup_econ_mcp/tools/tool_handlers.py +0 -1016
  192. aigroup_econ_mcp/tools/tool_registry.py +0 -478
  193. aigroup_econ_mcp/tools/validation.py +0 -482
  194. aigroup_econ_mcp-1.3.3.dist-info/METADATA +0 -525
  195. aigroup_econ_mcp-1.3.3.dist-info/RECORD +0 -30
  196. aigroup_econ_mcp-1.3.3.dist-info/entry_points.txt +0 -2
  197. /aigroup_econ_mcp-1.3.3.dist-info/licenses/LICENSE → /LICENSE +0 -0
  198. {aigroup_econ_mcp-1.3.3.dist-info → aigroup_econ_mcp-2.0.1.dist-info}/WHEEL +0 -0
@@ -0,0 +1,44 @@
1
+ """
2
+ 工具变量法测试
3
+ """
4
+
5
+ import numpy as np
6
+ import unittest
7
+ from econometrics.causal_inference.causal_identification_strategy import instrumental_variables_2sls
8
+
9
+
10
+ class TestInstrumentalVariables(unittest.TestCase):
11
+
12
+ def test_instrumental_variables_2sls(self):
13
+ """测试工具变量法"""
14
+ # 生成模拟数据
15
+ np.random.seed(42)
16
+ n = 100
17
+
18
+ # 工具变量
19
+ z = np.random.normal(0, 1, n)
20
+
21
+ # 内生变量(与误差项相关)
22
+ e1 = np.random.normal(0, 1, n)
23
+ x = 1 + 0.5 * z + e1
24
+
25
+ # 结果变量
26
+ e2 = np.random.normal(0, 1, n)
27
+ y = 2 + 1.5 * x + e2 + 0.3 * e1 # 包含内生性
28
+
29
+ # 执行工具变量回归
30
+ result = instrumental_variables_2sls(
31
+ y=y.tolist(),
32
+ x=x.reshape(-1, 1).tolist(),
33
+ instruments=z.reshape(-1, 1).tolist()
34
+ )
35
+
36
+ # 检查结果
37
+ self.assertIsNotNone(result.estimate)
38
+ self.assertIsNotNone(result.std_error)
39
+ self.assertIsNotNone(result.p_value)
40
+ self.assertGreater(result.n_observations, 0)
41
+
42
+
43
+ if __name__ == "__main__":
44
+ unittest.main()
@@ -0,0 +1,3 @@
1
+ """
2
+ 模型设定、诊断与稳健推断测试模块初始化文件
3
+ """
@@ -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,3 @@
1
+ """
2
+ 特定数据建模测试模块初始化文件
3
+ """
@@ -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测试通过!")
@@ -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("所有指数平滑测试通过!")