Spaces:
Running
Running
File size: 71,141 Bytes
2099da5 9246aa7 2099da5 0010624 6989587 f2d8d50 2099da5 9246aa7 0010624 9246aa7 2099da5 d3b6f35 2099da5 9246aa7 2099da5 9246aa7 70995df 9246aa7 5aa0880 37fe240 9246aa7 37fe240 9246aa7 0010624 9246aa7 37fe240 87523c1 6989587 bb05a74 6989587 d3b6f35 50dc123 d3b6f35 50dc123 0c51058 87523c1 bb05a74 87523c1 bb05a74 87523c1 bb05a74 f2d8d50 bb05a74 7b3d14f 0010624 7b3d14f 0010624 7b3d14f 6989587 bb05a74 6989587 7b3d14f 6989587 7b3d14f 6989587 7b3d14f 0010624 7b3d14f 0010624 7b3d14f 0010624 7b3d14f 0010624 87523c1 7b3d14f 87523c1 7b3d14f 87523c1 50dc123 7b3d14f 87523c1 7b3d14f 87523c1 7b3d14f 87523c1 7b3d14f 87523c1 7b3d14f 50dc123 bb05a74 50dc123 87523c1 7b3d14f 50dc123 7b3d14f 50dc123 7b3d14f 87523c1 7b3d14f 87523c1 7b3d14f 50dc123 7b3d14f 87523c1 50dc123 51b1a14 50dc123 51b1a14 50dc123 bb05a74 50dc123 bb05a74 50dc123 6989587 bb05a74 6989587 bb05a74 6989587 50dc123 bb05a74 50dc123 c4cd7c6 6989587 c4cd7c6 6989587 c4cd7c6 6989587 c4cd7c6 6989587 7b3d14f bb05a74 7b3d14f bb05a74 7b3d14f 2099da5 9246aa7 2099da5 9246aa7 2099da5 9246aa7 87523c1 9246aa7 0010624 9246aa7 2099da5 9246aa7 2099da5 9246aa7 95e27f5 37fe240 2099da5 9246aa7 2099da5 9246aa7 2099da5 9246aa7 2099da5 87523c1 9246aa7 37fe240 9246aa7 2885bcc 95e27f5 9246aa7 2099da5 9246aa7 49fb892 2885bcc 7b3d14f 2885bcc bb05a74 2885bcc bb05a74 2885bcc 50dc123 2885bcc 87523c1 2885bcc 50dc123 2885bcc 0010624 2885bcc 0010624 c4cd7c6 2885bcc 2099da5 49fb892 0010624 50dc123 0010624 49fb892 2885bcc 49fb892 95e27f5 0010624 49fb892 0010624 49fb892 0010624 49fb892 2099da5 49fb892 0010624 49fb892 0010624 49fb892 0010624 49fb892 7b3d14f 49fb892 2099da5 0010624 49fb892 9246aa7 0c51058 9246aa7 6989587 9246aa7 0010624 9246aa7 0010624 9246aa7 d3b6f35 49fb892 0010624 9246aa7 d3b6f35 0010624 9246aa7 87523c1 50dc123 51b1a14 50dc123 7b3d14f 6989587 0c51058 50dc123 7b3d14f 9246aa7 6989587 9246aa7 2099da5 9246aa7 2099da5 9246aa7 dba351a 9246aa7 6989587 2885bcc c4cd7c6 54a42dd 6989587 2885bcc 50dc123 2885bcc 6989587 2885bcc 50dc123 2885bcc c4cd7c6 2885bcc c4cd7c6 2885bcc c4cd7c6 2885bcc 50dc123 51b1a14 50dc123 6989587 54a42dd d3b6f35 54a42dd d3b6f35 50dc123 d3b6f35 6989587 50dc123 6989587 50dc123 6989587 d3b6f35 50dc123 d3b6f35 50dc123 d3b6f35 50dc123 d3b6f35 50dc123 d3b6f35 50dc123 6989587 50dc123 51b1a14 50dc123 6989587 50dc123 7b3d14f 6989587 50dc123 6989587 | 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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 | """
analytics/props_mapper.py
Maps sportsbook HR prop rows to the shared HR probability engine and computes
edge for the Props page.
"""
from __future__ import annotations
from typing import Any, Callable
import pandas as pd
from analytics.no_vig_props import american_to_implied_prob, compute_bet_ev, compute_edge
from analytics.model_voice import build_hr_model_voice, build_strikeout_model_voice
from data.mlb_starters import lookup_pitchers_for_game, lookup_batter_current_team
from data.odds_name_map import map_odds_name_to_model_name
from models.hr_probability_engine import build_hr_probability_result
from models.pitcher_adjustment import build_pitcher_feature_row
from visualization.cards.player_identity import normalize_for_matching, to_canonical_name
def build_strikeout_probability_result_v2(*args, **kwargs):
from models.strikeout_probability_engine_v2 import (
build_strikeout_probability_result_v2 as _build_strikeout_probability_result_v2,
)
return _build_strikeout_probability_result_v2(*args, **kwargs)
def _build_statcast_name_index(statcast_df: pd.DataFrame) -> dict[str, str]:
if statcast_df.empty or "player_name" not in statcast_df.columns:
return {}
index: dict[str, str] = {}
for name in statcast_df["player_name"].astype(str).unique():
normalized = map_odds_name_to_model_name(name)
if normalized not in index:
index[normalized] = name
canonical = to_canonical_name(name)
if canonical != name:
canonical_norm = map_odds_name_to_model_name(canonical)
if canonical_norm not in index:
index[canonical_norm] = name
suffix_stripped = normalize_for_matching(canonical)
if suffix_stripped and suffix_stripped not in index:
index[suffix_stripped] = name
return index
def _build_game_context_from_row(row: Any) -> dict[str, Any]:
return {
"away_team": str(row.get("away_team", "") or "").strip(),
"home_team": str(row.get("home_team", "") or "").strip(),
"venue": str(
row.get("venue")
or row.get("stadium")
or row.get("venue_name")
or row.get("park")
or ""
).strip(),
"game_datetime_utc": str(
row.get("game_datetime_utc")
or row.get("commence_time")
or ""
).strip(),
"game_date": str(row.get("game_date", "") or "").strip(),
"lineup_slot": row.get("lineup_slot"),
"lineup_slot_source": row.get("lineup_slot_source"),
"team_total": row.get("team_total"),
"team_total_source": row.get("team_total_source"),
}
def _normalize_team_name(value: Any) -> str:
return " ".join(str(value or "").strip().lower().split())
def _to_display_name(value: Any) -> str:
return str(value or "").strip()
def _normalize_person_name(value: Any) -> str:
return normalize_for_matching(to_canonical_name(str(value or "").strip()))
def _names_match(left: Any, right: Any) -> bool:
left_norm = _normalize_person_name(left)
right_norm = _normalize_person_name(right)
return bool(left_norm and right_norm and left_norm == right_norm)
def _compute_verdict(
bet_ev: float | None,
edge: float | None,
confidence_score: float | None,
is_modeled: bool,
) -> str:
if not is_modeled:
return "tracked"
try:
ev = float(bet_ev if bet_ev is not None else -9.0)
ed = float(edge if edge is not None else -9.0)
conf = float(confidence_score if confidence_score is not None else 0.0)
except Exception:
return "pass"
if ev >= 0.05 and ed >= 0.01 and conf >= 62:
return "bet"
if ev >= -0.03 and ed >= -0.01 and conf >= 45:
return "watch"
return "pass"
def _confidence_display_remap(raw_score: float | None) -> float | None:
try:
raw = float(raw_score)
except Exception:
return None
if raw <= 40.0:
return max(1.0, min(100.0, raw))
return max(1.0, min(100.0, 40.0 + ((raw - 40.0) * 1.45)))
def _normalize_confidence_components(value: Any) -> list[dict[str, Any]]:
if not isinstance(value, list):
return []
normalized: list[dict[str, Any]] = []
for item in value:
if not isinstance(item, dict):
continue
label = str(item.get("label") or "").strip()
if not label:
continue
try:
component_value = float(item.get("value") or 0.0)
except Exception:
component_value = 0.0
normalized.append(
{
"label": label,
"value": round(component_value, 1),
"direction": str(item.get("direction") or "").strip().lower() or None,
}
)
return normalized
def _select_confidence_primary_driver(
penalties: list[dict[str, Any]],
bonuses: list[dict[str, Any]],
) -> dict[str, Any] | None:
penalty_candidates = [item for item in penalties if float(item.get("value") or 0.0) > 0.0]
bonus_candidates = [item for item in bonuses if float(item.get("value") or 0.0) > 0.0]
if penalty_candidates:
return max(penalty_candidates, key=lambda item: float(item.get("value") or 0.0))
if bonus_candidates:
return max(bonus_candidates, key=lambda item: float(item.get("value") or 0.0))
return None
def _build_strikeout_confidence_payload(
probability_result: dict[str, Any],
) -> dict[str, Any]:
source = str(probability_result.get("confidence_source") or "strikeout_v2_live")
raw_score = probability_result.get("confidence_score_raw", probability_result.get("confidence_score"))
raw_bucket = probability_result.get("confidence_bucket")
reasons = list(probability_result.get("confidence_reasons") or [])
bonuses = _normalize_confidence_components(probability_result.get("confidence_component_bonuses"))
penalties = _normalize_confidence_components(probability_result.get("confidence_component_penalties"))
raw_score_float = float(raw_score) if raw_score is not None else None
display_score = _confidence_display_remap(raw_score_float)
display_bucket = None
if display_score is not None:
if display_score >= 75:
display_bucket = "high"
elif display_score >= 55:
display_bucket = "medium"
else:
display_bucket = "low"
primary_driver = _select_confidence_primary_driver(penalties, bonuses)
summary_label = str((primary_driver or {}).get("label") or "").strip() or None
return {
"confidence_score_raw": round(raw_score_float, 1) if raw_score_float is not None else None,
"confidence_score_display": round(display_score, 1) if display_score is not None else None,
"confidence_source": source,
"confidence_component_bonuses": bonuses,
"confidence_component_penalties": penalties,
"confidence_primary_driver": primary_driver,
"confidence_summary_label": summary_label,
"confidence_bucket_raw": raw_bucket,
"confidence_bucket_display": display_bucket,
"confidence_reasons": reasons[:5],
}
def _classify_strikeout_probability_status(
*,
fair_prob: float | None,
implied: float | None,
pitcher_name: str,
probability_result: dict[str, Any],
) -> str:
if fair_prob is not None:
return "modeled_ok" if implied is not None else "missing_implied_prob"
if not str(pitcher_name or "").strip():
return "missing_pitcher_context"
if str(probability_result.get("pitcher_resolution_status") or "").strip().lower() == "unresolved":
return "missing_pitcher_context"
if str(probability_result.get("projected_starter_match_status") or "").strip().lower() == "resolved_pitcher_mismatch":
return "projected_starter_mismatch"
return "empty_probability_result"
def _classify_hr_probability_status(
*,
threshold_int: int,
is_modeled: bool,
model_prob: float | None,
implied: float | None,
probability_result: dict[str, Any],
statcast_df: pd.DataFrame | None,
pitcher_name: str,
) -> str:
if threshold_int != 1 or not is_modeled:
return "unmodeled_ladder"
if model_prob is not None:
return "modeled_ok" if implied is not None else "missing_implied_prob"
if statcast_df is None or statcast_df.empty:
return "missing_baseline"
baseline_prob = probability_result.get("baseline_hr_prob")
pitcher_status = str(probability_result.get("pitcher_resolution_status") or "").strip().lower()
skipped_layers = str(probability_result.get("skipped_layers") or "").strip().lower()
batter_rows_missing = baseline_prob is None
if batter_rows_missing:
return "missing_baseline"
if implied is None:
return "missing_implied_prob"
if not str(pitcher_name or "").strip():
return "missing_pitcher_context"
if pitcher_status in {"pitcher_missing", "unresolved", "matchup_incomplete"}:
return "missing_pitcher_context"
if "pitcher_missing" in skipped_layers or "matchup_incomplete" in skipped_layers:
return "missing_pitcher_context"
if baseline_prob is not None:
return "empty_probability_result"
return "unknown"
def _infer_batter_team(
batter_name: str,
batter_statcast_df: pd.DataFrame,
) -> str:
if (
batter_statcast_df is None
or batter_statcast_df.empty
or not batter_name
or "player_name" not in batter_statcast_df.columns
):
return ""
normalized_target = _normalize_person_name(batter_name)
player_rows = batter_statcast_df[
batter_statcast_df["player_name"].astype(str).map(_normalize_person_name) == normalized_target
].copy()
if player_rows.empty:
return ""
team_values: list[str] = []
if {"inning_topbot", "home_team", "away_team"}.issubset(player_rows.columns):
inning_half = player_rows["inning_topbot"].fillna("").astype(str).str.lower()
top_mask = inning_half.str.contains("top")
bottom_mask = inning_half.str.contains("bot|bottom")
if top_mask.any():
team_values.extend(
player_rows.loc[top_mask, "away_team"].dropna().astype(str).tolist()
)
if bottom_mask.any():
team_values.extend(
player_rows.loc[bottom_mask, "home_team"].dropna().astype(str).tolist()
)
for col in ["team", "batter_team", "team_name"]:
if col in player_rows.columns:
team_values.extend(player_rows[col].dropna().astype(str).tolist())
normalized = [_normalize_team_name(v) for v in team_values if str(v).strip()]
if not normalized:
return ""
return pd.Series(normalized).mode().iloc[0]
def _resolve_batter_team(
row: Any,
batter_name: str,
batter_statcast_df: pd.DataFrame,
) -> tuple[str, str]:
row_team, row_source = _resolve_batter_team_from_row_context(
row=row,
batter_name=batter_name,
)
if row_team:
return (row_team, row_source)
away_team = _to_display_name(row.get("away_team"))
home_team = _to_display_name(row.get("home_team"))
away_norm = _normalize_team_name(away_team)
home_norm = _normalize_team_name(home_team)
statcast_ok = (
batter_statcast_df is not None
and not batter_statcast_df.empty
and batter_name
and "player_name" in batter_statcast_df.columns
)
if statcast_ok:
normalized_target = _normalize_person_name(batter_name)
player_rows = batter_statcast_df[
batter_statcast_df["player_name"].astype(str).map(_normalize_person_name) == normalized_target
].copy()
if not player_rows.empty:
if "source_season" in player_rows.columns:
current_rows = player_rows[pd.to_numeric(player_rows["source_season"], errors="coerce") == 2026].copy()
current_team = _infer_batter_team(batter_name=batter_name, batter_statcast_df=current_rows)
if current_team:
if current_team == away_norm and away_team:
return (away_team, "current_season_statcast")
if current_team == home_norm and home_team:
return (home_team, "current_season_statcast")
historical_team = _infer_batter_team(batter_name=batter_name, batter_statcast_df=player_rows)
if historical_team:
if historical_team == away_norm and away_team:
return (away_team, "historical_statcast")
if historical_team == home_norm and home_team:
return (home_team, "historical_statcast")
# historical_team doesn't match either current game team (player changed teams);
# fall through to roster lookup instead of returning a stale team name
# Level 4: current-season MLB roster lookup (handles offseason moves and new players)
if batter_name:
roster_team = lookup_batter_current_team(batter_name, away_team or "", home_team or "")
if roster_team:
return (roster_team, "mlb_roster_lookup")
return ("", "unresolved")
def _resolve_batter_team_from_row_context(
row: Any,
batter_name: str,
) -> tuple[str, str]:
away_team = _to_display_name(row.get("away_team"))
home_team = _to_display_name(row.get("home_team"))
away_norm = _normalize_team_name(away_team)
home_norm = _normalize_team_name(home_team)
for key in ("batter_team", "player_team", "team", "team_name"):
value = _to_display_name(row.get(key))
value_norm = _normalize_team_name(value)
if value_norm == away_norm and away_team:
return (away_team, f"row_{key}")
if value_norm == home_norm and home_team:
return (home_team, f"row_{key}")
return ("", "unknown")
def _infer_lineup_slot(
batter_name: str,
batter_statcast_df: pd.DataFrame,
) -> tuple[int | None, str]:
if (
batter_statcast_df is None
or batter_statcast_df.empty
or not batter_name
or "player_name" not in batter_statcast_df.columns
):
return (None, "unknown")
player_rows = batter_statcast_df[
batter_statcast_df["player_name"].astype(str).str.casefold() == batter_name.casefold()
].copy()
if player_rows.empty:
return (None, "unknown")
for col in ["lineup_slot", "lineup_position", "batting_order", "bat_order"]:
if col not in player_rows.columns:
continue
numeric = pd.to_numeric(player_rows[col], errors="coerce").dropna()
numeric = numeric[(numeric >= 1) & (numeric <= 9)]
if not numeric.empty:
mode = numeric.round().astype(int).mode()
if not mode.empty:
return (int(mode.iloc[0]), "projected")
return (None, "unknown")
def _resolve_pitcher_hand(
pitcher_name: str,
pitcher_statcast_df: pd.DataFrame | None,
) -> tuple[str, str]:
if not pitcher_name or pitcher_statcast_df is None or pitcher_statcast_df.empty:
return ("", "unavailable")
if {"player_name", "p_throws"}.issubset(pitcher_statcast_df.columns):
direct_rows = pitcher_statcast_df[
pitcher_statcast_df["player_name"].astype(str).map(_normalize_person_name) == _normalize_person_name(pitcher_name)
].copy()
if not direct_rows.empty:
direct_hand = str(direct_rows.iloc[0].get("p_throws") or "").strip().upper()
if direct_hand:
return (direct_hand, "statcast_direct")
try:
pitcher_row = build_pitcher_feature_row(
statcast_df=pitcher_statcast_df,
pitcher_name=pitcher_name,
)
hand = str(pitcher_row.get("p_throws") or "").strip().upper()
return (hand, "pitcher_feature_row" if hand else "unavailable")
except Exception:
return ("", "unavailable")
def _resolve_team_total(
row: Any,
batter_team: str,
) -> tuple[float | None, str]:
direct_keys = ["team_total", "implied_team_total", "batter_team_total"]
for key in direct_keys:
value = row.get(key)
try:
if value is not None and str(value).strip() not in {"", "nan", "None"}:
return (float(value), "projected")
except Exception:
continue
away_norm = _normalize_team_name(row.get("away_team"))
home_norm = _normalize_team_name(row.get("home_team"))
batter_team_norm = _normalize_team_name(batter_team)
if batter_team_norm and batter_team_norm == away_norm:
for key in ["away_team_total", "away_implied_total"]:
value = row.get(key)
try:
if value is not None and str(value).strip() not in {"", "nan", "None"}:
return (float(value), "projected")
except Exception:
continue
if batter_team_norm and batter_team_norm == home_norm:
for key in ["home_team_total", "home_implied_total"]:
value = row.get(key)
try:
if value is not None and str(value).strip() not in {"", "nan", "None"}:
return (float(value), "projected")
except Exception:
continue
return (None, "unknown")
def _resolve_pitcher_name(
row: Any,
batter_team: str,
probable_starters: dict | None,
) -> tuple[str, str, str]:
explicit_pitcher = str(
row.get("pitcher_name")
or row.get("pitcher")
or row.get("opposing_pitcher")
or ""
).strip()
away_team = str(row.get("away_team") or "").strip()
home_team = str(row.get("home_team") or "").strip()
if explicit_pitcher and (not away_team or not home_team or not probable_starters):
return (explicit_pitcher, "row_explicit", "resolved")
if not probable_starters:
return ("", "probable_starters_unavailable", "unresolved")
if not away_team or not home_team:
return ("", "matchup_incomplete", "unresolved")
starters = lookup_pitchers_for_game(
away_team=away_team,
home_team=home_team,
starters_map=probable_starters,
)
if not starters:
return ("", "matchup_not_found", "unresolved")
away_norm = _normalize_team_name(away_team)
home_norm = _normalize_team_name(home_team)
batter_team_norm = _normalize_team_name(batter_team)
home_pitcher = str(starters.get("home_pitcher") or "").strip()
away_pitcher = str(starters.get("away_pitcher") or "").strip()
if explicit_pitcher:
if _names_match(home_pitcher, explicit_pitcher) or _names_match(away_pitcher, explicit_pitcher):
return (explicit_pitcher, "row_explicit_validated", "resolved")
if batter_team_norm and batter_team_norm == away_norm:
return (
home_pitcher,
"probable_starters_matchup",
"resolved",
)
if batter_team_norm and batter_team_norm == home_norm:
return (
away_pitcher,
"probable_starters_matchup",
"resolved",
)
if home_pitcher and not away_pitcher:
return (home_pitcher, "probable_starters_single_side", "resolved")
if away_pitcher and not home_pitcher:
return (away_pitcher, "probable_starters_single_side", "resolved")
if explicit_pitcher:
return (explicit_pitcher, "row_explicit_unvalidated", "resolved")
return ("", "batter_team_unresolved", "unresolved")
def _lookup_projected_starter_context(
row: Any,
probable_starters: dict | None,
) -> dict[str, Any]:
away_team = str(row.get("away_team") or "").strip()
home_team = str(row.get("home_team") or "").strip()
out = {
"projected_home_pitcher": "",
"projected_away_pitcher": "",
"projected_starter_available": False,
"projected_starter_source": "probable_starters_unavailable" if not probable_starters else "matchup_incomplete",
"projected_home_pitcher_source": "",
"projected_away_pitcher_source": "",
"starter_cache_source": "probable_starters_unavailable" if not probable_starters else "matchup_incomplete",
"fallback_used": False,
}
if not probable_starters or not away_team or not home_team:
return out
starters = lookup_pitchers_for_game(
away_team=away_team,
home_team=home_team,
starters_map=probable_starters,
)
if not starters:
out["projected_starter_source"] = "matchup_not_found"
return out
projected_home = str(starters.get("home_pitcher") or "").strip()
projected_away = str(starters.get("away_pitcher") or "").strip()
out.update(
{
"projected_home_pitcher": projected_home,
"projected_away_pitcher": projected_away,
"projected_starter_available": bool(projected_home or projected_away),
"projected_starter_source": str(starters.get("starter_cache_source") or "probable_starters_matchup"),
"projected_home_pitcher_source": str(starters.get("home_pitcher_source") or ""),
"projected_away_pitcher_source": str(starters.get("away_pitcher_source") or ""),
"starter_cache_source": str(starters.get("starter_cache_source") or "probable_starters_matchup"),
"fallback_used": bool(starters.get("fallback_used")),
}
)
return out
def _projected_starter_match_status(
resolved_pitcher_name: str,
projected_home_pitcher: str,
projected_away_pitcher: str,
) -> str:
resolved = str(resolved_pitcher_name or "").strip()
if not projected_home_pitcher and not projected_away_pitcher:
return "projected_starter_unavailable"
if not resolved:
return "projected_starter_available_but_unresolved"
if _names_match(projected_home_pitcher, resolved):
return "matched_projected_home"
if _names_match(projected_away_pitcher, resolved):
return "matched_projected_away"
return "resolved_pitcher_mismatch"
def _resolve_pitcher_team_and_opponent(
row: Any,
pitcher_name: str,
probable_starters: dict | None,
) -> tuple[str, str]:
away_team = str(row.get("away_team") or "").strip()
home_team = str(row.get("home_team") or "").strip()
if not away_team or not home_team or not pitcher_name or not probable_starters:
return ("", "")
starters = lookup_pitchers_for_game(
away_team=away_team,
home_team=home_team,
starters_map=probable_starters,
)
if not starters:
return ("", "")
away_pitcher = str(starters.get("away_pitcher") or "").strip()
home_pitcher = str(starters.get("home_pitcher") or "").strip()
if _names_match(away_pitcher, pitcher_name):
return (away_team, home_team)
if _names_match(home_pitcher, pitcher_name):
return (home_team, away_team)
return ("", "")
def _resolve_strikeout_pitcher_name(
row: Any,
probable_starters: dict | None,
) -> tuple[str, str, str]:
explicit_pitcher = _to_display_name(row.get("player_name_raw") or row.get("player_name"))
away_team = str(row.get("away_team") or "").strip()
home_team = str(row.get("home_team") or "").strip()
if not explicit_pitcher and not probable_starters:
return ("", "missing_pitcher_name", "unresolved")
if not probable_starters or not away_team or not home_team:
return (explicit_pitcher, "row_explicit", "resolved" if explicit_pitcher else "unresolved")
starters = lookup_pitchers_for_game(
away_team=away_team,
home_team=home_team,
starters_map=probable_starters,
)
if not starters:
return (explicit_pitcher, "row_explicit", "resolved" if explicit_pitcher else "unresolved")
projected_home = str(starters.get("home_pitcher") or "").strip()
projected_away = str(starters.get("away_pitcher") or "").strip()
if explicit_pitcher and (
_names_match(projected_home, explicit_pitcher)
or _names_match(projected_away, explicit_pitcher)
):
return (explicit_pitcher, "row_explicit_validated", "resolved")
if projected_home and not projected_away:
return (projected_home, "probable_starters_single_side", "resolved")
if projected_away and not projected_home:
return (projected_away, "probable_starters_single_side", "resolved")
if projected_home and projected_away:
return ("", "row_explicit_mismatch", "unresolved")
return (explicit_pitcher, "row_explicit", "resolved" if explicit_pitcher else "unresolved")
def _extract_team_batters_from_statcast(
team_name: str,
batter_statcast_df: pd.DataFrame | None,
max_players: int = 9,
) -> list[str]:
if (
not team_name
or batter_statcast_df is None
or batter_statcast_df.empty
or "player_name" not in batter_statcast_df.columns
):
return []
team_norm = _normalize_team_name(team_name)
if not team_norm:
return []
working = batter_statcast_df.copy()
if "source_season" in working.columns:
current_rows = working[pd.to_numeric(working["source_season"], errors="coerce") == 2026].copy()
if not current_rows.empty:
working = current_rows
players = (
working.get("player_name", pd.Series(dtype="object"))
.dropna()
.astype(str)
.tolist()
)
if not players:
return []
matched_names: list[str] = []
seen_norms: set[str] = set()
for player_name in players:
inferred_team = _infer_batter_team(player_name, working)
if inferred_team != team_norm:
continue
player_norm = _normalize_person_name(player_name)
if not player_norm or player_norm in seen_norms:
continue
seen_norms.add(player_norm)
matched_names.append(player_name)
if len(matched_names) >= max_players:
break
return matched_names
def _lookup_baseline_metadata(
statcast_df: pd.DataFrame | None,
player_name: str,
) -> dict[str, Any]:
default = {
"baseline_mode": None,
"prior_sample_size": None,
"season_2026_sample_size": None,
"prior_weight": None,
"season_2026_weight": None,
"baseline_driver": None,
"rolling_overlay_active": None,
}
if (
statcast_df is None
or statcast_df.empty
or not player_name
or "player_name" not in statcast_df.columns
):
return default
normalized_target = _normalize_person_name(player_name)
if not normalized_target:
return default
normalized_series = statcast_df["player_name"].astype(str).map(_normalize_person_name)
rows = statcast_df[normalized_series == normalized_target].copy()
if rows.empty:
return default
first_row = rows.iloc[0]
return {
"baseline_mode": first_row.get("baseline_mode"),
"prior_sample_size": first_row.get("prior_sample_size"),
"season_2026_sample_size": first_row.get("season_2026_sample_size"),
"prior_weight": first_row.get("prior_weight"),
"season_2026_weight": first_row.get("season_2026_weight"),
"baseline_driver": first_row.get("baseline_driver"),
"rolling_overlay_active": first_row.get("rolling_overlay_active"),
}
def get_player_hr_prob(
player_name_normalized: str,
statcast_df: pd.DataFrame,
_name_index: dict[str, str] | None = None,
) -> tuple[float | None, str]:
"""
Backward-compatible wrapper for callers expecting (prob, source).
"""
name_index = _name_index if _name_index is not None else _build_statcast_name_index(statcast_df)
statcast_name = name_index.get(player_name_normalized, player_name_normalized)
result = build_hr_probability_result(
batter_statcast_df=statcast_df,
batter_name=statcast_name,
mode="pregame",
)
prob = result.get("calibrated_hr_prob")
if prob is None:
return (None, "unavailable")
return (float(prob), "shared_pregame_engine")
def map_hr_props_to_model(
props_df: pd.DataFrame,
statcast_df: pd.DataFrame,
prob_fn: Callable[..., Any] | None = None,
pitcher_stats_df: pd.DataFrame | None = None,
pitcher_statcast_df: pd.DataFrame | None = None,
probable_starters: dict | None = None,
) -> pd.DataFrame:
"""
Join HR prop rows to shared-engine HR probabilities and compute edge.
Adds columns:
implied_prob, model_hr_prob, model_hr_prob_source, edge
and shared-engine diagnostics:
baseline_hr_prob, pregame_hr_prob, probability_mode,
component adjustment columns, applied_layers, skipped_layers
"""
del prob_fn
if props_df.empty:
return pd.DataFrame()
hr_df = props_df[props_df["market"] == "hr"].copy()
if hr_df.empty:
return pd.DataFrame()
pitcher_df = (
pitcher_statcast_df
if pitcher_statcast_df is not None
else pitcher_stats_df
if pitcher_stats_df is not None
else statcast_df
)
name_index = _build_statcast_name_index(statcast_df)
runtime_cache: dict[str, Any] = {"name_index": name_index}
projected_starter_cache: dict[tuple[str, str, str], dict[str, Any]] = {}
batter_team_cache: dict[tuple[str, str, str, str], tuple[str, str]] = {}
pitcher_resolution_cache: dict[tuple[str, str, str, str], tuple[str, str, str]] = {}
pitcher_hand_cache: dict[str, tuple[Any, Any]] = {}
baseline_meta_cache: dict[tuple[int, str], dict[str, Any]] = {}
lineup_slot_cache: dict[tuple[str, str, str], tuple[Any, Any]] = {}
team_total_cache: dict[tuple[str, str, str, str], tuple[Any, Any]] = {}
mapped_rows: list[dict[str, Any]] = []
for _, row in hr_df.iterrows():
odds = row.get("odds_american")
batter_name_normalized = str(row.get("player_name") or "").strip()
batter_name = name_index.get(batter_name_normalized, batter_name_normalized)
threshold = row.get("threshold")
try:
threshold_int = int(threshold) if threshold is not None and str(threshold).strip() not in {"", "nan", "None"} else 1
except Exception:
threshold_int = 1
is_modeled = bool(row.get("is_modeled")) if pd.notna(row.get("is_modeled")) else threshold_int == 1
batter_team_key = (
str(row.get("away_team") or "").strip().lower(),
str(row.get("home_team") or "").strip().lower(),
str(batter_name or "").strip().lower(),
str(row.get("event_id") or "").strip(),
)
if batter_team_key not in batter_team_cache:
batter_team_cache[batter_team_key] = _resolve_batter_team(
row=row,
batter_name=batter_name,
batter_statcast_df=statcast_df,
)
batter_team, batter_team_source = batter_team_cache[batter_team_key]
starter_key = (
str(row.get("away_team") or "").strip().lower(),
str(row.get("home_team") or "").strip().lower(),
str(row.get("event_id") or "").strip(),
)
if starter_key not in projected_starter_cache:
projected_starter_cache[starter_key] = _lookup_projected_starter_context(
row=row,
probable_starters=probable_starters,
)
projected_starter_context = projected_starter_cache[starter_key]
pitcher_resolution_key = (
starter_key[0],
starter_key[1],
str(batter_team or "").strip().lower(),
str(row.get("pitcher_name") or row.get("pitcher") or "").strip().lower(),
)
if pitcher_resolution_key not in pitcher_resolution_cache:
pitcher_resolution_cache[pitcher_resolution_key] = _resolve_pitcher_name(
row=row,
batter_team=batter_team,
probable_starters=probable_starters,
)
pitcher_name, resolved_pitcher_source, pitcher_resolution_status = pitcher_resolution_cache[pitcher_resolution_key]
projected_starter_match_status = _projected_starter_match_status(
resolved_pitcher_name=pitcher_name,
projected_home_pitcher=str(projected_starter_context.get("projected_home_pitcher") or ""),
projected_away_pitcher=str(projected_starter_context.get("projected_away_pitcher") or ""),
)
pitcher_hand_key = str(pitcher_name or "").strip().lower()
if pitcher_hand_key not in pitcher_hand_cache:
pitcher_hand_cache[pitcher_hand_key] = _resolve_pitcher_hand(
pitcher_name=pitcher_name,
pitcher_statcast_df=pitcher_df,
)
pitcher_hand, pitcher_hand_source = pitcher_hand_cache[pitcher_hand_key]
batter_meta_key = (id(statcast_df), str(batter_name or "").strip().lower())
if batter_meta_key not in baseline_meta_cache:
baseline_meta_cache[batter_meta_key] = _lookup_baseline_metadata(statcast_df, batter_name)
batter_baseline_meta = baseline_meta_cache[batter_meta_key]
pitcher_meta_key = (id(pitcher_df), str(pitcher_name or "").strip().lower())
if pitcher_meta_key not in baseline_meta_cache:
baseline_meta_cache[pitcher_meta_key] = _lookup_baseline_metadata(pitcher_df, pitcher_name)
pitcher_baseline_meta = baseline_meta_cache[pitcher_meta_key]
lineup_slot_key = (
str(batter_team or "").strip().lower(),
str(batter_name or "").strip().lower(),
str(pitcher_hand or "").strip().upper(),
)
if lineup_slot_key not in lineup_slot_cache:
lineup_slot, lineup_slot_source = _infer_lineup_slot(
batter_name=batter_name,
batter_statcast_df=statcast_df,
)
lineup_slot_cache[lineup_slot_key] = (lineup_slot, lineup_slot_source)
lineup_slot, lineup_slot_source = lineup_slot_cache[lineup_slot_key]
team_total_key = (
str(row.get("away_team") or "").strip().lower(),
str(row.get("home_team") or "").strip().lower(),
str(batter_team or "").strip().lower(),
str(row.get("event_id") or "").strip(),
str(row.get("sportsbook") or "").strip().lower(),
str(row.get("team_total") or row.get("away_team_total") or row.get("home_team_total") or "").strip(),
)
if team_total_key not in team_total_cache:
team_total_cache[team_total_key] = _resolve_team_total(row=row, batter_team=batter_team)
team_total, team_total_source = team_total_cache[team_total_key]
try:
implied = american_to_implied_prob(odds) if odds is not None else None
except Exception:
implied = None
if is_modeled:
probability_result = build_hr_probability_result(
batter_statcast_df=statcast_df,
batter_name=batter_name,
pitcher_statcast_df=pitcher_df,
pitcher_name=pitcher_name,
game_row={
**_build_game_context_from_row(row),
"lineup_slot": lineup_slot,
"lineup_slot_source": lineup_slot_source,
"team_total": team_total,
"team_total_source": team_total_source,
"projected_home_pitcher": projected_starter_context.get("projected_home_pitcher"),
"projected_away_pitcher": projected_starter_context.get("projected_away_pitcher"),
"projected_starter_available": projected_starter_context.get("projected_starter_available"),
"projected_starter_match_status": projected_starter_match_status,
},
weather_row=None,
mode="pregame",
runtime_cache=runtime_cache,
)
model_prob = probability_result.get("calibrated_hr_prob")
if model_prob is not None and implied is not None:
edge = compute_edge(model_prob, implied)
bet_ev = compute_bet_ev(model_prob, odds) if odds is not None else None
source = "shared_pregame_engine"
else:
edge = None
bet_ev = None
source = "unavailable"
else:
probability_result = {
"baseline_hr_prob": None,
"raw_hr_prob": None,
"calibrated_hr_prob": None,
"pregame_hr_prob": None,
"mode": "pregame",
"applied_layers": "",
"skipped_layers": "unmodeled_hr_ladder",
"confidence_score": None,
"confidence_bucket": None,
"confidence_reasons": [],
"opportunity_hr_adjustment": None,
"expected_pa": None,
"pa_multiplier": None,
"lineup_slot_used": lineup_slot,
"lineup_slot_source": lineup_slot_source,
"team_total_used": team_total,
"team_total_source": team_total_source,
"opportunity_mode": None,
"opportunity_reason": None,
"pregame_pitcher_context_adj": None,
"pregame_park_context_adj": None,
"pregame_weather_context_adj": None,
"pregame_context_applied": False,
"pitcher_hr_adjustment": None,
"trend_hr_adjustment": None,
"zone_hr_adjustment": None,
"family_zone_hr_adjustment": None,
"arsenal_hr_adjustment": None,
"pulled_contact_hr_adjustment": None,
"env_hr_adjustment": None,
"park_hr_adjustment": None,
"weather_hr_adjustment": None,
"platoon_hr_adjustment": None,
"trajectory_hr_adjustment": None,
"rolling_hr_adjustment": None,
"pitcher_reliability": None,
"trend_reliability": None,
"zone_reliability": None,
"family_zone_reliability": None,
"arsenal_reliability": None,
"pulled_contact_reliability": None,
"environment_reliability": None,
"trajectory_reliability": None,
"rolling_reliability": None,
"opportunity_reliability": None,
"matchup_platoon_multiplier": None,
"matchup_platoon_reason": "unmodeled_hr_ladder",
"pitcher_resolution_status": "unmodeled_hr_ladder",
"zone_status": "unmodeled_hr_ladder",
"family_zone_status": "unmodeled_hr_ladder",
"arsenal_status": "unmodeled_hr_ladder",
"reason_candidate_count": 0,
"zone_store_sample_size": None,
"family_zone_batter_sample_size": None,
"family_zone_pitcher_sample_size": None,
"arsenal_batter_sample_size": None,
"arsenal_pitcher_sample_size": None,
"model_voice_reason_candidates": [],
"model_voice_tags": [],
}
model_prob = None
edge = None
bet_ev = None
source = "unmodeled_hr_ladder"
probability_status = _classify_hr_probability_status(
threshold_int=threshold_int,
is_modeled=is_modeled,
model_prob=model_prob,
implied=implied,
probability_result=probability_result,
statcast_df=statcast_df,
pitcher_name=pitcher_name,
)
row_dict = row.to_dict()
row_dict.update(
{
"implied_prob": implied,
"model_hr_prob": model_prob,
"fair_prob": model_prob,
"model_hr_prob_source": source,
"model_hr_prob_source_detail": probability_result.get("applied_layers", ""),
"edge": edge,
"bet_ev": bet_ev,
"baseline_hr_prob": probability_result.get("baseline_hr_prob"),
"raw_hr_prob": probability_result.get("raw_hr_prob"),
"calibrated_hr_prob": probability_result.get("calibrated_hr_prob"),
"pregame_hr_prob": probability_result.get("pregame_hr_prob"),
"probability_mode": probability_result.get("mode"),
"formula_version": probability_result.get("formula_version"),
"is_modeled": is_modeled,
"threshold": threshold_int,
"confidence_score": probability_result.get("confidence_score"),
"confidence_bucket": probability_result.get("confidence_bucket"),
"confidence_reasons": probability_result.get("confidence_reasons"),
"opportunity_hr_adjustment": probability_result.get("opportunity_hr_adjustment"),
"expected_pa": probability_result.get("expected_pa"),
"pa_multiplier": probability_result.get("pa_multiplier"),
"lineup_slot_used": probability_result.get("lineup_slot_used", lineup_slot),
"lineup_slot_source": probability_result.get("lineup_slot_source", lineup_slot_source),
"team_total_used": probability_result.get("team_total_used", team_total),
"team_total_source": probability_result.get("team_total_source", team_total_source),
"opportunity_mode": probability_result.get("opportunity_mode"),
"opportunity_reason": probability_result.get("opportunity_reason"),
"pregame_pitcher_context_adj": probability_result.get("pregame_pitcher_context_adj"),
"pregame_park_context_adj": probability_result.get("pregame_park_context_adj"),
"pregame_weather_context_adj": probability_result.get("pregame_weather_context_adj"),
"pregame_context_applied": probability_result.get("pregame_context_applied", False),
"pitcher_hr_adjustment": probability_result.get("pitcher_hr_adjustment"),
"trend_hr_adjustment": probability_result.get("trend_hr_adjustment"),
"zone_hr_adjustment": probability_result.get("zone_hr_adjustment"),
"family_zone_hr_adjustment": probability_result.get("family_zone_hr_adjustment"),
"arsenal_hr_adjustment": probability_result.get("arsenal_hr_adjustment"),
"pulled_contact_hr_adjustment": probability_result.get("pulled_contact_hr_adjustment"),
"env_hr_adjustment": probability_result.get("env_hr_adjustment"),
"park_hr_adjustment": probability_result.get("park_hr_adjustment"),
"weather_hr_adjustment": probability_result.get("weather_hr_adjustment"),
"platoon_hr_adjustment": probability_result.get("platoon_hr_adjustment"),
"trajectory_hr_adjustment": probability_result.get("trajectory_hr_adjustment"),
"rolling_hr_adjustment": probability_result.get("rolling_hr_adjustment"),
"damage_zone_alignment_subscore": probability_result.get("damage_zone_alignment_subscore"),
"pitch_mix_exposure_subscore": probability_result.get("pitch_mix_exposure_subscore"),
"tunnel_damage_subscore": probability_result.get("tunnel_damage_subscore"),
"count_pattern_damage_subscore": probability_result.get("count_pattern_damage_subscore"),
"handedness_damage_subscore": probability_result.get("handedness_damage_subscore"),
"arsenal_fit_subscore": probability_result.get("arsenal_fit_subscore"),
"environment_amplification_subscore": probability_result.get("environment_amplification_subscore"),
"hr_opportunity_projection": probability_result.get("hr_opportunity_projection"),
"matchup_coverage_confidence": probability_result.get("matchup_coverage_confidence"),
"component_source_map": probability_result.get("component_source_map"),
"expected_pitch_mix_by_count": probability_result.get("expected_pitch_mix_by_count"),
"expected_zone_mix_by_count": probability_result.get("expected_zone_mix_by_count"),
"expected_pitch_zone_mix_by_count": probability_result.get("expected_pitch_zone_mix_by_count"),
"tunnel_pair_scores": probability_result.get("tunnel_pair_scores"),
"predicted_attack_regions": probability_result.get("predicted_attack_regions"),
"predicted_damage_regions": probability_result.get("predicted_damage_regions"),
"predicted_whiff_regions": probability_result.get("predicted_whiff_regions"),
"pitcher_reliability": probability_result.get("pitcher_reliability"),
"trend_reliability": probability_result.get("trend_reliability"),
"zone_reliability": probability_result.get("zone_reliability"),
"family_zone_reliability": probability_result.get("family_zone_reliability"),
"arsenal_reliability": probability_result.get("arsenal_reliability"),
"pulled_contact_reliability": probability_result.get("pulled_contact_reliability"),
"environment_reliability": probability_result.get("environment_reliability"),
"trajectory_reliability": probability_result.get("trajectory_reliability"),
"rolling_reliability": probability_result.get("rolling_reliability"),
"opportunity_reliability": probability_result.get("opportunity_reliability"),
"applied_layers": probability_result.get("applied_layers"),
"skipped_layers": probability_result.get("skipped_layers"),
"matchup_platoon_multiplier": probability_result.get("matchup_platoon_multiplier"),
"matchup_platoon_reason": probability_result.get("matchup_platoon_reason"),
"resolved_pitcher_name": pitcher_name,
"projected_home_pitcher": projected_starter_context.get("projected_home_pitcher"),
"projected_away_pitcher": projected_starter_context.get("projected_away_pitcher"),
"projected_starter_available": projected_starter_context.get("projected_starter_available"),
"projected_starter_source": projected_starter_context.get("projected_starter_source"),
"projected_home_pitcher_source": projected_starter_context.get("projected_home_pitcher_source"),
"projected_away_pitcher_source": projected_starter_context.get("projected_away_pitcher_source"),
"starter_cache_source": projected_starter_context.get("starter_cache_source"),
"fallback_used": projected_starter_context.get("fallback_used"),
"projected_starter_match_status": projected_starter_match_status,
"batter_team": batter_team,
"batter_team_source": batter_team_source,
"resolved_pitcher_source": resolved_pitcher_source,
"pitcher_resolution_status": probability_result.get("pitcher_resolution_status", pitcher_resolution_status),
"pitcher_hand": pitcher_hand,
"pitcher_hand_source": pitcher_hand_source,
"zone_status": probability_result.get("zone_status"),
"family_zone_status": probability_result.get("family_zone_status"),
"arsenal_status": probability_result.get("arsenal_status"),
"reason_candidate_count": probability_result.get("reason_candidate_count"),
"zone_store_sample_size": probability_result.get("zone_store_sample_size"),
"family_zone_batter_sample_size": probability_result.get("family_zone_batter_sample_size"),
"family_zone_pitcher_sample_size": probability_result.get("family_zone_pitcher_sample_size"),
"arsenal_batter_sample_size": probability_result.get("arsenal_batter_sample_size"),
"arsenal_pitcher_sample_size": probability_result.get("arsenal_pitcher_sample_size"),
"model_voice_reason_candidates": probability_result.get("model_voice_reason_candidates", []),
"model_voice_tags": probability_result.get("model_voice_tags", []),
"selection_scope": row.get("selection_scope") or "player",
"expected_modeled_hr_row": bool(threshold_int == 1 and str(row.get("market_family") or row.get("market") or "").strip().lower() == "hr"),
"has_model_probability": model_prob is not None,
"has_modeled_edge": edge is not None,
"model_probability_status": probability_status,
"modeled_row_available": model_prob is not None,
"modeled_row_missing_reason": None if model_prob is not None else probability_status,
"baseline_mode": batter_baseline_meta.get("baseline_mode"),
"prior_sample_size": batter_baseline_meta.get("prior_sample_size"),
"season_2026_sample_size": batter_baseline_meta.get("season_2026_sample_size"),
"prior_weight": batter_baseline_meta.get("prior_weight"),
"season_2026_weight": batter_baseline_meta.get("season_2026_weight"),
"baseline_driver": batter_baseline_meta.get("baseline_driver"),
"rolling_overlay_active": batter_baseline_meta.get("rolling_overlay_active"),
"pitcher_baseline_mode": pitcher_baseline_meta.get("baseline_mode"),
"pitcher_prior_sample_size": pitcher_baseline_meta.get("prior_sample_size"),
"pitcher_season_2026_sample_size": pitcher_baseline_meta.get("season_2026_sample_size"),
"pitcher_prior_weight": pitcher_baseline_meta.get("prior_weight"),
"pitcher_season_2026_weight": pitcher_baseline_meta.get("season_2026_weight"),
"pitcher_baseline_driver": pitcher_baseline_meta.get("baseline_driver"),
"pitcher_rolling_overlay_active": pitcher_baseline_meta.get("rolling_overlay_active"),
}
)
row_dict["verdict"] = _compute_verdict(
bet_ev=bet_ev,
edge=edge,
confidence_score=row_dict.get("confidence_score"),
is_modeled=is_modeled,
)
row_dict.update(build_hr_model_voice(row_dict))
mapped_rows.append(row_dict)
result = pd.DataFrame(mapped_rows)
if result.empty:
return result
has_edge = result["edge"].notna()
with_edge = result[has_edge].sort_values("edge", ascending=False)
without_edge = result[~has_edge]
ordered = pd.concat([with_edge, without_edge], ignore_index=True)
try:
from analytics.execution_layer import enrich_with_execution_layer
return enrich_with_execution_layer(ordered)
except Exception:
return ordered
def map_strikeout_props_to_model(
props_df: pd.DataFrame,
batter_statcast_df: pd.DataFrame,
pitcher_statcast_df: pd.DataFrame | None = None,
probable_starters: dict | None = None,
) -> pd.DataFrame:
if props_df.empty:
return pd.DataFrame()
k_df = props_df[props_df["market"].astype(str).str.lower() == "k"].copy()
if k_df.empty:
return pd.DataFrame()
pitcher_df = pitcher_statcast_df if pitcher_statcast_df is not None else batter_statcast_df
runtime_cache: dict[str, Any] = {}
projected_starter_cache: dict[tuple[str, str, str], dict[str, Any]] = {}
pitcher_resolution_cache: dict[tuple[str, str, str], tuple[str, str, str]] = {}
team_context_cache: dict[tuple[str, str, str], tuple[str, str]] = {}
pitcher_hand_cache: dict[str, tuple[Any, Any]] = {}
baseline_meta_cache: dict[tuple[int, str], dict[str, Any]] = {}
lineup_cache: dict[str, list[str]] = {}
strikeout_probability_cache: dict[tuple[Any, ...], dict[str, Any]] = {}
mapped_rows: list[dict[str, Any]] = []
for _, row in k_df.iterrows():
line = row.get("line")
selection_side = str(row.get("selection_side") or "").strip().lower()
try:
implied = american_to_implied_prob(row.get("odds_american")) if row.get("odds_american") is not None else None
except Exception:
implied = None
starter_key = (
str(row.get("away_team") or "").strip().lower(),
str(row.get("home_team") or "").strip().lower(),
str(row.get("event_id") or "").strip(),
)
if starter_key not in projected_starter_cache:
projected_starter_cache[starter_key] = _lookup_projected_starter_context(
row=row,
probable_starters=probable_starters,
)
projected_starter_context = projected_starter_cache[starter_key]
pitcher_resolution_key = (
starter_key[0],
starter_key[1],
str(row.get("pitcher_name") or row.get("pitcher") or row.get("player_name") or "").strip().lower(),
)
if pitcher_resolution_key not in pitcher_resolution_cache:
pitcher_resolution_cache[pitcher_resolution_key] = _resolve_strikeout_pitcher_name(
row=row,
probable_starters=probable_starters,
)
pitcher_name, resolved_pitcher_source, pitcher_resolution_status = pitcher_resolution_cache[pitcher_resolution_key]
if pitcher_resolution_key not in team_context_cache:
team_context_cache[pitcher_resolution_key] = _resolve_pitcher_team_and_opponent(
row=row,
pitcher_name=pitcher_name,
probable_starters=probable_starters,
)
pitcher_team, opponent_team = team_context_cache[pitcher_resolution_key]
projected_starter_match_status = _projected_starter_match_status(
resolved_pitcher_name=pitcher_name,
projected_home_pitcher=str(projected_starter_context.get("projected_home_pitcher") or ""),
projected_away_pitcher=str(projected_starter_context.get("projected_away_pitcher") or ""),
)
pitcher_hand_key = str(pitcher_name or "").strip().lower()
if pitcher_hand_key not in pitcher_hand_cache:
pitcher_hand_cache[pitcher_hand_key] = _resolve_pitcher_hand(pitcher_name=pitcher_name, pitcher_statcast_df=pitcher_df)
pitcher_hand, _ = pitcher_hand_cache[pitcher_hand_key]
pitcher_meta_key = (id(pitcher_df), str(pitcher_name or "").strip().lower())
if pitcher_meta_key not in baseline_meta_cache:
baseline_meta_cache[pitcher_meta_key] = _lookup_baseline_metadata(pitcher_df, pitcher_name)
pitcher_baseline_meta = baseline_meta_cache[pitcher_meta_key]
lineup_key = str(opponent_team or "").strip().lower()
if lineup_key not in lineup_cache:
lineup_cache[lineup_key] = _extract_team_batters_from_statcast(
team_name=opponent_team,
batter_statcast_df=batter_statcast_df,
)
opponent_batters = lineup_cache[lineup_key]
canonical_game_row = _build_game_context_from_row(row)
canonical_game_row.update(
{
"projected_home_pitcher": projected_starter_context.get("projected_home_pitcher"),
"projected_away_pitcher": projected_starter_context.get("projected_away_pitcher"),
"projected_starter_available": projected_starter_context.get("projected_starter_available"),
"projected_starter_source": projected_starter_context.get("projected_starter_source"),
"projected_home_pitcher_source": projected_starter_context.get("projected_home_pitcher_source"),
"projected_away_pitcher_source": projected_starter_context.get("projected_away_pitcher_source"),
"starter_cache_source": projected_starter_context.get("starter_cache_source"),
"fallback_used": projected_starter_context.get("fallback_used"),
"projected_starter_match_status": projected_starter_match_status,
"resolved_pitcher_name": pitcher_name,
"resolved_pitcher_source": resolved_pitcher_source,
"pitcher_resolution_status": pitcher_resolution_status,
"pitcher_team": pitcher_team,
"opponent_team": opponent_team,
}
)
line_value = float(line) if line is not None and str(line).strip() not in {"", "nan", "None"} else None
probability_cache_key = (
str(pitcher_name or "").strip().lower(),
tuple(str(name or "").strip().lower() for name in opponent_batters),
str(opponent_team or "").strip().lower(),
line_value,
str(selection_side or "").strip().lower(),
str(canonical_game_row.get("away_team") or "").strip().lower(),
str(canonical_game_row.get("home_team") or "").strip().lower(),
str(canonical_game_row.get("projected_starter_match_status") or "").strip().lower(),
)
if probability_cache_key not in strikeout_probability_cache:
strikeout_probability_cache[probability_cache_key] = build_strikeout_probability_result_v2(
pitcher_statcast_df=pitcher_df,
pitcher_name=pitcher_name,
batter_statcast_df=batter_statcast_df,
opponent_batters=opponent_batters,
opponent_team=opponent_team,
line=line_value,
selection_side=selection_side,
game_row=canonical_game_row,
runtime_cache=runtime_cache,
)
probability_result_v2 = strikeout_probability_cache[probability_cache_key]
confidence_payload = _build_strikeout_confidence_payload(
probability_result=probability_result_v2,
)
fair_prob = probability_result_v2.get("fair_prob")
probability_status = _classify_strikeout_probability_status(
fair_prob=fair_prob,
implied=implied,
pitcher_name=pitcher_name,
probability_result={
**probability_result_v2,
"pitcher_resolution_status": pitcher_resolution_status,
"projected_starter_match_status": projected_starter_match_status,
},
)
if fair_prob is not None and implied is not None:
edge = compute_edge(fair_prob, implied)
bet_ev = compute_bet_ev(fair_prob, row.get("odds_american")) if row.get("odds_american") is not None else None
source = "shared_strikeout_engine_v2"
is_modeled = True
else:
edge = None
bet_ev = None
source = "unavailable"
is_modeled = False
row_dict = row.to_dict()
row_dict.update(
{
"selection_scope": row.get("selection_scope") or "pitcher",
"is_modeled": is_modeled,
"implied_prob": implied,
"fair_prob": fair_prob,
"model_k_prob": fair_prob,
"bet_ev": bet_ev,
"edge": edge,
"confidence_score": confidence_payload.get("confidence_score_display"),
"confidence_bucket": confidence_payload.get("confidence_bucket_display"),
"confidence_reasons": confidence_payload.get("confidence_reasons"),
"confidence_score_raw": confidence_payload.get("confidence_score_raw"),
"confidence_score_display": confidence_payload.get("confidence_score_display"),
"confidence_source": confidence_payload.get("confidence_source"),
"confidence_component_bonuses": confidence_payload.get("confidence_component_bonuses"),
"confidence_component_penalties": confidence_payload.get("confidence_component_penalties"),
"confidence_primary_driver": confidence_payload.get("confidence_primary_driver"),
"confidence_summary_label": confidence_payload.get("confidence_summary_label"),
"confidence_bucket_raw": confidence_payload.get("confidence_bucket_raw"),
"confidence_bucket_display": confidence_payload.get("confidence_bucket_display"),
"expected_strikeouts": probability_result_v2.get("expected_strikeouts"),
"expected_strikeouts_v2": probability_result_v2.get("expected_strikeouts_v2"),
"projected_pitch_count": probability_result_v2.get("projected_pitch_count"),
"projected_batters_faced": probability_result_v2.get("projected_batters_faced"),
"projected_innings": probability_result_v2.get("projected_innings"),
"pitches_per_bf": probability_result_v2.get("pitches_per_bf"),
"opportunity_confidence": probability_result_v2.get("opportunity_confidence"),
"opportunity_reasons": probability_result_v2.get("opportunity_reasons"),
"projected_k_rate": probability_result_v2.get("projected_k_rate"),
"fair_prob_v2": probability_result_v2.get("fair_prob_v2"),
"raw_k_prob_v2": probability_result_v2.get("raw_k_prob_v2"),
"calibrated_k_prob_v2": probability_result_v2.get("calibrated_k_prob_v2"),
"confidence_score_v2": probability_result_v2.get("confidence_score_v2"),
"confidence_score_raw_v2": probability_result_v2.get("confidence_score_raw_v2"),
"confidence_score_display_v2": probability_result_v2.get("confidence_score_display_v2"),
"confidence_source_v2": probability_result_v2.get("confidence_source_v2"),
"confidence_bucket_v2": probability_result_v2.get("confidence_bucket_v2"),
"confidence_reasons_v2": probability_result_v2.get("confidence_reasons_v2"),
"confidence_component_bonuses_v2": probability_result_v2.get("confidence_component_bonuses_v2"),
"confidence_component_penalties_v2": probability_result_v2.get("confidence_component_penalties_v2"),
"confidence_primary_driver_v2": probability_result_v2.get("confidence_primary_driver_v2"),
"confidence_summary_label_v2": probability_result_v2.get("confidence_summary_label_v2"),
"k_rate_pitch_signal": probability_result_v2.get("k_rate_pitch_signal"),
"k_rate_anchor": probability_result_v2.get("k_rate_anchor"),
"bb_rate_anchor": probability_result_v2.get("bb_rate_anchor"),
"command_efficiency_signal": probability_result_v2.get("command_efficiency_signal"),
"swing_miss_subscore": probability_result_v2.get("swing_miss_subscore"),
"called_strike_subscore": probability_result_v2.get("called_strike_subscore"),
"command_efficiency_subscore": probability_result_v2.get("command_efficiency_subscore"),
"lineup_whiff_subscore": probability_result_v2.get("lineup_whiff_subscore"),
"zone_matchup_subscore": probability_result_v2.get("zone_matchup_subscore"),
"family_zone_matchup_subscore": probability_result_v2.get("family_zone_matchup_subscore"),
"arsenal_fit_subscore": probability_result_v2.get("arsenal_fit_subscore"),
"tunneling_subscore": probability_result_v2.get("tunneling_subscore"),
"release_consistency_subscore": probability_result_v2.get("release_consistency_subscore"),
"sequencing_subscore": probability_result_v2.get("sequencing_subscore"),
"count_leverage_subscore": probability_result_v2.get("count_leverage_subscore"),
"leash_risk_subscore": probability_result_v2.get("leash_risk_subscore"),
"role_certainty_score": probability_result_v2.get("role_certainty_score"),
"times_through_order_penalty": probability_result_v2.get("times_through_order_penalty"),
"telemetry_path_status": probability_result_v2.get("telemetry_path_status"),
"model_tier": probability_result_v2.get("model_tier"),
"variance_band_low": probability_result_v2.get("variance_band_low"),
"variance_band_high": probability_result_v2.get("variance_band_high"),
"matchup_coverage_confidence": probability_result_v2.get("matchup_coverage_confidence"),
"component_source_map": probability_result_v2.get("component_source_map"),
"predicted_whiff_regions": probability_result_v2.get("predicted_whiff_regions"),
"predicted_attack_regions": probability_result_v2.get("predicted_attack_regions"),
"predicted_damage_regions": probability_result_v2.get("predicted_damage_regions"),
"tunnel_pair_scores": probability_result_v2.get("tunnel_pair_scores"),
"formula_version": probability_result_v2.get("formula_version"),
"pitcher_swstr_rate": probability_result_v2.get("pitcher_swstr_rate"),
"pitcher_csw_rate": probability_result_v2.get("pitcher_csw_rate"),
"pitcher_ball_rate": probability_result_v2.get("pitcher_ball_rate"),
"arsenal_whiff_risk": probability_result_v2.get("arsenal_fit_subscore"),
"family_zone_whiff_risk": probability_result_v2.get("family_zone_matchup_subscore"),
"zone_whiff_risk": probability_result_v2.get("zone_matchup_subscore"),
"trajectory_tunnel_score": probability_result_v2.get("tunneling_subscore"),
"trajectory_release_consistency_score": probability_result_v2.get("release_consistency_subscore"),
"sequencing_score": probability_result_v2.get("sequencing_subscore"),
"applied_layers": probability_result_v2.get("applied_layers"),
"skipped_layers": probability_result_v2.get("skipped_layers"),
"model_k_prob_source": source,
"model_k_prob_source_detail": probability_result_v2.get("applied_layers", ""),
"resolved_pitcher_name": pitcher_name,
"resolved_pitcher_source": resolved_pitcher_source,
"projected_home_pitcher": projected_starter_context.get("projected_home_pitcher"),
"projected_away_pitcher": projected_starter_context.get("projected_away_pitcher"),
"projected_starter_available": projected_starter_context.get("projected_starter_available"),
"projected_starter_source": projected_starter_context.get("projected_starter_source"),
"projected_home_pitcher_source": projected_starter_context.get("projected_home_pitcher_source"),
"projected_away_pitcher_source": projected_starter_context.get("projected_away_pitcher_source"),
"starter_cache_source": projected_starter_context.get("starter_cache_source"),
"fallback_used": projected_starter_context.get("fallback_used"),
"projected_starter_match_status": projected_starter_match_status,
"pitcher_resolution_status": pitcher_resolution_status,
"pitcher_team": pitcher_team,
"opponent_team": opponent_team,
"has_model_probability": fair_prob is not None,
"has_modeled_edge": edge is not None,
"model_probability_status": probability_status,
"modeled_row_available": fair_prob is not None,
"modeled_row_missing_reason": None if fair_prob is not None else probability_status,
"baseline_mode": pitcher_baseline_meta.get("baseline_mode"),
"prior_sample_size": pitcher_baseline_meta.get("prior_sample_size"),
"season_2026_sample_size": pitcher_baseline_meta.get("season_2026_sample_size"),
"prior_weight": pitcher_baseline_meta.get("prior_weight"),
"season_2026_weight": pitcher_baseline_meta.get("season_2026_weight"),
"baseline_driver": pitcher_baseline_meta.get("baseline_driver"),
"rolling_overlay_active": pitcher_baseline_meta.get("rolling_overlay_active"),
}
)
row_dict["verdict"] = _compute_verdict(
bet_ev=bet_ev,
edge=edge,
confidence_score=row_dict.get("confidence_score"),
is_modeled=is_modeled,
)
row_dict.update(build_strikeout_model_voice(row_dict))
mapped_rows.append(row_dict)
return pd.DataFrame(mapped_rows)
def map_no_home_run_props(
props_df: pd.DataFrame,
) -> pd.DataFrame:
if props_df.empty:
return pd.DataFrame()
no_hr_df = props_df[props_df["market_family"].astype(str).str.lower() == "no_hr"].copy()
if no_hr_df.empty:
return pd.DataFrame()
for idx, row in no_hr_df.iterrows():
implied = american_to_implied_prob(row.get("odds_american")) if row.get("odds_american") is not None else None
no_hr_df.at[idx, "selection_scope"] = "game"
no_hr_df.at[idx, "implied_prob"] = implied
no_hr_df.at[idx, "fair_prob"] = None
no_hr_df.at[idx, "edge"] = None
no_hr_df.at[idx, "bet_ev"] = None
no_hr_df.at[idx, "confidence_score"] = None
no_hr_df.at[idx, "confidence_bucket"] = None
no_hr_df.at[idx, "confidence_reasons"] = ["No-HR fair probability model not active yet"]
no_hr_df.at[idx, "verdict"] = "tracked"
no_hr_df.at[idx, "model_voice_for"] = "Market is tracked for future release"
no_hr_df.at[idx, "model_voice_against"] = "No-HR fair probability model is not active yet"
return no_hr_df
def map_props_to_models(
props_df: pd.DataFrame,
statcast_df: pd.DataFrame,
pitcher_statcast_df: pd.DataFrame | None = None,
probable_starters: dict | None = None,
) -> pd.DataFrame:
frames: list[pd.DataFrame] = []
hr_df = map_hr_props_to_model(
props_df,
statcast_df,
pitcher_statcast_df=pitcher_statcast_df,
probable_starters=probable_starters,
)
if not hr_df.empty:
frames.append(hr_df)
k_df = map_strikeout_props_to_model(
props_df,
batter_statcast_df=statcast_df,
pitcher_statcast_df=pitcher_statcast_df,
probable_starters=probable_starters,
)
if not k_df.empty:
frames.append(k_df)
no_hr_df = map_no_home_run_props(props_df)
if not no_hr_df.empty:
frames.append(no_hr_df)
if not frames:
return pd.DataFrame()
return pd.concat(frames, ignore_index=True, sort=False)
|