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.
- .gitignore +253 -0
- PKG-INFO +732 -0
- README.md +687 -0
- __init__.py +14 -0
- aigroup_econ_mcp-2.0.1.dist-info/METADATA +732 -0
- aigroup_econ_mcp-2.0.1.dist-info/RECORD +170 -0
- aigroup_econ_mcp-2.0.1.dist-info/entry_points.txt +2 -0
- aigroup_econ_mcp-2.0.1.dist-info/licenses/LICENSE +21 -0
- cli.py +32 -0
- econometrics/README.md +18 -0
- econometrics/__init__.py +191 -0
- econometrics/advanced_methods/modern_computing_machine_learning/__init__.py +30 -0
- econometrics/advanced_methods/modern_computing_machine_learning/causal_forest.py +253 -0
- econometrics/advanced_methods/modern_computing_machine_learning/double_ml.py +268 -0
- econometrics/advanced_methods/modern_computing_machine_learning/gradient_boosting.py +249 -0
- econometrics/advanced_methods/modern_computing_machine_learning/hierarchical_clustering.py +243 -0
- econometrics/advanced_methods/modern_computing_machine_learning/kmeans_clustering.py +293 -0
- econometrics/advanced_methods/modern_computing_machine_learning/neural_network.py +264 -0
- econometrics/advanced_methods/modern_computing_machine_learning/random_forest.py +195 -0
- econometrics/advanced_methods/modern_computing_machine_learning/support_vector_machine.py +226 -0
- econometrics/advanced_methods/modern_computing_machine_learning/test_all_modules.py +329 -0
- econometrics/advanced_methods/modern_computing_machine_learning/test_report.md +107 -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/__init__.py +66 -0
- econometrics/causal_inference/causal_identification_strategy/__init__.py +104 -0
- econometrics/causal_inference/causal_identification_strategy/control_function.py +112 -0
- econometrics/causal_inference/causal_identification_strategy/difference_in_differences.py +107 -0
- econometrics/causal_inference/causal_identification_strategy/event_study.py +119 -0
- econometrics/causal_inference/causal_identification_strategy/first_difference.py +89 -0
- econometrics/causal_inference/causal_identification_strategy/fixed_effects.py +103 -0
- econometrics/causal_inference/causal_identification_strategy/hausman_test.py +69 -0
- econometrics/causal_inference/causal_identification_strategy/instrumental_variables.py +145 -0
- econometrics/causal_inference/causal_identification_strategy/mediation_analysis.py +121 -0
- econometrics/causal_inference/causal_identification_strategy/moderation_analysis.py +109 -0
- econometrics/causal_inference/causal_identification_strategy/propensity_score_matching.py +140 -0
- econometrics/causal_inference/causal_identification_strategy/random_effects.py +100 -0
- econometrics/causal_inference/causal_identification_strategy/regression_discontinuity.py +98 -0
- econometrics/causal_inference/causal_identification_strategy/synthetic_control.py +111 -0
- econometrics/causal_inference/causal_identification_strategy/triple_difference.py +86 -0
- econometrics/distribution_analysis/__init__.py +28 -0
- econometrics/distribution_analysis/oaxaca_blinder.py +184 -0
- econometrics/distribution_analysis/time_series_decomposition.py +152 -0
- econometrics/distribution_analysis/variance_decomposition.py +179 -0
- econometrics/missing_data/__init__.py +18 -0
- econometrics/missing_data/imputation_methods.py +219 -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/__init__.py +35 -0
- econometrics/nonparametric/gam_model.py +117 -0
- econometrics/nonparametric/kernel_regression.py +161 -0
- econometrics/nonparametric/nonparametric_semiparametric_methods/__init__.py +0 -0
- econometrics/nonparametric/quantile_regression.py +249 -0
- econometrics/nonparametric/spline_regression.py +100 -0
- econometrics/spatial_econometrics/__init__.py +68 -0
- econometrics/spatial_econometrics/geographically_weighted_regression.py +211 -0
- econometrics/spatial_econometrics/gwr_simple.py +154 -0
- econometrics/spatial_econometrics/spatial_autocorrelation.py +356 -0
- econometrics/spatial_econometrics/spatial_durbin_model.py +177 -0
- econometrics/spatial_econometrics/spatial_econometrics_new/__init__.py +0 -0
- econometrics/spatial_econometrics/spatial_regression.py +315 -0
- econometrics/spatial_econometrics/spatial_weights.py +226 -0
- econometrics/specific_data_modeling/micro_discrete_limited_data/README.md +164 -0
- econometrics/specific_data_modeling/micro_discrete_limited_data/__init__.py +40 -0
- econometrics/specific_data_modeling/micro_discrete_limited_data/count_data_models.py +311 -0
- econometrics/specific_data_modeling/micro_discrete_limited_data/discrete_choice_models.py +294 -0
- econometrics/specific_data_modeling/micro_discrete_limited_data/limited_dependent_variable_models.py +282 -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/__init__.py +21 -0
- econometrics/statistical_inference/bootstrap_methods.py +162 -0
- econometrics/statistical_inference/permutation_test.py +177 -0
- econometrics/statistical_inference/statistical_inference_techniques/__init__.py +0 -0
- econometrics/statistics/distribution_decomposition_methods/__init__.py +0 -0
- econometrics/survival_analysis/__init__.py +18 -0
- econometrics/survival_analysis/survival_models.py +259 -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/causal_inference_tests/__init__.py +3 -0
- econometrics/tests/causal_inference_tests/detailed_test.py +441 -0
- econometrics/tests/causal_inference_tests/test_all_methods.py +418 -0
- econometrics/tests/causal_inference_tests/test_causal_identification_strategy.py +202 -0
- econometrics/tests/causal_inference_tests/test_difference_in_differences.py +53 -0
- econometrics/tests/causal_inference_tests/test_instrumental_variables.py +44 -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_micro_discrete_limited_data.py +189 -0
- econometrics/tests/specific_data_modeling_tests/test_unit_root.py +156 -0
- econometrics/tests/specific_data_modeling_tests/test_var.py +124 -0
- 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
- prompts/__init__.py +0 -0
- prompts/analysis_guides.py +43 -0
- pyproject.toml +85 -0
- resources/MCP_MASTER_GUIDE.md +422 -0
- resources/MCP_TOOLS_DATA_FORMAT_GUIDE.md +185 -0
- resources/__init__.py +0 -0
- server.py +97 -0
- tools/README.md +88 -0
- tools/__init__.py +119 -0
- tools/causal_inference_adapter.py +658 -0
- tools/data_loader.py +213 -0
- tools/decorators.py +38 -0
- tools/distribution_analysis_adapter.py +121 -0
- tools/econometrics_adapter.py +286 -0
- tools/gwr_simple_adapter.py +54 -0
- tools/machine_learning_adapter.py +567 -0
- tools/mcp_tool_groups/__init__.py +15 -0
- tools/mcp_tool_groups/basic_parametric_tools.py +173 -0
- tools/mcp_tool_groups/causal_inference_tools.py +643 -0
- tools/mcp_tool_groups/distribution_analysis_tools.py +169 -0
- tools/mcp_tool_groups/machine_learning_tools.py +422 -0
- tools/mcp_tool_groups/microecon_tools.py +325 -0
- tools/mcp_tool_groups/missing_data_tools.py +117 -0
- tools/mcp_tool_groups/model_specification_tools.py +402 -0
- tools/mcp_tool_groups/nonparametric_tools.py +225 -0
- tools/mcp_tool_groups/spatial_econometrics_tools.py +323 -0
- tools/mcp_tool_groups/statistical_inference_tools.py +131 -0
- tools/mcp_tool_groups/time_series_tools.py +494 -0
- tools/mcp_tools_registry.py +124 -0
- tools/microecon_adapter.py +412 -0
- tools/missing_data_adapter.py +73 -0
- tools/model_specification_adapter.py +369 -0
- tools/nonparametric_adapter.py +190 -0
- tools/output_formatter.py +563 -0
- tools/spatial_econometrics_adapter.py +318 -0
- tools/statistical_inference_adapter.py +90 -0
- tools/survival_analysis_adapter.py +46 -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 -19
- aigroup_econ_mcp/tools/base.py +0 -470
- aigroup_econ_mcp/tools/cache.py +0 -533
- aigroup_econ_mcp/tools/data_loader.py +0 -195
- aigroup_econ_mcp/tools/file_parser.py +0 -1027
- 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 -186
- 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 -619
- aigroup_econ_mcp/tools/regression.py +0 -214
- aigroup_econ_mcp/tools/statistics.py +0 -154
- aigroup_econ_mcp/tools/time_series.py +0 -698
- aigroup_econ_mcp/tools/timeout.py +0 -283
- aigroup_econ_mcp/tools/tool_descriptions.py +0 -410
- aigroup_econ_mcp/tools/tool_handlers.py +0 -1016
- aigroup_econ_mcp/tools/tool_registry.py +0 -478
- aigroup_econ_mcp/tools/validation.py +0 -482
- aigroup_econ_mcp-1.3.3.dist-info/METADATA +0 -525
- aigroup_econ_mcp-1.3.3.dist-info/RECORD +0 -30
- aigroup_econ_mcp-1.3.3.dist-info/entry_points.txt +0 -2
- /aigroup_econ_mcp-1.3.3.dist-info/licenses/LICENSE → /LICENSE +0 -0
- {aigroup_econ_mcp-1.3.3.dist-info → aigroup_econ_mcp-2.0.1.dist-info}/WHEEL +0 -0
|
@@ -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,189 @@
|
|
|
1
|
+
"""
|
|
2
|
+
微观离散与受限数据模型测试
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import pandas as pd
|
|
7
|
+
import sys
|
|
8
|
+
import os
|
|
9
|
+
from scipy import stats
|
|
10
|
+
|
|
11
|
+
# 添加项目根目录到路径
|
|
12
|
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))
|
|
13
|
+
|
|
14
|
+
try:
|
|
15
|
+
from econometrics.specific_data_modeling.micro_discrete_limited_data import (
|
|
16
|
+
LogitModel,
|
|
17
|
+
ProbitModel,
|
|
18
|
+
TobitModel,
|
|
19
|
+
PoissonModel,
|
|
20
|
+
NegativeBinomialModel
|
|
21
|
+
)
|
|
22
|
+
HAS_MODELS = True
|
|
23
|
+
except ImportError as e:
|
|
24
|
+
print(f"导入模型时出错: {e}")
|
|
25
|
+
HAS_MODELS = False
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_logit_model():
|
|
29
|
+
"""测试Logit模型"""
|
|
30
|
+
print("测试Logit模型...")
|
|
31
|
+
|
|
32
|
+
# 生成模拟数据
|
|
33
|
+
np.random.seed(42)
|
|
34
|
+
n = 1000
|
|
35
|
+
X = np.random.normal(0, 1, (n, 2))
|
|
36
|
+
coef_true = np.array([1.0, -0.5])
|
|
37
|
+
linear_pred = np.dot(X, coef_true)
|
|
38
|
+
prob = 1 / (1 + np.exp(-linear_pred))
|
|
39
|
+
y = np.random.binomial(1, prob)
|
|
40
|
+
|
|
41
|
+
# 拟合模型
|
|
42
|
+
model = LogitModel()
|
|
43
|
+
model.fit(X, y)
|
|
44
|
+
|
|
45
|
+
print(f"真实系数: {coef_true}")
|
|
46
|
+
print(f"估计系数: {model.results_.params[1:]}") # 排除常数项
|
|
47
|
+
print(f"常数项: {model.results_.params[0]}")
|
|
48
|
+
print(f"AIC: {model.results_.aic}")
|
|
49
|
+
print(f"BIC: {model.results_.bic}")
|
|
50
|
+
print()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_probit_model():
|
|
54
|
+
"""测试Probit模型"""
|
|
55
|
+
print("测试Probit模型...")
|
|
56
|
+
|
|
57
|
+
# 生成模拟数据
|
|
58
|
+
np.random.seed(42)
|
|
59
|
+
n = 1000
|
|
60
|
+
X = np.random.normal(0, 1, (n, 2))
|
|
61
|
+
coef_true = np.array([0.5, -0.3])
|
|
62
|
+
linear_pred = np.dot(X, coef_true)
|
|
63
|
+
prob = np.clip(stats.norm.cdf(linear_pred), 1e-10, 1-1e-10)
|
|
64
|
+
y = np.random.binomial(1, prob)
|
|
65
|
+
|
|
66
|
+
# 拟合模型
|
|
67
|
+
model = ProbitModel()
|
|
68
|
+
model.fit(X, y)
|
|
69
|
+
|
|
70
|
+
print(f"真实系数: {coef_true}")
|
|
71
|
+
print(f"估计系数: {model.results_.params[1:]}") # 排除常数项
|
|
72
|
+
print(f"常数项: {model.results_.params[0]}")
|
|
73
|
+
print(f"AIC: {model.results_.aic}")
|
|
74
|
+
print(f"BIC: {model.results_.bic}")
|
|
75
|
+
print()
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_tobit_model():
|
|
79
|
+
"""测试Tobit模型"""
|
|
80
|
+
print("测试Tobit模型...")
|
|
81
|
+
|
|
82
|
+
# 生成模拟数据
|
|
83
|
+
np.random.seed(42)
|
|
84
|
+
n = 1000
|
|
85
|
+
X = np.random.normal(0, 1, (n, 2))
|
|
86
|
+
coef_true = np.array([1.0, -0.5])
|
|
87
|
+
sigma_true = 0.5
|
|
88
|
+
|
|
89
|
+
# 生成潜在变量
|
|
90
|
+
y_latent = np.dot(X, coef_true) + np.random.normal(0, sigma_true, n)
|
|
91
|
+
|
|
92
|
+
# 截断:低于0的值设为0
|
|
93
|
+
y = np.where(y_latent > 0, y_latent, 0)
|
|
94
|
+
|
|
95
|
+
# 拟合模型
|
|
96
|
+
model = TobitModel(lower_bound=0)
|
|
97
|
+
model.fit(X, y)
|
|
98
|
+
|
|
99
|
+
print(f"真实系数: {coef_true}")
|
|
100
|
+
print(f"估计系数: {model.results_.params[1:]}") # 排除常数项
|
|
101
|
+
print(f"常数项: {model.results_.params[0]}")
|
|
102
|
+
print(f"对数似然: {model.results_.llf}")
|
|
103
|
+
print()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_poisson_model():
|
|
107
|
+
"""测试泊松模型"""
|
|
108
|
+
print("测试泊松模型...")
|
|
109
|
+
|
|
110
|
+
# 生成模拟数据
|
|
111
|
+
np.random.seed(42)
|
|
112
|
+
n = 1000
|
|
113
|
+
X = np.random.normal(0, 1, (n, 2))
|
|
114
|
+
coef_true = np.array([0.5, -0.3])
|
|
115
|
+
mu = np.exp(np.dot(X, coef_true))
|
|
116
|
+
y = np.random.poisson(mu)
|
|
117
|
+
|
|
118
|
+
# 拟合模型
|
|
119
|
+
model = PoissonModel()
|
|
120
|
+
model.fit(X, y)
|
|
121
|
+
|
|
122
|
+
print(f"真实系数: {coef_true}")
|
|
123
|
+
print(f"估计系数: {model.results_.params[1:]}") # 排除常数项
|
|
124
|
+
print(f"常数项: {model.results_.params[0]}")
|
|
125
|
+
print(f"对数似然: {model.results_.llf}")
|
|
126
|
+
print(f"AIC: {model.results_.aic}")
|
|
127
|
+
print(f"BIC: {model.results_.bic}")
|
|
128
|
+
|
|
129
|
+
# 预测测试
|
|
130
|
+
y_pred = model.predict(X[:5])
|
|
131
|
+
print(f"前5个样本的预测值: {y_pred}")
|
|
132
|
+
print()
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def test_negative_binomial_model():
|
|
136
|
+
"""测试负二项模型"""
|
|
137
|
+
print("测试负二项模型...")
|
|
138
|
+
|
|
139
|
+
# 生成模拟数据
|
|
140
|
+
np.random.seed(42)
|
|
141
|
+
n = 1000
|
|
142
|
+
X = np.random.normal(0, 1, (n, 2))
|
|
143
|
+
coef_true = np.array([0.5, -0.3])
|
|
144
|
+
|
|
145
|
+
mu = np.exp(np.dot(X, coef_true))
|
|
146
|
+
# 生成负二项分布数据
|
|
147
|
+
alpha = 0.5
|
|
148
|
+
size = 1.0 / alpha
|
|
149
|
+
prob = size / (size + mu)
|
|
150
|
+
y = np.random.negative_binomial(size, prob)
|
|
151
|
+
|
|
152
|
+
# 拟合模型
|
|
153
|
+
model = NegativeBinomialModel()
|
|
154
|
+
model.fit(X, y)
|
|
155
|
+
|
|
156
|
+
print(f"真实系数: {coef_true}")
|
|
157
|
+
print(f"估计系数: {model.results_.params[1:-1]}") # 排除常数项和alpha参数
|
|
158
|
+
print(f"常数项: {model.results_.params[0]}")
|
|
159
|
+
print(f"对数似然: {model.results_.llf}")
|
|
160
|
+
print(f"AIC: {model.results_.aic}")
|
|
161
|
+
print(f"BIC: {model.results_.bic}")
|
|
162
|
+
|
|
163
|
+
# 预测测试
|
|
164
|
+
y_pred = model.predict(X[:5])
|
|
165
|
+
print(f"前5个样本的预测值: {y_pred}")
|
|
166
|
+
print()
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
if __name__ == "__main__":
|
|
170
|
+
if not HAS_MODELS:
|
|
171
|
+
print("模型不可用,请确保已安装statsmodels库")
|
|
172
|
+
exit(1)
|
|
173
|
+
|
|
174
|
+
try:
|
|
175
|
+
import statsmodels.api as sm
|
|
176
|
+
except ImportError:
|
|
177
|
+
print("需要安装statsmodels库")
|
|
178
|
+
exit(1)
|
|
179
|
+
|
|
180
|
+
print("微观离散与受限数据模型测试")
|
|
181
|
+
print("=" * 50)
|
|
182
|
+
|
|
183
|
+
test_logit_model()
|
|
184
|
+
test_probit_model()
|
|
185
|
+
test_tobit_model()
|
|
186
|
+
test_poisson_model()
|
|
187
|
+
test_negative_binomial_model()
|
|
188
|
+
|
|
189
|
+
print("所有测试完成!")
|
|
@@ -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测试通过!")
|