NBA — additional Python functions — Models and calculators: walk_forward–wp_variants
walk_forward
walk_forward(model: 'AnyModel', possessions: 'pl.DataFrame', *, checkpoint_dates: 'Optional[List[datetime.date]]' = None, horizon_days: 'int' = 14, min_games_before_first_checkpoint: 'int' = 15) -> 'WalkForwardResult'
Oracle 6: time-ordered "predict tomorrow" retrodiction.
For each checkpoint date D: fit on games with game_date <= D, predict
games with D < game_date <= D + horizon_days, aggregate to
per-(game, team) margins -- reusing _fit_on / _design_with_ids
/ predict_points / _team_game_margins (the same machinery
retrodiction uses). carry_forward_rmse reapplies the PREVIOUS
checkpoint's fit (no refit) to the current window. random_fold_rmse is
retrodiction's pooled game-margin RMSE on the same possessions.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
model | AnyModel | A harness model (RapmModel/RatingsModel/PriorModel). | |
possessions | DataFrame | A season possession+lineup frame with game_date (from compile_nba_season), game_id, offense_team_id, points, and the ten lineup columns. | |
checkpoint_dates | Optional[List[date]] | None | Explicit checkpoint grid; derived from possessions via horizon_days/min_games_before_first_checkpoint when None (default). |
horizon_days | int | 14 | Days-ahead prediction window per checkpoint (default 14). |
min_games_before_first_checkpoint | int | 15 | Distinct-game-date index of the first checkpoint when deriving the default grid (default 15, "~game 15 of the season"). |
Returns
WalkForwardResult. All metrics nan and counts 0 when possessions is empty, lacks a game_date column, or the derived/given grid produces zero non-degenerate checkpoints.
Example
from sportsdataverse.nba.nba_model_validation import RidgeRapmModel, walk_forward
res = walk_forward(RidgeRapmModel(), season_possessions)
print(res.game_margin_rmse, res.carry_forward_rmse, res.random_fold_rmse)
win_prob_from_margin
win_prob_from_margin(exp_margin: 'float', *, league_id: 'str' = '00') -> 'float'
Home win probability from an expected margin (normal-CDF closed form).
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
exp_margin | float | Expected home-minus-away margin in points. | |
league_id | str | '00' | "00"/"10"/"20" -- selects the fitted margin sigma. |
Returns
Probability the home team wins, in (0, 1).
Example
from sportsdataverse.nba.nba_game_predict import win_prob_from_margin
win_prob_from_margin(5.0)
wp_variants
wp_variants(pbp: 'pl.DataFrame', market_home_prob: 'float | None' = None, *, league: 'str | None' = None, regulation_periods: 'int | None' = None, neutral_site: 'bool | None' = False) -> 'pl.DataFrame'
Two extra per-play home WP columns for one game: naive (prior 0.5) and market-anchored.
Both rescore the plays with ~sportsdataverse.nba.nba_game_page.score_win_prob, the same
in-game model as home_win_prob, with only the pregame prior swapped: 0.5 for
home_win_prob_naive (the game state alone: score, clock and possession), market_home_prob for
home_win_prob_market. End-of-game rows settle to 1 / 0 as in score_win_prob.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pbp | DataFrame | One game in the basketball pbp schema, as score_win_prob takes it. | |
market_home_prob | float | None | None | The game's pregame no-vig market home WP (game_lines market_home_wp_close); None or NaN leaves home_win_prob_market null. |
league | str | None | None | "nba", "wnba", "mbb" or "wbb" (default NBA). |
regulation_periods | int | None | None | The game's regulation periods; see score_win_prob. |
neutral_site | bool | None | False | Passed to score_win_prob (it does not change a given prior). |
Returns
game_id (when present), game_play_number (when present), home_win_prob_naive and home_win_prob_market, one row per play in play order; empty with those columns for an empty pbp.
| col_name | type | description |
|---|---|---|
game_id | Int32 / Int64 | ESPN game id, the join key to the play-by-play and the play_wp table (the dtype the pbp carries). |
game_play_number | Int32 / Int64 | Play order within the game, as in the play-by-play (the dtype the pbp carries). |
home_win_prob_naive | Float64 | Home win probability from the game state alone (score, clock and possession), the in-game model with a 0.5 prior. |
home_win_prob_market | Float64 | Home win probability with the closing no-vig market WP as the prior; null without a line. |
Raises
| Exception | Description |
|---|---|
ValueError | If the league is unknown, market_home_prob is outside [0, 1], or pbp holds more than one game. |
polars.exceptions.ColumnNotFoundError | If a column score_win_prob needs is missing. |
Example
import polars as pl
from sportsdataverse.nba.nba_game_lines import wp_variants
from sportsdataverse.nba.nba_loaders import load_nba_pbp
one = load_nba_pbp([2025]).filter(pl.col("game_id") == 401704638)
wp = wp_variants(one, 0.31, league="nba")
# Where the market prior moved the curve most
wp.with_columns((pl.col("home_win_prob_market") - pl.col("home_win_prob_naive")).alias("d")).sort("d")