Skip to main content

Men's college basketball — additional Python functions — Models and calculators: build_player–mbb_team

build_player_context​

build_player_context(players: 'list[PlayerOnOffStats]', lineups: 'list[LineupStatSet]', players_baseline: 'dict[PlayerId, IndivStatSet]', stats_averages: 'PureStatSet', avg_efficiency: 'float', agg_value_key: 'ValueKey' = 'value', config: 'RapmConfig' = {'prior_mode': -1, 'removal_pct': 0.06, 'fixed_regression': -1}) -> 'RapmPlayerContext'

Build the context object the RAPM matrix-solve layer consumes.

Faithful port of RapmUtils.buildPlayerContext (RapmUtils.ts:427-541). Removes low-possession players (config["removal_pct"] of total on+off possessions), flags fully-removed lineups (mutating lineups in place -- see the module docstring's landmine 5), builds the player-to-column index, and folds build_priors into prior_info.

Parameters

ParameterTypeDefaultDescription
playerslist[PlayerOnOffStats]The per-player on/off splits (PlayerOnOffStats), e.g. mbb_lineup_stats.lineup_to_team_report(...)["players"].
lineupslist[LineupStatSet]The per-lineup LineupStatSet docs feeding this team's aggregate (mutated in place -- see landmine 5).
players_baselinedict[PlayerId, IndivStatSet]{player_id: IndivStatSet} -- forwarded to build_priors unchanged.
stats_averagesPureStatSetLeague/context average stat set -- forwarded to build_priors unchanged.
avg_efficiencyfloatLeague/context average efficiency.
agg_value_keyValueKey'value'"value" or "old_value" -- forwarded to build_priors as its value_key (only affects prior calculations, not the lineup-filtering/aggregation above it).
configRapmConfig{'prior_mode': -1, 'removal_pct': 0.06, 'fixed_regression': -1}Removal-percent / prior-mode / regression config. Defaults to DEFAULT_RAPM_CONFIG; never mutated by this function (only config["removal_pct"]/config["prior_mode"] are read), matching the TS default parameter's own read-only usage.

Returns

A RapmPlayerContext.

Raises

ExceptionDescription
ZeroDivisionErrorIf total_lineups (derived from players[0]'s on/off possession totals) is exactly 0 -- see the module docstring's landmine 2. Not exercised by this task's oracle fixtures.

Example

from sportsdataverse.mbb.mbb_lineup_stats import lineup_to_team_report
from sportsdataverse.mbb.mbb_rapm import build_player_context, DEFAULT_RAPM_CONFIG

report = lineup_to_team_report({"lineups": buckets, "error_code": None})
ctx = build_player_context(
report["players"], buckets, {}, {}, 100.0, "value", DEFAULT_RAPM_CONFIG
)
print(ctx["num_players"], ctx["team_info"]["off_poss"]["value"])

# Filtering lineups by side (the ``filtered_lineups`` closure)

off_lineups = ctx["filtered_lineups"]("off")
def_lineups = ctx["filtered_lineups"]("def")

build_priors​

build_priors(players_baseline: 'dict[PlayerId, IndivStatSet]', stats_averages: 'PureStatSet', avg_efficiency: 'float', col_to_player: 'list[str]', prior_mode: 'float', value_key: 'ValueKey' = 'value') -> 'RapmPriorInfo'

Build strong/weak per-player RAPM priors for every column.

Faithful port of RapmUtils.buildPriors (RapmUtils.ts:237-407). See the module docstring's landmine list, item 1, for the critical Python-vs-JS {}-truthiness gotcha this function's implementation deliberately avoids.

Parameters

ParameterTypeDefaultDescription
players_baselinedict[PlayerId, IndivStatSet]{player_id: IndivStatSet} -- the most-general per-player baseline info (in production, sourced from mbb_ratings.build_productivity's output; see the module docstring's "RAPM prior source" note).
stats_averagesPureStatSetLeague/context average stat set, used by the (currently dead-code, see landmine 4) get_prior_basis fallback and by with_avg_or_undef's nil-check gate.
avg_efficiencyfloatLeague/context average efficiency.
col_to_playerlist[str]The player ids, in column order -- playersStrong/ playersWeak are index-aligned with this list.
prior_modefloat-1 for adaptive mode, -2 (or lower) for no prior, 0-1 for a fixed strong-prior weight.
value_keyValueKey'value'"value" or "old_value" -- allows priors to be built from luck-adjusted parameters.

Returns

A RapmPriorInfo.

Example

from sportsdataverse.mbb.mbb_rapm import build_priors

priors = build_priors({}, {}, 100.0, ["Wiggins, Aaron"], -1)
print(priors["players_weak"][0])

build_productivity​

build_productivity(o_rtg: 'float', o_adj: 'float', usage: 'float', avg_efficiency: 'float') -> 'dict[str, float]'

Public port of RatingUtils.buildProductivity (RatingUtils.ts:963-990).

Promoted to public -- see the module docstring's "Ported behavior" section for the promotion rationale (RAPM needs to import this across module boundaries).

Converts ORtg and a few other numbers into "productivity" using Dean Oliver's PUE ("Player Usage Efficiency") formulation, SoS-adjusted via o_adj = avgEfficiency / Def_SOS. RAPM prior source: Adj_ORtgPlus is the value RAPM uses as an individual-offense prior.

Parameters

ParameterTypeDefaultDescription
o_rtgfloatThe player's (possibly override-adjusted) ORtg.
o_adjfloatavg_efficiency / Def_SOS -- the strength-of-schedule adjustment factor.
usagefloat100 * TotPoss / (Team_Poss or 1) -- the player's possession-usage percentage.
avg_efficiencyfloatThe league/context average efficiency (100 in every vendored jest call).

Returns

{"Adj_ORtg": float, "Adj_ORtgPlus": float, "Usage_Bonus": float, "SoS_Bonus": float} -- keys kept TS-verbatim (see module docstring's naming-convention note).

build_weak_prior_from_rapm​

build_weak_prior_from_rapm(rapm_results: 'list[float]', off_or_def: 'str') -> 'list[dict[str, float]]'

Wrap a flat RAPM-estimate vector into playersWeak-shaped dicts.

Faithful port of RapmUtils.buildWeakPriorFromRapm (RapmUtils.ts:410-419), used only by pick_ridge_regression's use_recursive_weak_prior branch to substitute the just-computed (pre-strong-prior) RAPM values as the weak prior for a follow-up apply_weak_priors call -- "the recursive prior" per the upstream /** For "recursive" prior */ comment.

Uncovered by the oracle -- semiRealRapmResults.testContext.priorInfo .useRecursiveWeakPrior is false, so RapmUtils.test.ts's "pickRidgeRegression" test never calls this function. Ported faithfully from TS regardless (per "TS governs"); flagged as a documented gap rather than backed by a synthetic test, matching this module's existing convention for other upstream-untested branches.

Parameters

ParameterTypeDefaultDescription
rapm_resultslist[float]A flat per-player RAPM estimate vector, e.g. pick_ridge_regression's own results_pre_prior.
off_or_defstr"off" or "def" -- selects the output key, f"{off_or_def}_adj_ppp".

Returns

One {f"{off_or_def}_adj_ppp": rapm} dict per input element, index-aligned with rapm_results.

Example

from sportsdataverse.mbb.mbb_rapm import build_weak_prior_from_rapm

weak_prior = build_weak_prior_from_rapm([5.0, 4.5], "off")
print(weak_prior[0]) # {"off_adj_ppp": 5.0}

calc_collinearity_diag​

calc_collinearity_diag(weight_matrix: 'NDArray[np.float64]', ctx: 'RapmPlayerContext') -> 'RapmPreProcDiagnostics'

Multi-collinearity diagnostic between the players in an off/def design matrix.

Faithful port of RapmUtils.calcCollinearityDiag (RapmUtils.ts:1629-1760). Runs an SVD of weight_matrix, builds condition indices ("lineup combos") from the ratio of the largest to each singular value, and a variance-decomposition-proportions ("VDP") matrix identifying which players load onto which collinear combo -- the classic Belsley-Kuh-Welsch collinearity-diagnostics recipe (see the upstream comment's colldiag.m citation). Also builds a plain Pearson player/player correlation matrix (_calc_player_correlations) and folds it into a possession -weighted adaptive_correl_weights summary per player.

numpy.linalg.svd(weight_matrix, full_matrices=False) replaces svd-js's SVD(weightMatrix, false). Both are the standard Golub-Kahan-Reinsch decomposition (A = U @ diag(S) @ Vᵀ); numpy's Vh return value already is Vᵀ (what the TS code separately computes via transpose(matrix(v))), so this port skips that transpose. The TS code (and this port) never reads u/the first SVD return -- only q/S (singular values) and v/Vᵀ. Singular -vector sign is immaterial here: every place V is used (phiMatrix/phi_matrix) squares each entry (val * val), and a per-singular-value sign flip on U/V together is a valid SVD regardless -- so any U/V sign convention difference between svd-js and LAPACK (numpy's backend) cannot change this function's output. Singular-value ordering is likewise immaterial: both this port and the TS source explicitly re-sort q (ascending, carrying the original index along) before using it, so whichever order either SVD implementation returns values in, the final result only depends on the values themselves (up to the explicit resort), not on numpy's native descending convention vs whatever order svd-js happens to return.

correl_matrix/poss_correl_matrix stay numpy.ndarray (see the module docstring for why an ndarray here does not break deep == comparisons).

Parameters

ParameterTypeDefaultDescription
weight_matrixNDArray[float64]An off/def design matrix, shape (num_lineups, ctx["num_players"]) (e.g. calc_player_weights's first return value, or a hand-built matrix for isolated testing).
ctxRapmPlayerContextA RapmPlayerContext. ctx["num_players"] sizes every per-player structure; ctx["col_to_player"] keys player_combos.

Returns

A RapmPreProcDiagnostics.

Raises

ExceptionDescription
numpy.linalg.LinAlgErrorIf the SVD fails to converge (not reachable via either oracle fixture).

Example

from sportsdataverse.mbb.mbb_rapm import calc_collinearity_diag, calc_player_weights

off_weights, _ = calc_player_weights(ctx)
diag = calc_collinearity_diag(off_weights, ctx)
print(diag["lineup_combos"][0]) # the worst-conditioned combo

calc_lineup_outputs​

calc_lineup_outputs(field: 'str', off_offset: 'float', def_offset: 'float', ctx: 'RapmPlayerContext', adaptive_correl_weights: 'list[float] | None' = None, use_old_val_if_possible: 'tuple[bool, bool]' = (False, False)) -> 'list[NDArray[np.float64]]'

Build the off/def target vectors the RAPM design matrices are fit against.

Faithful port of RapmUtils.calcLineupOutputs (RapmUtils.ts:598-751). For each filtered lineup, computes a possession-weighted residual: the lineup's own stat value, plus any global luck adjustment, minus the accumulated "prior offset" contributed by every player on the lineup (a strong-prior blend for kept players -- see _get_strong_weight -- or a fixed baseline contribution for removed players).

Upstream keeps this as a plain Array<Array<number>> (not a mathjs Matrix, unlike calc_player_weights's offWeights/ defWeights -- RapmUtils.test.ts's own tidyResults helper for this function has a visibly different shape, see the classification map in tests/fixtures/hoop_explorer/README.md). This port still materializes both output vectors as numpy.ndarray for consistency with calc_player_weights at the same dict -> array boundary -- the ridge-regression solve consumes both as arrays regardless of the upstream distinction.

Parameters

ParameterTypeDefaultDescription
fieldstrThe stat suffix to read off each lineup, e.g. "adj_ppp" (read as {prefix}_{field}, e.g. "off_adj_ppp").
off_offsetfloatThe D1-average offensive value for field (the regression's starting/baseline value on the RHS).
def_offsetfloatThe D1-average defensive value for field.
ctxRapmPlayerContextA RapmPlayerContext, e.g. from build_player_context.
adaptive_correl_weightslist[float] | NoneNoneOptional per-player adaptive-correlation weights (index-aligned with ctx["col_to_player"]), used as the strong-prior blend fallback when ctx["prior_info"] ["strong_weight"] < 0 -- see _get_strong_weight.
use_old_val_if_possibletuple[bool, bool](False, False)(use_old_val_for_off, use_old_val_for_def) -- whether to prefer each lineup/team stat's luck-adjusted old_value over its raw value when present. This is the luck-adjustment hook, an inherited coverage gap: the vendored oracle fixture has old_value == value on every field (via insertOldValues), so neither jest nor this port's replay test ever observes this flag change the resulting numbers -- only that passing it doesn't crash. See the module docstring's coverage-gap note.

Returns

[off_outputs, def_outputs] -- two 1-D numpy.ndarray target vectors, index-aligned with ctx["filtered_lineups"]("off"/"def") (plus one extra element each when ctx["unbias_weight"] > 0, an "unbiasing observation" target -- always unreached in production, same as calc_player_weights's extra row).

Example

from sportsdataverse.mbb.mbb_rapm import calc_lineup_outputs

off_outputs, def_outputs = calc_lineup_outputs(
"adj_ppp", 100.0, 100.0, ctx
)
print(off_outputs.shape) # (num_off_lineups,)

# Luck-adjusted variant (reads ``old_value`` where present)

off_luck, def_luck = calc_lineup_outputs(
"adj_ppp", 100.0, 100.0, ctx, use_old_val_if_possible=(True, True)
)

calc_player_weights​

calc_player_weights(ctx: 'RapmPlayerContext') -> 'list[NDArray[np.float64]]'

Build the off/def player-weight (design) matrices for the RAPM solve.

Faithful port of RapmUtils.calcPlayerWeights (RapmUtils.ts:544-595). One row per (filtered) lineup, one column per remaining player; each filled cell is sqrt(lineup_possessions / total_side_possessions) -- the possession-weighted design-matrix entry the ridge regression solves against. This is the first function in the module where a dict-shaped RapmPlayerContext gets materialized into a numpy.ndarray -- see the module docstring's "dict -> numpy.ndarray boundary" note.

Parameters

ParameterTypeDefaultDescription
ctxRapmPlayerContextA RapmPlayerContext, e.g. from build_player_context.

Returns

[off_weights, def_weights] -- two numpy.ndarray matrices of shape (num_{off,def}_lineups [+1 if ctx["unbias_weight"] > 0], ctx["num_players"]). The optional extra row (only emitted when ctx["unbias_weight"] > 0 -- always 0.0 in production per build_player_context's hardcoded local, but settable directly on the returned context dict, as the oracle test does) holds each column's unbias_weight-scaled sum-of-squares, an "unbiasing observation" row (RapmUtils.ts:578-593).

Example

from sportsdataverse.mbb.mbb_rapm import calc_player_weights

off_weights, def_weights = calc_player_weights(ctx)
print(off_weights.shape) # (num_off_lineups, num_players)

calc_slow_pseudo_inverse​

calc_slow_pseudo_inverse(player_weight_matrix: 'NDArray[np.float64]', ridge_lambda: 'float', ctx: 'RapmPlayerContext') -> 'NDArray[np.float64]'

Per-parameter variance terms for the ridge-regression standard errors.

Faithful port of the private RapmUtils.calcSlowPseudoInverse (RapmUtils.ts:1544-1557): the same (XᵀX + ridge_lambda·I)⁻¹ as slow_regression's bottomInv, but this function returns the square root of its diagonal instead of the full solver matrix -- the paramErrs term consumed by the standard-error formula (see calculate_sd_rapm).

Parameters

ParameterTypeDefaultDescription
player_weight_matrixNDArray[float64]The off/def design matrix, same shape as slow_regression's.
ridge_lambdafloatThe Tikhonov regularization strength (must match the ridge_lambda used to build the corresponding slow_regression solver, for the SEs to be meaningful).
ctxRapmPlayerContextA RapmPlayerContext -- only ctx["num_players"] is read.

Returns

A length-num_players array, sqrt(diag((XᵀX + λI)⁻¹)).

Example

from sportsdataverse.mbb.mbb_rapm import calc_slow_pseudo_inverse

param_errs = calc_slow_pseudo_inverse(x, 1.0, ctx)

calculate_predicted_out​

calculate_predicted_out(player_weight_matrix: 'NDArray[np.float64]', regressed_players: 'list[float]', ctx: 'RapmPlayerContext') -> 'NDArray[np.float64]'

Predict per-lineup outputs from fitted per-player RAPM values.

Faithful port of RapmUtils.calculatePredictedOut (RapmUtils.ts:1559-1567). ctx is accepted for signature parity with the TS source but unused in the body (ported verbatim -- upstream's own ctx param is likewise dead in this function).

Parameters

ParameterTypeDefaultDescription
player_weight_matrixNDArray[float64]The off/def design matrix, shape (num_lineups, num_players).
regressed_playerslist[float]The fitted per-player values (e.g. the final, strong-prior-blended RAPM from the pickRidgeRegression, or a raw calculate_rapm output), length num_players.
ctxRapmPlayerContextA RapmPlayerContext (unused).

Returns

The predicted per-lineup value, length num_lineups -- feed into calculate_residual_error alongside the actual lineup outputs.

Example

from sportsdataverse.mbb.mbb_rapm import calculate_predicted_out

predicted = calculate_predicted_out(x, [0.875, 1.375], ctx)

calculate_rapm​

calculate_rapm(regression_matrix: 'NDArray[np.float64]', player_outputs: 'list[float]') -> 'NDArray[np.float64]'

Apply a regression solver matrix to a target-outputs vector.

Faithful port of RapmUtils.calculateRapm (RapmUtils.ts:772-775). Note the TS signature carries no ctx parameter (unlike its solve-layer siblings) -- ported verbatim, param-for-param.

Parameters

ParameterTypeDefaultDescription
regression_matrixNDArray[float64]The (num_players, num_lineups) solver from slow_regression.
player_outputslist[float]The per-lineup target vector, length num_lineups (e.g. calc_lineup_outputs's off_outputs/def_outputs).

Returns

The per-player RAPM estimate, length num_players.

Example

from sportsdataverse.mbb.mbb_rapm import calculate_rapm

rapm = calculate_rapm(solver, [1.0, 2.0, 3.0])
print(rapm.shape) # (num_players,)

calculate_residual_error​

calculate_residual_error(player_outs: 'list[float]', regressed_outs: 'list[float]', ctx: 'RapmPlayerContext') -> 'float'

Sum of squared residuals between actual and predicted lineup outputs.

Faithful port of RapmUtils.calculateResidualError (RapmUtils.ts:1569-1579). ctx is accepted for signature parity but unused in the body (dead upstream too).

NaN/shape regime (landmine 7): TS zips the two arrays via lodash .zip(pads the shorter side withundefined, so a length mismatch silently contributes NaNto the running sum viaundefined - number) then reduces with plain +. This port instead subtracts the two as numpyarrays: a length mismatch **raises**ValueError(numpy broadcast rules), rather than the TS silent-NaN behavior -- not reachable via either language's own call sites (both arguments are always index-aligned to the same lineup count in production), so this is a divergence in dead territory, not a fixed bug. ANaN*value already present* inside either input (as opposed to a length mismatch) propagates through thenumpy` subtraction/sum exactly as it would through the JS arithmetic (both regimes: numpy-propagate).

Parameters

ParameterTypeDefaultDescription
player_outslist[float]The actual per-lineup target values (e.g. calc_lineup_outputs's output).
regressed_outslist[float]The predicted per-lineup values (e.g. calculate_predicted_out's output).
ctxRapmPlayerContextA RapmPlayerContext (unused).

Returns

sum((player_outs[i] - regressed_outs[i]) ** 2) -- the errSq term consumed by calculate_sd_rapm.

Raises

ExceptionDescription
ValueErrorIf player_outs and regressed_outs have different lengths -- see landmine 7 above.

Example

from sportsdataverse.mbb.mbb_rapm import calculate_residual_error

err_sq = calculate_residual_error([1.0, 2.0, 3.0], [0.875, 1.375, 2.25], ctx)

calculate_sd_rapm​

calculate_sd_rapm(param_errs: 'NDArray[np.float64]', err_sq: 'float', num_lineups: 'int', num_players: 'int') -> 'NDArray[np.float64]'

Per-player RAPM standard errors.

Faithful port of the inline sdRapm computation in RapmUtils.pickRidgeRegression (RapmUtils.ts:1373-1390, not itself a named TS function -- promoted to a standalone, independently testable helper here since the brief calls out the formula explicitly). Cites arXiv:1509.09169.

Two NaN/error regimes (landmines 8-9):

  1. dof_inv = 1.0 / (num_lineups - num_players) -- if num_lineups == num_players exactly, JS silently produces Infinity (float division by zero); this port instead raises ZeroDivisionError (Python float division by zero), matching this module's already-established landmine-2 convention (unguarded division, Python-raises vs JS-Infinity/NaN). Not reachable via the oracle fixtures (num_off_lineups/num_def_lineups always comfortably exceed num_players there).
  2. sqrt(sqrt(param_errs) * err_sq * dof_inv) -- a negative param_errs entry (only possible if XᵀX + λI isn't actually positive-definite, e.g. ridge_lambda < 0) silently numpy-propagates to NaN (matching JS Math.sqrt(negative) -> NaN, with a RuntimeWarning rather than a raise) -- both language regimes agree here, unlike landmine 8.

Parameters

ParameterTypeDefaultDescription
param_errsNDArray[float64]Per-player variance terms from calc_slow_pseudo_inverse, length num_players.
err_sqfloatThe residual sum of squares from calculate_residual_error.
num_lineupsintctx["num_off_lineups"] or ctx["num_def_lineups"] (whichever side param_errs/err_sq were computed for).
num_playersintctx["num_players"].

Returns

A length-num_players array of per-player RAPM standard errors.

Raises

ExceptionDescription
ZeroDivisionErrorIf num_lineups == num_players -- see landmine 8 above.

Example

from sportsdataverse.mbb.mbb_rapm import calculate_sd_rapm

sd_rapm = calculate_sd_rapm(param_errs, err_sq, num_lineups=3, num_players=2)

calibration_table​

calibration_table(y_true: 'np.ndarray', p_pred: 'np.ndarray', n_bins: 'int' = 10) -> 'pl.DataFrame'

Bucket predicted probabilities into bins and compare to actual outcome rates.

Parameters

ParameterTypeDefaultDescription
y_truendarrayArray of binary outcomes (0/1).
p_predndarrayArray of predicted probabilities in [0, 1].
n_binsint10Number of equal-width probability bins.

Returns

A polars.DataFrame with columns bin_mid, mean_pred, mean_actual, n (one row per non-empty bin).

col_nametypedescription
bin_midFloat64
mean_predFloat64
mean_actualFloat64
nInt64

Example

import numpy as np
from sportsdataverse._common.metrics import calibration_table
calibration_table(np.array([1, 0, 1, 0]), np.array([0.9, 0.1, 0.8, 0.2]))

fit_shrinkage_k​

fit_shrinkage_k(scored: 'pl.DataFrame', *, seed: 'int' = 0) -> 'float'

Fit the talent shrinkage k split-half (see module docstring).

Parameters

ParameterTypeDefaultDescription
scoredDataFramembb_shot_quality output.
seedint0Split seed (deterministic fit).

Returns

The k in [1, 5000] minimizing talent_split_mse.

Example

from sportsdataverse.mbb.mbb_shooter_talent import fit_shrinkage_k
k = fit_shrinkage_k(scored)

get_constants​

get_constants(league: 'str') -> 'LeagueConstants'

Return the LeagueConstants for a league.

Parameters

ParameterTypeDefaultDescription
leaguestrEither "mens" or "womens".

Returns

The league's LeagueConstants.

Raises

ExceptionDescription
ValueErrorIf league is not a known key of LEAGUE_CONSTANTS.

Example

from sportsdataverse.mbb.mbb_prediction_constants import get_constants
get_constants("mens").hfa

get_player_value_constants​

get_player_value_constants(league: 'str') -> 'PlayerValueConstants'

Return the PlayerValueConstants for a league.

Parameters

ParameterTypeDefaultDescription
leaguestr"mens" or "womens".

Returns

The league's PlayerValueConstants.

Raises

ExceptionDescription
ValueErrorIf league is not a known key.

Example

from sportsdataverse.mbb.mbb_player_value_constants import get_player_value_constants
get_player_value_constants("mens").bundle_prefix

in_game_features​

in_game_features(pbp: 'pl.DataFrame', pregame_home_prob: 'float') -> 'pl.DataFrame'

Per-play in-game win-probability features from a load_mbb_pbp frame.

Parameters

ParameterTypeDefaultDescription
pbpDataFramePlay-by-play frame with start_game_seconds_remaining, home_score, away_score, team_id (event team) and home_team_id (the load_mbb_pbp schema).
pregame_home_probfloatThe pregame home win probability (e.g. from win_prob_from_margin), encoded as a constant logit column. Clipped to [1e-6, 1 - 1e-6] so a saturated CDF (exact 0/1) cannot crash the logit.

Returns

One row per input play: score_diff (home - away), sec_left (clipped at 0 -- overtime plays count as 0 seconds left; the game page gives overtime plays the period clock first), sqrt_sec_left, pregame_logit, home_has_ball (Int8; dead-ball / unknown-team plays are 0), margin_per_sqrt_sec (score_diff / sqrt(sec_left + 1)) and sqrt_sec_left_60 (sqrt(max(sec_left, 60))). The same features as the NBA / WNBA model (sportsdataverse.nba.nba_game_predict.in_game_features).

col_nametypedescription
score_diffFloat64
sec_leftFloat64
sqrt_sec_leftFloat64
pregame_logitFloat64
home_has_ballInt64
margin_per_sqrt_secFloat64Score margin over the time left, score_diff / sqrt(sec_left + 1): a fixed lead grows as the clock runs down. The basketball in-game boosters read the final minute only through this column.
sqrt_sec_left_60Float64Square root of the seconds left, floored at one minute (sqrt(max(sec_left, 60))): the clock the basketball in-game boosters read outside the final minute.

Example

from sportsdataverse.mbb.mbb_game_predict import in_game_features
from sportsdataverse.mbb.mbb_loaders import load_mbb_pbp
pbp = load_mbb_pbp([2024]).filter(pl.col("game_id") == 401638643)
feats = in_game_features(pbp, 0.62)

inject_rapm_into_players​

inject_rapm_into_players(players: 'list[PlayerOnOffStats]', off_rapm_input: 'RapmProcessingInputs', def_rapm_input: 'RapmProcessingInputs', stats_averages: 'PureStatSet', ctx: 'RapmPlayerContext', adaptive_correl_weights: 'list[float] | None', read_value_keys: 'tuple[ValueKey, ValueKey]' = ('value', 'value'), write_value_key: 'ValueKey' = 'value') -> 'None'

Write pick_ridge_regression's RAPM predictions back onto each player.

Faithful port of RapmUtils.injectRapmIntoPlayers (RapmUtils.ts:781-916). For every onOffReportReplacement field (minus the possession/title/ separator/adj_opp housekeeping keys -- see landmine 11 for the exact, faithfully-ported omit-key quirk), re-derives that field's off/def target vectors via calc_lineup_outputs, applies each side's calculate_rapm solver, blends in the strong prior (mirroring pick_ridge_regression's own blend, except for adj_ppp which reuses off_rapm_input["rapm_adj_ppp"]/def_rapm_input["rapm_adj_ppp"] directly rather than recomputing), then writes {playerId}.rapm[field] = {write_value_key: result, "override": ...} onto every player not in ctx["removed_players"].

NOTE (upstream comment, verbatim): when write_value_key == "old_value", this must be called after an initial write_value_key == "value" call on the same players list -- the old_value pass .merges (_lodash_merge) its results into each player's *existing* rapmdict rather than replacing it, so a player'srapm["field"]ends up carrying both avalue(from the first call) and anold_value` (from the second) side by side.

Parameters

ParameterTypeDefaultDescription
playerslist[PlayerOnOffStats]The players to write RAPM results onto (mutated in place -- each qualifying player gets a "rapm" key set/merged).
off_rapm_inputRapmProcessingInputspick_ridge_regression's offensive output.
def_rapm_inputRapmProcessingInputspick_ridge_regression's defensive output.
stats_averagesPureStatSetLeague/context average stat set -- consulted for each field's off/def offset before ctx["team_info"].
ctxRapmPlayerContextA RapmPlayerContext (the same one pick_ridge_regression was called with).
adaptive_correl_weightslist[float] | NoneOptional per-player adaptive-correlation weights, forwarded to calc_lineup_outputs / _get_strong_weight exactly as pick_ridge_regression does.
read_value_keystuple[ValueKey, ValueKey]('value', 'value')(off_key, def_key) -- which key ("value"/"old_value") to prefer when reading stats_averages/ctx["team_info"] offsets and when calling calc_lineup_outputs (forwarded as its use_old_val_if_possible flag).
write_value_keyValueKey'value'"value" or "old_value" -- which key each written field carries its result under.

Raises

ExceptionDescription
TypeErrorIf a player in players is absent from both ctx["removed_players"] and ctx["player_to_col"] -- see landmine 12 (a Python-vs-JS divergence in genuinely dead territory for the oracle).

Example

from sportsdataverse.mbb.mbb_rapm import inject_rapm_into_players

inject_rapm_into_players(players, off_results, def_results, {}, ctx, None)
print(players[0]["rapm"]["off_adj_ppp"]) # {"value": ..., "override": None}

# Luck-adjusted two-call sequence (``"value"`` first, THEN ``"old_value"``)

inject_rapm_into_players(
players, off_results, def_results, {}, ctx, None, ("value", "old_value"), "value"
)
inject_rapm_into_players(
players, off_results, def_results, {}, ctx, None, ("old_value", "old_value"), "old_value"
)

kmeans_fit​

kmeans_fit(X: 'np.ndarray', k: 'int', seed: 'int', n_init: 'int' = 10, max_iter: 'int' = 100) -> "'tuple[np.ndarray, np.ndarray]'"

Seeded Lloyd's KMeans, best-of-n_init by inertia.

Parameters

ParameterTypeDefaultDescription
XndarrayFeature matrix (n, d) (standardize first).
kintNumber of clusters.
seedintRNG seed (deterministic output).
n_initint10Independent restarts.
max_iterint100Lloyd iterations per restart.

Returns

(centers[k, d], labels[n]).

Example

centers, labels = kmeans_fit(Z, k=8, seed=0)

load_artifact​

load_artifact(name: 'str') -> 'dict'

Read a bundled player-value artifact (mbb/models/<name>.json).

Parameters

ParameterTypeDefaultDescription
namestrArtifact stem, e.g. "mbb_box_bpm".

Returns

The parsed JSON dict.

Example

from sportsdataverse.mbb.mbb_player_value_constants import load_artifact
art = load_artifact("mbb_box_bpm")

load_preseason_prior​

load_preseason_prior(season: 'int', schedule: 'pl.DataFrame', *, league: 'str' = 'mens') -> 'pl.DataFrame'

The preseason prior for season from the previous season's released schedule and team box.

sportsdataverse.mbb.mbb_college_season.season_prior over the previous season's release, for this season's Division I teams: the prior the season sim blends in (~sportsdataverse.mbb.mbb_prediction_constants.PRESEASON_PRIOR).

Parameters

ParameterTypeDefaultDescription
seasonintThe season being rated.
scheduleDataFrameThat season's ESPN schedule (load_mbb_schedule / load_wbb_schedule rows); its Division I teams get a prior.
leaguestr'mens'"mens" / "womens".

Returns

PRIOR_SCHEMA rows, empty when the previous season is not released (no prior).

col_nametypedescription
seasonInt64The season the prior is for (named by its end year).
team_idStringESPN team id of a Division I team this season.
off_devFloat64The prior's offense, points per 100 possessions above the prior season's league average; last season's final AdjO deviation times the regression factor, or half the new-team default for a program new to Division I.
def_devFloat64The prior's defense, points per 100 allowed above the prior season's league average (lower is better); last season's final AdjD deviation times the regression factor, or minus half the new-team default.
prior_avgFloat64The prior season's league-average efficiency, the ratings' baseline before this season has a game.
new_teamBooleanTrue when the team was not Division I last season and starts at the new-team quantile of last season's AdjEM.

Example

from sportsdataverse.mbb.mbb_loaders import load_mbb_schedule
from sportsdataverse.mbb.mbb_team_ratings import load_preseason_prior
prior = load_preseason_prior(2026, load_mbb_schedule([2026]))

log_loss_score​

log_loss_score(y_true: 'np.ndarray', p_pred: 'np.ndarray', eps: 'float' = 1e-15) -> 'float'

Binary cross-entropy loss between predicted probabilities and outcomes.

Parameters

ParameterTypeDefaultDescription
y_truendarrayArray of binary outcomes (0/1).
p_predndarrayArray of predicted probabilities in [0, 1].
epsfloat1e-15Clipping bound to avoid log(0).

Returns

The mean log loss.

Example

import numpy as np
from sportsdataverse._common.metrics import log_loss_score
log_loss_score(np.array([1, 0]), np.array([0.9, 0.1]))

logistic_fit​

logistic_fit(X: 'np.ndarray', y: 'np.ndarray', lam: 'float' = 1.0) -> 'np.ndarray'

L2-penalized logistic regression via L-BFGS (intercept unpenalized).

Parameters

ParameterTypeDefaultDescription
XndarrayFeature matrix (n, d).
yndarrayBinary outcomes (0/1).
lamfloat1.0L2 penalty on the non-intercept coefficients.

Returns

Coefficient vector of length d + 1 (intercept first).

Example

coef = logistic_fit(X, drafted, lam=1.0)

mae​

mae(a: 'np.ndarray', b: 'np.ndarray') -> 'float'

Mean absolute error between two arrays.

Parameters

ParameterTypeDefaultDescription
andarrayFirst array of values.
bndarraySecond array of values (same length as a).

Returns

The mean absolute error.

Example

import numpy as np
from sportsdataverse._common.metrics import mae
mae(np.array([1.0, 2.0]), np.array([1.5, 2.5]))

mbb_box_bpm​

mbb_box_bpm(seasons: "'Union[int, list[int]]'", *, league: 'str' = 'mens', return_as_pandas: 'bool' = False) -> "'Union[pl.DataFrame, pd.DataFrame]'"

Per-player-season box Plus/Minus (offense, defense, total).

Aggregates the season's player boxscores, scores the per-100 features through the bundled team-constrained coefficients, and applies the BPM team adjustment so each team's minutes-weighted player scores sum to its adjusted efficiency margin (points per 100 possessions above league average; positive = good on both ends).

Parameters

ParameterTypeDefaultDescription
seasonsUnion[int, list[int]]A season (e.g. 2025) or list of seasons.
leaguestr'mens'"mens" or "womens" (selects the bundled artifact).
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One row per (player_id, season, team_id): player_id:Utf8, player, season, team_id:Utf8, min, box_obpm, box_dbpm, box_bpm. Empty input returns the schema with zero rows.

col_nametypedescription
player_idStringUnique player identifier.
playerStringPlayer name.
seasonInt64Season year.
team_idStringUnique team identifier.
minFloat64Minutes played.
box_obpmFloat64
box_dbpmFloat64
box_bpmFloat64

Example

from sportsdataverse.mbb import mbb_box_bpm
bpm = mbb_box_bpm(2025)

# Pipeline next step (one line)

bpm.filter(pl.col("min") >= 400).sort("box_bpm", descending=True).head(15)

mbb_bracket_sim​

mbb_bracket_sim(seeded_field: 'pl.DataFrame', ratings: 'pl.DataFrame', *, n_sims: 'int' = 10000, seed: 'int' = 0, league: 'str' = 'mens', return_as_pandas: 'bool' = False) -> 'pl.DataFrame | pd.DataFrame'

Single-elimination Monte Carlo over a bracket-ordered field.

Rows of seeded_field are bracket slots: adjacent rows meet in round 1 and winners of adjacent games meet next round (the standard fold). All games are neutral-site. Round columns are named from the END of a 64-team bracket (champion back to reach_r32); with a smaller field the early columns are 1.0 for everyone (trivially reached).

Parameters

ParameterTypeDefaultDescription
seeded_fieldDataFrameBracket-ordered rows with team_id (and typically seed for reference).
ratingsDataFrameOne row per team: team_id, adj_em.
n_simsint10000Number of simulated brackets.
seedint0Seed for numpy.random.default_rng.
leaguestr'mens'"mens" or "womens".
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One row per field team: team_id, seed?, reach_r32, reach_s16, reach_e8, reach_f4, reach_final, champion (probabilities).

No returns table is published for this function: no capture: it needs a bracket-ordered field whose size is a power of two, and no package function returns one.

Raises

ExceptionDescription
ValueErrorIf the field size is not a power of two.
KeyErrorIf a field team is missing from ratings.

Example

from sportsdataverse.mbb.mbb_season_sim import mbb_bracket_sim
odds = mbb_bracket_sim(field_64, ratings, n_sims=20000, seed=42)

mbb_draft_projection​

mbb_draft_projection(seasons: "'Union[int, list[int]]'", *, league: 'str' = 'mens', return_as_pandas: 'bool' = False) -> "'Union[pl.DataFrame, pd.DataFrame]'"

Draft probability, projected pick, and pro tier per player-season.

draft_prob is the probability of being selected in the draft immediately following the college season; projected_pick is the expected overall pick conditional on being drafted (lower = better); pro_tier buckets the pick through the bundled tier edges.

Parameters

ParameterTypeDefaultDescription
seasonsUnion[int, list[int]]A season (e.g. 2025, feeding the June 2025 draft) or list of seasons.
leaguestr'mens'"mens" or "womens" (selects the bundled artifact; womens = WNBA draft).
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One row per qualifying player-season: player_id:Utf8, player, season, team_id:Utf8, draft_prob, projected_pick, pro_tier. Empty input returns the schema with zero rows.

col_nametypedescription
player_idStringUnique player identifier.
playerStringPlayer name.
seasonInt64Season year.
team_idStringUnique team identifier.
draft_probFloat64
projected_pickFloat64
pro_tierString

Example

from sportsdataverse.mbb import mbb_draft_projection
board = mbb_draft_projection(2025)

# Pipeline next step (one line)

board.sort("draft_prob", descending=True).head(30)

mbb_in_game_win_prob​

mbb_in_game_win_prob(pbp: 'pl.DataFrame', pregame_home_prob: 'float', *, league: 'str' = 'mens', return_as_pandas: 'bool' = False) -> 'Union[pl.DataFrame, pd.DataFrame]'

Per-play home win probability from the bundled in-game model.

Scores in_game_features through the committed artifact (sportsdataverse/mbb/models/mbb_in_game_wp.ubj men's, wbb_in_game_wp.ubj women's): a shallow monotone-constrained xgboost booster fit by tools/models/mbb_prediction/train_in_game_wp.py on 2022, 2023 and 2025, held out on 2026 (2024, the pregame constants' fit season and the confirmation season, is in neither). It is the NBA / WNBA model's contract: within the final minute the clock reaches the model only through margin_per_sqrt_sec, so at a fixed lead the probability moves toward the leader as time runs out. This is the raw model output: it never applies the game result (nba_game_page.score_win_prob settles a finished game's last play).

Parameters

ParameterTypeDefaultDescription
pbpDataFramePlay-by-play for ONE game in the load_mbb_pbp schema (start_game_seconds_remaining, home_score, away_score, team_id, home_team_id).
pregame_home_probfloatPregame home win probability (e.g. from win_prob_from_margin).
leaguestr'mens'"mens" or "womens" (selects the bundled artifact).
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One row per play: the in_game_features columns plus home_win_prob.

col_nametypedescription
score_diffFloat64
sec_leftFloat64
sqrt_sec_leftFloat64
pregame_logitFloat64
home_has_ballInt64
margin_per_sqrt_secFloat64Score margin over the time left, score_diff / sqrt(sec_left + 1): a fixed lead grows as the clock runs down. The basketball in-game boosters read the final minute only through this column.
sqrt_sec_left_60Float64Square root of the seconds left, floored at one minute (sqrt(max(sec_left, 60))): the clock the basketball in-game boosters read outside the final minute.
home_win_probFloat64

Raises

ExceptionDescription
ImportErrorIf the bundled artifact is an xgboost tree (.ubj) and xgboost is not installed (pip install sportsdataverse[models]).

Example

from sportsdataverse.mbb.mbb_game_predict import mbb_in_game_win_prob
from sportsdataverse.mbb.mbb_loaders import load_mbb_pbp
pbp = load_mbb_pbp([2024]).filter(pl.col("game_id") == 401638643)
wp = mbb_in_game_win_prob(pbp, 0.62)

mbb_predict_games​

mbb_predict_games(games: 'pl.DataFrame', ratings: 'pl.DataFrame', *, league: 'str' = 'mens', return_as_pandas: 'bool' = False) -> 'Union[pl.DataFrame, pd.DataFrame]'

Vectorized pregame predictions for a schedule of games.

Joins the ratings frame twice (home / away) and applies the closed-form predict_margin / win_prob_from_margin / predict_total math column-wise.

Parameters

ParameterTypeDefaultDescription
gamesDataFrameOne row per game with game_id, home_team_id, away_team_id and optionally neutral_site (missing column means every game is a true home game). Team-id dtypes must match ratings['team_id'] exactly.
ratingsDataFrameOne row per team with team_id, adj_o, adj_d, adj_em, adj_tempo (the mbb_team_ratings output for one season / as-of date).
leaguestr'mens'"mens" or "womens".
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One row per input game: game_id, home_team_id, away_team_id, exp_margin, home_win_prob, exp_total. Games whose teams are missing from ratings carry nulls.

No returns table is published for this function: no capture: it needs a schedule with home_team_id / away_team_id columns, and no package function returns one (load_mbb_schedule ships home_id / away_id).

Raises

ExceptionDescription
ValueErrorIf a join-key dtype mismatches ratings['team_id'], or if ratings has duplicate team_id rows (e.g. multiple seasons passed at once).

Example

from sportsdataverse.mbb.mbb_game_predict import mbb_predict_games
from sportsdataverse.mbb.mbb_team_ratings import mbb_team_ratings
preds = mbb_predict_games(games, mbb_team_ratings([2024]))

mbb_pregame_projections​

mbb_pregame_projections(season: 'int', *, league: 'str' = 'mens', prior_games: 'float | None' = None, return_as_pandas: 'bool' = False) -> 'pl.DataFrame | pd.DataFrame'

Rated pregame projection for every ESPN game of a college season, as of each game's date.

The number the game page's pregame card prints: the closed form on each team's adjusted efficiency margin before the game, with the preseason prior blended in; see the module docstring for the model and the as-of boundary.

Parameters

ParameterTypeDefaultDescription
seasonintThe season, named by the year it ends (2027 is 2026-27).
leaguestr'mens'"mens" (MBB) or "womens" (WBB).
prior_gamesfloat | NoneNoneThe prior's weight in games; None (default) takes the fitted weight (4 men, 3 women), 0 turns the blend off.
return_as_pandasboolFalseReturn a pandas frame instead of polars.

Returns

One row per game of the ESPN schedule release (scheduled games included): season, game_id, game_date, as_of_date (the ratings use completed games dated strictly before it), home_team_id / away_team_id (ESPN ids, Utf8, the mbb_ratings key), neutral_site, each side's rating (AdjEM with the prior), rank (dense, 1 best), games and rating_raw (AdjEM without the prior), prior_games, hfa (points, 0 at a neutral site), exp_home_margin (points) and rated_home_prob.

col_nametypedescription
seasonInt32Season, named by the year it ends (2027 is 2026-27; the WNBA season is its calendar year).
game_idInt32ESPN game id (Int32).
game_dateDateGame date (ESPN schedule).
as_of_dateDateThe ratings use the completed games dated strictly before this date: the game's date, or the day after the last completed game for a game not yet reached.
home_team_idStringESPN home team id (Utf8, the mbb_ratings / wbb_ratings team_id key).
away_team_idStringESPN away team id (Utf8).
neutral_siteBooleanNeutral-site flag; a neutral game gets no home term.
home_ratingFloat64Home team's adjusted efficiency margin before the game with the preseason prior blended in (AdjEM, points per 100 over an average opponent); its prior alone before its first game.
away_ratingFloat64Away team's adjusted efficiency margin before the game, on the same basis.
home_rankInt64Dense rank of home_rating among the teams rated as of the date (1 = best).
away_rankInt64Dense rank of away_rating among the teams rated as of the date.
home_gamesInt64Home team's games behind its rating (0 before its first game).
away_gamesInt64Away team's games behind its rating.
home_rating_rawFloat64Home team's AdjEM before the game without the prior: the season-to-date mbb_ratings number.
away_rating_rawFloat64Away team's AdjEM before the game without the prior.
prior_gamesFloat64The preseason prior's weight in games (4 men, 3 women); 0 when the blend is off or the previous season is not released.
hfaFloat64Home-court term in points (the league's fitted hfa), 0 at a neutral site.
exp_home_marginFloat64Expected home margin in points, the producer's closed form on the two ratings (positive favours the home team).
rated_home_probFloat64Pregame home win probability, Phi(exp_home_margin / margin_sd) with the league's fitted margin_sd; null when a side has no rating.

Raises

ExceptionDescription
ValueErrorIf league is not "mens" or "womens".

Example

from sportsdataverse.mbb.mbb_pregame_projection import mbb_pregame_projections
out = mbb_pregame_projections(2026)
out.select("game_id", "game_date", "exp_home_margin", "rated_home_prob").head()

# Women's, with no blend

mbb_pregame_projections(2026, league="womens", prior_games=0)

mbb_recruiting_projection​

mbb_recruiting_projection(seasons: "'Union[int, list[int]]'", *, league: 'str' = 'mens', return_as_pandas: 'bool' = False) -> "'Union[pl.DataFrame, pd.DataFrame]'"

Expected freshman box-BPM per recruit + over/under-performance residual.

Scores each recruit of the season's incoming class through the bundled recruiting ridge (composite grade + log national rank; missing values imputed with the class median / the bubble rank). When the freshman season is already observable, resume_residual = realized box_bpm - exp_box_bpm (null otherwise, and player_id carries the matched college athlete id).

Parameters

ParameterTypeDefaultDescription
seasonsUnion[int, list[int]]Freshman college season(s) (e.g. 2025 = the class arriving for 2024-25).
leaguestr'mens'"mens" or "womens" (selects the bundled artifact).
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One row per recruit: recruit_id:Utf8, player_id:Utf8 (nullable), player, season, team_id:Utf8, composite, rank_nat, exp_box_bpm, resume_residual. Empty input returns the schema with zero rows.

col_nametypedescription
recruit_idString
player_idStringUnique player identifier.
playerStringPlayer name.
seasonInt64Season year.
team_idStringUnique team identifier.
compositeFloat64
rank_natInt64
exp_box_bpmFloat64
resume_residualFloat64

Example

from sportsdataverse.mbb import mbb_recruiting_projection
proj = mbb_recruiting_projection(2026)

# Pipeline next step (one line)

proj.sort("exp_box_bpm", descending=True).head(15)

mbb_season_projections​

mbb_season_projections(season: 'int', *, as_of_date: 'datetime.date | None' = None, n_sims: 'int' = 10000, seed: 'int' = 0, league: 'str' = 'mens', ineligible: 'Iterable[str] | None' = None, return_as_pandas: 'bool' = False) -> 'pl.DataFrame | pd.DataFrame'

The daily season_sim table: projected W-L, conference standings, title and bid odds.

Loads the released ESPN schedule and team box, rates every team on the games strictly before as_of_date, and runs ~sportsdataverse.mbb.mbb_college_season.simulate_season: the remaining regular season, the regular-season conference titles, every unfinished conference tournament (the auto-bid), and the 68-team field and its seeds, n_sims times from numpy.random.default_rng(seed). The same arguments give the same table.

Parameters

ParameterTypeDefaultDescription
seasonintSeason to project (e.g. 2024).
as_of_datedate | NoneNoneOnly use games strictly before this date; None uses every completed game (today's table).
n_simsint10000Simulated seasons.
seedint0RNG seed.
leaguestr'mens'"mens" or "womens".
ineligibleIterable[str] | NoneNoneTeam ids barred from the NCAA tournament (no bid; see ~sportsdataverse.mbb.mbb_bracketology.mbb_bracketology). None (default) reads the bundled list.
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One row per Division I team: current and projected overall and conference W-L, the projected conference standing (proj_conf_win_pct_rank, 1 best within the conference), reg_season_title_prob (co-champions count), conf_tourney_title_prob, auto_bid_prob (the champion's, or the runner-up's when the champion is ineligible), at_large_prob, bid_prob, seed_mean (over sims with a bid) and seed_1_prob .. seed_16_prob.

col_nametypedescription
seasonInt64Season, named by its end year (2024 is 2023-24).
as_of_dateDateThe as-of date: only games strictly before it count as played. Null when every completed game is used.
team_idStringESPN team id (Utf8).
teamStringTeam (ESPN location name).
conference_idStringESPN conference id, read off the schedule (Utf8).
conferenceStringConference short name. Null for independents, which get no auto-bid.
winsInt64Wins as of the date (every opponent).
lossesInt64Losses as of the date.
conf_winsInt64Regular-season conference wins as of the date.
conf_lossesInt64Regular-season conference losses as of the date.
proj_winsFloat64Projected wins: played wins plus the mean simulated wins in the remaining regular-season games (conference tournaments not counted).
proj_lossesFloat64Projected losses, on the same games.
proj_conf_winsFloat64Projected regular-season conference wins.
proj_conf_lossesFloat64Projected regular-season conference losses.
proj_conf_win_pctFloat64proj_conf_wins / (proj_conf_wins + proj_conf_losses).
proj_conf_win_pct_rankFloat64Projected conference standing: rank of proj_conf_win_pct within the conference, 1 is best, average ties. Null for independents.
proj_conf_win_pct_pctFloat64Percentile of proj_conf_win_pct within the conference.
reg_season_title_probFloat64Share of sims with the conference's best regular-season conference winning percentage; co-champions all count, so a conference can sum past 1.
conf_tourney_title_probFloat64Share of sims winning the conference tournament (a played final is certain). Ineligible teams play it too, so this can differ from auto_bid_prob.
auto_bid_probFloat64Share of sims holding the conference's auto-bid: the tournament champion, or the runner-up when the champion is ineligible.
at_large_probFloat64Share of sims with an at-large bid.
bid_probFloat64Share of sims in the 68-team field (auto_bid_prob + at_large_prob).
seed_meanFloat64Mean seed line over the sims with a bid; null when never in the field.
seed_1_probFloat64Share of all sims with a 1 seed (unconditional; the 16 lines sum to bid_prob).
seed_2_probFloat64Share of all sims with a 2 seed (unconditional; the 16 lines sum to bid_prob).
seed_3_probFloat64Share of all sims with a 3 seed (unconditional; the 16 lines sum to bid_prob).
seed_4_probFloat64Share of all sims with a 4 seed (unconditional; the 16 lines sum to bid_prob).
seed_5_probFloat64Share of all sims with a 5 seed (unconditional; the 16 lines sum to bid_prob).
seed_6_probFloat64Share of all sims with a 6 seed (unconditional; the 16 lines sum to bid_prob).
seed_7_probFloat64Share of all sims with a 7 seed (unconditional; the 16 lines sum to bid_prob).
seed_8_probFloat64Share of all sims with a 8 seed (unconditional; the 16 lines sum to bid_prob).
seed_9_probFloat64Share of all sims with a 9 seed (unconditional; the 16 lines sum to bid_prob).
seed_10_probFloat64Share of all sims with a 10 seed (unconditional; the 16 lines sum to bid_prob).
seed_11_probFloat64Share of all sims with a 11 seed (unconditional; the 16 lines sum to bid_prob).
seed_12_probFloat64Share of all sims with a 12 seed (unconditional; the 16 lines sum to bid_prob).
seed_13_probFloat64Share of all sims with a 13 seed (unconditional; the 16 lines sum to bid_prob).
seed_14_probFloat64Share of all sims with a 14 seed (unconditional; the 16 lines sum to bid_prob).
seed_15_probFloat64Share of all sims with a 15 seed (unconditional; the 16 lines sum to bid_prob).
seed_16_probFloat64Share of all sims with a 16 seed (unconditional; the 16 lines sum to bid_prob).
n_simsInt64Simulated seasons.

Example

import datetime
from sportsdataverse.mbb import mbb_season_projections
odds = mbb_season_projections(2024, as_of_date=datetime.date(2024, 2, 1), n_sims=2000)

# Pipeline next step (one line)

odds.sort("bid_prob", descending=True).head(68)

mbb_season_sim​

mbb_season_sim(ratings: 'pl.DataFrame', remaining_schedule: 'pl.DataFrame', *, n_sims: 'int' = 10000, seed: 'int' = 0, league: 'str' = 'mens', return_as_pandas: 'bool' = False) -> 'pl.DataFrame | pd.DataFrame'

Monte Carlo the remaining schedule: expected wins + title odds.

Parameters

ParameterTypeDefaultDescription
ratingsDataFrameOne row per team: season, team_id, adj_em and optionally conference (enables conf_title_prob) and current_wins (added to the simulated remaining wins).
remaining_scheduleDataFrameGames to simulate: home_team_id, away_team_id, neutral_site.
n_simsint10000Number of simulated seasons.
seedint0Seed for numpy.random.default_rng (deterministic output).
leaguestr'mens'"mens" or "womens".
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One row per team: season, team_id, exp_wins (mean simulated total wins), playoff_prob (share of sims finishing in the top 68 win totals -- a field-size proxy, ties broken by adj_em) and conf_title_prob (share of sims with the most wins among conference members; ties count for every tied team; null without a conference column).

No returns table is published for this function: no capture: it needs a schedule with home_team_id / away_team_id columns, and no package function returns one (load_mbb_schedule ships home_id / away_id).

Raises

ExceptionDescription
KeyErrorIf a scheduled team is missing from ratings -- every home_team_id / away_team_id must have a ratings row.

Example

from sportsdataverse.mbb.mbb_season_sim import mbb_season_sim
odds = mbb_season_sim(ratings, remaining, n_sims=5000, seed=42)

mbb_shooter_talent​

mbb_shooter_talent(scored: 'pl.DataFrame', *, league: 'str' = 'mens', k: "'float | None'" = None, return_as_pandas: 'bool' = False) -> "'Union[pl.DataFrame, pd.DataFrame]'"

Per-shooter EB-regressed make% over expected + points over expected.

Parameters

ParameterTypeDefaultDescription
scoredDataFramembb_shot_quality output (needs shooter_id, made, point_value, xmake, xpoints).
leaguestr'mens'"mens" or "womens" (default k source).
kfloat | NoneNoneShrinkage pseudo-shots; None uses get_constants(league).shrink_k_talent (fitted split-half).
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One row per shooter: shooter_id:Utf8, n_shots, make_rate, xmake_mean, oe_pct, oe_pct_regressed, points_over_expected, poe_per_100. Empty input returns the zero-row schema.

col_nametypedescription
shooter_idStringUnique identifier for shooter.
n_shotsInt64
make_rateFloat64
xmake_meanFloat64
oe_pctFloat64
oe_pct_regressedFloat64
points_over_expectedFloat64
poe_per_100Float64

Example

from sportsdataverse.mbb import mbb_shot_data, mbb_shot_quality, mbb_shooter_talent
talent = mbb_shooter_talent(mbb_shot_quality(mbb_shot_data(2025)))

# Pipeline next step (one line)

talent.filter(pl.col("n_shots") >= 200).sort("oe_pct_regressed", descending=True).head(15)

mbb_shot_quality​

mbb_shot_quality(shots: 'pl.DataFrame', *, model: "'pl.DataFrame | None'" = None, league: 'str' = 'mens', return_as_pandas: 'bool' = False) -> "'Union[pl.DataFrame, pd.DataFrame]'"

Score each shot with xmake / xpoints from the cell table.

Parameters

ParameterTypeDefaultDescription
shotsDataFrameCanonical shot frame.
modelDataFrame | NoneNoneA mbb_shot_quality_model table. When None it is built from shots itself -- convenient, but leakage-safe evaluation should pass a model fit on PRIOR data.
leaguestr'mens'"mens" or "womens".
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

shots's columns plus xmake:Float64, xpoints:Float64 (null for cells absent from the model). Empty input returns the input schema plus the two columns, zero rows.

col_nametypedescription
game_idStringUnique game identifier.
seasonInt64Season year.
team_idStringUnique team identifier.
shooter_idStringUnique identifier for shooter.
shot_xFloat64
shot_yFloat64
dist_ftFloat64
shot_zoneString
shot_typeStringShot type label (e.g. 'Jump Shot', 'Layup').
madeBoolean
point_valueInt64
periodInt64Period of the game (1-4 quarters; 5+ for OT).
sec_leftFloat64
sourceString
xmakeFloat64
xpointsFloat64

Example

from sportsdataverse.mbb import mbb_shot_data, mbb_shot_quality
scored = mbb_shot_quality(mbb_shot_data(2025))

# Pipeline next step (one line)

scored.group_by("team_id").agg(pl.col("xpoints").sum()).sort("xpoints", descending=True)

mbb_shot_quality_model​

mbb_shot_quality_model(shots: 'pl.DataFrame', *, league: 'str' = 'mens', return_as_pandas: 'bool' = False) -> "'Union[pl.DataFrame, pd.DataFrame]'"

Empirical-Bayes zone x type make-rate / xPoints table.

Each cell's raw make rate is shrunk toward its PARENT-ZONE mean by n / (n + k) with k = get_constants(league).shrink_k_zone pseudo-attempts, so sparse cells (e.g. tip-ins in the mid zone) borrow strength from their zone; xpoints = make_rate_shrunk * point_value (the cell's modal point value).

Parameters

ParameterTypeDefaultDescription
shotsDataFrameCanonical shot frame (needs shot_zone, shot_type, made, point_value).
leaguestr'mens'"mens" or "womens" (selects the shrinkage k).
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One row per (shot_zone, shot_type): shot_zone, shot_type, n, make_rate_raw, make_rate_shrunk, point_value, xpoints. Empty input returns the zero-row schema.

col_nametypedescription
shot_zoneString
shot_typeStringShot type label (e.g. 'Jump Shot', 'Layup').
nInt64
make_rate_rawFloat64
make_rate_shrunkFloat64
point_valueFloat64
xpointsFloat64

Example

from sportsdataverse.mbb import mbb_shot_data, mbb_shot_quality_model
model = mbb_shot_quality_model(mbb_shot_data(2025))

# Pipeline next step (one line)

model.sort("xpoints", descending=True).head(5)

mbb_team_ratings​

mbb_team_ratings(seasons: 'int | list[int]', *, league: 'str' = 'mens', return_as_pandas: 'bool' = False) -> 'pl.DataFrame | pd.DataFrame'

Opponent-adjusted team ratings (AdjO/AdjD/AdjEM/AdjTempo) per team-season.

Loads schedule + team boxscore for seasons, computes per-game efficiency, runs the opponent-adjustment fixed points, and adds a per-season dense rank (on adj_em descending) and adj_em_z (z-score of adj_em).

The _blend columns are the same fixed point with the season sim's preseason prior blended in (load_preseason_prior): each Division I team is shrunk toward last season's regressed rating as if it had played prior_games extra games at it (~sportsdataverse.mbb.mbb_prediction_constants.PRESEASON_PRIOR: 4 men, 3 women), so a team with g games is rated about g / (g + prior_games) on this season. The raw columns are unchanged.

Parameters

ParameterTypeDefaultDescription
seasonsint | list[int]A season (e.g. 2024) or list of seasons.
leaguestr'mens'"mens" / "womens" -- selects the constants.
return_as_pandasboolFalseReturn a pandas frame instead of polars.

Returns

One row per (season, team_id) with columns season, team_id, adj_o, adj_d, adj_em, adj_tempo, raw_o, raw_d, games, rank, adj_em_z, then adj_o_blend, adj_d_blend, adj_em_blend, rank_blend (dense, on adj_em_blend) and prior_games. A season whose previous season is not released has no prior: prior_games 0 and the blend equals the raw rating. Empty input returns that schema with zero rows.

col_nametypedescription
seasonInt64Season year.
team_idStringUnique team identifier.
adj_oFloat64Adj o.
adj_dFloat64Adj d.
adj_emFloat64Adj em.
adj_tempoFloat64
raw_oFloat64Raw o.
raw_dFloat64Raw d.
gamesInt64Games played.
rankInt64Rank.
adj_em_zFloat64
adj_o_blendFloat64Adjusted offensive efficiency with the season sim's preseason prior blended in: the same fixed point as adj_o, each Division I team shrunk toward last season's regressed rating as if it had played prior_games extra games at it.
adj_d_blendFloat64Adjusted defensive efficiency with the preseason prior blended in, on the same basis as adj_o_blend (lower is better).
adj_em_blendFloat64adj_o_blend - adj_d_blend: the adjusted efficiency margin with the preseason prior, the rating the pregame projection uses. A team with g games is rated about g / (g + prior_games) on this season.
rank_blendInt64Dense rank of adj_em_blend within the season (1 = best).
prior_gamesFloat64The preseason prior's weight in games (4 men, 3 women, fit by tools/fit_preseason_prior.py); 0 when the previous season is not released, and the blend then equals the raw rating. The five blend columns are in season assets built after 2026-11-13 (#1193); an asset published before then lacks them until the producer rebuilds it.

Raises

ExceptionDescription
InsufficientInputErrorWhen more than 10% of a requested season's games have no turnovers under any turnover key (ESPN's pre-2009 women's box schema), so its possession estimate -- and every rating derived from it -- would be wrong rather than merely missing.

Example

from sportsdataverse.mbb.mbb_team_ratings import mbb_team_ratings
ratings = mbb_team_ratings(2024)
ratings.sort("rank").head()