(risk_tolerance: float, index_dir: Path, signals: int = 2, shadow: bool = True)
| 63 | |
| 64 | |
| 65 | def make_v2_predictor(risk_tolerance: float, index_dir: Path, signals: int = 2, shadow: bool = True): |
| 66 | sig_a = MetadataSignal() |
| 67 | sig_c = EmbeddingSignal( |
| 68 | index_path=index_dir / "seed_embeddings.npy", |
| 69 | labels_path=index_dir / "seed_labels.json", |
| 70 | model_name="BAAI/bge-small-en-v1.5", |
| 71 | ) |
| 72 | calibrator = _load_calibrator_if_exists(index_dir) |
| 73 | |
| 74 | if signals == 3: |
| 75 | # Force Signal B active (for testing / comparison) |
| 76 | from uncommon_route.signals.structural import StructuralSignal |
| 77 | sig_b = StructuralSignal() |
| 78 | ensemble = Ensemble( |
| 79 | weights=[0.50, 0.10, 0.40], |
| 80 | risk_tolerance=risk_tolerance, |
| 81 | calibrator=calibrator, |
| 82 | ) |
| 83 | |
| 84 | def predict_3(row: dict) -> int: |
| 85 | vote_a = sig_a.predict(row) |
| 86 | vote_b = sig_b.predict(row) |
| 87 | vote_c = sig_c.predict(row) |
| 88 | result = ensemble.decide([vote_a, vote_b, vote_c]) |
| 89 | return 1 if result.tier_id is None else result.tier_id |
| 90 | |
| 91 | return predict_3, None # no shadow tracker in forced mode |
| 92 | |
| 93 | # Default: 2-signal with conditional Signal B activation and optional shadow |
| 94 | ensemble_2sig = Ensemble( |
| 95 | weights=[0.55, 0.45], |
| 96 | risk_tolerance=risk_tolerance, |
| 97 | calibrator=calibrator, |
| 98 | ) |
| 99 | ensemble_3sig = None |
| 100 | |
| 101 | shadow_tracker = None |
| 102 | sig_b = None |
| 103 | if shadow: |
| 104 | from uncommon_route.signals.structural import StructuralSignal |
| 105 | from uncommon_route.learning.shadow import ShadowTracker |
| 106 | sig_b = StructuralSignal() |
| 107 | shadow_tracker = ShadowTracker(eval_window=200, promote_after=3) |
| 108 | ensemble_3sig = Ensemble( |
| 109 | weights=[0.50, 0.10, 0.40], |
| 110 | risk_tolerance=risk_tolerance, |
| 111 | calibrator=calibrator, |
| 112 | ) |
| 113 | |
| 114 | def predict_2(row: dict) -> int: |
| 115 | vote_a = sig_a.predict(row) |
| 116 | vote_c = sig_c.predict(row) |
| 117 | |
| 118 | activate_b = _should_activate_signal_b(row) |
| 119 | promoted_b = shadow_tracker is not None and shadow_tracker.promoted |
| 120 | if (activate_b or promoted_b) and sig_b is not None and ensemble_3sig is not None: |
| 121 | vote_b = sig_b.predict(row) |
| 122 | result = ensemble_3sig.decide([vote_a, vote_b, vote_c]) |
no test coverage detected