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,402 @@
1
+ """
2
+ 模型规范、诊断和稳健推断工具组
3
+ 包含诊断检验、GLS、WLS、稳健标准误、模型选择、正则化、联立方程等方法
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 (
12
+ diagnostic_tests_adapter,
13
+ gls_adapter,
14
+ wls_adapter,
15
+ robust_errors_adapter,
16
+ model_selection_adapter,
17
+ regularization_adapter,
18
+ simultaneous_equations_adapter
19
+ )
20
+
21
+
22
+ class ModelSpecificationTools(ToolGroup):
23
+ """模型规范、诊断和稳健推断工具组"""
24
+
25
+ name = "MODEL SPECIFICATION, DIAGNOSTICS & ROBUST INFERENCE"
26
+ description = "模型规范检验、诊断测试和稳健推断方法工具"
27
+ version = "2.0.0"
28
+
29
+ @classmethod
30
+ def get_tools(cls) -> List[Dict[str, Any]]:
31
+ """返回工具列表"""
32
+ return [
33
+ {
34
+ "name": "model_diagnostic_tests",
35
+ "handler": cls.diagnostic_tests_tool,
36
+ "description": "Model Diagnostic Tests (Heteroskedasticity, Autocorrelation, Normality, VIF)"
37
+ },
38
+ {
39
+ "name": "generalized_least_squares",
40
+ "handler": cls.gls_tool,
41
+ "description": "Generalized Least Squares (GLS) Regression"
42
+ },
43
+ {
44
+ "name": "weighted_least_squares",
45
+ "handler": cls.wls_tool,
46
+ "description": "Weighted Least Squares (WLS) Regression"
47
+ },
48
+ {
49
+ "name": "robust_errors_regression",
50
+ "handler": cls.robust_errors_tool,
51
+ "description": "Robust Standard Errors Regression (Heteroskedasticity-Robust)"
52
+ },
53
+ {
54
+ "name": "model_selection_criteria",
55
+ "handler": cls.model_selection_tool,
56
+ "description": "Model Selection Criteria (AIC, BIC, HQIC, Cross-Validation)"
57
+ },
58
+ {
59
+ "name": "regularized_regression",
60
+ "handler": cls.regularization_tool,
61
+ "description": "Regularized Regression (Ridge, LASSO, Elastic Net)"
62
+ },
63
+ {
64
+ "name": "simultaneous_equations_model",
65
+ "handler": cls.simultaneous_equations_tool,
66
+ "description": "Simultaneous Equations Model (2SLS)"
67
+ }
68
+ ]
69
+
70
+ @classmethod
71
+ def get_help_text(cls) -> str:
72
+ """返回帮助文档"""
73
+ return """
74
+ 15. Model Diagnostic Tests (model_diagnostic_tests)
75
+ - Heteroskedasticity: Breusch-Pagan, White tests
76
+ - Autocorrelation: Durbin-Watson test
77
+ - Normality: Jarque-Bera test
78
+ - Multicollinearity: VIF calculation
79
+ - Comprehensive model diagnostics
80
+
81
+ 16. Generalized Least Squares (generalized_least_squares)
82
+ - GLS: Handle heteroskedasticity and autocorrelation
83
+ - Covariance matrix: User-specified or estimated
84
+ - Efficient: More efficient than OLS under GLS assumptions
85
+
86
+ 17. Weighted Least Squares (weighted_least_squares)
87
+ - WLS: Handle heteroskedasticity with known weights
88
+ - Weights: Inverse of variance or other weighting schemes
89
+ - Applications: Survey data, grouped data
90
+
91
+ 18. Robust Standard Errors (robust_errors_regression)
92
+ - Heteroskedasticity-robust: HC0, HC1, HC2, HC3
93
+ - Inference: Valid inference under heteroskedasticity
94
+ - Applications: Cross-sectional data with heteroskedasticity
95
+
96
+ 19. Model Selection Criteria (model_selection_criteria)
97
+ - Information criteria: AIC, BIC, HQIC
98
+ - Cross-validation: K-fold, leave-one-out
99
+ - Granger causality: Test for causality in time series
100
+ - Model comparison: Select best model specification
101
+
102
+ 20. Regularized Regression (regularized_regression)
103
+ - Ridge regression: L2 penalty for multicollinearity
104
+ - LASSO: L1 penalty for variable selection
105
+ - Elastic Net: Combined L1 and L2 penalties
106
+ - Applications: High-dimensional data, feature selection
107
+
108
+ 21. Simultaneous Equations Model (simultaneous_equations_model)
109
+ - 2SLS: Two-stage least squares for simultaneous equations
110
+ - Endogeneity: Handle endogenous regressors
111
+ - Instrumental variables: Valid instruments required
112
+ - Applications: Supply-demand models, macroeconomic models
113
+ """
114
+
115
+ @staticmethod
116
+ async def diagnostic_tests_tool(
117
+ y_data: Optional[List[float]] = None,
118
+ x_data: Optional[Union[List[float], List[List[float]]]] = None,
119
+ file_path: Optional[str] = None,
120
+ feature_names: Optional[List[str]] = None,
121
+ constant: bool = True,
122
+ output_format: str = "json",
123
+ save_path: Optional[str] = None,
124
+ ctx: Context[ServerSession, None] = None
125
+ ) -> str:
126
+ """Model Diagnostic Tests"""
127
+ try:
128
+ if ctx:
129
+ await ctx.info("Starting model diagnostic tests...")
130
+
131
+ result = diagnostic_tests_adapter(
132
+ y_data=y_data,
133
+ x_data=x_data,
134
+ file_path=file_path,
135
+ feature_names=feature_names,
136
+ constant=constant,
137
+ output_format=output_format,
138
+ save_path=save_path
139
+ )
140
+
141
+ if ctx:
142
+ await ctx.info("Model diagnostic tests complete")
143
+
144
+ return result
145
+ except Exception as e:
146
+ if ctx:
147
+ await ctx.error(f"Error: {str(e)}")
148
+ raise
149
+
150
+ @staticmethod
151
+ async def gls_tool(
152
+ y_data: Optional[List[float]] = None,
153
+ x_data: Optional[Union[List[float], List[List[float]]]] = None,
154
+ file_path: Optional[str] = None,
155
+ sigma: Optional[List[List[float]]] = None,
156
+ feature_names: Optional[List[str]] = None,
157
+ constant: bool = True,
158
+ confidence_level: float = 0.95,
159
+ output_format: str = "json",
160
+ save_path: Optional[str] = None,
161
+ ctx: Context[ServerSession, None] = None
162
+ ) -> str:
163
+ """Generalized Least Squares Regression"""
164
+ try:
165
+ if ctx:
166
+ await ctx.info("Starting GLS regression...")
167
+
168
+ result = gls_adapter(
169
+ y_data=y_data,
170
+ x_data=x_data,
171
+ file_path=file_path,
172
+ sigma=sigma,
173
+ feature_names=feature_names,
174
+ constant=constant,
175
+ confidence_level=confidence_level,
176
+ output_format=output_format,
177
+ save_path=save_path
178
+ )
179
+
180
+ if ctx:
181
+ await ctx.info("GLS regression complete")
182
+
183
+ return result
184
+ except Exception as e:
185
+ if ctx:
186
+ await ctx.error(f"Error: {str(e)}")
187
+ raise
188
+
189
+ @staticmethod
190
+ async def wls_tool(
191
+ y_data: Optional[List[float]] = None,
192
+ x_data: Optional[Union[List[float], List[List[float]]]] = None,
193
+ file_path: Optional[str] = None,
194
+ weights: Optional[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
+ ctx: Context[ServerSession, None] = None
201
+ ) -> str:
202
+ """Weighted Least Squares Regression"""
203
+ try:
204
+ if ctx:
205
+ await ctx.info("Starting WLS regression...")
206
+
207
+ result = wls_adapter(
208
+ y_data=y_data,
209
+ x_data=x_data,
210
+ file_path=file_path,
211
+ weights=weights,
212
+ feature_names=feature_names,
213
+ constant=constant,
214
+ confidence_level=confidence_level,
215
+ output_format=output_format,
216
+ save_path=save_path
217
+ )
218
+
219
+ if ctx:
220
+ await ctx.info("WLS regression complete")
221
+
222
+ return result
223
+ except Exception as e:
224
+ if ctx:
225
+ await ctx.error(f"Error: {str(e)}")
226
+ raise
227
+
228
+ @staticmethod
229
+ async def robust_errors_tool(
230
+ y_data: Optional[List[float]] = None,
231
+ x_data: Optional[Union[List[float], List[List[float]]]] = None,
232
+ file_path: Optional[str] = None,
233
+ feature_names: Optional[List[str]] = None,
234
+ constant: bool = True,
235
+ confidence_level: float = 0.95,
236
+ cov_type: str = "HC1",
237
+ output_format: str = "json",
238
+ save_path: Optional[str] = None,
239
+ ctx: Context[ServerSession, None] = None
240
+ ) -> str:
241
+ """Robust Standard Errors Regression"""
242
+ try:
243
+ if ctx:
244
+ await ctx.info("Starting robust errors regression...")
245
+
246
+ result = robust_errors_adapter(
247
+ y_data=y_data,
248
+ x_data=x_data,
249
+ file_path=file_path,
250
+ feature_names=feature_names,
251
+ constant=constant,
252
+ confidence_level=confidence_level,
253
+ cov_type=cov_type,
254
+ output_format=output_format,
255
+ save_path=save_path
256
+ )
257
+
258
+ if ctx:
259
+ await ctx.info("Robust errors regression complete")
260
+
261
+ return result
262
+ except Exception as e:
263
+ if ctx:
264
+ await ctx.error(f"Error: {str(e)}")
265
+ raise
266
+
267
+ @staticmethod
268
+ async def model_selection_tool(
269
+ y_data: Optional[List[float]] = None,
270
+ x_data: Optional[Union[List[float], List[List[float]]]] = None,
271
+ file_path: Optional[str] = None,
272
+ feature_names: Optional[List[str]] = None,
273
+ constant: bool = True,
274
+ cv_folds: Optional[int] = None,
275
+ output_format: str = "json",
276
+ save_path: Optional[str] = None,
277
+ ctx: Context[ServerSession, None] = None
278
+ ) -> str:
279
+ """Model Selection Criteria"""
280
+ try:
281
+ if ctx:
282
+ await ctx.info("Starting model selection analysis...")
283
+
284
+ result = model_selection_adapter(
285
+ y_data=y_data,
286
+ x_data=x_data,
287
+ file_path=file_path,
288
+ feature_names=feature_names,
289
+ constant=constant,
290
+ cv_folds=cv_folds,
291
+ output_format=output_format,
292
+ save_path=save_path
293
+ )
294
+
295
+ if ctx:
296
+ await ctx.info("Model selection analysis complete")
297
+
298
+ return result
299
+ except Exception as e:
300
+ if ctx:
301
+ await ctx.error(f"Error: {str(e)}")
302
+ raise
303
+
304
+ @staticmethod
305
+ async def regularization_tool(
306
+ y_data: Optional[List[float]] = None,
307
+ x_data: Optional[Union[List[float], List[List[float]]]] = None,
308
+ file_path: Optional[str] = None,
309
+ method: str = "ridge",
310
+ alpha: float = 1.0,
311
+ l1_ratio: float = 0.5,
312
+ feature_names: Optional[List[str]] = None,
313
+ fit_intercept: bool = True,
314
+ output_format: str = "json",
315
+ save_path: Optional[str] = None,
316
+ ctx: Context[ServerSession, None] = None
317
+ ) -> str:
318
+ """Regularized Regression"""
319
+ try:
320
+ if ctx:
321
+ await ctx.info(f"Starting {method} regularized regression...")
322
+
323
+ result = regularization_adapter(
324
+ y_data=y_data,
325
+ x_data=x_data,
326
+ file_path=file_path,
327
+ method=method,
328
+ alpha=alpha,
329
+ l1_ratio=l1_ratio,
330
+ feature_names=feature_names,
331
+ fit_intercept=fit_intercept,
332
+ output_format=output_format,
333
+ save_path=save_path
334
+ )
335
+
336
+ if ctx:
337
+ await ctx.info(f"{method} regularized regression complete")
338
+
339
+ return result
340
+ except Exception as e:
341
+ if ctx:
342
+ await ctx.error(f"Error: {str(e)}")
343
+ raise
344
+
345
+ @staticmethod
346
+ async def simultaneous_equations_tool(
347
+ y_data: Optional[List[List[float]]] = None,
348
+ x_data: Optional[List[List[float]]] = None,
349
+ file_path: Optional[str] = None,
350
+ instruments: Optional[List[List[float]]] = None,
351
+ equation_names: Optional[List[str]] = None,
352
+ instrument_names: Optional[List[str]] = None,
353
+ constant: bool = True,
354
+ output_format: str = "json",
355
+ save_path: Optional[str] = None,
356
+ ctx: Context[ServerSession, None] = None
357
+ ) -> str:
358
+ """
359
+ Simultaneous Equations Model (2SLS)
360
+
361
+ 数据格式说明:
362
+ - y_data: 因变量数据,二维列表,每个子列表代表一个方程的因变量时间序列
363
+ - x_data: 自变量数据,二维列表,每个子列表代表一个观测的所有自变量值
364
+ - instruments: 工具变量数据,二维列表,每个子列表代表一个观测的所有工具变量值
365
+
366
+ 重要: 所有数据的观测数量必须相同
367
+
368
+ 示例调用:
369
+ {
370
+ "y_data": [[1.0, 1.2, 1.4, 1.6], [2.0, 2.2, 2.4, 2.6]],
371
+ "x_data": [[1.5, 2.5], [1.7, 2.7], [1.9, 2.9], [2.1, 3.1]],
372
+ "instruments": [[1.8, 2.8], [2.0, 3.0], [2.2, 3.2], [2.4, 3.4]],
373
+ "equation_names": ["Demand", "Supply"],
374
+ "instrument_names": ["Income", "Price"],
375
+ "constant": true,
376
+ "output_format": "json"
377
+ }
378
+ """
379
+ try:
380
+ if ctx:
381
+ await ctx.info("Starting simultaneous equations model analysis...")
382
+
383
+ result = simultaneous_equations_adapter(
384
+ y_data=y_data,
385
+ x_data=x_data,
386
+ file_path=file_path,
387
+ instruments=instruments,
388
+ equation_names=equation_names,
389
+ instrument_names=instrument_names,
390
+ constant=constant,
391
+ output_format=output_format,
392
+ save_path=save_path
393
+ )
394
+
395
+ if ctx:
396
+ await ctx.info("Simultaneous equations model analysis complete")
397
+
398
+ return result
399
+ except Exception as e:
400
+ if ctx:
401
+ await ctx.error(f"Error: {str(e)}")
402
+ raise
@@ -0,0 +1,225 @@
1
+ """
2
+ 非参数与半参数方法工具组
3
+ 包含核回归、分位数回归、样条回归和GAM
4
+ """
5
+
6
+ from typing import List, Optional, 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 ..nonparametric_adapter import (
12
+ kernel_regression_adapter,
13
+ quantile_regression_adapter,
14
+ spline_regression_adapter,
15
+ gam_adapter
16
+ )
17
+
18
+
19
+ class NonparametricTools(ToolGroup):
20
+ """非参数与半参数方法工具组"""
21
+
22
+ name = "NONPARAMETRIC & SEMIPARAMETRIC METHODS"
23
+ description = "非参数与半参数分析工具"
24
+ version = "1.0.0"
25
+
26
+ @classmethod
27
+ def get_tools(cls) -> List[Dict[str, Any]]:
28
+ """返回工具列表"""
29
+ return [
30
+ {
31
+ "name": "nonparametric_kernel_regression",
32
+ "handler": cls.kernel_regression_tool,
33
+ "description": "Kernel Regression (Nonparametric)"
34
+ },
35
+ {
36
+ "name": "nonparametric_quantile_regression",
37
+ "handler": cls.quantile_regression_tool,
38
+ "description": "Quantile Regression"
39
+ },
40
+ {
41
+ "name": "nonparametric_spline_regression",
42
+ "handler": cls.spline_regression_tool,
43
+ "description": "Spline Regression"
44
+ },
45
+ {
46
+ "name": "nonparametric_gam_model",
47
+ "handler": cls.gam_tool,
48
+ "description": "Generalized Additive Model (GAM)"
49
+ }
50
+ ]
51
+
52
+ @classmethod
53
+ def get_help_text(cls) -> str:
54
+ """返回帮助文档"""
55
+ return """
56
+ 非参数与半参数方法工具组 - 4个工具
57
+
58
+ 1. Kernel Regression (nonparametric_kernel_regression)
59
+ - 核回归估计
60
+ - 支持核函数: Gaussian, Epanechnikov, Uniform, Triangular, Biweight
61
+ - 带宽选择: 交叉验证, AIC, 正态参考
62
+ - 基于: statsmodels.nonparametric
63
+
64
+ 2. Quantile Regression (nonparametric_quantile_regression)
65
+ - 分位数回归
66
+ - 分析条件分位数
67
+ - 稳健于异常值
68
+ - 基于: statsmodels.regression.quantile_regression
69
+
70
+ 3. Spline Regression (nonparametric_spline_regression)
71
+ - 样条回归
72
+ - 灵活的非线性拟合
73
+ - 基于: sklearn.preprocessing
74
+
75
+ 4. Generalized Additive Model (nonparametric_gam_model)
76
+ - 广义可加模型
77
+ - 多个平滑函数的加和
78
+ - 基于: pygam
79
+ """
80
+
81
+ @staticmethod
82
+ async def kernel_regression_tool(
83
+ y_data: Optional[List[float]] = None,
84
+ x_data: Optional[List[List[float]]] = None,
85
+ file_path: Optional[str] = None,
86
+ kernel_type: str = "gaussian",
87
+ bandwidth: Optional[List[float]] = None,
88
+ bandwidth_method: str = "cv_ls",
89
+ variable_type: Optional[str] = None,
90
+ output_format: str = "json",
91
+ save_path: Optional[str] = None,
92
+ ctx: Context[ServerSession, None] = None
93
+ ) -> str:
94
+ """核回归分析"""
95
+ try:
96
+ if ctx:
97
+ await ctx.info(f"Starting kernel regression ({kernel_type} kernel)...")
98
+
99
+ result = kernel_regression_adapter(
100
+ y_data=y_data,
101
+ x_data=x_data,
102
+ file_path=file_path,
103
+ kernel_type=kernel_type,
104
+ bandwidth=bandwidth,
105
+ bandwidth_method=bandwidth_method,
106
+ variable_type=variable_type,
107
+ output_format=output_format,
108
+ save_path=save_path
109
+ )
110
+
111
+ if ctx:
112
+ await ctx.info("Kernel regression complete")
113
+
114
+ return result
115
+ except Exception as e:
116
+ if ctx:
117
+ await ctx.error(f"Error: {str(e)}")
118
+ raise
119
+
120
+ @staticmethod
121
+ async def quantile_regression_tool(
122
+ y_data: Optional[List[float]] = None,
123
+ x_data: Optional[List[List[float]]] = None,
124
+ file_path: Optional[str] = None,
125
+ quantile: float = 0.5,
126
+ feature_names: Optional[List[str]] = None,
127
+ confidence_level: float = 0.95,
128
+ output_format: str = "json",
129
+ save_path: Optional[str] = None,
130
+ ctx: Context[ServerSession, None] = None
131
+ ) -> str:
132
+ """分位数回归分析"""
133
+ try:
134
+ if ctx:
135
+ await ctx.info(f"Starting quantile regression (τ={quantile})...")
136
+
137
+ result = quantile_regression_adapter(
138
+ y_data=y_data,
139
+ x_data=x_data,
140
+ file_path=file_path,
141
+ quantile=quantile,
142
+ feature_names=feature_names,
143
+ confidence_level=confidence_level,
144
+ output_format=output_format,
145
+ save_path=save_path
146
+ )
147
+
148
+ if ctx:
149
+ await ctx.info("Quantile regression complete")
150
+
151
+ return result
152
+ except Exception as e:
153
+ if ctx:
154
+ await ctx.error(f"Error: {str(e)}")
155
+ raise
156
+
157
+ @staticmethod
158
+ async def spline_regression_tool(
159
+ y_data: List[float],
160
+ x_data: List[float],
161
+ n_knots: int = 5,
162
+ degree: int = 3,
163
+ knots: str = "uniform",
164
+ output_format: str = "json",
165
+ save_path: Optional[str] = None,
166
+ ctx: Context[ServerSession, None] = None
167
+ ) -> str:
168
+ """样条回归"""
169
+ try:
170
+ if ctx:
171
+ await ctx.info(f"Starting spline regression (degree={degree}, knots={n_knots})...")
172
+
173
+ result = spline_regression_adapter(
174
+ y_data=y_data,
175
+ x_data=x_data,
176
+ n_knots=n_knots,
177
+ degree=degree,
178
+ knots=knots,
179
+ output_format=output_format,
180
+ save_path=save_path
181
+ )
182
+
183
+ if ctx:
184
+ await ctx.info("Spline regression complete")
185
+
186
+ return result
187
+ except Exception as e:
188
+ if ctx:
189
+ await ctx.error(f"Error: {str(e)}")
190
+ raise
191
+
192
+ @staticmethod
193
+ async def gam_tool(
194
+ y_data: List[float],
195
+ x_data: List[List[float]],
196
+ problem_type: str = "regression",
197
+ n_splines: int = 10,
198
+ lam: float = 0.6,
199
+ output_format: str = "json",
200
+ save_path: Optional[str] = None,
201
+ ctx: Context[ServerSession, None] = None
202
+ ) -> str:
203
+ """广义可加模型(GAM)"""
204
+ try:
205
+ if ctx:
206
+ await ctx.info(f"Starting GAM model ({problem_type})...")
207
+
208
+ result = gam_adapter(
209
+ y_data=y_data,
210
+ x_data=x_data,
211
+ problem_type=problem_type,
212
+ n_splines=n_splines,
213
+ lam=lam,
214
+ output_format=output_format,
215
+ save_path=save_path
216
+ )
217
+
218
+ if ctx:
219
+ await ctx.info("GAM model complete")
220
+
221
+ return result
222
+ except Exception as e:
223
+ if ctx:
224
+ await ctx.error(f"Error: {str(e)}")
225
+ raise