Skip to content

Instantly share code, notes, and snippets.

@jdmoore7
Last active June 12, 2022 18:53
Show Gist options
  • Save jdmoore7/4a50a9da385f4ac9fbbdca1b47fecc1b to your computer and use it in GitHub Desktop.
Save jdmoore7/4a50a9da385f4ac9fbbdca1b47fecc1b to your computer and use it in GitHub Desktop.
Bayesian 2PL implementation with PyMC3
import pymc3 as pm
from theano import tensor as tt
import arviz as az
import numpy as np
# Binary, correct answer array
scores = np.array([1,1,1,0,0,0
]).flatten()
# (student:question) tuples
# order corresponds to scores
student_question_map = [(0,1), (0,2), (0,3),
(1,2), (1,3),
(3,4)]
with pm.Model() as model:
# Priors
questions = pm.Normal("questions", mu=0,
sigma=1,
shape=(5,))
students = pm.Normal("students", mu=0,
sigma=1,
shape=(5,))
slopes = pm.Normal("slopes", mu=0,
sigma=1,
shape=(5,))
# Transformed parameter
deltas = []
for s_idx, q_idx in student_question_map:
s = students[s_idx,]
q = questions[q_idx,]
d = slopes[q_idx]
deltas.append( d * (s - q) )
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=["questions", "students", "slopes"] ,compact=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment