inference
inference
¶
Probabilistic inference modules for FPLX.
HMMInference
¶
HMMInference(
transition_matrix: Optional[ndarray] = None,
emission_params: Optional[dict] = None,
initial_dist: Optional[ndarray] = None,
)
Hidden Markov Model for discrete player form states.
Supports dynamic transition matrix perturbation so that external signals (news, injuries) can shift state probabilities mid-sequence.
| PARAMETER | DESCRIPTION |
|---|---|
transition_matrix
|
transition_matrix[i,j] = P(S_{t+1}=j | S_t=i). Rows must sum to 1.
TYPE:
|
emission_params
|
{state_index: (mean, std)} for Gaussian emissions.
TYPE:
|
initial_dist
|
Prior over initial state.
TYPE:
|
Source code in fplx/inference/hmm.py
inject_news_perturbation
¶
Perturb transition matrix at a specific timestep based on news.
For each source state, the transition probability toward boosted target states is multiplied by the boost factor (scaled by confidence), then the row is renormalized.
| PARAMETER | DESCRIPTION |
|---|---|
timestep
|
The gameweek at which the perturbation applies.
TYPE:
|
state_boost
|
{target_state: multiplicative_boost}. E.g., {0: 10.0} means "10x more likely to transition to Injured."
TYPE:
|
confidence
|
Scales the perturbation. 0 = no effect, 1 = full effect.
TYPE:
|
Source code in fplx/inference/hmm.py
clear_perturbations
¶
forward
¶
Forward algorithm with dynamic transition matrices.
| PARAMETER | DESCRIPTION |
|---|---|
observations
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
forward_messages
|
Normalized forward messages. forward_messages[t] = P(S_t | y_1:t)
TYPE:
|
scale
|
Per-timestep normalization constants.
TYPE:
|
Source code in fplx/inference/hmm.py
forward_backward
¶
Compute smoothed posteriors P(S_t | y_1:num_timesteps).
| PARAMETER | DESCRIPTION |
|---|---|
observations
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
smoothed_posteriors
|
smoothed_posteriors[t, s] = P(S_t=s | y_1:num_timesteps)
TYPE:
|
Source code in fplx/inference/hmm.py
viterbi
¶
Most likely state sequence via Viterbi decoding.
| PARAMETER | DESCRIPTION |
|---|---|
observations
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
best_path
|
TYPE:
|
Source code in fplx/inference/hmm.py
predict_next
¶
Predict next timestep's points distribution.
Runs forward algorithm, then propagates one step ahead via the transition matrix.
| PARAMETER | DESCRIPTION |
|---|---|
observations
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
expected_points
|
E[Y_{num_timesteps+1} | y_1:num_timesteps]
TYPE:
|
variance
|
Var[Y_{num_timesteps+1} | y_1:num_timesteps] (from law of total variance)
TYPE:
|
next_state_dist
|
P(S_{num_timesteps+1} | y_1:num_timesteps)
TYPE:
|
Source code in fplx/inference/hmm.py
fit
¶
Learn transition matrix and emission parameters via Baum-Welch EM.
| PARAMETER | DESCRIPTION |
|---|---|
observations
|
Training sequence.
TYPE:
|
n_iter
|
Maximum EM iterations.
TYPE:
|
tol
|
Convergence tolerance on log-likelihood.
TYPE:
|
verbose
|
Print progress.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
self
|
|
Source code in fplx/inference/hmm.py
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 | |
KalmanFilter
¶
KalmanFilter(
process_noise: float = 1.0,
observation_noise: float = 4.0,
initial_state_mean: float = 4.0,
initial_state_covariance: float = 2.0,
)
1D Kalman Filter for tracking latent point potential.
| PARAMETER | DESCRIPTION |
|---|---|
process_noise
|
Default process noise variance (form drift rate).
TYPE:
|
observation_noise
|
Default observation noise variance (weekly point noise).
TYPE:
|
initial_state_mean
|
Initial state estimate.
TYPE:
|
initial_state_covariance
|
Initial state uncertainty (variance).
TYPE:
|
Source code in fplx/inference/kalman.py
inject_process_shock
¶
Inflate process noise at a specific timestep.
Use when news indicates a sudden form change (injury, transfer). process_noise_t = default_process_noise * multiplier.
| PARAMETER | DESCRIPTION |
|---|---|
timestep
|
Gameweek index.
TYPE:
|
multiplier
|
Process noise multiplier (>1 = more uncertainty about form drift).
TYPE:
|
Source code in fplx/inference/kalman.py
inject_observation_noise
¶
Adjust observation noise at a specific timestep.
Use for fixture difficulty: harder opponents → less predictable points. observation_noise_t = default_observation_noise * factor.
| PARAMETER | DESCRIPTION |
|---|---|
timestep
|
Gameweek index.
TYPE:
|
factor
|
Observation noise factor (>1 = harder fixture, noisier observation).
TYPE:
|
Source code in fplx/inference/kalman.py
clear_overrides
¶
get_process_noise_override
¶
set_noise_overrides
¶
set_noise_overrides(
process_noise_overrides: dict[int, float],
observation_noise_overrides: dict[int, float],
)
Replace per-timestep noise overrides.
Source code in fplx/inference/kalman.py
copy_with_overrides
¶
copy_with_overrides(
max_timestep: Optional[int] = None,
) -> KalmanFilter
Create a parameter-identical filter with copied noise overrides.
| PARAMETER | DESCRIPTION |
|---|---|
max_timestep
|
If provided, only overrides for timesteps <= max_timestep are copied.
TYPE:
|
Source code in fplx/inference/kalman.py
filter
¶
Run Kalman filter on observations with per-timestep noise.
| PARAMETER | DESCRIPTION |
|---|---|
observations
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
filtered_state_means
|
Filtered state estimates (posterior mean).
TYPE:
|
filtered_state_covariances
|
Filtered state uncertainties (posterior variance).
TYPE:
|
Source code in fplx/inference/kalman.py
predict_next
¶
Predict next observation with uncertainty.
Returns the predictive distribution for Y_{t+1} (the observation), not X_{t+1} (the latent state). This ensures consistency with the HMM predict_next which also returns observation-level variance.
Var[Y_{t+1}] = Var[X_{t+1}|y_{1:t}] + R = (P_t + Q) + R
Must call filter() first.
| RETURNS | DESCRIPTION |
|---|---|
predicted_mean
|
E[Y_{t+1} | y_{1:t}].
TYPE:
|
predicted_var
|
Var[Y_{t+1} | y_{1:t}] (observation-level, includes R).
TYPE:
|
Source code in fplx/inference/kalman.py
smooth
¶
Run RTS smoother (backward pass after forward Kalman filter).
| PARAMETER | DESCRIPTION |
|---|---|
observations
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
smoothed_state_means
|
Smoothed state estimates.
TYPE:
|
smoothed_state_covariances
|
Smoothed state uncertainties.
TYPE:
|
Source code in fplx/inference/kalman.py
MultivariateHMM
¶
MultivariateHMM(
position: str = "MID",
transition_matrix: Optional[ndarray] = None,
initial_dist: Optional[ndarray] = None,
)
Position-aware HMM with multivariate diagonal Gaussian emissions.
| PARAMETER | DESCRIPTION |
|---|---|
position
|
GK, DEF, MID, FWD. Determines feature set and default emissions.
TYPE:
|
Source code in fplx/inference/multivariate_hmm.py
inject_news_perturbation
¶
Perturb transition matrix at timestep (same API as scalar HMM).
Source code in fplx/inference/multivariate_hmm.py
forward
¶
Forward algorithm. observations: (T, D).
Source code in fplx/inference/multivariate_hmm.py
forward_backward
¶
Smoothed posteriors P(S_t | y_{1:T}).
Source code in fplx/inference/multivariate_hmm.py
viterbi
¶
Most likely state sequence.
Source code in fplx/inference/multivariate_hmm.py
predict_next_features
¶
Predict next gameweek's feature vector.
Returns mean, var (per feature), and state distribution.
Source code in fplx/inference/multivariate_hmm.py
one_step_point_predictions
¶
One-step-ahead point predictions for each historical timestep.
Returns array preds where preds[t] predicts points at timestep t, using information up to t-1 (preds[0] is NaN).
Source code in fplx/inference/multivariate_hmm.py
predict_next_points
¶
Convert predicted features → expected FPL points.
Uses FPL scoring rules applied to predicted feature rates.
Source code in fplx/inference/multivariate_hmm.py
fit
¶
Baum-Welch EM with MAP-style prior interpolation.
| PARAMETER | DESCRIPTION |
|---|---|
observations
|
Feature matrix with shape (T, D).
TYPE:
|
n_iter
|
Maximum EM iterations.
TYPE:
|
tol
|
Convergence tolerance on log-likelihood.
TYPE:
|
prior_weight
|
Weight on prior parameters in [0, 1]. Higher values increase regularization toward position-level default emissions/transitions.
TYPE:
|
Source code in fplx/inference/multivariate_hmm.py
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 | |
InferenceResult
dataclass
¶
InferenceResult(
filtered_beliefs: ndarray,
smoothed_beliefs: ndarray,
viterbi_path: ndarray,
hmm_predicted_mean: float = 0.0,
hmm_predicted_var: float = 0.0,
kalman_filtered: ndarray = (lambda: array([]))(),
kalman_uncertainty: ndarray = (lambda: array([]))(),
kf_predicted_mean: float = 0.0,
kf_predicted_var: float = 0.0,
fused_mean: ndarray = (lambda: array([]))(),
fused_var: ndarray = (lambda: array([]))(),
fusion_alpha: Optional[float] = None,
predicted_mean: float = 0.0,
predicted_var: float = 0.0,
)
Container for inference pipeline outputs.
PlayerInferencePipeline
¶
PlayerInferencePipeline(
hmm_params: Optional[dict] = None,
kf_params: Optional[dict] = None,
hmm_variance_floor: float = 1.0,
news_params: Optional[dict] = None,
fusion_mode: str = "precision",
fusion_params: Optional[dict] = None,
)
Orchestrates HMM + Kalman inference for a single player.
| PARAMETER | DESCRIPTION |
|---|---|
hmm_params
|
Override HMM parameters: transition_matrix, emission_params, initial_dist.
TYPE:
|
kf_params
|
Override Kalman parameters: Q, R, x0, P0.
TYPE:
|
Source code in fplx/inference/pipeline.py
ingest_observations
¶
Set the player's historical points sequence.
| PARAMETER | DESCRIPTION |
|---|---|
points
|
Weekly points history.
TYPE:
|
Source code in fplx/inference/pipeline.py
inject_news
¶
Inject a news signal into the inference at a specific gameweek.
Bridges from existing NewsSignal.generate_signal() output format.
| PARAMETER | DESCRIPTION |
|---|---|
news_signal
|
Output from NewsSignal.generate_signal(). Must contain: 'availability', 'minutes_risk', 'confidence'.
TYPE:
|
timestep
|
The gameweek index to apply the perturbation.
TYPE:
|
Source code in fplx/inference/pipeline.py
inject_fixture_difficulty
¶
Inject fixture difficulty into Kalman observation noise.
| PARAMETER | DESCRIPTION |
|---|---|
difficulty
|
Fixture difficulty score (1-5, from FixtureSignal).
TYPE:
|
timestep
|
The gameweek index.
TYPE:
|
Source code in fplx/inference/pipeline.py
run
¶
run() -> InferenceResult
Run full inference pipeline: HMM + Kalman + Fusion.
| RETURNS | DESCRIPTION |
|---|---|
InferenceResult
|
All inference outputs. |
Source code in fplx/inference/pipeline.py
predict_next
¶
Get the fused one-step-ahead forecast.
| RETURNS | DESCRIPTION |
|---|---|
expected_points
|
TYPE:
|
variance
|
TYPE:
|
Source code in fplx/inference/pipeline.py
learn_parameters
¶
Run Baum-Welch to learn HMM parameters from current observations.
Call this before run() if you want data-driven parameters.
Source code in fplx/inference/pipeline.py
batch_enriched_predict
¶
Run enriched prediction for all players. Returns ep, var, downside_risk dicts.
Source code in fplx/inference/enriched.py
compute_xpoints
¶
Compute per-GW expected points from ALL underlying components.
Source code in fplx/inference/enriched.py
enriched_predict
¶
Predict expected points with fixture awareness and semi-variance.
| PARAMETER | DESCRIPTION |
|---|---|
timeseries
|
TYPE:
|
position
|
TYPE:
|
alpha
|
EWMA decay.
TYPE:
|
lookback
|
Max recent GWs (increased from 10 to 15 for more data).
TYPE:
|
upcoming_fixture
|
{"was_home": bool, "opponent_team": int, "xP": float}
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
expected_points
|
TYPE:
|
variance
|
TYPE:
|
downside_risk
|
TYPE:
|
Source code in fplx/inference/enriched.py
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 | |
fuse_estimates
¶
fuse_estimates(
hmm_mean: float,
hmm_var: float,
kf_mean: float,
kf_var: float,
) -> tuple[float, float]
Fuse a single HMM estimate with a single Kalman estimate.
Uses inverse-variance weighting: fused_mean = (hmm_mean/hmm_var + kf_mean/kf_var) / (1/hmm_var + 1/kf_var) fused_var = 1 / (1/hmm_var + 1/kf_var)
| PARAMETER | DESCRIPTION |
|---|---|
hmm_mean
|
HMM expected points (from state posterior weighted emission means).
TYPE:
|
hmm_var
|
HMM variance (law of total variance over state posterior).
TYPE:
|
kf_mean
|
Kalman filtered point estimate.
TYPE:
|
kf_var
|
Kalman filtered uncertainty (posterior variance).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
fused_mean
|
TYPE:
|
fused_var
|
TYPE:
|
Source code in fplx/inference/fusion.py
fuse_sequences
¶
fuse_sequences(
hmm_gamma: ndarray,
kalman_x: ndarray,
kalman_P: ndarray,
emission_params: dict,
) -> tuple[ndarray, ndarray]
Fuse full sequences of HMM posteriors and Kalman estimates.
| PARAMETER | DESCRIPTION |
|---|---|
hmm_gamma
|
Smoothed state posteriors from HMM.
TYPE:
|
kalman_x
|
Kalman filtered estimates.
TYPE:
|
kalman_P
|
Kalman filtered uncertainties.
TYPE:
|
emission_params
|
{state_index: (mean, std)} from HMM.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
fused_mean
|
TYPE:
|
fused_var
|
TYPE:
|
Source code in fplx/inference/fusion.py
build_feature_matrix
¶
Extract position-specific feature matrix from player timeseries.
| PARAMETER | DESCRIPTION |
|---|---|
timeseries
|
Player gameweek history from vaastav dataset.
TYPE:
|
position
|
GK, DEF, MID, or FWD.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
np.ndarray, shape (T, D) where D depends on position.
|
|