I am trying to build a dynamic system model in PyMC3 in order to infer two parameters. The model is the main SIR commonly used in epidemiology:
dS / dt = - r0 * g * S * I
dI / dt = g * i (r * S - 1)
where r0 and g are the parameters that should be inferred. So far I can’t go far. The only examples I saw to put together a Markov chain like this bring errors in too deep recursion. Here is my sample code.
t = np.linspace(0, 8, 200)
def SIR(y, t, r0, gamma) :
S = - r0 * gamma * y[0] * y[1]
I = r0 * gamma * y[0] * y[1] - gamma * y[1]
return [S, I]
solution = odeint(SIR, [0.99, 0.01, 0], t, args=(16., 0.5))
with pymc.Model() as model :
r0 = pymc.Normal("r0", 15, sd=10)
gamma = pymc.Uniform("gamma", 0.3, 1.)
dt = t[1] - t[0]
S = [0.99]
I = [0.01]
for i in range(1, len(t)) :
S.append(pymc.Normal("S%i" % i, \
mu = S[-1] + dt * (-r0 * gamma * S[-1] * I[-1]), \
sd = solution[:, 0].std()))
I.append(pymc.Normal("I%i" % i, \
mu = I[-1] + dt * ( r0 * gamma * S[-1] * I[-1] - gamma * I[-1]), \
sd = solution[:, 1].std()))
Imcmc = pymc.Normal("Imcmc", mu = I, sd = solution[:, 1].std(), observed = solution[:, 1])
trace = pymc.sample(2000, pymc.NUTS())
Any help would be greatly appreciated. Thanks!