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,286 @@
|
|
|
1
|
+
"""
|
|
2
|
+
计量经济学核心算法适配器
|
|
3
|
+
复用 econometrics/ 中的核心实现,避免代码重复
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import List, Optional, Union
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
import json
|
|
10
|
+
|
|
11
|
+
# 确保可以导入econometrics模块
|
|
12
|
+
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
13
|
+
|
|
14
|
+
# 导入核心算法实现
|
|
15
|
+
from econometrics.basic_parametric_estimation.ols.ols_model import (
|
|
16
|
+
ols_regression as core_ols_regression,
|
|
17
|
+
OLSResult as CoreOLSResult
|
|
18
|
+
)
|
|
19
|
+
from econometrics.basic_parametric_estimation.mle.mle_model import (
|
|
20
|
+
mle_estimation as core_mle_estimation,
|
|
21
|
+
MLEResult as CoreMLEResult
|
|
22
|
+
)
|
|
23
|
+
from econometrics.basic_parametric_estimation.gmm.gmm_model import (
|
|
24
|
+
gmm_estimation as core_gmm_estimation,
|
|
25
|
+
GMMResult as CoreGMMResult
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
# 导入数据加载和格式化组件
|
|
29
|
+
from .data_loader import DataLoader, MLEDataLoader
|
|
30
|
+
from .output_formatter import OutputFormatter
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class DataValidator:
|
|
34
|
+
"""数据验证器"""
|
|
35
|
+
|
|
36
|
+
@staticmethod
|
|
37
|
+
def validate_ols_gmm_data(y_data: List[float], x_data: List[List[float]], feature_names: Optional[List[str]] = None):
|
|
38
|
+
"""验证OLS和GMM数据格式"""
|
|
39
|
+
if len(y_data) != len(x_data):
|
|
40
|
+
raise ValueError(f"因变量长度({len(y_data)})与自变量长度({len(x_data)})不一致")
|
|
41
|
+
|
|
42
|
+
# 检查所有x_data行的长度是否一致
|
|
43
|
+
if x_data:
|
|
44
|
+
first_row_len = len(x_data[0])
|
|
45
|
+
for i, row in enumerate(x_data):
|
|
46
|
+
if len(row) != first_row_len:
|
|
47
|
+
raise ValueError(f"自变量第{i}行长度({len(row)})与第一行长度({first_row_len})不一致")
|
|
48
|
+
|
|
49
|
+
# 验证feature_names
|
|
50
|
+
if feature_names and len(feature_names) != len(x_data[0]) if x_data else 0:
|
|
51
|
+
raise ValueError(f"特征名称数量({len(feature_names)})与自变量列数({len(x_data[0]) if x_data else 0})不一致")
|
|
52
|
+
|
|
53
|
+
@staticmethod
|
|
54
|
+
def convert_to_2d_list(data: Union[List[float], List[List[float]]]) -> List[List[float]]:
|
|
55
|
+
"""将数据转换为二维列表格式"""
|
|
56
|
+
if not data:
|
|
57
|
+
return []
|
|
58
|
+
|
|
59
|
+
# 如果是一维列表,转换为二维列表
|
|
60
|
+
if isinstance(data[0], (int, float)):
|
|
61
|
+
return [[x] for x in data]
|
|
62
|
+
|
|
63
|
+
# 已经是二维列表
|
|
64
|
+
return data
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class EconometricsAdapter:
|
|
68
|
+
"""
|
|
69
|
+
计量经济学适配器
|
|
70
|
+
将core算法适配为MCP工具,支持文件输入和多种输出格式
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
@staticmethod
|
|
74
|
+
def ols_regression(
|
|
75
|
+
y_data: Optional[List[float]] = None,
|
|
76
|
+
x_data: Optional[List[List[float]]] = None,
|
|
77
|
+
file_path: Optional[str] = None,
|
|
78
|
+
feature_names: Optional[List[str]] = None,
|
|
79
|
+
constant: bool = True,
|
|
80
|
+
confidence_level: float = 0.95,
|
|
81
|
+
output_format: str = "json",
|
|
82
|
+
save_path: Optional[str] = None
|
|
83
|
+
) -> str:
|
|
84
|
+
"""
|
|
85
|
+
OLS回归适配器
|
|
86
|
+
|
|
87
|
+
优势:复用econometrics/核心算法,避免代码重复
|
|
88
|
+
"""
|
|
89
|
+
# 1. 数据准备
|
|
90
|
+
if file_path:
|
|
91
|
+
data = DataLoader.load_from_file(file_path)
|
|
92
|
+
y_data = data["y_data"]
|
|
93
|
+
x_data = data["x_data"]
|
|
94
|
+
feature_names = data.get("feature_names") or feature_names
|
|
95
|
+
elif y_data is None or x_data is None:
|
|
96
|
+
raise ValueError("必须提供文件路径(file_path)或直接数据(y_data和x_data)")
|
|
97
|
+
|
|
98
|
+
# 数据验证和转换
|
|
99
|
+
x_data = DataValidator.convert_to_2d_list(x_data)
|
|
100
|
+
DataValidator.validate_ols_gmm_data(y_data, x_data, feature_names)
|
|
101
|
+
|
|
102
|
+
# 2. 调用核心算法(复用!)
|
|
103
|
+
result: CoreOLSResult = core_ols_regression(
|
|
104
|
+
y_data=y_data,
|
|
105
|
+
x_data=x_data,
|
|
106
|
+
feature_names=feature_names,
|
|
107
|
+
constant=constant,
|
|
108
|
+
confidence_level=confidence_level
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
# 3. 格式化输出
|
|
112
|
+
if output_format == "json":
|
|
113
|
+
json_result = json.dumps(result.dict(), ensure_ascii=False, indent=2)
|
|
114
|
+
if save_path:
|
|
115
|
+
OutputFormatter.save_to_file(json_result, save_path)
|
|
116
|
+
return f"分析完成!结果已保存到: {save_path}\n\n{json_result}"
|
|
117
|
+
return json_result
|
|
118
|
+
else:
|
|
119
|
+
# 尝试使用格式化器,失败则回退到JSON
|
|
120
|
+
try:
|
|
121
|
+
formatted = OutputFormatter.format_ols_result(result, output_format)
|
|
122
|
+
if save_path:
|
|
123
|
+
OutputFormatter.save_to_file(formatted, save_path)
|
|
124
|
+
return f"分析完成!\n\n{formatted}\n\n已保存到: {save_path}"
|
|
125
|
+
return formatted
|
|
126
|
+
except Exception as e:
|
|
127
|
+
# 回退到JSON格式
|
|
128
|
+
json_result = json.dumps(result.dict(), ensure_ascii=False, indent=2)
|
|
129
|
+
warning = f"警告: {output_format}格式化失败({str(e)}),返回JSON格式\n\n"
|
|
130
|
+
if save_path:
|
|
131
|
+
OutputFormatter.save_to_file(json_result, save_path)
|
|
132
|
+
return f"{warning}分析完成!结果已保存到: {save_path}\n\n{json_result}"
|
|
133
|
+
return warning + json_result
|
|
134
|
+
|
|
135
|
+
@staticmethod
|
|
136
|
+
def mle_estimation(
|
|
137
|
+
data: Optional[List[float]] = None,
|
|
138
|
+
file_path: Optional[str] = None,
|
|
139
|
+
distribution: str = "normal",
|
|
140
|
+
initial_params: Optional[List[float]] = None,
|
|
141
|
+
confidence_level: float = 0.95,
|
|
142
|
+
output_format: str = "json",
|
|
143
|
+
save_path: Optional[str] = None
|
|
144
|
+
) -> str:
|
|
145
|
+
"""
|
|
146
|
+
MLE估计适配器
|
|
147
|
+
|
|
148
|
+
优势:复用econometrics/核心算法
|
|
149
|
+
"""
|
|
150
|
+
# 1. 数据准备
|
|
151
|
+
if file_path:
|
|
152
|
+
data_dict = MLEDataLoader.load_from_file(file_path)
|
|
153
|
+
data = data_dict["data"]
|
|
154
|
+
elif data is None:
|
|
155
|
+
raise ValueError("必须提供文件路径(file_path)或直接数据(data)")
|
|
156
|
+
|
|
157
|
+
# 2. 调用核心算法(复用!)
|
|
158
|
+
result: CoreMLEResult = core_mle_estimation(
|
|
159
|
+
data=data,
|
|
160
|
+
distribution=distribution,
|
|
161
|
+
initial_params=initial_params,
|
|
162
|
+
confidence_level=confidence_level
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
# 3. 格式化输出
|
|
166
|
+
if output_format == "json":
|
|
167
|
+
json_result = json.dumps(result.dict(), ensure_ascii=False, indent=2)
|
|
168
|
+
if save_path:
|
|
169
|
+
OutputFormatter.save_to_file(json_result, save_path)
|
|
170
|
+
return f"分析完成!结果已保存到: {save_path}\n\n{json_result}"
|
|
171
|
+
return json_result
|
|
172
|
+
else:
|
|
173
|
+
# 尝试使用格式化器,失败则回退到JSON
|
|
174
|
+
try:
|
|
175
|
+
formatted = OutputFormatter.format_mle_result(result, output_format)
|
|
176
|
+
if save_path:
|
|
177
|
+
OutputFormatter.save_to_file(formatted, save_path)
|
|
178
|
+
return f"分析完成!\n\n{formatted}\n\n已保存到: {save_path}"
|
|
179
|
+
return formatted
|
|
180
|
+
except Exception as e:
|
|
181
|
+
# 回退到JSON格式
|
|
182
|
+
json_result = json.dumps(result.dict(), ensure_ascii=False, indent=2)
|
|
183
|
+
warning = f"警告: {output_format}格式化失败({str(e)}),返回JSON格式\n\n"
|
|
184
|
+
if save_path:
|
|
185
|
+
OutputFormatter.save_to_file(json_result, save_path)
|
|
186
|
+
return f"{warning}分析完成!结果已保存到: {save_path}\n\n{json_result}"
|
|
187
|
+
return warning + json_result
|
|
188
|
+
|
|
189
|
+
@staticmethod
|
|
190
|
+
def gmm_estimation(
|
|
191
|
+
y_data: Optional[List[float]] = None,
|
|
192
|
+
x_data: Optional[List[List[float]]] = None,
|
|
193
|
+
file_path: Optional[str] = None,
|
|
194
|
+
instruments: Optional[List[List[float]]] = None,
|
|
195
|
+
feature_names: Optional[List[str]] = None,
|
|
196
|
+
constant: bool = True,
|
|
197
|
+
confidence_level: float = 0.95,
|
|
198
|
+
output_format: str = "json",
|
|
199
|
+
save_path: Optional[str] = None
|
|
200
|
+
) -> str:
|
|
201
|
+
"""
|
|
202
|
+
GMM估计适配器
|
|
203
|
+
|
|
204
|
+
优势:复用econometrics/核心算法
|
|
205
|
+
增强:添加数值稳定性检查
|
|
206
|
+
"""
|
|
207
|
+
# 1. 数据准备
|
|
208
|
+
if file_path:
|
|
209
|
+
data = DataLoader.load_from_file(file_path)
|
|
210
|
+
y_data = data["y_data"]
|
|
211
|
+
x_data = data["x_data"]
|
|
212
|
+
feature_names = data.get("feature_names") or feature_names
|
|
213
|
+
elif y_data is None or x_data is None:
|
|
214
|
+
raise ValueError("必须提供文件路径(file_path)或直接数据(y_data和x_data)")
|
|
215
|
+
|
|
216
|
+
# 数据验证和转换
|
|
217
|
+
x_data = DataValidator.convert_to_2d_list(x_data)
|
|
218
|
+
DataValidator.validate_ols_gmm_data(y_data, x_data, feature_names)
|
|
219
|
+
|
|
220
|
+
# 转换工具变量格式
|
|
221
|
+
if instruments:
|
|
222
|
+
instruments = DataValidator.convert_to_2d_list(instruments)
|
|
223
|
+
|
|
224
|
+
# 2. 调用核心算法(复用!)
|
|
225
|
+
try:
|
|
226
|
+
result: CoreGMMResult = core_gmm_estimation(
|
|
227
|
+
y_data=y_data,
|
|
228
|
+
x_data=x_data,
|
|
229
|
+
instruments=instruments,
|
|
230
|
+
feature_names=feature_names,
|
|
231
|
+
constant=constant,
|
|
232
|
+
confidence_level=confidence_level
|
|
233
|
+
)
|
|
234
|
+
except Exception as e:
|
|
235
|
+
# 提供更详细的错误信息
|
|
236
|
+
error_msg = f"GMM估计失败: {str(e)}\n"
|
|
237
|
+
error_msg += "可能原因:\n"
|
|
238
|
+
error_msg += "1. 数据存在多重共线性\n"
|
|
239
|
+
error_msg += "2. 工具变量不足或无效\n"
|
|
240
|
+
error_msg += "3. 矩阵奇异(数值不稳定)\n"
|
|
241
|
+
error_msg += "建议:\n"
|
|
242
|
+
error_msg += "- 检查数据质量\n"
|
|
243
|
+
error_msg += "- 增加工具变量数量\n"
|
|
244
|
+
error_msg += "- 尝试标准化数据\n"
|
|
245
|
+
raise ValueError(error_msg) from e
|
|
246
|
+
|
|
247
|
+
# 3. 格式化输出
|
|
248
|
+
if output_format == "json":
|
|
249
|
+
json_result = json.dumps(result.dict(), ensure_ascii=False, indent=2)
|
|
250
|
+
if save_path:
|
|
251
|
+
OutputFormatter.save_to_file(json_result, save_path)
|
|
252
|
+
return f"分析完成!结果已保存到: {save_path}\n\n{json_result}"
|
|
253
|
+
return json_result
|
|
254
|
+
else:
|
|
255
|
+
# 尝试使用格式化器,失败则回退到JSON
|
|
256
|
+
try:
|
|
257
|
+
formatted = OutputFormatter.format_gmm_result(result, output_format)
|
|
258
|
+
if save_path:
|
|
259
|
+
OutputFormatter.save_to_file(formatted, save_path)
|
|
260
|
+
return f"分析完成!\n\n{formatted}\n\n已保存到: {save_path}"
|
|
261
|
+
return formatted
|
|
262
|
+
except Exception as e:
|
|
263
|
+
# 回退到JSON格式
|
|
264
|
+
json_result = json.dumps(result.dict(), ensure_ascii=False, indent=2)
|
|
265
|
+
warning = f"警告: {output_format}格式化失败({str(e)}),返回JSON格式\n\n"
|
|
266
|
+
if save_path:
|
|
267
|
+
OutputFormatter.save_to_file(json_result, save_path)
|
|
268
|
+
return f"{warning}分析完成!结果已保存到: {save_path}\n\n{json_result}"
|
|
269
|
+
return warning + json_result
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
# 便捷别名
|
|
273
|
+
ols_adapter = EconometricsAdapter.ols_regression
|
|
274
|
+
mle_adapter = EconometricsAdapter.mle_estimation
|
|
275
|
+
|
|
276
|
+
# 导入模型规范、诊断和稳健推断适配器
|
|
277
|
+
from .model_specification_adapter import (
|
|
278
|
+
diagnostic_tests_adapter,
|
|
279
|
+
gls_adapter,
|
|
280
|
+
wls_adapter,
|
|
281
|
+
robust_errors_adapter,
|
|
282
|
+
model_selection_adapter,
|
|
283
|
+
regularization_adapter,
|
|
284
|
+
simultaneous_equations_adapter
|
|
285
|
+
)
|
|
286
|
+
gmm_adapter = EconometricsAdapter.gmm_estimation
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""MCP工具组包"""
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"""
|
|
2
|
+
基础参数估计工具组
|
|
3
|
+
包含 OLS、MLE、GMM 三个核心工具
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import List, Optional, Union, Dict, Any
|
|
7
|
+
from mcp.server.fastmcp import Context
|
|
8
|
+
from mcp.server.session import ServerSession
|
|
9
|
+
|
|
10
|
+
from ..mcp_tools_registry import ToolGroup
|
|
11
|
+
from ..econometrics_adapter import ols_adapter, mle_adapter, gmm_adapter
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BasicParametricTools(ToolGroup):
|
|
15
|
+
"""基础参数估计工具组"""
|
|
16
|
+
|
|
17
|
+
name = "BASIC PARAMETRIC ESTIMATION"
|
|
18
|
+
description = "使用 econometrics/ 核心算法的基础参数估计工具"
|
|
19
|
+
version = "2.0.0"
|
|
20
|
+
|
|
21
|
+
@classmethod
|
|
22
|
+
def get_tools(cls) -> List[Dict[str, Any]]:
|
|
23
|
+
"""返回工具列表"""
|
|
24
|
+
return [
|
|
25
|
+
{
|
|
26
|
+
"name": "basic_parametric_estimation_ols",
|
|
27
|
+
"handler": cls.ols_tool,
|
|
28
|
+
"description": "OLS Regression Analysis"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"name": "basic_parametric_estimation_mle",
|
|
32
|
+
"handler": cls.mle_tool,
|
|
33
|
+
"description": "Maximum Likelihood Estimation"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"name": "basic_parametric_estimation_gmm",
|
|
37
|
+
"handler": cls.gmm_tool,
|
|
38
|
+
"description": "Generalized Method of Moments"
|
|
39
|
+
}
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
@classmethod
|
|
43
|
+
def get_help_text(cls) -> str:
|
|
44
|
+
"""返回帮助文档"""
|
|
45
|
+
return """
|
|
46
|
+
1. OLS Regression (basic_parametric_estimation_ols)
|
|
47
|
+
- Reuses: econometrics/basic_parametric_estimation/ols/ols_model.py
|
|
48
|
+
- Input: Direct (y_data + x_data) or File (file_path)
|
|
49
|
+
- Formats: txt/json/csv/excel
|
|
50
|
+
|
|
51
|
+
2. Maximum Likelihood Estimation (basic_parametric_estimation_mle)
|
|
52
|
+
- Reuses: econometrics/basic_parametric_estimation/mle/mle_model.py
|
|
53
|
+
- Input: Direct (data) or File (file_path)
|
|
54
|
+
- Distributions: normal, poisson, exponential
|
|
55
|
+
- Formats: txt/json/csv/excel
|
|
56
|
+
|
|
57
|
+
3. Generalized Method of Moments (basic_parametric_estimation_gmm)
|
|
58
|
+
- Reuses: econometrics/basic_parametric_estimation/gmm/gmm_model.py
|
|
59
|
+
- Input: Direct (y_data + x_data) or File (file_path)
|
|
60
|
+
- Fixed: j_p_value bug
|
|
61
|
+
- Formats: txt/json/csv/excel
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
@staticmethod
|
|
65
|
+
async def ols_tool(
|
|
66
|
+
y_data: Optional[List[float]] = None,
|
|
67
|
+
x_data: Optional[Union[List[float], List[List[float]]]] = None,
|
|
68
|
+
file_path: Optional[str] = None,
|
|
69
|
+
feature_names: Optional[List[str]] = None,
|
|
70
|
+
constant: bool = True,
|
|
71
|
+
confidence_level: float = 0.95,
|
|
72
|
+
output_format: str = "json",
|
|
73
|
+
save_path: Optional[str] = None,
|
|
74
|
+
ctx: Context[ServerSession, None] = None
|
|
75
|
+
) -> str:
|
|
76
|
+
"""OLS Regression Analysis"""
|
|
77
|
+
try:
|
|
78
|
+
if ctx:
|
|
79
|
+
await ctx.info("Starting OLS regression...")
|
|
80
|
+
|
|
81
|
+
result = ols_adapter(
|
|
82
|
+
y_data=y_data,
|
|
83
|
+
x_data=x_data,
|
|
84
|
+
file_path=file_path,
|
|
85
|
+
feature_names=feature_names,
|
|
86
|
+
constant=constant,
|
|
87
|
+
confidence_level=confidence_level,
|
|
88
|
+
output_format=output_format,
|
|
89
|
+
save_path=save_path
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
if ctx:
|
|
93
|
+
await ctx.info("OLS regression complete")
|
|
94
|
+
|
|
95
|
+
return result
|
|
96
|
+
except Exception as e:
|
|
97
|
+
if ctx:
|
|
98
|
+
await ctx.error(f"Error: {str(e)}")
|
|
99
|
+
raise
|
|
100
|
+
|
|
101
|
+
@staticmethod
|
|
102
|
+
async def mle_tool(
|
|
103
|
+
data: Optional[List[float]] = None,
|
|
104
|
+
file_path: Optional[str] = None,
|
|
105
|
+
distribution: str = "normal",
|
|
106
|
+
initial_params: Optional[List[float]] = None,
|
|
107
|
+
confidence_level: float = 0.95,
|
|
108
|
+
output_format: str = "json",
|
|
109
|
+
save_path: Optional[str] = None,
|
|
110
|
+
ctx: Context[ServerSession, None] = None
|
|
111
|
+
) -> str:
|
|
112
|
+
"""Maximum Likelihood Estimation"""
|
|
113
|
+
try:
|
|
114
|
+
if ctx:
|
|
115
|
+
await ctx.info("Starting MLE estimation...")
|
|
116
|
+
|
|
117
|
+
result = mle_adapter(
|
|
118
|
+
data=data,
|
|
119
|
+
file_path=file_path,
|
|
120
|
+
distribution=distribution,
|
|
121
|
+
initial_params=initial_params,
|
|
122
|
+
confidence_level=confidence_level,
|
|
123
|
+
output_format=output_format,
|
|
124
|
+
save_path=save_path
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
if ctx:
|
|
128
|
+
await ctx.info("MLE estimation complete")
|
|
129
|
+
|
|
130
|
+
return result
|
|
131
|
+
except Exception as e:
|
|
132
|
+
if ctx:
|
|
133
|
+
await ctx.error(f"Error: {str(e)}")
|
|
134
|
+
raise
|
|
135
|
+
|
|
136
|
+
@staticmethod
|
|
137
|
+
async def gmm_tool(
|
|
138
|
+
y_data: Optional[List[float]] = None,
|
|
139
|
+
x_data: Optional[Union[List[float], List[List[float]]]] = None,
|
|
140
|
+
file_path: Optional[str] = None,
|
|
141
|
+
instruments: Optional[Union[List[float], List[List[float]]]] = None,
|
|
142
|
+
feature_names: Optional[List[str]] = None,
|
|
143
|
+
constant: bool = True,
|
|
144
|
+
confidence_level: float = 0.95,
|
|
145
|
+
output_format: str = "json",
|
|
146
|
+
save_path: Optional[str] = None,
|
|
147
|
+
ctx: Context[ServerSession, None] = None
|
|
148
|
+
) -> str:
|
|
149
|
+
"""Generalized Method of Moments"""
|
|
150
|
+
try:
|
|
151
|
+
if ctx:
|
|
152
|
+
await ctx.info("Starting GMM estimation...")
|
|
153
|
+
|
|
154
|
+
result = gmm_adapter(
|
|
155
|
+
y_data=y_data,
|
|
156
|
+
x_data=x_data,
|
|
157
|
+
file_path=file_path,
|
|
158
|
+
instruments=instruments,
|
|
159
|
+
feature_names=feature_names,
|
|
160
|
+
constant=constant,
|
|
161
|
+
confidence_level=confidence_level,
|
|
162
|
+
output_format=output_format,
|
|
163
|
+
save_path=save_path
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
if ctx:
|
|
167
|
+
await ctx.info("GMM estimation complete")
|
|
168
|
+
|
|
169
|
+
return result
|
|
170
|
+
except Exception as e:
|
|
171
|
+
if ctx:
|
|
172
|
+
await ctx.error(f"Error: {str(e)}")
|
|
173
|
+
raise
|