aigroup-econ-mcp 1.4.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.
- PKG-INFO +344 -322
- README.md +335 -320
- __init__.py +1 -1
- aigroup_econ_mcp-2.0.1.dist-info/METADATA +732 -0
- aigroup_econ_mcp-2.0.1.dist-info/RECORD +170 -0
- cli.py +4 -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/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/nonparametric/__init__.py +35 -0
- econometrics/nonparametric/gam_model.py +117 -0
- econometrics/nonparametric/kernel_regression.py +161 -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_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/statistical_inference/__init__.py +21 -0
- econometrics/statistical_inference/bootstrap_methods.py +162 -0
- econometrics/statistical_inference/permutation_test.py +177 -0
- econometrics/survival_analysis/__init__.py +18 -0
- econometrics/survival_analysis/survival_models.py +259 -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/specific_data_modeling_tests/test_micro_discrete_limited_data.py +189 -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
- pyproject.toml +9 -2
- server.py +15 -1
- tools/__init__.py +75 -1
- tools/causal_inference_adapter.py +658 -0
- tools/distribution_analysis_adapter.py +121 -0
- tools/gwr_simple_adapter.py +54 -0
- tools/machine_learning_adapter.py +567 -0
- tools/mcp_tool_groups/__init__.py +15 -1
- 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/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_tools_registry.py +13 -3
- tools/microecon_adapter.py +412 -0
- tools/missing_data_adapter.py +73 -0
- tools/nonparametric_adapter.py +190 -0
- tools/spatial_econometrics_adapter.py +318 -0
- tools/statistical_inference_adapter.py +90 -0
- tools/survival_analysis_adapter.py +46 -0
- aigroup_econ_mcp-1.4.3.dist-info/METADATA +0 -710
- aigroup_econ_mcp-1.4.3.dist-info/RECORD +0 -92
- {aigroup_econ_mcp-1.4.3.dist-info → aigroup_econ_mcp-2.0.1.dist-info}/WHEEL +0 -0
- {aigroup_econ_mcp-1.4.3.dist-info → aigroup_econ_mcp-2.0.1.dist-info}/entry_points.txt +0 -0
- {aigroup_econ_mcp-1.4.3.dist-info → aigroup_econ_mcp-2.0.1.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
# AIGroup Econometrics MCP 未开发大类优先级分析
|
|
2
|
+
|
|
3
|
+
## 项目现状概览
|
|
4
|
+
|
|
5
|
+
根据项目结构和测试报告分析,当前已开发 **49个计量经济学工具**,涵盖以下主要类别:
|
|
6
|
+
|
|
7
|
+
### 已开发工具类别统计
|
|
8
|
+
|
|
9
|
+
| 类别 | 工具数量 | 状态 |
|
|
10
|
+
|------|----------|------|
|
|
11
|
+
| **基础参数估计** | 3个 | ✅ 已开发 |
|
|
12
|
+
| **因果推断方法** | 13个 | ✅ 已开发 |
|
|
13
|
+
| **机器学习方法** | 8个 | ✅ 已开发 |
|
|
14
|
+
| **微观计量模型** | 7个 | ✅ 已开发 |
|
|
15
|
+
| **模型规范与诊断** | 7个 | ✅ 已开发 |
|
|
16
|
+
| **时间序列与面板数据** | 11个 | ✅ 已开发 |
|
|
17
|
+
| **总计** | **49个** | ✅ 已开发 |
|
|
18
|
+
|
|
19
|
+
## 未开发大类识别
|
|
20
|
+
|
|
21
|
+
根据 README.md 中的分类体系,识别出以下尚未开发的大类:
|
|
22
|
+
|
|
23
|
+
### 1. 空间计量经济学 (SPATIAL ECONOMETRICS) - 高优先级
|
|
24
|
+
|
|
25
|
+
**核心问题**: 处理数据的空间依赖性和空间异质性
|
|
26
|
+
|
|
27
|
+
**主要方法**:
|
|
28
|
+
- 空间权重矩阵构建(邻接、距离、K近邻矩阵)
|
|
29
|
+
- 空间自相关检验(Moran's I、Geary's C、局部空间自相关LISA)
|
|
30
|
+
- 空间回归模型(SAR、SEM、SDM、GWR、空间面板数据模型)
|
|
31
|
+
|
|
32
|
+
**优先级理由**:
|
|
33
|
+
- 在区域经济、地理经济学中应用广泛
|
|
34
|
+
- 具有独特的方法论价值
|
|
35
|
+
- 实际研究需求强烈
|
|
36
|
+
|
|
37
|
+
**推荐Python库**:
|
|
38
|
+
- `libpysal`: 空间分析核心库
|
|
39
|
+
- `spreg`: 空间回归模型
|
|
40
|
+
- `esda`: 探索性空间数据分析
|
|
41
|
+
- `mgwr`: 地理加权回归
|
|
42
|
+
|
|
43
|
+
### 2. 非参数与半参数方法 (NONPARAMETRIC & SEMIPARAMETRIC) - 高优先级
|
|
44
|
+
|
|
45
|
+
**核心问题**: 放宽函数形式的线性或参数化假设
|
|
46
|
+
|
|
47
|
+
**主要方法**:
|
|
48
|
+
- 核回归
|
|
49
|
+
- 局部回归
|
|
50
|
+
- 样条回归
|
|
51
|
+
- 广义可加模型 (GAM)
|
|
52
|
+
- 部分线性模型
|
|
53
|
+
- 非参数工具变量估计
|
|
54
|
+
|
|
55
|
+
**优先级理由**:
|
|
56
|
+
- 现代计量经济学重要发展方向
|
|
57
|
+
- 对函数形式假设要求较低
|
|
58
|
+
- 在机器学习交叉领域应用广泛
|
|
59
|
+
|
|
60
|
+
**推荐Python库**:
|
|
61
|
+
- `statsmodels.nonparametric`: 核回归、局部回归
|
|
62
|
+
- `scikit-learn`: 样条回归、GAM
|
|
63
|
+
- `pygam`: 广义可加模型
|
|
64
|
+
- `linearmodels`: 部分线性模型
|
|
65
|
+
|
|
66
|
+
### 3. 分布分析与分解方法 (DISTRIBUTION ANALYSIS & DECOMPOSITION) - 中优先级
|
|
67
|
+
|
|
68
|
+
**核心问题**: 分析因变量整个条件分布的特征,而非仅仅条件均值
|
|
69
|
+
|
|
70
|
+
**主要方法**:
|
|
71
|
+
- 分位数回归
|
|
72
|
+
- 分解方法(Oaxaca-Blinder、DiNardo-Fortin-Lemieux)
|
|
73
|
+
- 方差分解、ANOVA分解
|
|
74
|
+
- Shapley值分解
|
|
75
|
+
- 时间序列分解(趋势-季节-随机)
|
|
76
|
+
|
|
77
|
+
**优先级理由**:
|
|
78
|
+
- 提供更全面的分布信息
|
|
79
|
+
- 在收入不平等、劳动经济学中应用重要
|
|
80
|
+
- 与机器学习解释性方法结合
|
|
81
|
+
|
|
82
|
+
**推荐Python库**:
|
|
83
|
+
- `statsmodels.regression.quantile_regression`: 分位数回归
|
|
84
|
+
- `linearmodels`: Oaxaca-Blinder分解
|
|
85
|
+
- `shap`: Shapley值分解
|
|
86
|
+
- `statsmodels.tsa.seasonal`: 时间序列分解
|
|
87
|
+
|
|
88
|
+
### 4. 统计推断技术 (STATISTICAL INFERENCE TECHNIQUES) - 中优先级
|
|
89
|
+
|
|
90
|
+
**核心问题**: 在理论分布难以推导或模型复杂时进行可靠推断
|
|
91
|
+
|
|
92
|
+
**主要方法**:
|
|
93
|
+
- 重采样方法(Bootstrap、Pairs Bootstrap、Residual Bootstrap、Wild Bootstrap、Block Bootstrap)
|
|
94
|
+
- 模拟方法(蒙特卡洛模拟、置换检验)
|
|
95
|
+
- 渐近方法(Delta方法、聚类稳健推断)
|
|
96
|
+
|
|
97
|
+
**优先级理由**:
|
|
98
|
+
- 为复杂模型提供统计推断基础
|
|
99
|
+
- 在小样本情况下特别重要
|
|
100
|
+
- 现代实证研究的标准工具
|
|
101
|
+
|
|
102
|
+
**推荐Python库**:
|
|
103
|
+
- `scipy.stats.bootstrap`: Bootstrap方法
|
|
104
|
+
- `resample`: 重采样工具
|
|
105
|
+
- `numpy.random`: 蒙特卡洛模拟
|
|
106
|
+
- `scipy.stats.permutation_test`: 置换检验
|
|
107
|
+
|
|
108
|
+
### 5. 缺失数据与测量误差 (MISSING DATA & MEASUREMENT ERROR) - 中优先级
|
|
109
|
+
|
|
110
|
+
**核心问题**: 处理数据不完整或变量测量不准确的问题
|
|
111
|
+
|
|
112
|
+
**主要方法**:
|
|
113
|
+
- 缺失数据处理(列表删除、均值插补、回归插补、多重插补MICE/Amelia、EM算法)
|
|
114
|
+
- 测量误差(工具变量法、SIMEX方法)
|
|
115
|
+
|
|
116
|
+
**优先级理由**:
|
|
117
|
+
- 实际数据中普遍存在的问题
|
|
118
|
+
- 对估计结果有重要影响
|
|
119
|
+
- 现代数据处理的关键技术
|
|
120
|
+
|
|
121
|
+
**推荐Python库**:
|
|
122
|
+
- `sklearn.impute`: 缺失值插补
|
|
123
|
+
- `statsmodels.imputation`: 多重插补
|
|
124
|
+
- `fancyimpute`: 高级插补方法
|
|
125
|
+
- `measurement_error`: 测量误差处理
|
|
126
|
+
|
|
127
|
+
### 6. 生存/持续时间数据 (SURVIVAL/DURATION DATA) - 低优先级
|
|
128
|
+
|
|
129
|
+
**核心问题**: 分析"事件发生时间"数据并处理右删失
|
|
130
|
+
|
|
131
|
+
**主要方法**:
|
|
132
|
+
- Kaplan-Meier估计量
|
|
133
|
+
- Cox比例风险模型
|
|
134
|
+
- 加速失效时间模型
|
|
135
|
+
|
|
136
|
+
**优先级理由**:
|
|
137
|
+
- 在特定领域(医学、工程可靠性)应用重要
|
|
138
|
+
- 但应用范围相对较窄
|
|
139
|
+
- 可与其他工具配合使用
|
|
140
|
+
|
|
141
|
+
**推荐Python库**:
|
|
142
|
+
- `lifelines`: 生存分析库
|
|
143
|
+
- `scikit-survival`: 生存分析机器学习
|
|
144
|
+
- `statsmodels.duration`: 持续时间模型
|
|
145
|
+
|
|
146
|
+
## 开发优先级排序
|
|
147
|
+
|
|
148
|
+
### 第一优先级 (立即开发)
|
|
149
|
+
1. **空间计量经济学**
|
|
150
|
+
- 应用广泛,方法论独特
|
|
151
|
+
- 实际研究需求强烈
|
|
152
|
+
|
|
153
|
+
2. **非参数与半参数方法**
|
|
154
|
+
- 现代计量重要方向
|
|
155
|
+
- 与机器学习交叉应用
|
|
156
|
+
|
|
157
|
+
### 第二优先级 (中期开发)
|
|
158
|
+
3. **分布分析与分解方法**
|
|
159
|
+
- 提供更全面的分析视角
|
|
160
|
+
- 在政策评估中应用重要
|
|
161
|
+
|
|
162
|
+
4. **统计推断技术**
|
|
163
|
+
- 为其他方法提供统计基础
|
|
164
|
+
- 现代实证研究标准工具
|
|
165
|
+
|
|
166
|
+
### 第三优先级 (长期开发)
|
|
167
|
+
5. **缺失数据与测量误差**
|
|
168
|
+
- 数据处理关键技术
|
|
169
|
+
- 对数据质量影响重要
|
|
170
|
+
|
|
171
|
+
6. **生存/持续时间数据**
|
|
172
|
+
- 特定领域应用
|
|
173
|
+
- 可与其他工具配合
|
|
174
|
+
|
|
175
|
+
## 开发规范参考
|
|
176
|
+
|
|
177
|
+
### 三层架构设计
|
|
178
|
+
|
|
179
|
+
新工具的开发应严格遵循项目的三层架构:
|
|
180
|
+
|
|
181
|
+
```
|
|
182
|
+
┌─────────────────────────────────────────────────┐
|
|
183
|
+
│ MCP工具层 (tools/mcp_tool_groups/) │
|
|
184
|
+
│ - 工具注册和接口定义 │
|
|
185
|
+
│ - 参数验证 │
|
|
186
|
+
│ - 异步函数封装 │
|
|
187
|
+
└─────────────────────────────────────────────────┘
|
|
188
|
+
↓
|
|
189
|
+
┌─────────────────────────────────────────────────┐
|
|
190
|
+
│ 适配器层 (tools/*_adapter.py) │
|
|
191
|
+
│ - 数据加载和格式转换 │
|
|
192
|
+
│ - 调用核心算法 │
|
|
193
|
+
│ - 输出格式化 │
|
|
194
|
+
└─────────────────────────────────────────────────┘
|
|
195
|
+
↓
|
|
196
|
+
┌─────────────────────────────────────────────────┐
|
|
197
|
+
│ 核心算法层 (econometrics/) │
|
|
198
|
+
│ - 统计模型实现 │
|
|
199
|
+
│ - 基于成熟Python库 │
|
|
200
|
+
│ - Pydantic结果模型 │
|
|
201
|
+
└─────────────────────────────────────────────────┘
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### 1. 核心算法层开发规范
|
|
205
|
+
|
|
206
|
+
**参考模板**: `econometrics/basic_parametric_estimation/ols/ols_model.py`
|
|
207
|
+
|
|
208
|
+
#### 核心要点:
|
|
209
|
+
```python
|
|
210
|
+
"""
|
|
211
|
+
方法名称模型实现
|
|
212
|
+
"""
|
|
213
|
+
from typing import List, Dict, Any, Optional
|
|
214
|
+
from pydantic import BaseModel, Field
|
|
215
|
+
import numpy as np
|
|
216
|
+
# 导入相关的成熟统计库
|
|
217
|
+
import statsmodels.api as sm # 或其他库
|
|
218
|
+
|
|
219
|
+
class MethodResult(BaseModel):
|
|
220
|
+
"""方法结果的Pydantic模型"""
|
|
221
|
+
# 定义所有输出字段
|
|
222
|
+
coefficients: List[float] = Field(..., description="系数估计")
|
|
223
|
+
std_errors: List[float] = Field(..., description="标准误")
|
|
224
|
+
# ... 其他统计量
|
|
225
|
+
|
|
226
|
+
def method_analysis(
|
|
227
|
+
# 输入参数
|
|
228
|
+
data: List[float],
|
|
229
|
+
# ... 其他参数
|
|
230
|
+
) -> MethodResult:
|
|
231
|
+
"""
|
|
232
|
+
方法的核心实现
|
|
233
|
+
|
|
234
|
+
Args:
|
|
235
|
+
data: 输入数据说明
|
|
236
|
+
|
|
237
|
+
Returns:
|
|
238
|
+
MethodResult: 分析结果
|
|
239
|
+
|
|
240
|
+
Raises:
|
|
241
|
+
ValueError: 输入验证失败时抛出
|
|
242
|
+
"""
|
|
243
|
+
# 1. 输入验证
|
|
244
|
+
if not data:
|
|
245
|
+
raise ValueError("数据不能为空")
|
|
246
|
+
|
|
247
|
+
# 2. 数据预处理
|
|
248
|
+
# 使用numpy进行数据转换
|
|
249
|
+
|
|
250
|
+
# 3. 调用成熟统计库执行计算
|
|
251
|
+
# 基于statsmodels、scipy、sklearn等
|
|
252
|
+
|
|
253
|
+
# 4. 提取并返回结果
|
|
254
|
+
return MethodResult(
|
|
255
|
+
coefficients=...,
|
|
256
|
+
std_errors=...,
|
|
257
|
+
# ...
|
|
258
|
+
)
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
**关键原则**:
|
|
262
|
+
- ✅ 使用Pydantic定义结果模型
|
|
263
|
+
- ✅ 完整的类型注解
|
|
264
|
+
- ✅ 详细的文档字符串
|
|
265
|
+
- ✅ 输入验证和错误处理
|
|
266
|
+
- ✅ 基于成熟的Python统计库
|
|
267
|
+
- ❌ 不要重复造轮子
|
|
268
|
+
|
|
269
|
+
### 2. 适配器层开发规范
|
|
270
|
+
|
|
271
|
+
**参考模板**: `tools/econometrics_adapter.py`
|
|
272
|
+
|
|
273
|
+
#### 核心要点:
|
|
274
|
+
```python
|
|
275
|
+
"""
|
|
276
|
+
方法适配器
|
|
277
|
+
将核心算法适配为MCP工具
|
|
278
|
+
"""
|
|
279
|
+
from typing import List, Optional
|
|
280
|
+
from .data_loader import DataLoader
|
|
281
|
+
from .output_formatter import OutputFormatter
|
|
282
|
+
from econometrics.category.method.method_model import (
|
|
283
|
+
method_analysis as core_method,
|
|
284
|
+
MethodResult as CoreMethodResult
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
def method_adapter(
|
|
288
|
+
# 输入参数(支持直接数据和文件路径)
|
|
289
|
+
data: Optional[List[float]] = None,
|
|
290
|
+
file_path: Optional[str] = None,
|
|
291
|
+
# ... 其他参数
|
|
292
|
+
output_format: str = "json",
|
|
293
|
+
save_path: Optional[str] = None
|
|
294
|
+
) -> str:
|
|
295
|
+
"""
|
|
296
|
+
方法适配器
|
|
297
|
+
|
|
298
|
+
功能:
|
|
299
|
+
1. 数据加载(文件或直接数据)
|
|
300
|
+
2. 调用核心算法
|
|
301
|
+
3. 格式化输出
|
|
302
|
+
"""
|
|
303
|
+
# 1. 数据准备
|
|
304
|
+
if file_path:
|
|
305
|
+
data_dict = DataLoader.load_from_file(file_path)
|
|
306
|
+
data = data_dict["data"]
|
|
307
|
+
elif data is None:
|
|
308
|
+
raise ValueError("必须提供file_path或data")
|
|
309
|
+
|
|
310
|
+
# 2. 调用核心算法(复用!)
|
|
311
|
+
result: CoreMethodResult = core_method(data=data, ...)
|
|
312
|
+
|
|
313
|
+
# 3. 格式化输出
|
|
314
|
+
if output_format == "json":
|
|
315
|
+
json_result = json.dumps(result.dict(), ensure_ascii=False, indent=2)
|
|
316
|
+
if save_path:
|
|
317
|
+
OutputFormatter.save_to_file(json_result, save_path)
|
|
318
|
+
return f"分析完成!结果已保存到: {save_path}\n\n{json_result}"
|
|
319
|
+
return json_result
|
|
320
|
+
else:
|
|
321
|
+
# 其他格式处理
|
|
322
|
+
formatted = OutputFormatter.format_result(result, output_format)
|
|
323
|
+
if save_path:
|
|
324
|
+
OutputFormatter.save_to_file(formatted, save_path)
|
|
325
|
+
return formatted
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
**关键原则**:
|
|
329
|
+
- ✅ 支持文件输入和直接数据输入
|
|
330
|
+
- ✅ 使用DataLoader统一数据加载
|
|
331
|
+
- ✅ 使用OutputFormatter统一输出格式化
|
|
332
|
+
- ✅ 统一的错误处理机制
|
|
333
|
+
|
|
334
|
+
### 3. MCP工具注册规范
|
|
335
|
+
|
|
336
|
+
**参考模板**: `tools/mcp_tool_groups/basic_parametric_tools.py`
|
|
337
|
+
|
|
338
|
+
#### 核心要点:
|
|
339
|
+
```python
|
|
340
|
+
"""
|
|
341
|
+
方法类别工具组
|
|
342
|
+
"""
|
|
343
|
+
from typing import List, Dict, Any, Optional
|
|
344
|
+
from mcp.server.fastmcp import Context
|
|
345
|
+
from mcp.server.session import ServerSession
|
|
346
|
+
from ..mcp_tools_registry import ToolGroup
|
|
347
|
+
from ..category_adapter import method_adapter
|
|
348
|
+
|
|
349
|
+
class CategoryTools(ToolGroup):
|
|
350
|
+
"""类别工具组"""
|
|
351
|
+
|
|
352
|
+
name = "CATEGORY NAME"
|
|
353
|
+
description = "类别描述"
|
|
354
|
+
version = "1.0.0"
|
|
355
|
+
|
|
356
|
+
@classmethod
|
|
357
|
+
def get_tools(cls) -> List[Dict[str, Any]]:
|
|
358
|
+
"""返回工具列表"""
|
|
359
|
+
return [
|
|
360
|
+
{
|
|
361
|
+
"name": "category_method_name",
|
|
362
|
+
"handler": cls.method_tool,
|
|
363
|
+
"description": "Method Description"
|
|
364
|
+
},
|
|
365
|
+
# 更多工具...
|
|
366
|
+
]
|
|
367
|
+
|
|
368
|
+
@staticmethod
|
|
369
|
+
async def method_tool(
|
|
370
|
+
# 参数定义(与适配器一致)
|
|
371
|
+
data: Optional[List[float]] = None,
|
|
372
|
+
file_path: Optional[str] = None,
|
|
373
|
+
output_format: str = "json",
|
|
374
|
+
save_path: Optional[str] = None,
|
|
375
|
+
ctx: Context[ServerSession, None] = None
|
|
376
|
+
) -> str:
|
|
377
|
+
"""方法工具(异步函数)"""
|
|
378
|
+
try:
|
|
379
|
+
if ctx:
|
|
380
|
+
await ctx.info("Starting method analysis...")
|
|
381
|
+
|
|
382
|
+
# 调用适配器
|
|
383
|
+
result = method_adapter(
|
|
384
|
+
data=data,
|
|
385
|
+
file_path=file_path,
|
|
386
|
+
output_format=output_format,
|
|
387
|
+
save_path=save_path
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
if ctx:
|
|
391
|
+
await ctx.info("Method analysis complete")
|
|
392
|
+
|
|
393
|
+
return result
|
|
394
|
+
except Exception as e:
|
|
395
|
+
if ctx:
|
|
396
|
+
await ctx.error(f"Error: {str(e)}")
|
|
397
|
+
raise
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
**关键原则**:
|
|
401
|
+
- ✅ 继承ToolGroup基类
|
|
402
|
+
- ✅ 实现get_tools()类方法
|
|
403
|
+
- ✅ 每个工具都是async函数
|
|
404
|
+
- ✅ 使用Context进行日志记录
|
|
405
|
+
- ✅ 统一的异常处理
|
|
406
|
+
|
|
407
|
+
### 4. 输入输出格式规范
|
|
408
|
+
|
|
409
|
+
**参考文档**: `resources/MCP_TOOLS_DATA_FORMAT_GUIDE.md`
|
|
410
|
+
|
|
411
|
+
#### 输入格式要求:
|
|
412
|
+
|
|
413
|
+
1. **直接数据输入**:
|
|
414
|
+
- 一维数据: `List[float]`
|
|
415
|
+
- 二维数据: `List[List[float]]`
|
|
416
|
+
- 标识符: `List[str]` 或 `List[int]`
|
|
417
|
+
|
|
418
|
+
2. **文件输入**:
|
|
419
|
+
- 支持格式: txt, json, csv, excel
|
|
420
|
+
- 使用DataLoader统一加载
|
|
421
|
+
|
|
422
|
+
3. **数据验证**:
|
|
423
|
+
- 非空检查
|
|
424
|
+
- 维度一致性检查
|
|
425
|
+
- 数据类型验证
|
|
426
|
+
|
|
427
|
+
#### 输出格式要求:
|
|
428
|
+
|
|
429
|
+
1. **JSON格式** (默认):
|
|
430
|
+
```json
|
|
431
|
+
{
|
|
432
|
+
"coefficients": [1.0, 2.0],
|
|
433
|
+
"std_errors": [0.1, 0.2],
|
|
434
|
+
"statistics": {...}
|
|
435
|
+
}
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
2. **Markdown格式**:
|
|
439
|
+
```markdown
|
|
440
|
+
# 分析结果
|
|
441
|
+
|
|
442
|
+
## 系数估计
|
|
443
|
+
- 系数1: 1.0 (标准误: 0.1)
|
|
444
|
+
- 系数2: 2.0 (标准误: 0.2)
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
3. **文本格式**:
|
|
448
|
+
```
|
|
449
|
+
分析结果
|
|
450
|
+
=========
|
|
451
|
+
系数估计: [1.0, 2.0]
|
|
452
|
+
标准误: [0.1, 0.2]
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
## 模块化开发架构设计
|
|
456
|
+
|
|
457
|
+
### 文件结构设计原则
|
|
458
|
+
```
|
|
459
|
+
econometrics/
|
|
460
|
+
├── spatial_econometrics/ # 空间计量经济学
|
|
461
|
+
│ ├── spatial_weights.py # 空间权重矩阵(单一方法)
|
|
462
|
+
│ ├── spatial_autocorrelation.py # 空间自相关检验(单一方法)
|
|
463
|
+
│ ├── spatial_regression.py # 空间回归模型(单一方法)
|
|
464
|
+
│ └── __init__.py
|
|
465
|
+
├── nonparametric_methods/ # 非参数方法
|
|
466
|
+
│ ├── kernel_regression.py # 核回归(单一方法)
|
|
467
|
+
│ ├── local_regression.py # 局部回归(单一方法)
|
|
468
|
+
│ ├── spline_regression.py # 样条回归(单一方法)
|
|
469
|
+
│ ├── gam_model.py # 广义可加模型(单一方法)
|
|
470
|
+
│ └── __init__.py
|
|
471
|
+
├── distribution_analysis/ # 分布分析
|
|
472
|
+
│ ├── quantile_regression.py # 分位数回归(单一方法)
|
|
473
|
+
│ ├── decomposition_methods.py # 分解方法(单一方法)
|
|
474
|
+
│ ├── variance_decomposition.py # 方差分解(单一方法)
|
|
475
|
+
│ └── __init__.py
|
|
476
|
+
└── ... (其他类别类似结构)
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
### 每个方法独立脚本的优势
|
|
480
|
+
1. **易于维护**: 每个方法独立,修改不影响其他功能
|
|
481
|
+
2. **便于测试**: 可以为每个方法编写独立的单元测试
|
|
482
|
+
3. **灵活组合**: 用户可以根据需要选择特定方法
|
|
483
|
+
4. **错误隔离**: 单个方法出错不会影响整个系统
|
|
484
|
+
5. **版本控制**: 可以独立更新和发布单个方法
|
|
485
|
+
|
|
486
|
+
## 建议开发计划
|
|
487
|
+
|
|
488
|
+
### 第一阶段 (1-2个月)
|
|
489
|
+
- 开发空间计量经济学工具组 (5-7个工具)
|
|
490
|
+
- 基于 `libpysal` 和 `spreg` 库
|
|
491
|
+
- 每个空间模型独立脚本
|
|
492
|
+
- 开发非参数与半参数方法工具组 (5-6个工具)
|
|
493
|
+
- 基于 `statsmodels.nonparametric` 和 `pygam`
|
|
494
|
+
- 每个非参数方法独立脚本
|
|
495
|
+
|
|
496
|
+
### 第二阶段 (2-3个月)
|
|
497
|
+
- 开发分布分析与分解方法工具组 (4-5个工具)
|
|
498
|
+
- 基于 `statsmodels` 和 `linearmodels`
|
|
499
|
+
- 每个分解方法独立脚本
|
|
500
|
+
- 开发统计推断技术工具组 (5-6个工具)
|
|
501
|
+
- 基于 `scipy.stats` 和 `resample`
|
|
502
|
+
- 每个推断方法独立脚本
|
|
503
|
+
|
|
504
|
+
### 第三阶段 (3-4个月)
|
|
505
|
+
- 开发缺失数据与测量误差工具组 (4-5个工具)
|
|
506
|
+
- 基于 `sklearn.impute` 和 `fancyimpute`
|
|
507
|
+
- 每个插补方法独立脚本
|
|
508
|
+
- 开发生存/持续时间数据工具组 (3-4个工具)
|
|
509
|
+
- 基于 `lifelines` 库
|
|
510
|
+
- 每个生存模型独立脚本
|
|
511
|
+
|
|
512
|
+
## 预期工具总数
|
|
513
|
+
|
|
514
|
+
完成所有类别开发后,预计新增 **26-33个工具**,使总工具数达到 **75-82个**,形成完整的计量经济学分析工具集。
|
|
515
|
+
|
|
516
|
+
## 技术实现原则
|
|
517
|
+
|
|
518
|
+
### 1. 基于现有Python统计库
|
|
519
|
+
- **不重复造轮子**: 充分利用成熟的统计库
|
|
520
|
+
- **标准化接口**: 统一输入输出格式
|
|
521
|
+
- **性能优化**: 利用底层库的优化实现
|
|
522
|
+
|
|
523
|
+
### 2. 模块化设计
|
|
524
|
+
- **单一职责**: 每个脚本只实现一个核心方法
|
|
525
|
+
- **独立测试**: 每个方法都有完整的单元测试
|
|
526
|
+
- **清晰依赖**: 明确的方法依赖关系
|
|
527
|
+
|
|
528
|
+
### 3. 向后兼容
|
|
529
|
+
- **接口稳定**: 保持现有MCP工具接口不变
|
|
530
|
+
- **数据格式**: 统一使用现有数据格式标准
|
|
531
|
+
- **错误处理**: 统一的错误处理机制
|
|
532
|
+
|
|
533
|
+
### 4. 文档完善
|
|
534
|
+
- **使用示例**: 每个方法提供完整的使用示例
|
|
535
|
+
- **参数说明**: 详细的参数文档
|
|
536
|
+
- **输出格式**: 明确的输出格式说明
|
|
537
|
+
|
|
538
|
+
---
|
|
539
|
+
**分析时间**: 2025-11-06
|
|
540
|
+
**分析依据**: 项目结构分析、测试报告、README文档
|
|
541
|
+
**当前工具总数**: 49个
|
|
542
|
+
**预计新增工具**: 26-33个
|
|
543
|
+
**最终工具总数**: 75-82个
|
|
544
|
+
**开发原则**: 基于现有Python统计库 + 模块化独立脚本设计 + 严格遵循三层架构规范
|
pyproject.toml
CHANGED
|
@@ -4,8 +4,8 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "aigroup_econ_mcp"
|
|
7
|
-
version = "
|
|
8
|
-
description = "专业计量经济学MCP工具 -
|
|
7
|
+
version = "2.0.1"
|
|
8
|
+
description = "专业计量经济学MCP工具 - 支持CSV/JSON/TXT/Excel格式,让大模型更智能地进行数据分析"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
11
11
|
authors = [
|
|
@@ -26,6 +26,13 @@ dependencies = [
|
|
|
26
26
|
"PyYAML>=6.0",
|
|
27
27
|
"arch>=6.0.0",
|
|
28
28
|
"openpyxl>=3.0.0",
|
|
29
|
+
"lifelines>=0.27.0",
|
|
30
|
+
"libpysal>=4.7.0",
|
|
31
|
+
"esda>=2.4.0",
|
|
32
|
+
"spreg>=1.4.0",
|
|
33
|
+
"joblib>=1.2.0",
|
|
34
|
+
"xgboost>=1.7.0",
|
|
35
|
+
"matplotlib>=3.5.0",
|
|
29
36
|
]
|
|
30
37
|
keywords = ["mcp", "economics", "statistics", "regression", "data-analysis", "econometrics", "time-series", "panel-data"]
|
|
31
38
|
classifiers = [
|
server.py
CHANGED
|
@@ -5,6 +5,7 @@ AIGroup 计量经济学 MCP 服务器 - 简化修复版
|
|
|
5
5
|
|
|
6
6
|
import sys
|
|
7
7
|
import os
|
|
8
|
+
import asyncio
|
|
8
9
|
from typing import List, Optional, Union
|
|
9
10
|
from mcp.server.fastmcp import FastMCP, Context
|
|
10
11
|
from mcp.server.session import ServerSession
|
|
@@ -29,6 +30,11 @@ mcp = FastMCP("aigroup-econ-mcp")
|
|
|
29
30
|
print("正在自动发现工具组...")
|
|
30
31
|
registry.auto_discover_groups()
|
|
31
32
|
|
|
33
|
+
# 显示发现的工具组
|
|
34
|
+
print(f"发现工具组数量: {len(registry.tool_groups)}")
|
|
35
|
+
for group in registry.tool_groups:
|
|
36
|
+
print(f" - {group.name}")
|
|
37
|
+
|
|
32
38
|
# 直接注册所有工具
|
|
33
39
|
print("正在注册工具...")
|
|
34
40
|
for tool_name, tool_info in registry.get_all_tools().items():
|
|
@@ -76,7 +82,15 @@ def main():
|
|
|
76
82
|
print("\n启动服务器...")
|
|
77
83
|
print("=" * 60)
|
|
78
84
|
|
|
79
|
-
|
|
85
|
+
# 正确使用FastMCP.run方法,不传递timeout参数
|
|
86
|
+
try:
|
|
87
|
+
mcp.run(transport="stdio")
|
|
88
|
+
except KeyboardInterrupt:
|
|
89
|
+
print("\n服务器已停止")
|
|
90
|
+
except Exception as e:
|
|
91
|
+
print(f"\n服务器运行出错: {e}")
|
|
92
|
+
import traceback
|
|
93
|
+
traceback.print_exc()
|
|
80
94
|
|
|
81
95
|
|
|
82
96
|
if __name__ == "__main__":
|
tools/__init__.py
CHANGED
|
@@ -18,6 +18,46 @@ from .time_series_panel_data_tools import (
|
|
|
18
18
|
dynamic_panel_model
|
|
19
19
|
)
|
|
20
20
|
|
|
21
|
+
# 因果推断工具适配器
|
|
22
|
+
from .causal_inference_adapter import (
|
|
23
|
+
did_adapter,
|
|
24
|
+
iv_adapter,
|
|
25
|
+
psm_adapter,
|
|
26
|
+
fixed_effects_adapter,
|
|
27
|
+
random_effects_adapter,
|
|
28
|
+
rdd_adapter,
|
|
29
|
+
synthetic_control_adapter,
|
|
30
|
+
event_study_adapter,
|
|
31
|
+
triple_difference_adapter,
|
|
32
|
+
mediation_adapter,
|
|
33
|
+
moderation_adapter,
|
|
34
|
+
control_function_adapter,
|
|
35
|
+
first_difference_adapter
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# 机器学习工具适配器
|
|
39
|
+
from .machine_learning_adapter import (
|
|
40
|
+
random_forest_adapter,
|
|
41
|
+
gradient_boosting_adapter,
|
|
42
|
+
svm_adapter,
|
|
43
|
+
neural_network_adapter,
|
|
44
|
+
kmeans_clustering_adapter,
|
|
45
|
+
hierarchical_clustering_adapter,
|
|
46
|
+
double_ml_adapter,
|
|
47
|
+
causal_forest_adapter
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# 微观计量模型工具适配器
|
|
51
|
+
from .microecon_adapter import (
|
|
52
|
+
logit_adapter,
|
|
53
|
+
probit_adapter,
|
|
54
|
+
multinomial_logit_adapter,
|
|
55
|
+
poisson_adapter,
|
|
56
|
+
negative_binomial_adapter,
|
|
57
|
+
tobit_adapter,
|
|
58
|
+
heckman_adapter
|
|
59
|
+
)
|
|
60
|
+
|
|
21
61
|
# 保持向后兼容性
|
|
22
62
|
ols_adapter = EconometricsAdapter.ols_regression
|
|
23
63
|
mle_adapter = EconometricsAdapter.mle_estimation
|
|
@@ -41,5 +81,39 @@ __all__ = [
|
|
|
41
81
|
"unit_root_tests",
|
|
42
82
|
"var_svar_model",
|
|
43
83
|
"cointegration_analysis",
|
|
44
|
-
"dynamic_panel_model"
|
|
84
|
+
"dynamic_panel_model",
|
|
85
|
+
|
|
86
|
+
# 因果推断工具
|
|
87
|
+
"did_adapter",
|
|
88
|
+
"iv_adapter",
|
|
89
|
+
"psm_adapter",
|
|
90
|
+
"fixed_effects_adapter",
|
|
91
|
+
"random_effects_adapter",
|
|
92
|
+
"rdd_adapter",
|
|
93
|
+
"synthetic_control_adapter",
|
|
94
|
+
"event_study_adapter",
|
|
95
|
+
"triple_difference_adapter",
|
|
96
|
+
"mediation_adapter",
|
|
97
|
+
"moderation_adapter",
|
|
98
|
+
"control_function_adapter",
|
|
99
|
+
"first_difference_adapter",
|
|
100
|
+
|
|
101
|
+
# 机器学习工具
|
|
102
|
+
"random_forest_adapter",
|
|
103
|
+
"gradient_boosting_adapter",
|
|
104
|
+
"svm_adapter",
|
|
105
|
+
"neural_network_adapter",
|
|
106
|
+
"kmeans_clustering_adapter",
|
|
107
|
+
"hierarchical_clustering_adapter",
|
|
108
|
+
"double_ml_adapter",
|
|
109
|
+
"causal_forest_adapter",
|
|
110
|
+
|
|
111
|
+
# 微观计量模型工具
|
|
112
|
+
"logit_adapter",
|
|
113
|
+
"probit_adapter",
|
|
114
|
+
"multinomial_logit_adapter",
|
|
115
|
+
"poisson_adapter",
|
|
116
|
+
"negative_binomial_adapter",
|
|
117
|
+
"tobit_adapter",
|
|
118
|
+
"heckman_adapter"
|
|
45
119
|
]
|