Variya
2026-03-11 9d2d45fc70bf749a50e3a04658059cee715d741f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env python3
"""
技术指标验证模块
验证技术指标的预测能力和有效性
"""
 
import pandas as pd
import numpy as np
from typing import Dict, List, Optional, Tuple
from scipy.stats import spearmanr
from dataclasses import dataclass
import json
import os
from datetime import datetime
import warnings
warnings.filterwarnings('ignore')
 
 
@dataclass
class IndicatorValidationResult:
    """指标验证结果"""
    indicator_name: str
    ic: float
    ir: float
    ic_pvalue: float
    is_valid: bool
    decay_rate: float
    half_life: float
    recommendation: str
    validation_date: str
 
 
class IndicatorValidator:
    """技术指标验证器"""
    
    def __init__(
        self,
        ic_threshold: float = 0.02,
        ir_threshold: float = 0.3,
        pvalue_threshold: float = 0.05,
        rolling_window: int = 20
    ):
        """
        初始化指标验证器
        
        Args:
            ic_threshold: IC有效阈值
            ir_threshold: IR有效阈值
            pvalue_threshold: p值阈值
            rolling_window: 滚动计算窗口
        """
        self.ic_threshold = ic_threshold
        self.ir_threshold = ir_threshold
        self.pvalue_threshold = pvalue_threshold
        self.rolling_window = rolling_window
        self.validation_results: Dict[str, IndicatorValidationResult] = {}
    
    def validate_indicator(
        self,
        indicator_series: pd.Series,
        future_returns: pd.Series,
        indicator_name: str = "indicator"
    ) -> IndicatorValidationResult:
        """
        验证单个指标的预测能力
        
        Args:
            indicator_series: 指标值序列
            future_returns: 未来收益率序列
            indicator_name: 指标名称
            
        Returns:
            验证结果
        """
        indicator_series = indicator_series.dropna()
        future_returns = future_returns.dropna()
        
        common_index = indicator_series.index.intersection(future_returns.index)
        
        if len(common_index) < 30:
            return IndicatorValidationResult(
                indicator_name=indicator_name,
                ic=0.0,
                ir=0.0,
                ic_pvalue=1.0,
                is_valid=False,
                decay_rate=0.0,
                half_life=np.inf,
                recommendation="数据不足,无法验证",
                validation_date=datetime.now().strftime('%Y-%m-%d')
            )
        
        indicator_values = indicator_series.loc[common_index]
        return_values = future_returns.loc[common_index]
        
        ic, pvalue = spearmanr(indicator_values, return_values)
        
        if np.isnan(ic):
            ic = 0.0
        if np.isnan(pvalue):
            pvalue = 1.0
        
        ic_std = self._calculate_rolling_ic_std(indicator_values, return_values)
        ir = ic / ic_std if ic_std > 0 else 0.0
        
        decay_info = self._calculate_ic_decay(indicator_values, return_values)
        
        is_valid = (
            abs(ic) >= self.ic_threshold and
            abs(ir) >= self.ir_threshold and
            pvalue <= self.pvalue_threshold
        )
        
        if is_valid:
            if ic > 0:
                recommendation = "有效正向指标,建议保留"
            else:
                recommendation = "有效负向指标,建议反向使用"
        elif abs(ic) >= self.ic_threshold * 0.5:
            recommendation = "弱有效指标,建议优化参数"
        else:
            recommendation = "无效指标,建议弃用"
        
        result = IndicatorValidationResult(
            indicator_name=indicator_name,
            ic=round(ic, 4),
            ir=round(ir, 4),
            ic_pvalue=round(pvalue, 4),
            is_valid=is_valid,
            decay_rate=decay_info['decay_rate'],
            half_life=decay_info['half_life'],
            recommendation=recommendation,
            validation_date=datetime.now().strftime('%Y-%m-%d')
        )
        
        self.validation_results[indicator_name] = result
        
        return result
    
    def _calculate_rolling_ic_std(
        self,
        indicator_series: pd.Series,
        return_series: pd.Series
    ) -> float:
        """计算滚动IC标准差"""
        if len(indicator_series) < self.rolling_window:
            return 0.01
        
        ic_values = []
        for i in range(self.rolling_window, len(indicator_series)):
            window_indicator = indicator_series.iloc[i-self.rolling_window:i]
            window_return = return_series.iloc[i-self.rolling_window:i]
            
            if len(window_indicator) > 10:
                ic, _ = spearmanr(window_indicator, window_return)
                if not np.isnan(ic):
                    ic_values.append(ic)
        
        return np.std(ic_values) if ic_values else 0.01
    
    def _calculate_ic_decay(
        self,
        indicator_series: pd.Series,
        return_series: pd.Series
    ) -> Dict[str, float]:
        """计算IC衰减"""
        n = len(indicator_series)
        if n < 20:
            return {'decay_rate': 0.0, 'half_life': np.inf}
        
        first_half_ic, _ = spearmanr(
            indicator_series.iloc[:n//2],
            return_series.iloc[:n//2]
        )
        second_half_ic, _ = spearmanr(
            indicator_series.iloc[n//2:],
            return_series.iloc[n//2:]
        )
        
        if np.isnan(first_half_ic) or np.isnan(second_half_ic):
            return {'decay_rate': 0.0, 'half_life': np.inf}
        
        decay_rate = (abs(first_half_ic) - abs(second_half_ic)) / abs(first_half_ic) if first_half_ic != 0 else 0
        
        half_life = np.log(2) / abs(decay_rate) if decay_rate != 0 else np.inf
        
        return {
            'decay_rate': round(decay_rate, 4),
            'half_life': round(half_life, 2) if half_life != np.inf else np.inf
        }
    
    def validate_all_indicators(
        self,
        indicator_df: pd.DataFrame,
        return_series: pd.Series
    ) -> pd.DataFrame:
        """
        验证所有指标
        
        Args:
            indicator_df: 指标数据DataFrame,每列为一个指标
            return_series: 未来收益率序列
            
        Returns:
            验证结果DataFrame
        """
        results = []
        
        for column in indicator_df.columns:
            if column in ['date', 'stock_code', 'return_1d']:
                continue
            
            result = self.validate_indicator(
                indicator_df[column],
                return_series,
                column
            )
            
            results.append({
                'indicator': result.indicator_name,
                'IC': result.ic,
                'IR': result.ir,
                'p-value': result.ic_pvalue,
                'is_valid': result.is_valid,
                'decay_rate': result.decay_rate,
                'half_life': result.half_life,
                'recommendation': result.recommendation
            })
        
        return pd.DataFrame(results).sort_values('IC', key=abs, ascending=False)
    
    def get_valid_indicators(self) -> List[str]:
        """获取有效指标列表"""
        return [
            name for name, result in self.validation_results.items()
            if result.is_valid
        ]
    
    def get_indicator_report(self) -> str:
        """生成指标验证报告"""
        if not self.validation_results:
            return "暂无验证结果"
        
        report_lines = [
            "=" * 60,
            "技术指标验证报告".center(60),
            "=" * 60,
            f"\n验证时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
            f"验证标准: IC绝对值 >= {self.ic_threshold}, IR绝对值 >= {self.ir_threshold}",
            f"\n共验证 {len(self.validation_results)} 个指标",
            f"有效指标: {len(self.get_valid_indicators())} 个",
            "\n" + "-" * 60,
            "详细结果:",
            "-" * 60
        ]
        
        for name, result in sorted(
            self.validation_results.items(),
            key=lambda x: abs(x[1].ic),
            reverse=True
        ):
            status = "✓ 有效" if result.is_valid else "✗ 无效"
            report_lines.append(
                f"\n{result.indicator_name}: {status}"
            )
            report_lines.append(f"  IC: {result.ic:.4f}, IR: {result.ir:.4f}, p-value: {result.ic_pvalue:.4f}")
            report_lines.append(f"  衰减率: {result.decay_rate:.2%}, 半衰期: {result.half_life}")
            report_lines.append(f"  建议: {result.recommendation}")
        
        return "\n".join(report_lines)
    
    def save_results(self, output_path: str = "data/indicator_validation_results.json"):
        """保存验证结果"""
        os.makedirs(os.path.dirname(output_path), exist_ok=True)
        
        results_dict = {
            name: {
                'ic': result.ic,
                'ir': result.ir,
                'ic_pvalue': result.ic_pvalue,
                'is_valid': result.is_valid,
                'decay_rate': result.decay_rate,
                'half_life': result.half_life if result.half_life != np.inf else None,
                'recommendation': result.recommendation,
                'validation_date': result.validation_date
            }
            for name, result in self.validation_results.items()
        }
        
        with open(output_path, 'w', encoding='utf-8') as f:
            json.dump(results_dict, f, ensure_ascii=False, indent=2)
        
        print(f"验证结果已保存到: {output_path}")
 
 
class TechnicalIndicatorValidator:
    """技术指标验证器 - 统一接口"""
    
    def __init__(self):
        """初始化技术指标验证器"""
        self.validator = IndicatorValidator()
        self._validation_summary = None
    
    def validate(
        self,
        data: pd.DataFrame,
        indicator_columns: List[str] = None,
        return_column: str = 'return_1d'
    ) -> Dict:
        """
        验证技术指标
        
        Args:
            data: 包含指标和收益率的数据
            indicator_columns: 指标列名列表,如果为None则自动检测
            return_column: 收益率列名
            
        Returns:
            验证结果字典
        """
        if return_column not in data.columns:
            return {
                'success': False,
                'message': f'收益率列 {return_column} 不存在'
            }
        
        if indicator_columns is None:
            exclude_columns = {'date', 'stock_code', 'return_1d', 'close', 'open', 'high', 'low', 'volume', 'amount'}
            indicator_columns = [col for col in data.columns if col not in exclude_columns]
        
        if not indicator_columns:
            return {
                'success': False,
                'message': '未找到指标列'
            }
        
        results_df = self.validator.validate_all_indicators(
            data[indicator_columns],
            data[return_column]
        )
        
        self._validation_summary = {
            'total_indicators': len(indicator_columns),
            'valid_indicators': len(self.validator.get_valid_indicators()),
            'results': results_df.to_dict('records'),
            'validation_date': datetime.now().strftime('%Y-%m-%d')
        }
        
        return {
            'success': True,
            'summary': self._validation_summary,
            'valid_indicators': self.validator.get_valid_indicators()
        }
    
    def get_report(self) -> str:
        """获取验证报告"""
        return self.validator.get_indicator_report()
    
    def save_results(self, output_path: str = "data/indicator_validation_results.json"):
        """保存验证结果"""
        self.validator.save_results(output_path)
 
 
if __name__ == "__main__":
    np.random.seed(42)
    n_days = 252
    dates = pd.date_range(start='2023-01-01', periods=n_days, freq='B')
    
    data = pd.DataFrame({
        'date': dates,
        'rsi_14': np.random.uniform(30, 70, n_days),
        'macd': np.random.normal(0, 1, n_days),
        'momentum_20': np.random.normal(0, 0.02, n_days),
        'volatility_20': np.random.uniform(0.01, 0.05, n_days),
        'return_1d': np.random.normal(0.001, 0.02, n_days)
    })
    
    validator = TechnicalIndicatorValidator()
    result = validator.validate(data)
    
    print(validator.get_report())
    validator.save_results()