Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clip negative values to to avoid NaN correlation term #7

Merged
merged 4 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions bpl/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def dixon_coles_correlation_term(
home_rate: jnp.array,
away_rate: jnp.array,
corr_coef: jnp.array,
tol: float = 0, # FIXME workaround to clip negative values to tol to avoid NaNs
) -> jnp.array:
# correlation term from dixon and coles paper
corr_term = jnp.zeros_like(home_rate)
Expand All @@ -19,28 +20,39 @@ def dixon_coles_correlation_term(
corr_term,
(..., nil_nil),
jnp.log(
1.0
- corr_coef[..., None] * home_rate[..., nil_nil] * away_rate[..., nil_nil]
jnp.clip(
1.0
- corr_coef[..., None]
* home_rate[..., nil_nil]
* away_rate[..., nil_nil],
a_min=tol,
)
),
)

one_nil = (home_goals == 1) & (away_goals == 0)
corr_term = jax.ops.index_update(
corr_term,
(..., one_nil),
jnp.log(1.0 + corr_coef[..., None] * away_rate[..., one_nil]),
jnp.log(
jnp.clip(1.0 + corr_coef[..., None] * away_rate[..., one_nil], a_min=tol)
),
)

nil_one = (home_goals == 0) & (away_goals == 1)
corr_term = jax.ops.index_update(
corr_term,
(..., nil_one),
jnp.log(1.0 + corr_coef[..., None] * home_rate[..., nil_one]),
jnp.log(
jnp.clip(1.0 + corr_coef[..., None] * home_rate[..., nil_one], a_min=tol)
),
)

one_one = (home_goals == 1) & (away_goals == 1)
corr_term = jax.ops.index_update(
corr_term, (..., one_one), jnp.log(1.0 - corr_coef[..., None])
corr_term,
(..., one_one),
jnp.log(jnp.clip(1.0 - corr_coef[..., None], a_min=tol)),
)

return corr_term
1 change: 0 additions & 1 deletion bpl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,3 @@ def predict_concede_n_proba(

# sum probability all scorelines where team conceded n goals
return probs.sum(axis=0)

2 changes: 1 addition & 1 deletion bpl/dixon_coles.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from numpyro.infer import MCMC, NUTS
from numpyro.infer.reparam import LocScaleReparam

from bpl.base import BaseMatchPredictor
from bpl._util import dixon_coles_correlation_term
from bpl.base import BaseMatchPredictor

__all__ = ["DixonColesMatchPredictor"]

Expand Down
2 changes: 1 addition & 1 deletion bpl/extended_dixon_coles.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from numpyro.infer import MCMC, NUTS
from numpyro.infer.reparam import LocScaleReparam

from bpl.base import BaseMatchPredictor
from bpl._util import dixon_coles_correlation_term
from bpl.base import BaseMatchPredictor

__all__ = ["ExtendedDixonColesMatchPredictor"]

Expand Down