log
2026-07-10 80582255d56c42a3acf7f84281798726313b1edb
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python3
"""
高级因子系统 v3.0 - 机器学习增强
解决传统因子IR过低的问题
 
核心改进:
1. 横截面相对强度因子(行业相对、市场相对)
2. 行业轮动因子
3. 机器学习因子组合(XGBoost/LightGBM)
4. 因子衰减分析
"""
 
import pickle
import pandas as pd
import numpy as np
from scipy.stats import spearmanr
from sklearn.ensemble import HistGradientBoostingRegressor, RandomForestRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import TimeSeriesSplit
from sklearn.linear_model import Ridge
import warnings
warnings.filterwarnings('ignore')
 
 
class AdvancedFactorSystem:
    """高级因子系统"""
    
    def __init__(self):
        self.data_file = 'data/akshare_real_data_fixed.pkl'
        self.output_file = 'data/advanced_factors.pkl'
        
    def load_data(self) -> pd.DataFrame:
        """加载数据"""
        with open(self.data_file, 'rb') as f:
            df = pickle.load(f)
        print(f"✓ 加载数据: {len(df)} 条, {df['stock_code'].nunique()} 只股票")
        return df
    
    def calculate_cross_sectional_factors(self, df: pd.DataFrame) -> pd.DataFrame:
        """计算横截面因子"""
        print("\n=== 计算横截面因子 ===")
        
        df = df.sort_values(['stock_code', 'date']).copy().reset_index(drop=True)
        
        # 分配行业
        df['industry'] = df['stock_code'].apply(self._get_industry)
        
        all_data = []
        for stock_code, group in df.groupby('stock_code'):
            group = group.sort_values('date').copy()
            returns = group['close'].pct_change()
            
            # ===== 1. 基础因子 =====
            group['momentum_20'] = group['close'].pct_change(20)
            group['momentum_60'] = group['close'].pct_change(60)
            
            # 波动率
            group['volatility_20'] = returns.rolling(20).std()
            group['volatility_60'] = returns.rolling(60).std()
            
            # 下行波动率
            down_ret = returns.copy()
            down_ret[down_ret > 0] = 0
            group['downside_vol'] = down_ret.rolling(60).std()
            
            # 反转
            group['reversal_5'] = -group['close'].pct_change(5)
            group['reversal_10'] = -group['close'].pct_change(10)
            
            # 价格位置
            high_60 = group['high'].rolling(60).max()
            low_60 = group['low'].rolling(60).min()
            group['price_position'] = (group['close'] - low_60) / (high_60 - low_60 + 1e-10)
            
            # 换手率
            group['turnover_ma'] = group['turnover'].rolling(20).mean()
            
            # Amihud非流动性
            group['amihud'] = (abs(returns) / (group['amount'] + 1e-10)).rolling(20).mean()
            
            all_data.append(group)
        
        df = pd.concat(all_data, ignore_index=True)
        
        # ===== 2. 横截面相对因子 =====
        print("  计算横截面相对因子...")
        
        # 市场相对动量
        df['market_ret'] = df.groupby('date')['change_pct'].transform('mean')
        df['rel_momentum'] = df['momentum_20'] - df['market_ret']
        
        # 行业相对动量
        df['industry_ret'] = df.groupby(['date', 'industry'])['change_pct'].transform('mean')
        df['rel_industry_momentum'] = df['momentum_20'] - df['industry_ret']
        
        # 行业内排名
        df['industry_rank'] = df.groupby(['date', 'industry'])['momentum_20'].rank(pct=True)
        
        # 市场排名
        df['market_rank'] = df.groupby('date')['momentum_20'].rank(pct=True)
        
        # ===== 3. 行业轮动因子 =====
        print("  计算行业轮动因子...")
        
        # 行业动量
        industry_momentum = df.groupby(['date', 'industry'])['momentum_20'].mean().reset_index()
        industry_momentum.columns = ['date', 'industry', 'industry_mom']
        df = df.merge(industry_momentum, on=['date', 'industry'], how='left')
        
        # 行业强度(行业相对市场的超额收益)
        df['industry_strength'] = df['industry_mom'] - df['market_ret']
        
        # ===== 4. 量价因子 =====
        print("  计算量价因子...")
        
        # 成交量变化
        df['vol_change'] = df.groupby('stock_code')['volume'].pct_change()
        df['vol_ma_ratio'] = df['volume'] / (df.groupby('stock_code')['volume'].rolling(20).mean().reset_index(0, drop=True) + 1e-10)
        
        # 量价趋势
        df['vol_price_trend'] = df.groupby('stock_code').apply(
            lambda g: self._calc_vol_price_trend(g)
        ).reset_index(0, drop=True)
        
        print(f"✓ 横截面因子计算完成")
        return df
    
    def _calc_vol_price_trend(self, group: pd.DataFrame) -> pd.Series:
        """计算量价趋势"""
        vol = group['volume'].values
        price = group['close'].values
        
        result = np.zeros(len(group))
        for i in range(20, len(group)):
            if i >= 20:
                vol_window = vol[i-20:i]
                price_window = price[i-20:i]
                if len(vol_window) == 20 and len(price_window) == 20:
                    corr = np.corrcoef(vol_window, price_window)[0, 1]
                    result[i] = corr if not np.isnan(corr) else 0
        
        return pd.Series(result, index=group.index)
    
    def train_ml_factor_combination(self, df: pd.DataFrame) -> tuple:
        """训练机器学习因子组合"""
        print("\n=== 训练机器学习因子组合 ===")
        
        # 准备目标变量
        df = df.sort_values(['stock_code', 'date']).reset_index(drop=True)
        df['return_1d'] = df.groupby('stock_code')['close'].pct_change().shift(-1)
        df['return_5d'] = df.groupby('stock_code')['close'].pct_change(5).shift(-5)
        
        # 特征列
        feature_cols = [
            'momentum_20', 'momentum_60', 'volatility_20', 'volatility_60',
            'downside_vol', 'reversal_5', 'reversal_10', 'price_position',
            'turnover_ma', 'amihud', 'rel_momentum', 'rel_industry_momentum',
            'industry_rank', 'market_rank', 'industry_strength',
            'vol_change', 'vol_ma_ratio', 'vol_price_trend'
        ]
        
        # 过滤有效特征
        feature_cols = [c for c in feature_cols if c in df.columns]
        
        # 训练数据
        df_train = df[feature_cols + ['return_1d', 'date', 'stock_code']].dropna()
        
        if len(df_train) < 1000:
            print("⚠️ 训练数据不足")
            return None, df
        
        # 按时间分割
        dates = sorted(df_train['date'].unique())
        train_dates = dates[:int(len(dates) * 0.7)]
        test_dates = dates[int(len(dates) * 0.7):]
        
        train_df = df_train[df_train['date'].isin(train_dates)]
        test_df = df_train[df_train['date'].isin(test_dates)]
        
        X_train = train_df[feature_cols].values
        y_train = train_df['return_1d'].values
        X_test = test_df[feature_cols].values
        y_test = test_df['return_1d'].values
        
        # 标准化
        scaler = StandardScaler()
        X_train_scaled = scaler.fit_transform(X_train)
        X_test_scaled = scaler.transform(X_test)
        
        # 训练模型
        print(f"  训练数据: {len(X_train)} 条")
        print(f"  测试数据: {len(X_test)} 条")
        
        # 使用HistGradientBoosting(支持NaN)
        model = HistGradientBoostingRegressor(
            max_iter=100,
            max_depth=3,
            learning_rate=0.1,
            random_state=42
        )
        
        model.fit(X_train_scaled, y_train)
        
        # 预测
        y_pred = model.predict(X_test_scaled)
        
        # 计算IC
        ic_values = []
        for date in test_dates:
            mask = test_df['date'] == date
            if mask.sum() >= 30:
                ic, _ = spearmanr(y_test[mask], y_pred[mask])
                if not np.isnan(ic):
                    ic_values.append(ic)
        
        if len(ic_values) > 0:
            ic_mean = np.mean(ic_values)
            ic_std = np.std(ic_values)
            ir = ic_mean / ic_std if ic_std > 0 else 0
            
            print(f"\n  ML模型测试集表现:")
            print(f"    IC均值: {ic_mean:.4f}")
            print(f"    IC标准差: {ic_std:.4f}")
            print(f"    IR: {ir:.2f}")
        
        # 特征重要性(HistGradientBoosting不支持直接获取,跳过)
        print(f"\n  特征重要性: HistGradientBoosting不支持直接获取")
        
        # 对全量数据预测
        X_all = df[feature_cols].values
        # HistGradientBoosting支持NaN,直接预测
        df['ml_score'] = model.predict(X_all)
        
        return model, df
    
    def evaluate_all_factors(self, df: pd.DataFrame) -> dict:
        """评估所有因子"""
        print("\n=== 评估所有因子 ===")
        
        df = df.sort_values(['stock_code', 'date']).reset_index(drop=True)
        df['return_1d'] = df.groupby('stock_code')['close'].pct_change().shift(-1)
        
        results = {}
        
        # 所有因子
        factor_cols = [
            'momentum_20', 'momentum_60', 'volatility_20', 'volatility_60',
            'downside_vol', 'reversal_5', 'reversal_10', 'price_position',
            'turnover_ma', 'amihud', 'rel_momentum', 'rel_industry_momentum',
            'industry_rank', 'market_rank', 'industry_strength',
            'vol_change', 'vol_ma_ratio', 'vol_price_trend', 'ml_score'
        ]
        
        factor_cols = [c for c in factor_cols if c in df.columns]
        
        for factor in factor_cols:
            ic_values = []
            for date, group in df.groupby('date'):
                valid = group[[factor, 'return_1d']].dropna()
                if len(valid) >= 30:
                    ic, _ = spearmanr(valid[factor], valid['return_1d'])
                    if not np.isnan(ic):
                        ic_values.append(ic)
            
            if len(ic_values) >= 5:
                ic_mean = np.mean(ic_values)
                ic_std = np.std(ic_values)
                ir = ic_mean / ic_std if ic_std > 0 else 0
                
                # IC正向率
                ic_positive_rate = sum(1 for x in ic_values if x > 0) / len(ic_values)
                
                results[factor] = {
                    'ic_mean': ic_mean,
                    'ic_std': ic_std,
                    'ir': ir,
                    'ic_positive_rate': ic_positive_rate,
                    'effective': bool(abs(ic_mean) >= 0.02 and abs(ir) >= 0.3)
                }
        
        return results, df
    
    def select_best_factors(self, results: dict) -> list:
        """选择最佳因子"""
        print("\n=== 选择最佳因子 ===")
        
        sorted_factors = sorted(results.items(), key=lambda x: abs(x[1]['ir']), reverse=True)
        
        best_factors = []
        for factor, metrics in sorted_factors:
            if abs(metrics['ir']) >= 0.3:
                best_factors.append(factor)
                print(f"  ✅ {factor}: IC={metrics['ic_mean']:.4f}, IR={metrics['ir']:.2f}, 正向率={metrics['ic_positive_rate']:.1%}")
        
        if not best_factors:
            best_factors = [f[0] for f in sorted_factors[:5]]
            print(f"  ⚠️ 无IR≥0.3的因子,选择IR最高的5个")
            for f in best_factors:
                r = results[f]
                print(f"    {f}: IR={r['ir']:.2f}")
        
        return best_factors
    
    def calculate_final_score(self, df: pd.DataFrame, best_factors: list, results: dict) -> pd.DataFrame:
        """计算最终得分"""
        print("\n=== 计算最终得分 ===")
        
        # 按IR加权
        weights = {}
        total_ir = sum(abs(results[f]['ir']) for f in best_factors if f in results)
        
        for factor in best_factors:
            if factor in results:
                weights[factor] = abs(results[factor]['ir']) / total_ir if total_ir > 0 else 1/len(best_factors)
        
        print(f"因子权重: {weights}")
        
        # 计算得分
        all_data = []
        for date, group in df.groupby('date'):
            group = group.copy()
            
            score = np.zeros(len(group))
            for factor in best_factors:
                if factor in group.columns:
                    factor_vals = group[factor].values
                    factor_mean = np.nanmean(factor_vals)
                    factor_std = np.nanstd(factor_vals)
                    if factor_std > 0:
                        factor_z = (factor_vals - factor_mean) / factor_std
                    else:
                        factor_z = np.zeros(len(group))
                    
                    score += np.nan_to_num(factor_z) * weights.get(factor, 0)
            
            # 归一化
            score_min = np.nanmin(score)
            score_max = np.nanmax(score)
            if score_max > score_min:
                group['final_score'] = (score - score_min) / (score_max - score_min)
            else:
                group['final_score'] = 0.5
            
            all_data.append(group)
        
        df = pd.concat(all_data, ignore_index=True)
        print(f"✓ 最终得分计算完成")
        
        return df
    
    def run(self) -> pd.DataFrame:
        """运行完整流程"""
        print("="*60)
        print("高级因子系统 v3.0 - 机器学习增强")
        print("="*60)
        
        df = self.load_data()
        df = self.calculate_cross_sectional_factors(df)
        model, df = self.train_ml_factor_combination(df)
        results, df = self.evaluate_all_factors(df)
        best_factors = self.select_best_factors(results)
        df = self.calculate_final_score(df, best_factors, results)
        
        self._print_summary(results, best_factors)
        
        with open(self.output_file, 'wb') as f:
            pickle.dump(df, f)
        print(f"\n✓ 数据已保存: {self.output_file}")
        
        with open(self.data_file, 'wb') as f:
            pickle.dump(df, f)
        print(f"✓ 已更新主数据文件: {self.data_file}")
        
        return df
    
    def _get_industry(self, code: str) -> str:
        """根据代码分配行业"""
        code = str(code).replace('sh', '').replace('sz', '').replace('bj', '')
        if code.startswith('6'):
            return '工业'
        elif code.startswith('00'):
            return '制造'
        elif code.startswith('30') or code.startswith('68'):
            return '科技'
        return '其他'
    
    def _print_summary(self, results: dict, best_factors: list):
        """打印汇总"""
        print("\n" + "="*60)
        print("因子评估汇总")
        print("="*60)
        
        effective_count = sum(1 for r in results.values() if r['effective'])
        
        print(f"\n有效因子: {effective_count}/{len(results)}")
        
        print(f"\n所有因子表现 (按IR排序):")
        sorted_results = sorted(results.items(), key=lambda x: abs(x[1]['ir']), reverse=True)
        for factor, r in sorted_results[:15]:
            status = '✅' if r['effective'] else '⚠️'
            print(f"  {status} {factor}: IC={r['ic_mean']:.4f}, IR={r['ir']:.2f}, 正向率={r['ic_positive_rate']:.1%}")
 
 
def main():
    system = AdvancedFactorSystem()
    df = system.run()
    
    print("\n" + "="*60)
    print("✅ 高级因子系统执行完成")
    print("="*60)
    
    return 0
 
 
if __name__ == "__main__":
    import sys
    sys.exit(main())