Upload alpha_factory/personas/gatekeeper.py with huggingface_hub
Browse files
alpha_factory/personas/gatekeeper.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Production Gatekeeper — Persona 6 (Bigfish, run SPARINGLY)
|
| 3 |
+
Final go/no-go decision on promoted alphas.
|
| 4 |
+
Only invoked for alphas that survived all upstream gates.
|
| 5 |
+
"""
|
| 6 |
+
from ..infra.llm_client import LLMClient
|
| 7 |
+
from ..schemas import BrainMetrics, GatekeeperMemo, Blueprint
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
SYSTEM_PROMPT = """You are a senior portfolio manager with 20 years of experience.
|
| 11 |
+
You are making the FINAL decision on whether to add this alpha to a production portfolio.
|
| 12 |
+
|
| 13 |
+
You have seen the full pipeline output: the hypothesis, the expression, the BRAIN metrics,
|
| 14 |
+
the crowd scout's novelty assessment, and the performance surgeon's diagnosis.
|
| 15 |
+
|
| 16 |
+
Your memo must address:
|
| 17 |
+
1. STRENGTHS — what makes this alpha worth running?
|
| 18 |
+
2. WEAKNESSES — what could go wrong?
|
| 19 |
+
3. RISKS — regime fragility, crowding, capacity constraints
|
| 20 |
+
4. RECOMMENDATION — go (add to portfolio) or no-go (reject)
|
| 21 |
+
|
| 22 |
+
Standards for go:
|
| 23 |
+
- Sharpe OS ≥ 1.25
|
| 24 |
+
- No single-year catastrophic loss (> -15%)
|
| 25 |
+
- Turnover reasonable for the signal (not churning)
|
| 26 |
+
- Genuinely orthogonal to existing book (corr < 0.65)
|
| 27 |
+
- Academic rationale is defensible (not curve-fitted)
|
| 28 |
+
- Would you bet your own money on this? If no → no-go.
|
| 29 |
+
|
| 30 |
+
You must be SKEPTICAL by default. The bar for "go" is HIGH.
|
| 31 |
+
Most alphas should get no-go. That's correct behavior.
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
async def gate_alpha(
|
| 36 |
+
llm: LLMClient,
|
| 37 |
+
blueprint: Blueprint,
|
| 38 |
+
metrics: BrainMetrics,
|
| 39 |
+
max_corr: float,
|
| 40 |
+
fitness_score: float,
|
| 41 |
+
model: str | None = None,
|
| 42 |
+
) -> GatekeeperMemo:
|
| 43 |
+
"""
|
| 44 |
+
Final production gate. Invoked ONLY for alphas that passed all prior gates.
|
| 45 |
+
Uses Bigfish model (72B) for maximum reasoning quality.
|
| 46 |
+
"""
|
| 47 |
+
user_prompt = f"""PRODUCTION GATE REVIEW
|
| 48 |
+
|
| 49 |
+
## Alpha Blueprint
|
| 50 |
+
- Theme: {blueprint.theme}
|
| 51 |
+
- Anomaly: {blueprint.anomaly_tag.value}
|
| 52 |
+
- Archetype: {blueprint.archetype}
|
| 53 |
+
- Neutralization: {blueprint.neutralization.value}
|
| 54 |
+
- Decay: {blueprint.decay}
|
| 55 |
+
- Components: {len(blueprint.components)}
|
| 56 |
+
- Academic anchor: {blueprint.academic_anchor or 'none cited'}
|
| 57 |
+
- Novelty claim: {blueprint.novelty_claim}
|
| 58 |
+
|
| 59 |
+
## BRAIN Metrics
|
| 60 |
+
- Sharpe (full): {metrics.sharpe_full:.3f}
|
| 61 |
+
- Sharpe (IS): {metrics.sharpe_is:.3f}
|
| 62 |
+
- Sharpe (OS): {metrics.sharpe_os:.3f}
|
| 63 |
+
- Fitness: {metrics.fitness:.3f}
|
| 64 |
+
- Turnover: {metrics.turnover:.3f}
|
| 65 |
+
- Max Drawdown: {metrics.max_drawdown:.3f}
|
| 66 |
+
- Returns: {metrics.returns:.3f}
|
| 67 |
+
- Yearly Sharpe: {metrics.yearly_sharpe}
|
| 68 |
+
|
| 69 |
+
## Novelty Assessment
|
| 70 |
+
- Max correlation to library: {max_corr:.3f}
|
| 71 |
+
- Fitness score (composite): {fitness_score:.3f}
|
| 72 |
+
|
| 73 |
+
## Decision Required
|
| 74 |
+
Write a 1-page production memo. Be SKEPTICAL.
|
| 75 |
+
Would you bet your own capital on this alpha?
|
| 76 |
+
Output your go/no-go decision with confidence level (0.0 to 1.0)."""
|
| 77 |
+
|
| 78 |
+
memo = await llm.generate_json(
|
| 79 |
+
prompt=user_prompt,
|
| 80 |
+
schema=GatekeeperMemo,
|
| 81 |
+
model=model or llm.config.bigfish_model,
|
| 82 |
+
temperature=0.3,
|
| 83 |
+
system_prompt=SYSTEM_PROMPT,
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
return memo
|