def sir_likelihood(pop, event_db, recoverytimes, alpha, beta, gamma): """Determine the likelihood of the data from an SIR simulation given alpha, beta, and gamma. """ daily_likelihood=[0]*(np.max(event_db.time)-1) t=1 nonrecovered=find_nonrecovered(event_db, t) susceptible=find_susceptible(pop, event_db, t) for t in range(1, np.max(event_db.time)): new_nonrecovered=find_nonrecovered(event_db, t+1) new_susceptible=find_susceptible(pop, event_db, t+1) infection_probs=infect_prob(pop, alpha, beta, nonrecovered, susceptible) def new_nonrecovered_func(x): return any(susceptible.ind_ID[x] == new_nonrecovered.ind_ID) recovery_index = map(new_nonrecovered_func, susceptible.index) daily_likelihood[t-1]=np.prod(np.subtract(1, infection_probs[np.where(np.invert(recovery_index))]))*np.prod(infection_probs[np.where(recovery_index)]) nonrecovered=new_nonrecovered susceptible=new_susceptible return np.prod(daily_likelihood) *geometric_likelihood(recoverytimes, (1./gamma))
def si_likelihood(pop, event_db, alpha, beta): """Determine the likelihood of the data from an SI simulation given alpha and beta """ daily_likelihood=[0]*(np.max(event_db.time)-1) t=1 infectious=find_infectious(event_db, t) susceptible=find_susceptible(pop, event_db, t) for t in range(1, np.max(event_db.time)): new_infectious=find_infectious(event_db, t+1) new_susceptible=find_susceptible(pop, event_db, t+1) infection_probs=infect_prob(pop, alpha, beta, infectious, susceptible) def new_infections_func(x): return any(susceptible.ind_ID[x] == new_infectious.ind_ID) new_infections = map(new_infections_func, susceptible.index) daily_likelihood[t-1]=np.prod(np.subtract(1, infection_probs[np.where(np.invert(new_infections))]))*np.prod(infection_probs[np.where(new_infections)]) infectious=new_infectious susceptible=new_susceptible return np.prod(daily_likelihood)