aigroup-econ-mcp 0.1.6__py3-none-any.whl → 0.2.0__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.
- aigroup_econ_mcp/config.py +561 -0
- aigroup_econ_mcp/server.py +2503 -16
- aigroup_econ_mcp/tools/__init__.py +17 -7
- aigroup_econ_mcp/tools/base.py +271 -0
- aigroup_econ_mcp/tools/cache.py +533 -0
- aigroup_econ_mcp/tools/machine_learning.py +673 -0
- aigroup_econ_mcp/tools/monitoring.py +555 -0
- aigroup_econ_mcp/tools/optimized_example.py +229 -0
- aigroup_econ_mcp/tools/panel_data.py +527 -0
- aigroup_econ_mcp/tools/time_series.py +838 -260
- aigroup_econ_mcp/tools/validation.py +482 -0
- {aigroup_econ_mcp-0.1.6.dist-info → aigroup_econ_mcp-0.2.0.dist-info}/METADATA +5 -1
- aigroup_econ_mcp-0.2.0.dist-info/RECORD +20 -0
- aigroup_econ_mcp-0.1.6.dist-info/RECORD +0 -12
- {aigroup_econ_mcp-0.1.6.dist-info → aigroup_econ_mcp-0.2.0.dist-info}/WHEEL +0 -0
- {aigroup_econ_mcp-0.1.6.dist-info → aigroup_econ_mcp-0.2.0.dist-info}/entry_points.txt +0 -0
- {aigroup_econ_mcp-0.1.6.dist-info → aigroup_econ_mcp-0.2.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"""
|
|
2
|
+
优化示例模块
|
|
3
|
+
演示如何使用新的优化组件
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
from typing import List, Dict, Any, Optional
|
|
8
|
+
from pydantic import BaseModel
|
|
9
|
+
|
|
10
|
+
from .base import econometric_tool, validate_input, EconometricTool
|
|
11
|
+
from .validation import validate_econometric_data
|
|
12
|
+
from .cache import cache_result
|
|
13
|
+
from .monitoring import monitor_performance, track_progress
|
|
14
|
+
from ..config import get_config
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class OptimizedResult(BaseModel):
|
|
18
|
+
"""优化结果"""
|
|
19
|
+
mean: float
|
|
20
|
+
std: float
|
|
21
|
+
confidence_interval: Dict[str, float]
|
|
22
|
+
sample_size: int
|
|
23
|
+
performance_stats: Optional[Dict[str, Any]] = None
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class OptimizedExampleTool(EconometricTool):
|
|
27
|
+
"""
|
|
28
|
+
优化示例工具
|
|
29
|
+
演示如何使用所有优化组件
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self):
|
|
33
|
+
super().__init__("optimized_example")
|
|
34
|
+
|
|
35
|
+
@validate_input(data_type="econometric")
|
|
36
|
+
@econometric_tool("optimized_analysis")
|
|
37
|
+
def analyze_data(self, data: Dict[str, List[float]]) -> OptimizedResult:
|
|
38
|
+
"""
|
|
39
|
+
分析数据(使用所有优化组件)
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
data: 输入数据字典
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
OptimizedResult: 分析结果
|
|
46
|
+
"""
|
|
47
|
+
# 使用进度跟踪
|
|
48
|
+
with self.execute_with_progress(3, "数据分析") as tracker:
|
|
49
|
+
tracker.start_step("数据预处理")
|
|
50
|
+
# 数据预处理
|
|
51
|
+
processed_data = self._preprocess_data(data)
|
|
52
|
+
tracker.complete_step()
|
|
53
|
+
|
|
54
|
+
tracker.start_step("统计分析")
|
|
55
|
+
# 统计分析
|
|
56
|
+
result = self._perform_analysis(processed_data)
|
|
57
|
+
tracker.complete_step()
|
|
58
|
+
|
|
59
|
+
tracker.start_step("结果整理")
|
|
60
|
+
# 添加性能统计
|
|
61
|
+
performance_stats = self.get_performance_stats()
|
|
62
|
+
result.performance_stats = performance_stats
|
|
63
|
+
tracker.complete_step()
|
|
64
|
+
|
|
65
|
+
return result
|
|
66
|
+
|
|
67
|
+
def _preprocess_data(self, data: Dict[str, List[float]]) -> Dict[str, np.ndarray]:
|
|
68
|
+
"""数据预处理"""
|
|
69
|
+
processed = {}
|
|
70
|
+
for key, values in data.items():
|
|
71
|
+
# 转换为numpy数组并处理缺失值
|
|
72
|
+
arr = np.array(values)
|
|
73
|
+
arr = np.nan_to_num(arr, nan=np.nanmean(arr))
|
|
74
|
+
processed[key] = arr
|
|
75
|
+
return processed
|
|
76
|
+
|
|
77
|
+
def _perform_analysis(self, data: Dict[str, np.ndarray]) -> OptimizedResult:
|
|
78
|
+
"""执行分析"""
|
|
79
|
+
results = {}
|
|
80
|
+
|
|
81
|
+
for key, values in data.items():
|
|
82
|
+
mean = np.mean(values)
|
|
83
|
+
std = np.std(values)
|
|
84
|
+
n = len(values)
|
|
85
|
+
|
|
86
|
+
# 计算置信区间
|
|
87
|
+
confidence_level = 0.95
|
|
88
|
+
z_score = 1.96 # 95%置信水平的z值
|
|
89
|
+
margin_of_error = z_score * (std / np.sqrt(n))
|
|
90
|
+
|
|
91
|
+
results[key] = {
|
|
92
|
+
"mean": mean,
|
|
93
|
+
"std": std,
|
|
94
|
+
"confidence_interval": {
|
|
95
|
+
"lower": mean - margin_of_error,
|
|
96
|
+
"upper": mean + margin_of_error
|
|
97
|
+
},
|
|
98
|
+
"sample_size": n
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
# 返回第一个变量的结果作为示例
|
|
102
|
+
first_key = list(results.keys())[0]
|
|
103
|
+
result_data = results[first_key]
|
|
104
|
+
|
|
105
|
+
return OptimizedResult(
|
|
106
|
+
mean=result_data["mean"],
|
|
107
|
+
std=result_data["std"],
|
|
108
|
+
confidence_interval=result_data["confidence_interval"],
|
|
109
|
+
sample_size=result_data["sample_size"]
|
|
110
|
+
)
|
|
111
|
+
def get_performance_stats(self) -> Dict[str, Any]:
|
|
112
|
+
"""获取性能统计信息"""
|
|
113
|
+
from .monitoring import global_performance_monitor
|
|
114
|
+
|
|
115
|
+
stats = global_performance_monitor.get_function_stats("optimized_analysis")
|
|
116
|
+
if stats:
|
|
117
|
+
return {
|
|
118
|
+
"execution_time": stats.execution_time,
|
|
119
|
+
"call_count": stats.call_count,
|
|
120
|
+
"average_time": stats.average_time,
|
|
121
|
+
"last_execution": stats.last_execution.isoformat() if stats.last_execution else None
|
|
122
|
+
}
|
|
123
|
+
return {}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
# 使用便捷装饰器的独立函数示例
|
|
127
|
+
@validate_input(data_type="econometric")
|
|
128
|
+
@econometric_tool("quick_analysis")
|
|
129
|
+
def quick_data_analysis(data: Dict[str, List[float]]) -> Dict[str, Any]:
|
|
130
|
+
"""
|
|
131
|
+
快速数据分析(使用装饰器)
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
data: 输入数据
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
Dict[str, Any]: 分析结果
|
|
138
|
+
"""
|
|
139
|
+
# 验证数据
|
|
140
|
+
validated_data = validate_econometric_data(data)
|
|
141
|
+
|
|
142
|
+
results = {}
|
|
143
|
+
for key, values in validated_data.items():
|
|
144
|
+
arr = np.array(values)
|
|
145
|
+
results[key] = {
|
|
146
|
+
"mean": float(np.mean(arr)),
|
|
147
|
+
"std": float(np.std(arr)),
|
|
148
|
+
"min": float(np.min(arr)),
|
|
149
|
+
"max": float(np.max(arr)),
|
|
150
|
+
"count": len(arr)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return results
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@cache_result(ttl=3600) # 1小时缓存
|
|
157
|
+
@monitor_performance("cached_calculation")
|
|
158
|
+
def expensive_calculation(data: List[float], iterations: int = 1000) -> Dict[str, float]:
|
|
159
|
+
"""
|
|
160
|
+
昂贵的计算(使用缓存和性能监控)
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
data: 输入数据
|
|
164
|
+
iterations: 迭代次数
|
|
165
|
+
|
|
166
|
+
Returns:
|
|
167
|
+
Dict[str, float]: 计算结果
|
|
168
|
+
"""
|
|
169
|
+
arr = np.array(data)
|
|
170
|
+
result = 0.0
|
|
171
|
+
|
|
172
|
+
# 模拟昂贵计算
|
|
173
|
+
for i in range(iterations):
|
|
174
|
+
result += np.sum(arr * i) / (i + 1)
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
"result": float(result),
|
|
178
|
+
"iterations": iterations,
|
|
179
|
+
"data_length": len(data)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
# 使用配置的示例
|
|
184
|
+
def config_based_analysis(data: Dict[str, List[float]]) -> Dict[str, Any]:
|
|
185
|
+
"""
|
|
186
|
+
基于配置的分析
|
|
187
|
+
|
|
188
|
+
Args:
|
|
189
|
+
data: 输入数据
|
|
190
|
+
|
|
191
|
+
Returns:
|
|
192
|
+
Dict[str, Any]: 分析结果
|
|
193
|
+
"""
|
|
194
|
+
# 获取配置
|
|
195
|
+
cache_enabled = get_config("cache_enabled", True)
|
|
196
|
+
monitoring_enabled = get_config("monitoring_enabled", True)
|
|
197
|
+
min_sample_size = get_config("min_sample_size", 10)
|
|
198
|
+
|
|
199
|
+
results = {}
|
|
200
|
+
|
|
201
|
+
for key, values in data.items():
|
|
202
|
+
# 检查样本大小
|
|
203
|
+
if len(values) < min_sample_size:
|
|
204
|
+
raise ValueError(f"样本量不足: {key} (需要至少{min_sample_size}个观测点)")
|
|
205
|
+
|
|
206
|
+
# 执行分析
|
|
207
|
+
arr = np.array(values)
|
|
208
|
+
results[key] = {
|
|
209
|
+
"mean": float(np.mean(arr)),
|
|
210
|
+
"std": float(np.std(arr)),
|
|
211
|
+
"sample_size": len(arr),
|
|
212
|
+
"config_used": {
|
|
213
|
+
"cache_enabled": cache_enabled,
|
|
214
|
+
"monitoring_enabled": monitoring_enabled,
|
|
215
|
+
"min_sample_size": min_sample_size
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return results
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
# 导出主要类和函数
|
|
223
|
+
__all__ = [
|
|
224
|
+
"OptimizedResult",
|
|
225
|
+
"OptimizedExampleTool",
|
|
226
|
+
"quick_data_analysis",
|
|
227
|
+
"expensive_calculation",
|
|
228
|
+
"config_based_analysis"
|
|
229
|
+
]
|