-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_alphas.py
More file actions
249 lines (196 loc) · 8.08 KB
/
Copy pathselect_alphas.py
File metadata and controls
249 lines (196 loc) · 8.08 KB
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
#!/usr/bin/env python3
"""
Alpha Performance Assessment Script
===================================
Scans all 191 alphas and computes comprehensive performance metrics.
Optimized for speed: pre-loads data, uses multiprocessing for alpha
computation, and a lightweight metrics path.
Output:
alpha_performance.csv: A wide CSV containing detailed metrics for each alpha.
"""
import pandas as pd
import numpy as np
import importlib
import sys
import os
from pathlib import Path
from tqdm import tqdm
import warnings
from concurrent.futures import ThreadPoolExecutor
# Add parent directory to path to allow imports
sys.path.append(str(Path(__file__).parent))
from alpha191.utils import (
load_benchmark_csv,
get_benchmark_members,
load_stock_csv
)
from arxiv_2306.assessment import get_clean_factor_and_forward_returns, compute_performance_metrics_light
# Configuration
BENCHMARK = 'zz800'
HORIZONS = [5, 10, 20, 60]
OUTPUT_FILE = 'alpha_performances.csv'
warnings.filterwarnings("ignore")
def get_alpha_function(alpha_name):
"""Dynamically import alpha function."""
try:
module = importlib.import_module(f"alpha191.{alpha_name}")
func_name1 = alpha_name[:5] + "_" + alpha_name[5:] # alpha_001
func_name2 = alpha_name # alpha001
if hasattr(module, func_name1):
return getattr(module, func_name1)
elif hasattr(module, func_name2):
return getattr(module, func_name2)
except (ImportError, ModuleNotFoundError):
pass
return None
def preload_data(benchmark):
"""Load all stock data into memory using float32."""
benchmark_df = load_benchmark_csv(benchmark)
codes = get_benchmark_members(benchmark)
stock_cache = {}
def load_one(code):
try:
df = load_stock_csv(code, benchmark)
df['benchmark_close'] = benchmark_df['close'].reindex(df.index)
df['benchmark_open'] = benchmark_df['open'].reindex(df.index)
float_cols = df.select_dtypes(include=['float64']).columns
df[float_cols] = df[float_cols].astype(np.float32)
if 'volume' in df.columns:
df['volume'] = df['volume'].astype(np.float32)
return code, df
except Exception:
return code, None
with ThreadPoolExecutor(max_workers=8) as executor:
results = list(executor.map(load_one, codes))
for code, df in results:
if df is not None:
stock_cache[code] = df
return stock_cache, benchmark_df.index
def compute_alpha_for_all_stocks(alpha_func, stock_cache):
"""Compute alpha values using cached data."""
results = {}
prices = {}
for code, df in stock_cache.items():
try:
val = alpha_func(df)
if val is not None:
results[code] = val.replace([np.inf, -np.inf], np.nan).astype(np.float32)
prices[code] = df['close']
except Exception:
pass
return results, prices
def process_one_alpha(alpha_name, stock_cache, timeline):
"""Compute metrics for one alpha. Returns dict or None."""
alpha_func = get_alpha_function(alpha_name)
if not alpha_func:
return None
try:
factor_results, price_results = compute_alpha_for_all_stocks(alpha_func, stock_cache)
if not factor_results:
return None
factor_matrix = pd.DataFrame(factor_results).reindex(timeline)
price_matrix = pd.DataFrame(price_results).reindex(timeline)
factor_data = get_clean_factor_and_forward_returns(
factor_matrix,
price_matrix,
periods=HORIZONS,
max_loss=0.40
)
metrics = compute_performance_metrics_light(factor_data)
return extract_detailed_metrics(alpha_name, metrics)
except Exception as e:
print(f" [WARN] {alpha_name} failed: {e}")
return None
def append_results(filepath, results):
"""Append new results to CSV."""
df = pd.DataFrame(results)
if not os.path.exists(filepath):
df.to_csv(filepath, index=False)
else:
df.to_csv(filepath, mode='a', header=False, index=False)
def extract_detailed_metrics(alpha_name, metrics):
"""Flatten complex metrics dictionary into a single row."""
row = {'Alpha': alpha_name}
# 1. IC Summary Metrics (for each horizon)
ic_summary = metrics.get('ic_summary')
if ic_summary is not None:
for horizon in ic_summary.columns:
h_str = str(horizon)
row[f'IC_Mean_{h_str}'] = ic_summary.loc['IC Mean', horizon]
row[f'IC_Std_{h_str}'] = ic_summary.loc['IC Std.', horizon]
row[f'IC_IR_{h_str}'] = ic_summary.loc['Risk-Adjusted IC (IR)', horizon]
row[f'IC_Winrate_{h_str}'] = ic_summary.loc['IC Winrate', horizon]
# if 'IC Skew' in ic_summary.index:
# row[f'IC_Skew_{h_str}'] = ic_summary.loc['IC Skew', horizon]
# if 'IC Max Drawdown' in ic_summary.index:
# row[f'IC_MaxDD_{h_str}'] = ic_summary.loc['IC Max Drawdown', horizon]
# if 't-stat(IC)' in ic_summary.index:
# row[f'IC_tStat_{h_str}'] = ic_summary.loc['t-stat(IC)', horizon]
# # 2. Long-Short Portfolio Metrics (for each horizon)
# q_stats = metrics.get('q_stats')
# if q_stats:
# for horizon, stats_df in q_stats.items():
# if 'Long-Short' in stats_df.index:
# ls = stats_df.loc['Long-Short']
# row[f'LS_Sharpe_{horizon}'] = ls.get('sharpe', np.nan)
# row[f'LS_AnnRet_{horizon}'] = ls.get('ann_ret', np.nan)
# row[f'LS_MaxDD_{horizon}'] = ls.get('max_dd', np.nan)
# row[f'LS_Calmar_{horizon}'] = ls.get('calmar', np.nan)
# row[f'LS_MeanRet_{horizon}'] = ls.get('Mean', np.nan)
# # 3. Monotonicity (for each horizon)
# mono = metrics.get('mono_score')
# if mono is not None:
# for horizon, score in mono.items():
# row[f'Monotonicity_{horizon}'] = score
# # 4. Global Metrics
# row['Turnover'] = metrics.get('quantile_turnover', pd.Series()).mean()
# row['RRE'] = metrics.get('rre', np.nan)
# # 5. Quantile Spread (Q10 - Q1)
# mean_ret = metrics.get('mean_ret')
# if mean_ret is not None:
# max_q = mean_ret.index.max()
# min_q = mean_ret.index.min()
# for horizon in mean_ret.columns:
# spread = mean_ret.loc[max_q, horizon] - mean_ret.loc[min_q, horizon]
# row[f'Q_Spread_{horizon}'] = spread
return row
def main():
print(f"Alpha Performance Assessment — {BENCHMARK}")
# Load All Data
stock_cache, timeline = preload_data(BENCHMARK)
print(f"Loaded {len(stock_cache)} stocks, timeline={len(timeline)} days")
existing_alphas = set()
if os.path.exists(OUTPUT_FILE):
try:
existing_df = pd.read_csv(OUTPUT_FILE)
if not existing_df.empty:
existing_alphas = set(existing_df['Alpha'].astype(str).unique())
print(f"Resuming: {len(existing_alphas)} already done.")
except Exception:
pass
alpha_range = [f"alpha{i:03d}" for i in range(1, 192)]
alphas_to_process = [a for a in alpha_range if a not in existing_alphas]
if not alphas_to_process:
print("All alphas already processed.")
return
print(f"Processing {len(alphas_to_process)} alphas...")
batch_results = []
success_count = 0
fail_count = 0
for alpha_name in tqdm(alphas_to_process, desc="Alphas", ncols=80):
result = process_one_alpha(alpha_name, stock_cache, timeline)
if result is not None:
batch_results.append(result)
success_count += 1
else:
fail_count += 1
# Flush to disk every 10 successful alphas
if len(batch_results) >= 10:
append_results(OUTPUT_FILE, batch_results)
batch_results = []
# Final flush
if batch_results:
append_results(OUTPUT_FILE, batch_results)
print(f"Done. {success_count} succeeded, {fail_count} failed/skipped. Results → {OUTPUT_FILE}")
if __name__ == "__main__":
main()