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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Automated Alpha Factor Mining with Genetic Programming + LLM-Driven Discovery
|
| 2 |
+
|
| 3 |
+
Based on:
|
| 4 |
+
- Lopez de Prado: Genetic programming for alpha factor discovery
|
| 5 |
+
- QuantaAlpha (Han et al. 2026): LLM + MCTS evolutionary framework
|
| 6 |
+
- gplearn: Symbolic regression for finance
|
| 7 |
+
|
| 8 |
+
This replaces hand-coded RSI/MACD with DISCOVERED factors.
|
| 9 |
+
"""
|
| 10 |
+
import numpy as np
|
| 11 |
+
import pandas as pd
|
| 12 |
+
from typing import Dict, List, Optional, Callable, Tuple
|
| 13 |
+
import warnings
|
| 14 |
+
warnings.filterwarnings('ignore')
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
from gplearn.genetic import SymbolicTransformer
|
| 18 |
+
from gplearn.functions import make_function
|
| 19 |
+
GPLEARN_AVAILABLE = True
|
| 20 |
+
except ImportError:
|
| 21 |
+
GPLEARN_AVAILABLE = False
|
| 22 |
+
print("WARNING: gplearn not available. Install with: pip install gplearn")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class FinancialFunctionLibrary:
|
| 26 |
+
"""
|
| 27 |
+
Financial operators for genetic programming alpha mining.
|
| 28 |
+
|
| 29 |
+
Key principle: Standard math operators (+, -, *, /) are not enough.
|
| 30 |
+
Financial alpha requires TIME-SERIES and CROSS-SECTIONAL operators.
|
| 31 |
+
|
| 32 |
+
Operators:
|
| 33 |
+
- ts_*: Time-series (operate within one asset over time)
|
| 34 |
+
- cs_*: Cross-sectional (operate across assets at one time)
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
@staticmethod
|
| 38 |
+
def ts_delta(x):
|
| 39 |
+
"""First difference"""
|
| 40 |
+
result = np.empty_like(x)
|
| 41 |
+
result[0] = 0
|
| 42 |
+
result[1:] = np.diff(x)
|
| 43 |
+
return result
|
| 44 |
+
|
| 45 |
+
@staticmethod
|
| 46 |
+
def ts_delay(x, d=1):
|
| 47 |
+
"""Lag operator"""
|
| 48 |
+
result = np.empty_like(x)
|
| 49 |
+
result[:d] = x[0]
|
| 50 |
+
result[d:] = x[:-d]
|
| 51 |
+
return result
|
| 52 |
+
|
| 53 |
+
@staticmethod
|
| 54 |
+
def ts_mean(x, d=5):
|
| 55 |
+
"""Rolling mean"""
|
| 56 |
+
result = np.empty_like(x)
|
| 57 |
+
for i in range(len(x)):
|
| 58 |
+
start = max(0, i - d + 1)
|
| 59 |
+
result[i] = np.mean(x[start:i+1])
|
| 60 |
+
return result
|
| 61 |
+
|
| 62 |
+
@staticmethod
|
| 63 |
+
def ts_std(x, d=5):
|
| 64 |
+
"""Rolling standard deviation"""
|
| 65 |
+
result = np.empty_like(x)
|
| 66 |
+
for i in range(len(x)):
|
| 67 |
+
start = max(0, i - d + 1)
|
| 68 |
+
result[i] = np.std(x[start:i+1]) + 1e-10
|
| 69 |
+
return result
|
| 70 |
+
|
| 71 |
+
@staticmethod
|
| 72 |
+
def ts_rank(x, d=5):
|
| 73 |
+
"""Rolling rank (percentile within window)"""
|
| 74 |
+
result = np.empty_like(x)
|
| 75 |
+
for i in range(len(x)):
|
| 76 |
+
start = max(0, i - d + 1)
|
| 77 |
+
window = x[start:i+1]
|
| 78 |
+
if len(window) > 0 and np.std(window) > 0:
|
| 79 |
+
result[i] = np.sum(window < x[i]) / len(window)
|
| 80 |
+
else:
|
| 81 |
+
result[i] = 0.5
|
| 82 |
+
return result
|
| 83 |
+
|
| 84 |
+
@staticmethod
|
| 85 |
+
def ts_corr(x, y, d=5):
|
| 86 |
+
"""Rolling correlation"""
|
| 87 |
+
result = np.empty_like(x)
|
| 88 |
+
for i in range(len(x)):
|
| 89 |
+
start = max(0, i - d + 1)
|
| 90 |
+
wx, wy = x[start:i+1], y[start:i+1]
|
| 91 |
+
if len(wx) > 1 and np.std(wx) > 0 and np.std(wy) > 0:
|
| 92 |
+
result[i] = np.corrcoef(wx, wy)[0, 1]
|
| 93 |
+
else:
|
| 94 |
+
result[i] = 0
|
| 95 |
+
return result
|
| 96 |
+
|
| 97 |
+
@staticmethod
|
| 98 |
+
def ts_cov(x, y, d=5):
|
| 99 |
+
"""Rolling covariance"""
|
| 100 |
+
result = np.empty_like(x)
|
| 101 |
+
for i in range(len(x)):
|
| 102 |
+
start = max(0, i - d + 1)
|
| 103 |
+
wx, wy = x[start:i+1], y[start:i+1]
|
| 104 |
+
if len(wx) > 1:
|
| 105 |
+
result[i] = np.cov(wx, wy)[0, 1]
|
| 106 |
+
else:
|
| 107 |
+
result[i] = 0
|
| 108 |
+
return result
|
| 109 |
+
|
| 110 |
+
@staticmethod
|
| 111 |
+
def ts_max(x, d=5):
|
| 112 |
+
"""Rolling max"""
|
| 113 |
+
result = np.empty_like(x)
|
| 114 |
+
for i in range(len(x)):
|
| 115 |
+
start = max(0, i - d + 1)
|
| 116 |
+
result[i] = np.max(x[start:i+1])
|
| 117 |
+
return result
|
| 118 |
+
|
| 119 |
+
@staticmethod
|
| 120 |
+
def ts_min(x, d=5):
|
| 121 |
+
"""Rolling min"""
|
| 122 |
+
result = np.empty_like(x)
|
| 123 |
+
for i in range(len(x)):
|
| 124 |
+
start = max(0, i - d + 1)
|
| 125 |
+
result[i] = np.min(x[start:i+1])
|
| 126 |
+
return result
|
| 127 |
+
|
| 128 |
+
@staticmethod
|
| 129 |
+
def ts_sum(x, d=5):
|
| 130 |
+
"""Rolling sum"""
|
| 131 |
+
result = np.empty_like(x)
|
| 132 |
+
for i in range(len(x)):
|
| 133 |
+
start = max(0, i - d + 1)
|
| 134 |
+
result[i] = np.sum(x[start:i+1])
|
| 135 |
+
return result
|
| 136 |
+
|
| 137 |
+
@staticmethod
|
| 138 |
+
def ts_product(x, d=5):
|
| 139 |
+
"""Rolling product"""
|
| 140 |
+
result = np.empty_like(x)
|
| 141 |
+
for i in range(len(x)):
|
| 142 |
+
start = max(0, i - d + 1)
|
| 143 |
+
result[i] = np.prod(x[start:i+1] + 1) - 1
|
| 144 |
+
return result
|
| 145 |
+
|
| 146 |
+
@staticmethod
|
| 147 |
+
def ts_decay_linear(x, d=5):
|
| 148 |
+
"""Linearly weighted moving average (recent gets more weight)"""
|
| 149 |
+
result = np.empty_like(x)
|
| 150 |
+
weights = np.arange(1, d + 1)
|
| 151 |
+
for i in range(len(x)):
|
| 152 |
+
start = max(0, i - d + 1)
|
| 153 |
+
window = x[start:i+1]
|
| 154 |
+
w = weights[-len(window):]
|
| 155 |
+
result[i] = np.average(window, weights=w)
|
| 156 |
+
return result
|
| 157 |
+
|
| 158 |
+
@staticmethod
|
| 159 |
+
def sign(x):
|
| 160 |
+
"""Sign function"""
|
| 161 |
+
return np.sign(x)
|
| 162 |
+
|
| 163 |
+
@staticmethod
|
| 164 |
+
def signed_power(x, p=2):
|
| 165 |
+
"""Signed power: sign(x) * |x|^p"""
|
| 166 |
+
return np.sign(x) * np.power(np.abs(x), p)
|
| 167 |
+
|
| 168 |
+
@classmethod
|
| 169 |
+
def get_function_set(cls):
|
| 170 |
+
"""Get gplearn-compatible function set"""
|
| 171 |
+
if not GPLEARN_AVAILABLE:
|
| 172 |
+
return ['add', 'sub', 'mul', 'div', 'sqrt', 'log', 'abs', 'neg', 'inv']
|
| 173 |
+
|
| 174 |
+
functions = [
|
| 175 |
+
make_function(function=cls.ts_delta, name='ts_delta', arity=1),
|
| 176 |
+
make_function(function=cls.ts_mean, name='ts_mean5', arity=1),
|
| 177 |
+
make_function(function=cls.ts_std, name='ts_std5', arity=1),
|
| 178 |
+
make_function(function=cls.ts_rank, name='ts_rank5', arity=1),
|
| 179 |
+
make_function(function=cls.ts_max, name='ts_max5', arity=1),
|
| 180 |
+
make_function(function=cls.ts_min, name='ts_min5', arity=1),
|
| 181 |
+
make_function(function=cls.ts_sum, name='ts_sum5', arity=1),
|
| 182 |
+
make_function(function=cls.ts_decay_linear, name='ts_decay5', arity=1),
|
| 183 |
+
make_function(function=cls.sign, name='sign', arity=1),
|
| 184 |
+
make_function(function=cls.signed_power, name='signed_power', arity=1),
|
| 185 |
+
]
|
| 186 |
+
|
| 187 |
+
# Standard operators
|
| 188 |
+
std_ops = ['add', 'sub', 'mul', 'div', 'sqrt', 'log', 'abs', 'neg', 'inv']
|
| 189 |
+
|
| 190 |
+
return std_ops + functions
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
class AlphaMiner:
|
| 194 |
+
"""
|
| 195 |
+
Genetic Programming Alpha Factor Mining Engine.
|
| 196 |
+
|
| 197 |
+
Instead of hand-coding "RSI > 70 means sell," this EVOLVES factors
|
| 198 |
+
from raw data. The discovered formulas are:
|
| 199 |
+
1. Non-linear (can capture complex patterns)
|
| 200 |
+
2. Interpretable (symbolic formulas, not black boxes)
|
| 201 |
+
3. Novel (not in any textbook)
|
| 202 |
+
|
| 203 |
+
Pipeline:
|
| 204 |
+
1. Feed raw features (OHLCV-derived)
|
| 205 |
+
2. GP evolves formulas that predict returns
|
| 206 |
+
3. Select top formulas by IC (Information Coefficient)
|
| 207 |
+
4. Use as additional features for downstream ML models
|
| 208 |
+
|
| 209 |
+
Based on WorldQuant's 101 Formulaic Alphas and QuantaAlpha.
|
| 210 |
+
"""
|
| 211 |
+
|
| 212 |
+
def __init__(self,
|
| 213 |
+
n_factors: int = 50,
|
| 214 |
+
population_size: int = 1000,
|
| 215 |
+
generations: int = 20,
|
| 216 |
+
hall_of_fame: int = 100,
|
| 217 |
+
parsimony_coefficient: float = 0.01,
|
| 218 |
+
random_state: int = 42):
|
| 219 |
+
self.n_factors = n_factors
|
| 220 |
+
self.population_size = population_size
|
| 221 |
+
self.generations = generations
|
| 222 |
+
self.hall_of_fame = hall_of_fame
|
| 223 |
+
self.parsimony_coefficient = parsimony_coefficient
|
| 224 |
+
self.random_state = random_state
|
| 225 |
+
self.gp = None
|
| 226 |
+
self.discovered_factors = None
|
| 227 |
+
|
| 228 |
+
def fit(self, X: np.ndarray, y: np.ndarray) -> 'AlphaMiner':
|
| 229 |
+
"""
|
| 230 |
+
Mine alpha factors from features X predicting target y.
|
| 231 |
+
|
| 232 |
+
Args:
|
| 233 |
+
X: Features array (n_samples, n_features) - FLAT, not sequences
|
| 234 |
+
y: Target returns (n_samples,)
|
| 235 |
+
|
| 236 |
+
Returns:
|
| 237 |
+
self
|
| 238 |
+
"""
|
| 239 |
+
if not GPLEARN_AVAILABLE:
|
| 240 |
+
print("WARNING: gplearn not available. Returning identity features.")
|
| 241 |
+
self.discovered_factors = X
|
| 242 |
+
return self
|
| 243 |
+
|
| 244 |
+
print(f"Mining {self.n_factors} alpha factors with GP...")
|
| 245 |
+
print(f" Population: {self.population_size}, Generations: {self.generations}")
|
| 246 |
+
print(f" Input features: {X.shape[1]}")
|
| 247 |
+
|
| 248 |
+
function_set = FinancialFunctionLibrary.get_function_set()
|
| 249 |
+
|
| 250 |
+
# Genetic programming symbolic transformer
|
| 251 |
+
self.gp = SymbolicTransformer(
|
| 252 |
+
generations=self.generations,
|
| 253 |
+
population_size=self.population_size,
|
| 254 |
+
hall_of_fame=self.hall_of_fame,
|
| 255 |
+
n_components=self.n_factors,
|
| 256 |
+
function_set=function_set,
|
| 257 |
+
parsimony_coefficient=self.parsimony_coefficient,
|
| 258 |
+
max_samples=0.9,
|
| 259 |
+
verbose=1,
|
| 260 |
+
random_state=self.random_state,
|
| 261 |
+
n_jobs=-1
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
# Fit GP to discover symbolic expressions
|
| 265 |
+
self.gp.fit(X, y)
|
| 266 |
+
|
| 267 |
+
# Transform to get discovered factors
|
| 268 |
+
self.discovered_factors = self.gp.transform(X)
|
| 269 |
+
|
| 270 |
+
print(f" Discovered {self.discovered_factors.shape[1]} alpha factors")
|
| 271 |
+
|
| 272 |
+
# Evaluate and rank factors by IC
|
| 273 |
+
self._rank_factors(y)
|
| 274 |
+
|
| 275 |
+
return self
|
| 276 |
+
|
| 277 |
+
def transform(self, X: np.ndarray) -> np.ndarray:
|
| 278 |
+
"""Transform features using discovered alpha factors"""
|
| 279 |
+
if self.gp is None:
|
| 280 |
+
return X
|
| 281 |
+
|
| 282 |
+
return self.gp.transform(X)
|
| 283 |
+
|
| 284 |
+
def _rank_factors(self, y: np.ndarray):
|
| 285 |
+
"""Rank discovered factors by Information Coefficient"""
|
| 286 |
+
from scipy.stats import spearmanr
|
| 287 |
+
|
| 288 |
+
if self.discovered_factors is None:
|
| 289 |
+
return
|
| 290 |
+
|
| 291 |
+
ics = []
|
| 292 |
+
for i in range(self.discovered_factors.shape[1]):
|
| 293 |
+
factor = self.discovered_factors[:, i]
|
| 294 |
+
ic, _ = spearmanr(factor, y)
|
| 295 |
+
if not np.isnan(ic):
|
| 296 |
+
ics.append((i, abs(ic), ic))
|
| 297 |
+
|
| 298 |
+
ics.sort(key=lambda x: x[1], reverse=True)
|
| 299 |
+
|
| 300 |
+
print("\n Top 10 Discovered Alpha Factors (by |IC|):")
|
| 301 |
+
for i, (idx, abs_ic, ic) in enumerate(ics[:10], 1):
|
| 302 |
+
print(f" {i}. Factor {idx}: IC = {ic:+.4f}")
|
| 303 |
+
|
| 304 |
+
def get_factor_expressions(self) -> List[str]:
|
| 305 |
+
"""Get human-readable formulas for discovered factors"""
|
| 306 |
+
if self.gp is None:
|
| 307 |
+
return []
|
| 308 |
+
|
| 309 |
+
expressions = []
|
| 310 |
+
for program in self.gp._best_programs:
|
| 311 |
+
expressions.append(str(program))
|
| 312 |
+
|
| 313 |
+
return expressions
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
class LLMAlphaMiner:
|
| 317 |
+
"""
|
| 318 |
+
LLM-Driven Alpha Factor Discovery (Simplified Version).
|
| 319 |
+
|
| 320 |
+
Full implementation would use MCTS (Monte Carlo Tree Search) + LLM
|
| 321 |
+
to explore the space of possible formulas, using the LLM as a "policy"
|
| 322 |
+
to suggest promising formula modifications.
|
| 323 |
+
|
| 324 |
+
This simplified version uses LLM embeddings to cluster and suggest
|
| 325 |
+
factor combinations.
|
| 326 |
+
"""
|
| 327 |
+
|
| 328 |
+
def __init__(self, model_name: str = "sentence-transformers/all-MiniLM-L6-v2"):
|
| 329 |
+
self.model_name = model_name
|
| 330 |
+
self.embedder = None
|
| 331 |
+
|
| 332 |
+
def _load_embedder(self):
|
| 333 |
+
"""Lazy load sentence transformer"""
|
| 334 |
+
if self.embedder is None:
|
| 335 |
+
try:
|
| 336 |
+
from sentence_transformers import SentenceTransformer
|
| 337 |
+
self.embedder = SentenceTransformer(self.model_name)
|
| 338 |
+
except ImportError:
|
| 339 |
+
print("sentence-transformers not available. Using random projections.")
|
| 340 |
+
self.embedder = None
|
| 341 |
+
|
| 342 |
+
def suggest_factors(self, descriptions: List[str],
|
| 343 |
+
n_suggestions: int = 10) -> List[Dict]:
|
| 344 |
+
"""
|
| 345 |
+
Use LLM embeddings to suggest new factor combinations.
|
| 346 |
+
|
| 347 |
+
Args:
|
| 348 |
+
descriptions: List of existing factor descriptions/formulas
|
| 349 |
+
n_suggestions: Number of new factor ideas to generate
|
| 350 |
+
|
| 351 |
+
Returns:
|
| 352 |
+
List of suggested factor descriptions
|
| 353 |
+
"""
|
| 354 |
+
self._load_embedder()
|
| 355 |
+
|
| 356 |
+
if self.embedder is None:
|
| 357 |
+
# Fallback: random combinations
|
| 358 |
+
return self._random_suggestions(descriptions, n_suggestions)
|
| 359 |
+
|
| 360 |
+
# Get embeddings
|
| 361 |
+
embeddings = self.embedder.encode(descriptions)
|
| 362 |
+
|
| 363 |
+
# Find "gaps" in embedding space (regions with low density)
|
| 364 |
+
# Suggest combinations of distant factors
|
| 365 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 366 |
+
|
| 367 |
+
sim_matrix = cosine_similarity(embeddings)
|
| 368 |
+
|
| 369 |
+
suggestions = []
|
| 370 |
+
for _ in range(n_suggestions):
|
| 371 |
+
# Find least similar pair
|
| 372 |
+
min_sim = 1.0
|
| 373 |
+
min_pair = (0, 1)
|
| 374 |
+
for i in range(len(descriptions)):
|
| 375 |
+
for j in range(i+1, len(descriptions)):
|
| 376 |
+
if sim_matrix[i, j] < min_sim:
|
| 377 |
+
min_sim = sim_matrix[i, j]
|
| 378 |
+
min_pair = (i, j)
|
| 379 |
+
|
| 380 |
+
desc1, desc2 = descriptions[min_pair[0]], descriptions[min_pair[1]]
|
| 381 |
+
suggestions.append({
|
| 382 |
+
'type': 'combination',
|
| 383 |
+
'factors': [desc1, desc2],
|
| 384 |
+
'similarity': min_sim,
|
| 385 |
+
'description': f"Combine ({desc1}) with ({desc2})"
|
| 386 |
+
})
|
| 387 |
+
|
| 388 |
+
return suggestions
|
| 389 |
+
|
| 390 |
+
def _random_suggestions(self, descriptions: List[str],
|
| 391 |
+
n_suggestions: int) -> List[Dict]:
|
| 392 |
+
"""Fallback random suggestions"""
|
| 393 |
+
import random
|
| 394 |
+
suggestions = []
|
| 395 |
+
for _ in range(n_suggestions):
|
| 396 |
+
pair = random.sample(range(len(descriptions)), 2)
|
| 397 |
+
suggestions.append({
|
| 398 |
+
'type': 'combination',
|
| 399 |
+
'factors': [descriptions[pair[0]], descriptions[pair[1]]],
|
| 400 |
+
'similarity': 0.0,
|
| 401 |
+
'description': f"Combine ({descriptions[pair[0]]}) with ({descriptions[pair[1]]})"
|
| 402 |
+
})
|
| 403 |
+
return suggestions
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
class AlphaMiningPipeline:
|
| 407 |
+
"""
|
| 408 |
+
Complete pipeline: Raw data -> GP-discovered factors -> Enhanced features.
|
| 409 |
+
|
| 410 |
+
Usage:
|
| 411 |
+
pipeline = AlphaMiningPipeline(n_factors=50)
|
| 412 |
+
enhanced_features = pipeline.fit_transform(raw_features, returns)
|
| 413 |
+
|
| 414 |
+
The enhanced features combine:
|
| 415 |
+
- Original technical indicators
|
| 416 |
+
- GP-discovered nonlinear factors
|
| 417 |
+
- LLM-suggested factor combinations
|
| 418 |
+
"""
|
| 419 |
+
|
| 420 |
+
def __init__(self, n_gp_factors: int = 50,
|
| 421 |
+
gp_generations: int = 20,
|
| 422 |
+
use_llm: bool = True):
|
| 423 |
+
self.n_gp_factors = n_gp_factors
|
| 424 |
+
self.gp_generations = gp_generations
|
| 425 |
+
self.use_llm = use_llm
|
| 426 |
+
|
| 427 |
+
self.gp_miner = None
|
| 428 |
+
self.llm_miner = None
|
| 429 |
+
self.feature_names = []
|
| 430 |
+
|
| 431 |
+
def fit_transform(self, X: np.ndarray, y: np.ndarray,
|
| 432 |
+
feature_names: Optional[List[str]] = None) -> np.ndarray:
|
| 433 |
+
"""
|
| 434 |
+
Fit and transform in one call.
|
| 435 |
+
|
| 436 |
+
Args:
|
| 437 |
+
X: Raw features (n_samples, n_features)
|
| 438 |
+
y: Target returns (n_samples,)
|
| 439 |
+
feature_names: Names of original features (for LLM suggestions)
|
| 440 |
+
|
| 441 |
+
Returns:
|
| 442 |
+
Enhanced features (n_samples, n_original + n_gp_factors)
|
| 443 |
+
"""
|
| 444 |
+
print("=" * 60)
|
| 445 |
+
print("ALPHA MINING PIPELINE")
|
| 446 |
+
print("=" * 60)
|
| 447 |
+
|
| 448 |
+
# Step 1: GP Alpha Mining
|
| 449 |
+
print("\n[1/3] Genetic Programming Alpha Mining...")
|
| 450 |
+
self.gp_miner = AlphaMiner(
|
| 451 |
+
n_factors=self.n_gp_factors,
|
| 452 |
+
generations=self.gp_generations
|
| 453 |
+
)
|
| 454 |
+
gp_features = self.gp_miner.fit(X, y).transform(X)
|
| 455 |
+
|
| 456 |
+
# Step 2: LLM Suggestions (optional)
|
| 457 |
+
if self.use_llm and feature_names is not None:
|
| 458 |
+
print("\n[2/3] LLM Factor Suggestions...")
|
| 459 |
+
self.llm_miner = LLMAlphaMiner()
|
| 460 |
+
suggestions = self.llm_miner.suggest_factors(feature_names, n_suggestions=10)
|
| 461 |
+
print(f" Generated {len(suggestions)} factor ideas")
|
| 462 |
+
|
| 463 |
+
# Step 3: Combine
|
| 464 |
+
print("\n[3/3] Combining original + discovered features...")
|
| 465 |
+
enhanced = np.column_stack([X, gp_features])
|
| 466 |
+
|
| 467 |
+
self.feature_names = (feature_names or [f'f{i}' for i in range(X.shape[1])]) + \
|
| 468 |
+
[f'gp_alpha_{i}' for i in range(gp_features.shape[1])]
|
| 469 |
+
|
| 470 |
+
print(f"\nEnhanced features: {enhanced.shape[1]} (original: {X.shape[1]}, GP: {gp_features.shape[1]})")
|
| 471 |
+
|
| 472 |
+
return enhanced
|
| 473 |
+
|
| 474 |
+
def transform(self, X: np.ndarray) -> np.ndarray:
|
| 475 |
+
"""Transform new data using fitted miners"""
|
| 476 |
+
if self.gp_miner is None:
|
| 477 |
+
return X
|
| 478 |
+
|
| 479 |
+
gp_features = self.gp_miner.transform(X)
|
| 480 |
+
return np.column_stack([X, gp_features])
|
| 481 |
+
|
| 482 |
+
def get_discovered_expressions(self) -> List[str]:
|
| 483 |
+
"""Get human-readable discovered factor formulas"""
|
| 484 |
+
if self.gp_miner is None:
|
| 485 |
+
return []
|
| 486 |
+
return self.gp_miner.get_factor_expressions()
|
| 487 |
+
|
| 488 |
+
|
| 489 |
+
def mine_alphas_from_sequences(sequences: np.ndarray,
|
| 490 |
+
targets: np.ndarray,
|
| 491 |
+
n_factors: int = 50) -> Tuple[np.ndarray, AlphaMiningPipeline]:
|
| 492 |
+
"""
|
| 493 |
+
Convenience function: Flatten sequences and mine alphas.
|
| 494 |
+
|
| 495 |
+
Args:
|
| 496 |
+
sequences: (n_samples, seq_len, n_features)
|
| 497 |
+
targets: (n_samples,)
|
| 498 |
+
|
| 499 |
+
Returns:
|
| 500 |
+
enhanced_features: (n_samples, n_features + n_factors)
|
| 501 |
+
pipeline: Fitted AlphaMiningPipeline
|
| 502 |
+
"""
|
| 503 |
+
# Flatten sequences for GP (GP doesn't handle sequences natively)
|
| 504 |
+
n_samples, seq_len, n_features = sequences.shape
|
| 505 |
+
X_flat = sequences.reshape(n_samples, seq_len * n_features)
|
| 506 |
+
|
| 507 |
+
# Create feature names
|
| 508 |
+
feature_names = [f'f{t}_{f}' for t in range(seq_len) for f in range(n_features)]
|
| 509 |
+
|
| 510 |
+
pipeline = AlphaMiningPipeline(n_gp_factors=n_factors)
|
| 511 |
+
enhanced = pipeline.fit_transform(X_flat, targets, feature_names)
|
| 512 |
+
|
| 513 |
+
return enhanced, pipeline
|
| 514 |
+
|
| 515 |
+
|
| 516 |
+
if __name__ == '__main__':
|
| 517 |
+
# Test alpha mining on synthetic data
|
| 518 |
+
np.random.seed(42)
|
| 519 |
+
n_samples = 5000
|
| 520 |
+
n_features = 20
|
| 521 |
+
|
| 522 |
+
X = np.random.randn(n_samples, n_features)
|
| 523 |
+
# True relationship: y = x0 * x1 + sin(x2) + noise
|
| 524 |
+
y = X[:, 0] * X[:, 1] + np.sin(X[:, 2] * 2) + np.random.randn(n_samples) * 0.1
|
| 525 |
+
|
| 526 |
+
miner = AlphaMiner(n_factors=20, generations=5, population_size=500)
|
| 527 |
+
miner.fit(X, y)
|
| 528 |
+
|
| 529 |
+
print("\nDiscovered expressions (top 5):")
|
| 530 |
+
for expr in miner.get_factor_expressions()[:5]:
|
| 531 |
+
print(f" {expr}")
|