Scoring¶
FantasyScorer¶
Converts NFL stats into fantasy points using the league's scoring rules.
fantasyfb.scoring.fantasy_scoring.FantasyScorer ¶
Calculates fantasy points from player statistics using configurable scoring rules.
This class extracts the core scoring logic to work with any fantasy platform that can provide player stats in the expected DataFrame format.
Note: Yahoo also exposes a generic 'TD' scoring category alongside the specific Pass/Rush/Rec/Ret/Off Fumb TD categories above. Every TD pathway nflreadpy exposes is already covered by one of those specific categories, so 'TD' is intentionally left unwired here -- scoring it independently would risk double-counting the same touchdown twice.
Initialize scorer with league-specific scoring settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scoring_settings
|
Dict[str, float]
|
Dictionary mapping stat categories to point values.
Expected keys include:
- 'Rush Yds', 'Rush Att', 'Rush TD', 'Rush 1D'
- 'Rec', 'Rec Yds', 'Rec TD', 'Rec 1D' |
required |
calculate_points ¶
Calculate fantasy points for all players in the stats DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats_df
|
DataFrame
|
DataFrame with player statistics. Expected columns include: - position: Player position ('QB', 'RB', 'WR', 'TE', 'K', 'DEF') - Standard stat columns like rush_yds, rec, pass_td, etc. - For DEF: points_allowed, sacks, def_int, etc. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with added 'points' column containing calculated fantasy points. |
MatchupModel¶
Per-game multiplicative matchup factor: team implied scoring total
(Vegas), opponent's allowed fantasy points to the player's position,
and depth-chart string, each folded around 1.0 so
points_avg = points_rate * factor. Replaces V1's
basal + opp_elo_weight*elo_diff + string_weight*(1-string) formula.
fantasyfb.scoring.matchup_model.MatchupModel ¶
Per-game multiplicative matchup factor.
Build via :meth:from_history, which precomputes the league-wide
distributions of implied total and allowed-vs-position used to
z-score each input. Then call :meth:apply_factors to attach a
matchup_factor column to a players DataFrame for a given week.
from_history
classmethod
¶
from_history(stats_df: DataFrame, schedule_df: DataFrame, weights: Optional[Dict[str, _PositionWeights]] = None) -> 'MatchupModel'
Fit the league-average baselines from historical data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats_df
|
DataFrame
|
per-game player stats with |
required |
schedule_df
|
DataFrame
|
per-team-game schedule with |
required |
weights
|
Optional[Dict[str, _PositionWeights]]
|
optional per-position coefficient overrides. |
None
|
factor ¶
factor(position: str, team_implied_total: float, opp_implied_total: float, opp_team: str, string: float = 1.0) -> float
Per-player matchup multiplier centered at 1.0.
apply_factors ¶
Attach a matchup_factor column to a players-shaped DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
players
|
DataFrame
|
must have |
required |
schedule
|
DataFrame
|
provider schedule output (one row per team-week). |
required |
as_of
|
int
|
YYYYWW of the week we're projecting. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
A copy of |
DataFrame
|
inputs (team implied total, opp_team, opp allowed) joined on |
DataFrame
|
for debuggability. |
LineupOptimizer¶
Picks the best legal starting lineup given per-player projections and the league's roster slots.
fantasyfb.scoring.lineup_optimizer.LineupOptimizer ¶
Handles optimal lineup selection for fantasy football teams.
Initialize the lineup optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roster_spots
|
DataFrame with roster position requirements |
required | |
teams
|
List of team dictionaries |
required | |
client
|
FantasyPlatformClient
|
FantasyPlatformClient instance |
required |
set_optimal_lineup ¶
Identifies which players should be started on each fantasy team based on fantasy point projections and available roster spots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
players
|
DataFrame with player data including projections |
required | |
week
|
Week for which to identify starters |
required | |
season
|
Current season |
required | |
current_week
|
The actual current week |
required | |
latest_season
|
Most recent NFL season |
required | |
nfl_schedule
|
NFL schedule with Vegas implied totals |
required | |
matchup_model
|
MatchupModel that produces the per-week multiplicative factor on points_rate. Replaces V1's basal/opp_elo/string_weight formula. |
required |
Returns:
| Type | Description |
|---|---|
|
DataFrame with players marked as starters and injury/game factors added |