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,412 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Microeconometrics Adapter for Econometrics MCP Tools
|
|
3
|
+
Provides unified interfaces for discrete choice, count data, and limited dependent variable models
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
import pandas as pd
|
|
8
|
+
from typing import Union, Optional, Dict, Any, List
|
|
9
|
+
import json
|
|
10
|
+
import logging
|
|
11
|
+
|
|
12
|
+
# Import microeconometrics modules
|
|
13
|
+
from econometrics.specific_data_modeling.micro_discrete_limited_data import (
|
|
14
|
+
LogitModel,
|
|
15
|
+
ProbitModel,
|
|
16
|
+
MultinomialLogit,
|
|
17
|
+
OrderedLogit,
|
|
18
|
+
ConditionalLogit,
|
|
19
|
+
PoissonModel,
|
|
20
|
+
NegativeBinomialModel,
|
|
21
|
+
ZeroInflatedPoissonModel,
|
|
22
|
+
ZeroInflatedNegativeBinomialModel,
|
|
23
|
+
TobitModel,
|
|
24
|
+
HeckmanModel
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
from tools.data_loader import DataLoader
|
|
28
|
+
|
|
29
|
+
# Set up logging
|
|
30
|
+
logger = logging.getLogger(__name__)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def logit_adapter(
|
|
34
|
+
X_data: Optional[Union[List[float], List[List[float]]]] = None,
|
|
35
|
+
y_data: Optional[List[int]] = None,
|
|
36
|
+
file_path: Optional[str] = None,
|
|
37
|
+
feature_names: Optional[List[str]] = None,
|
|
38
|
+
output_format: str = 'json',
|
|
39
|
+
save_path: Optional[str] = None
|
|
40
|
+
) -> str:
|
|
41
|
+
"""Logistic regression adapter"""
|
|
42
|
+
try:
|
|
43
|
+
if file_path:
|
|
44
|
+
data = DataLoader.load_from_file(file_path)
|
|
45
|
+
X_data = data.get('x_data', data.get('X', data.get('features')))
|
|
46
|
+
y_data = data.get('y_data', data.get('y', data.get('target')))
|
|
47
|
+
if feature_names is None:
|
|
48
|
+
feature_names = data.get('feature_names')
|
|
49
|
+
|
|
50
|
+
if X_data is None or y_data is None:
|
|
51
|
+
raise ValueError("X_data and y_data must be provided")
|
|
52
|
+
|
|
53
|
+
X = np.array(X_data)
|
|
54
|
+
y = np.array(y_data)
|
|
55
|
+
|
|
56
|
+
if X.ndim == 1:
|
|
57
|
+
X = X.reshape(-1, 1)
|
|
58
|
+
|
|
59
|
+
model = LogitModel()
|
|
60
|
+
model.fit(X, y)
|
|
61
|
+
|
|
62
|
+
results = model.results_
|
|
63
|
+
formatted_results = {
|
|
64
|
+
'model_type': 'logit',
|
|
65
|
+
'coefficients': results.params.tolist(),
|
|
66
|
+
'std_errors': results.bse.tolist(),
|
|
67
|
+
'z_values': results.tvalues.tolist(),
|
|
68
|
+
'p_values': results.pvalues.tolist(),
|
|
69
|
+
'pseudo_r_squared': float(results.prsquared),
|
|
70
|
+
'log_likelihood': float(results.llf),
|
|
71
|
+
'aic': float(results.aic),
|
|
72
|
+
'bic': float(results.bic),
|
|
73
|
+
'n_obs': int(results.nobs),
|
|
74
|
+
'feature_names': feature_names or [f'X{i+1}' for i in range(X.shape[1])]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return json.dumps(formatted_results, indent=2, ensure_ascii=False)
|
|
78
|
+
|
|
79
|
+
except Exception as e:
|
|
80
|
+
logger.error(f"Logit failed: {str(e)}")
|
|
81
|
+
return json.dumps({'error': str(e)}, indent=2, ensure_ascii=False)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def probit_adapter(
|
|
85
|
+
X_data: Optional[Union[List[float], List[List[float]]]] = None,
|
|
86
|
+
y_data: Optional[List[int]] = None,
|
|
87
|
+
file_path: Optional[str] = None,
|
|
88
|
+
feature_names: Optional[List[str]] = None,
|
|
89
|
+
output_format: str = 'json',
|
|
90
|
+
save_path: Optional[str] = None
|
|
91
|
+
) -> str:
|
|
92
|
+
"""Probit regression adapter"""
|
|
93
|
+
try:
|
|
94
|
+
if file_path:
|
|
95
|
+
data = DataLoader.load_from_file(file_path)
|
|
96
|
+
X_data = data.get('x_data', data.get('X', data.get('features')))
|
|
97
|
+
y_data = data.get('y_data', data.get('y', data.get('target')))
|
|
98
|
+
if feature_names is None:
|
|
99
|
+
feature_names = data.get('feature_names')
|
|
100
|
+
|
|
101
|
+
if X_data is None or y_data is None:
|
|
102
|
+
raise ValueError("X_data and y_data must be provided")
|
|
103
|
+
|
|
104
|
+
X = np.array(X_data)
|
|
105
|
+
y = np.array(y_data)
|
|
106
|
+
|
|
107
|
+
if X.ndim == 1:
|
|
108
|
+
X = X.reshape(-1, 1)
|
|
109
|
+
|
|
110
|
+
model = ProbitModel()
|
|
111
|
+
model.fit(X, y)
|
|
112
|
+
|
|
113
|
+
results = model.results_
|
|
114
|
+
formatted_results = {
|
|
115
|
+
'model_type': 'probit',
|
|
116
|
+
'coefficients': results.params.tolist(),
|
|
117
|
+
'std_errors': results.bse.tolist(),
|
|
118
|
+
'z_values': results.tvalues.tolist(),
|
|
119
|
+
'p_values': results.pvalues.tolist(),
|
|
120
|
+
'pseudo_r_squared': float(results.prsquared),
|
|
121
|
+
'log_likelihood': float(results.llf),
|
|
122
|
+
'aic': float(results.aic),
|
|
123
|
+
'bic': float(results.bic),
|
|
124
|
+
'n_obs': int(results.nobs),
|
|
125
|
+
'feature_names': feature_names or [f'X{i+1}' for i in range(X.shape[1])]
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return json.dumps(formatted_results, indent=2, ensure_ascii=False)
|
|
129
|
+
|
|
130
|
+
except Exception as e:
|
|
131
|
+
logger.error(f"Probit failed: {str(e)}")
|
|
132
|
+
return json.dumps({'error': str(e)}, indent=2, ensure_ascii=False)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def multinomial_logit_adapter(
|
|
136
|
+
X_data: Optional[Union[List[float], List[List[float]]]] = None,
|
|
137
|
+
y_data: Optional[List[int]] = None,
|
|
138
|
+
file_path: Optional[str] = None,
|
|
139
|
+
feature_names: Optional[List[str]] = None,
|
|
140
|
+
output_format: str = 'json',
|
|
141
|
+
save_path: Optional[str] = None
|
|
142
|
+
) -> str:
|
|
143
|
+
"""Multinomial Logit adapter"""
|
|
144
|
+
try:
|
|
145
|
+
if file_path:
|
|
146
|
+
data = DataLoader.load_from_file(file_path)
|
|
147
|
+
X_data = data.get('x_data', data.get('X', data.get('features')))
|
|
148
|
+
y_data = data.get('y_data', data.get('y', data.get('target')))
|
|
149
|
+
if feature_names is None:
|
|
150
|
+
feature_names = data.get('feature_names')
|
|
151
|
+
|
|
152
|
+
if X_data is None or y_data is None:
|
|
153
|
+
raise ValueError("X_data and y_data must be provided")
|
|
154
|
+
|
|
155
|
+
X = np.array(X_data)
|
|
156
|
+
y = np.array(y_data)
|
|
157
|
+
|
|
158
|
+
if X.ndim == 1:
|
|
159
|
+
X = X.reshape(-1, 1)
|
|
160
|
+
|
|
161
|
+
model = MultinomialLogit()
|
|
162
|
+
model.fit(X, y)
|
|
163
|
+
|
|
164
|
+
results = model.results_
|
|
165
|
+
formatted_results = {
|
|
166
|
+
'model_type': 'multinomial_logit',
|
|
167
|
+
'coefficients': results.params.tolist(),
|
|
168
|
+
'std_errors': results.bse.tolist(),
|
|
169
|
+
'z_values': results.tvalues.tolist(),
|
|
170
|
+
'p_values': results.pvalues.tolist(),
|
|
171
|
+
'pseudo_r_squared': float(results.prsquared),
|
|
172
|
+
'log_likelihood': float(results.llf),
|
|
173
|
+
'aic': float(results.aic),
|
|
174
|
+
'bic': float(results.bic),
|
|
175
|
+
'n_obs': int(results.nobs),
|
|
176
|
+
'classes': model.classes_.tolist(),
|
|
177
|
+
'feature_names': feature_names or [f'X{i+1}' for i in range(X.shape[1])]
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return json.dumps(formatted_results, indent=2, ensure_ascii=False)
|
|
181
|
+
|
|
182
|
+
except Exception as e:
|
|
183
|
+
logger.error(f"Multinomial Logit failed: {str(e)}")
|
|
184
|
+
return json.dumps({'error': str(e)}, indent=2, ensure_ascii=False)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def poisson_adapter(
|
|
188
|
+
X_data: Optional[Union[List[float], List[List[float]]]] = None,
|
|
189
|
+
y_data: Optional[List[int]] = None,
|
|
190
|
+
file_path: Optional[str] = None,
|
|
191
|
+
feature_names: Optional[List[str]] = None,
|
|
192
|
+
output_format: str = 'json',
|
|
193
|
+
save_path: Optional[str] = None
|
|
194
|
+
) -> str:
|
|
195
|
+
"""Poisson regression adapter"""
|
|
196
|
+
try:
|
|
197
|
+
if file_path:
|
|
198
|
+
data = DataLoader.load_from_file(file_path)
|
|
199
|
+
X_data = data.get('x_data', data.get('X', data.get('features')))
|
|
200
|
+
y_data = data.get('y_data', data.get('y', data.get('target')))
|
|
201
|
+
if feature_names is None:
|
|
202
|
+
feature_names = data.get('feature_names')
|
|
203
|
+
|
|
204
|
+
if X_data is None or y_data is None:
|
|
205
|
+
raise ValueError("X_data and y_data must be provided")
|
|
206
|
+
|
|
207
|
+
X = np.array(X_data)
|
|
208
|
+
y = np.array(y_data).astype(int)
|
|
209
|
+
|
|
210
|
+
if X.ndim == 1:
|
|
211
|
+
X = X.reshape(-1, 1)
|
|
212
|
+
|
|
213
|
+
model = PoissonModel()
|
|
214
|
+
model.fit(X, y)
|
|
215
|
+
|
|
216
|
+
results = model.results_
|
|
217
|
+
formatted_results = {
|
|
218
|
+
'model_type': 'poisson',
|
|
219
|
+
'coefficients': results.params.tolist(),
|
|
220
|
+
'std_errors': results.bse.tolist(),
|
|
221
|
+
'z_values': results.tvalues.tolist(),
|
|
222
|
+
'p_values': results.pvalues.tolist(),
|
|
223
|
+
'pseudo_r_squared': float(results.prsquared),
|
|
224
|
+
'log_likelihood': float(results.llf),
|
|
225
|
+
'aic': float(results.aic),
|
|
226
|
+
'bic': float(results.bic),
|
|
227
|
+
'n_obs': int(results.nobs),
|
|
228
|
+
'feature_names': feature_names or [f'X{i+1}' for i in range(X.shape[1])]
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return json.dumps(formatted_results, indent=2, ensure_ascii=False)
|
|
232
|
+
|
|
233
|
+
except Exception as e:
|
|
234
|
+
logger.error(f"Poisson failed: {str(e)}")
|
|
235
|
+
return json.dumps({'error': str(e)}, indent=2, ensure_ascii=False)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def negative_binomial_adapter(
|
|
239
|
+
X_data: Optional[Union[List[float], List[List[float]]]] = None,
|
|
240
|
+
y_data: Optional[List[int]] = None,
|
|
241
|
+
file_path: Optional[str] = None,
|
|
242
|
+
feature_names: Optional[List[str]] = None,
|
|
243
|
+
distr: str = 'nb2',
|
|
244
|
+
output_format: str = 'json',
|
|
245
|
+
save_path: Optional[str] = None
|
|
246
|
+
) -> str:
|
|
247
|
+
"""Negative Binomial regression adapter"""
|
|
248
|
+
try:
|
|
249
|
+
if file_path:
|
|
250
|
+
data = DataLoader.load_from_file(file_path)
|
|
251
|
+
X_data = data.get('x_data', data.get('X', data.get('features')))
|
|
252
|
+
y_data = data.get('y_data', data.get('y', data.get('target')))
|
|
253
|
+
if feature_names is None:
|
|
254
|
+
feature_names = data.get('feature_names')
|
|
255
|
+
|
|
256
|
+
if X_data is None or y_data is None:
|
|
257
|
+
raise ValueError("X_data and y_data must be provided")
|
|
258
|
+
|
|
259
|
+
X = np.array(X_data)
|
|
260
|
+
y = np.array(y_data).astype(int)
|
|
261
|
+
|
|
262
|
+
if X.ndim == 1:
|
|
263
|
+
X = X.reshape(-1, 1)
|
|
264
|
+
|
|
265
|
+
model = NegativeBinomialModel(distr=distr)
|
|
266
|
+
model.fit(X, y)
|
|
267
|
+
|
|
268
|
+
results = model.results_
|
|
269
|
+
formatted_results = {
|
|
270
|
+
'model_type': 'negative_binomial',
|
|
271
|
+
'distribution': distr,
|
|
272
|
+
'coefficients': results.params.tolist(),
|
|
273
|
+
'std_errors': results.bse.tolist(),
|
|
274
|
+
'z_values': results.tvalues.tolist(),
|
|
275
|
+
'p_values': results.pvalues.tolist(),
|
|
276
|
+
'pseudo_r_squared': float(results.prsquared),
|
|
277
|
+
'log_likelihood': float(results.llf),
|
|
278
|
+
'aic': float(results.aic),
|
|
279
|
+
'bic': float(results.bic),
|
|
280
|
+
'n_obs': int(results.nobs),
|
|
281
|
+
'feature_names': feature_names or [f'X{i+1}' for i in range(X.shape[1])]
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return json.dumps(formatted_results, indent=2, ensure_ascii=False)
|
|
285
|
+
|
|
286
|
+
except Exception as e:
|
|
287
|
+
logger.error(f"Negative Binomial failed: {str(e)}")
|
|
288
|
+
return json.dumps({'error': str(e)}, indent=2, ensure_ascii=False)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def tobit_adapter(
|
|
292
|
+
X_data: Optional[Union[List[float], List[List[float]]]] = None,
|
|
293
|
+
y_data: Optional[List[float]] = None,
|
|
294
|
+
file_path: Optional[str] = None,
|
|
295
|
+
feature_names: Optional[List[str]] = None,
|
|
296
|
+
lower_bound: float = 0.0,
|
|
297
|
+
upper_bound: Optional[float] = None,
|
|
298
|
+
output_format: str = 'json',
|
|
299
|
+
save_path: Optional[str] = None
|
|
300
|
+
) -> str:
|
|
301
|
+
"""Tobit model adapter"""
|
|
302
|
+
try:
|
|
303
|
+
if file_path:
|
|
304
|
+
data = DataLoader.load_from_file(file_path)
|
|
305
|
+
X_data = data.get('x_data', data.get('X', data.get('features')))
|
|
306
|
+
y_data = data.get('y_data', data.get('y', data.get('target')))
|
|
307
|
+
if feature_names is None:
|
|
308
|
+
feature_names = data.get('feature_names')
|
|
309
|
+
|
|
310
|
+
if X_data is None or y_data is None:
|
|
311
|
+
raise ValueError("X_data and y_data must be provided")
|
|
312
|
+
|
|
313
|
+
X = np.array(X_data)
|
|
314
|
+
y = np.array(y_data)
|
|
315
|
+
|
|
316
|
+
if X.ndim == 1:
|
|
317
|
+
X = X.reshape(-1, 1)
|
|
318
|
+
|
|
319
|
+
model = TobitModel(lower_bound=lower_bound, upper_bound=upper_bound)
|
|
320
|
+
model.fit(X, y)
|
|
321
|
+
|
|
322
|
+
results = model.results_
|
|
323
|
+
formatted_results = {
|
|
324
|
+
'model_type': 'tobit',
|
|
325
|
+
'lower_bound': lower_bound,
|
|
326
|
+
'upper_bound': upper_bound,
|
|
327
|
+
'coefficients': results.params.tolist(),
|
|
328
|
+
'std_errors': results.bse.tolist(),
|
|
329
|
+
'z_values': results.tvalues.tolist(),
|
|
330
|
+
'p_values': results.pvalues.tolist(),
|
|
331
|
+
'log_likelihood': float(results.llf),
|
|
332
|
+
'aic': float(results.aic),
|
|
333
|
+
'bic': float(results.bic),
|
|
334
|
+
'n_obs': int(results.nobs),
|
|
335
|
+
'feature_names': feature_names or [f'X{i+1}' for i in range(X.shape[1])]
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return json.dumps(formatted_results, indent=2, ensure_ascii=False)
|
|
339
|
+
|
|
340
|
+
except Exception as e:
|
|
341
|
+
logger.error(f"Tobit failed: {str(e)}")
|
|
342
|
+
return json.dumps({'error': str(e)}, indent=2, ensure_ascii=False)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def heckman_adapter(
|
|
346
|
+
X_select_data: Optional[Union[List[float], List[List[float]]]] = None,
|
|
347
|
+
Z_data: Optional[Union[List[float], List[List[float]]]] = None,
|
|
348
|
+
y_data: Optional[List[float]] = None,
|
|
349
|
+
s_data: Optional[List[int]] = None,
|
|
350
|
+
file_path: Optional[str] = None,
|
|
351
|
+
selection_feature_names: Optional[List[str]] = None,
|
|
352
|
+
outcome_feature_names: Optional[List[str]] = None,
|
|
353
|
+
output_format: str = 'json',
|
|
354
|
+
save_path: Optional[str] = None
|
|
355
|
+
) -> str:
|
|
356
|
+
"""Heckman selection model adapter"""
|
|
357
|
+
try:
|
|
358
|
+
if file_path:
|
|
359
|
+
data = DataLoader.load_from_file(file_path)
|
|
360
|
+
X_select_data = data.get('X_select', data.get('selection_features'))
|
|
361
|
+
Z_data = data.get('Z', data.get('outcome_features'))
|
|
362
|
+
y_data = data.get('y', data.get('outcome'))
|
|
363
|
+
s_data = data.get('s', data.get('selection'))
|
|
364
|
+
if selection_feature_names is None:
|
|
365
|
+
selection_feature_names = data.get('selection_feature_names')
|
|
366
|
+
if outcome_feature_names is None:
|
|
367
|
+
outcome_feature_names = data.get('outcome_feature_names')
|
|
368
|
+
|
|
369
|
+
if X_select_data is None or Z_data is None or y_data is None or s_data is None:
|
|
370
|
+
raise ValueError("All data must be provided")
|
|
371
|
+
|
|
372
|
+
X_select = np.array(X_select_data)
|
|
373
|
+
Z = np.array(Z_data)
|
|
374
|
+
y = np.array(y_data)
|
|
375
|
+
s = np.array(s_data).astype(int)
|
|
376
|
+
|
|
377
|
+
if X_select.ndim == 1:
|
|
378
|
+
X_select = X_select.reshape(-1, 1)
|
|
379
|
+
if Z.ndim == 1:
|
|
380
|
+
Z = Z.reshape(-1, 1)
|
|
381
|
+
|
|
382
|
+
model = HeckmanModel()
|
|
383
|
+
model.fit(X_select, Z, y, s)
|
|
384
|
+
|
|
385
|
+
selection_names = selection_feature_names or [f'SelectX{i+1}' for i in range(X_select.shape[1])]
|
|
386
|
+
outcome_names = outcome_feature_names or [f'OutcomeZ{i+1}' for i in range(Z.shape[1])]
|
|
387
|
+
|
|
388
|
+
formatted_results = {
|
|
389
|
+
'model_type': 'heckman',
|
|
390
|
+
'selection_results': {
|
|
391
|
+
'coefficients': model.selection_results_.params.tolist(),
|
|
392
|
+
'std_errors': model.selection_results_.bse.tolist(),
|
|
393
|
+
'z_values': model.selection_results_.tvalues.tolist(),
|
|
394
|
+
'p_values': model.selection_results_.pvalues.tolist(),
|
|
395
|
+
'feature_names': selection_names
|
|
396
|
+
},
|
|
397
|
+
'outcome_results': {
|
|
398
|
+
'coefficients': model.outcome_results_.params.tolist(),
|
|
399
|
+
'std_errors': model.outcome_results_.bse.tolist(),
|
|
400
|
+
't_values': model.outcome_results_.tvalues.tolist(),
|
|
401
|
+
'p_values': model.outcome_results_.pvalues.tolist(),
|
|
402
|
+
'feature_names': outcome_names
|
|
403
|
+
},
|
|
404
|
+
'n_obs': len(y),
|
|
405
|
+
'n_selected': int(np.sum(s))
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return json.dumps(formatted_results, indent=2, ensure_ascii=False)
|
|
409
|
+
|
|
410
|
+
except Exception as e:
|
|
411
|
+
logger.error(f"Heckman failed: {str(e)}")
|
|
412
|
+
return json.dumps({'error': str(e)}, indent=2, ensure_ascii=False)
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""
|
|
2
|
+
缺失数据处理适配器
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import List, Optional
|
|
6
|
+
import json
|
|
7
|
+
|
|
8
|
+
from econometrics.missing_data import (
|
|
9
|
+
simple_imputation,
|
|
10
|
+
multiple_imputation,
|
|
11
|
+
SimpleImputationResult,
|
|
12
|
+
MultipleImputationResult
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
from .output_formatter import OutputFormatter
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def simple_imputation_adapter(
|
|
19
|
+
data: List[List[float]],
|
|
20
|
+
strategy: str = "mean",
|
|
21
|
+
fill_value: Optional[float] = None,
|
|
22
|
+
output_format: str = "json",
|
|
23
|
+
save_path: Optional[str] = None
|
|
24
|
+
) -> str:
|
|
25
|
+
"""简单插补适配器"""
|
|
26
|
+
|
|
27
|
+
result: SimpleImputationResult = simple_imputation(
|
|
28
|
+
data=data,
|
|
29
|
+
strategy=strategy,
|
|
30
|
+
fill_value=fill_value
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
if output_format == "json":
|
|
34
|
+
json_result = json.dumps(result.dict(), ensure_ascii=False, indent=2)
|
|
35
|
+
if save_path:
|
|
36
|
+
OutputFormatter.save_to_file(json_result, save_path)
|
|
37
|
+
return f"分析完成!结果已保存到: {save_path}\n\n{json_result}"
|
|
38
|
+
return json_result
|
|
39
|
+
else:
|
|
40
|
+
formatted = f"""# 简单插补结果\n\n{result.summary}"""
|
|
41
|
+
if save_path:
|
|
42
|
+
OutputFormatter.save_to_file(formatted, save_path)
|
|
43
|
+
return formatted
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def multiple_imputation_adapter(
|
|
47
|
+
data: List[List[float]],
|
|
48
|
+
n_imputations: int = 5,
|
|
49
|
+
max_iter: int = 10,
|
|
50
|
+
random_state: Optional[int] = None,
|
|
51
|
+
output_format: str = "json",
|
|
52
|
+
save_path: Optional[str] = None
|
|
53
|
+
) -> str:
|
|
54
|
+
"""多重插补适配器"""
|
|
55
|
+
|
|
56
|
+
result: MultipleImputationResult = multiple_imputation(
|
|
57
|
+
data=data,
|
|
58
|
+
n_imputations=n_imputations,
|
|
59
|
+
max_iter=max_iter,
|
|
60
|
+
random_state=random_state
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
if output_format == "json":
|
|
64
|
+
json_result = json.dumps(result.dict(), ensure_ascii=False, indent=2)
|
|
65
|
+
if save_path:
|
|
66
|
+
OutputFormatter.save_to_file(json_result, save_path)
|
|
67
|
+
return f"分析完成!结果已保存到: {save_path}\n\n{json_result}"
|
|
68
|
+
return json_result
|
|
69
|
+
else:
|
|
70
|
+
formatted = f"""# 多重插补结果\n\n{result.summary}"""
|
|
71
|
+
if save_path:
|
|
72
|
+
OutputFormatter.save_to_file(formatted, save_path)
|
|
73
|
+
return formatted
|