Spaces:
Sleeping
Sleeping
Update analytics/recommendation_engine.py
Browse files
analytics/recommendation_engine.py
CHANGED
|
@@ -29,7 +29,11 @@ def build_upcoming_hitter_recommendations(
|
|
| 29 |
) -> list[dict]:
|
| 30 |
"""
|
| 31 |
Decision-layer wrapper.
|
| 32 |
-
Uses simulated fair rows, then adds
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
Suppresses low-value PASS rows unless nothing else exists.
|
| 34 |
"""
|
| 35 |
rows = build_upcoming_simulated_rows(
|
|
@@ -41,6 +45,31 @@ def build_upcoming_hitter_recommendations(
|
|
| 41 |
recommendations: list[dict] = []
|
| 42 |
|
| 43 |
for row in rows:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
confidence_block = compute_confidence(row, game_row=game_row)
|
| 45 |
row.update(confidence_block)
|
| 46 |
|
|
|
|
| 29 |
) -> list[dict]:
|
| 30 |
"""
|
| 31 |
Decision-layer wrapper.
|
| 32 |
+
Uses simulated fair rows, then adds:
|
| 33 |
+
1) opportunity adjustment
|
| 34 |
+
2) confidence
|
| 35 |
+
3) recommendation tier
|
| 36 |
+
|
| 37 |
Suppresses low-value PASS rows unless nothing else exists.
|
| 38 |
"""
|
| 39 |
rows = build_upcoming_simulated_rows(
|
|
|
|
| 45 |
recommendations: list[dict] = []
|
| 46 |
|
| 47 |
for row in rows:
|
| 48 |
+
slot = row.get("slot", "current")
|
| 49 |
+
lineup_distance = _lineup_distance_from_slot(slot)
|
| 50 |
+
|
| 51 |
+
opportunity = estimate_plate_appearance_probability(
|
| 52 |
+
outs=game_row.get("outs", 0),
|
| 53 |
+
lineup_distance=lineup_distance,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
expected_pa = float(opportunity.get("expected_pa", 1.0) or 1.0)
|
| 57 |
+
|
| 58 |
+
# Apply opportunity adjustment to downstream probabilities
|
| 59 |
+
for prob_col in ["hit_prob", "hr_prob", "tb2p_prob"]:
|
| 60 |
+
if prob_col in row and row.get(prob_col) is not None:
|
| 61 |
+
try:
|
| 62 |
+
raw_prob = float(row.get(prob_col))
|
| 63 |
+
row[prob_col] = min(0.95, max(0.001, raw_prob * expected_pa))
|
| 64 |
+
except Exception:
|
| 65 |
+
pass
|
| 66 |
+
|
| 67 |
+
# Carry opportunity diagnostics forward for debugging/auditing
|
| 68 |
+
row["lineup_distance"] = lineup_distance
|
| 69 |
+
row["pa_prob_this_inning"] = opportunity.get("pa_prob_this_inning")
|
| 70 |
+
row["pa_prob_next_two_innings"] = opportunity.get("pa_prob_next_two_innings")
|
| 71 |
+
row["expected_pa"] = expected_pa
|
| 72 |
+
|
| 73 |
confidence_block = compute_confidence(row, game_row=game_row)
|
| 74 |
row.update(confidence_block)
|
| 75 |
|