2014-01-21 75 views
2

我試圖在PyMC3中組合一個動態系統模型來推斷兩個參數。該模型是基本SIR,在流行病學常用:PyMC3中的簡單動態模型

DS/DT = - R0 * G * S * I

的di/dt = G * I(R *,S - 1)

其中r0和g是要推斷的參數。到目前爲止,我無法做得很好。我所見過的將馬爾可夫鏈組合在一起的唯一例子會產生關於遞歸太深的錯誤。這是我的示例代碼。

# Time 
t = np.linspace(0, 8, 200) 

# Simulated observation 
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] 

# Currently no noise, we just want to infer params r0 = 16 and g = 0.5 
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.) 

    # Use forward Euler to solve 
    dt = t[1] - t[0] 

    # Initial conditions 
    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]) 

    #start = pymc.find_MAP() 
    trace = pymc.sample(2000, pymc.NUTS()) 

任何幫助將不勝感激。謝謝 !

+1

您是否能夠取得進展? –

回答

0

我會嘗試定義一個新的分佈。像下面這樣。但是,這不是很有效,我不太確定我做錯了什麼。

class SIR(Distribution): 
def __init__(self, gamma, r0,dt, std): 
    self.gamma = gamma 
    self.r0 = r0 
    self.std = std 
    self.dt = dt 

def logp(self, SI): 
    r0 = self.r0 
    std = self.std 
    gamma = self.gamma 
    dt = self.dt 

    S=SI[:,0] 
    I=SI[:,1] 

    Si = S[1:] 
    Si_m1 = S[:-1] 
    Ii = I[1:] 
    Ii_m1 = I[:-1] 

    Sdelta = (Si - Si_m1) 
    Idelta = (Ii - Ii_m1) 

    Sexpected_delta = dt* (-r0 * gamma * Si_m1 * Ii_m1) 
    Iexpected_delta = dt * gamma * Ii_m1 *(r0 * Si_m1 - 1) 


    return (Normal.dist(Sexpected_delta, sd=std).logp(Sdelta) + 
      Normal.dist(Iexpected_delta, sd=std).logp(Idelta)) 


with Model() as model: 
    r0 = pymc.Normal("r0", 15, sd=10) 
    gamma = pymc.Normal("gamma", 0.3, 1.) 
    std = .5 
    dt = t[1]-t[0] 


    SI = SIR('SI', gamma, r0, std,dt, observed=solution[:,:2]) 

    #start = pymc.find_MAP(start={'gamma' : .45, 'r0' : 17}) 
    trace = pymc.sample(2000, pymc.NUTS()) 
+0

非常感謝您的解決方案。不幸的是,我還沒有能夠對此進行測試,儘管它可能會讓我去某個地方。對延誤抱歉,我希望儘快通知你。 – Quentin