Skip to content

Instantly share code, notes, and snippets.

@jdmoore7
Created June 12, 2022 19:36
Show Gist options
  • Save jdmoore7/4a1ce11b3e277d464c6e484cb15b2918 to your computer and use it in GitHub Desktop.
Save jdmoore7/4a1ce11b3e277d464c6e484cb15b2918 to your computer and use it in GitHub Desktop.
Bayesian Sports Ranking with PyMC3
import pymc3 as pm
from theano import tensor as tt
import arviz as az
import numpy as np
scores = np.array([1,1,1,0,0,0
]).flatten()
games = [(0,1), (0,2), (0,3),
(1,2), (1,3),
(3,4)]
with pm.Model() as model:
z_team = pm.Normal("team", mu=0, sigma=1, shape=(5,))
# Transformed parameter
deltas = []
for left, right in games:
team_left = z_team[left,]
team_right = z_team[right,]
deltas.append(team_left - team_right)
thetas = pm.Deterministic("theta", tt.nnet.sigmoid(deltas))
# Likelihood
kij = pm.Bernoulli("kij", p=thetas, observed=scores)
trace = pm.sample(chains=4, )
az.plot_trace(trace, var_names=["team", "theta"] ,compact=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment