def test_deterministic(self): ode = DeterministicOde(self.states, self.params, birth_death=self.birth_deaths, transition=self.transitions) ode.parameters = self.param_eval ode.initial_values = (self.x0, self.t[0]) _solution = ode.integrate(self.t[1::])
def test_Vector_State1(self): # state is a vector state_list = ['y1:4'] param_list = [] # transitions call from the vector transition_list = [ Transition(origin='y[0]', destination='y[1]', equation='0.04*y[0]', transition_type=TransitionType.T), Transition(origin='y[1]', destination='y[0]', equation='1e4*y[1]*y[2]', transition_type=TransitionType.T), Transition(origin='y[1]', destination='y[2]', equation='3e7*y[1]*y[1]', transition_type=TransitionType.T) ] # initialize the model ode = DeterministicOde(state_list, param_list, transition=transition_list) ode.get_ode_eqn() t = numpy.append(0, 4*numpy.logspace(-6, 6, 1000)) ode.initial_values = ([1.0, 0.0, 0.0], t[0]) # try to integrate to see if there is any problem _solution, _output = ode.integrate(t[1::], full_output=True)
def test_Vector_State3(self): # state is a vector state_list = [ODEVariable('y1', 'y1'), ODEVariable('y2', 's'), ODEVariable('y3', 'x')] param_list = [] # transitions are explicit names transition_list = [ Transition(origin='y1', destination='y2', equation='0.04*y1', transition_type=TransitionType.T), Transition(origin='y2', destination='y1', equation='1e4*y2*y3', transition_type=TransitionType.T), Transition(origin='y2', destination='y3', equation='3e7*y2*y2', transition_type=TransitionType.T) ] ode = DeterministicOde(state_list, param_list, transition=transition_list) ode.get_ode_eqn() t = numpy.append(0, 4*numpy.logspace(-6, 6, 1000)) ode.initial_values = ([1.0, 0.0, 0.0], t[0]) # try to integrate to see if there is any problem solution, output = ode.integrate(t[1::], full_output=True)
def setUp(self): # initial values N = 2362205.0 self.x0 = [N, 3.0, 0.0] self.t = np.linspace(0, 150, 100).astype('float64') # params param_eval = [('beta', 0.5), ('gamma', 1.0 / 3.0), ('N', N)] state_list = ['S', 'I', 'R'] param_list = ['beta', 'gamma', 'N'] transition_list = [ Transition(origin='S', destination='I', equation='beta * S * I/N', transition_type=TransitionType.T), Transition(origin='I', destination='R', equation='gamma * I', transition_type=TransitionType.T) ] # initialize the model self.ode = DeterministicOde(state_list, param_list, transition=transition_list) self.ode.parameters = param_eval self.ode.initial_values = (self.x0, self.t[0]) # Standard. Find the solution. self.solution = self.ode.integrate(self.t[1::]) # initial value self.theta = np.array([0.4, 0.3]) # constraints EPSILON = np.sqrt(np.finfo(np.float).eps) self.box_bounds = [(EPSILON, 2), (EPSILON, 2)] self.target = np.array([0.5, 1.0 / 3.0])
Q = list(df['Q']) C = list(df['C']) R = list(df['R']) y = [] t = [] for i in range(1, len(S)): y.append([S[i], E[i], I[i], Q[i], R[i]]) t.append(i) states = ['S', 'E', 'I', 'Q', 'C', 'R'] params = ['A', 'q', 'mu1', 'mu2', 'mu3', 'd1', 'd2','d3', 'delta', 'delta1', 'delta2', 'p1', 'alpha', 'beta'] odeList = [ Transition(origin='S',equation='A - (delta*S) - (beta*S*I) - (alpha*C*S) - q*S',transition_type=TransitionType.ODE), Transition(origin='E',equation='(beta*I*S) - (delta*E) - (delta1*E) + (alpha*C*S)',transition_type=TransitionType.ODE), Transition(origin='I',equation='delta1*E - (delta*I) - (delta2*I) - (mu1*I) - (d1*I)',transition_type=TransitionType.ODE), Transition(origin='Q',equation='(p1*delta2*I) - (delta*Q) - (mu2*Q) - (d2*Q)',transition_type=TransitionType.ODE), Transition(origin='C',equation='(delta2*I*(1-p1)) - (delta*C) - (mu3*C) - (d3*C)',transition_type=TransitionType.ODE), Transition(origin='R',equation='mu1*I + mu2*Q + mu3*C - delta*R',transition_type=TransitionType.ODE)] model = DeterministicOde (states, params, ode =odeList) init_state = [60461803,2703,94, 127, 2948,1] param_eval = [('A', 0.007896),('q',0.01), ('mu1', 0.4554), ('mu2', 1.21382), ('mu3', 0.1325), ('d1',0.24203), ('d2', 0.55586),('d3', 0.07849), ('delta', 0.000213), ('delta1',0.196), ('delta2',0.996), ('alpha',0.000000196), ('beta', 0.000034196), ('p1',0.96)] model.intial_values = (init_state, [0]) model.parameters = param_eval # sol = odeint(model.ode, init_state, t[1:]) # print(sol) # print(sol.size) theta = [0.5, 0.9, 0.05, 0.05] bounds = [(0,1),(0,1),(0,1),(0,1)] objFH = SquareLoss(theta=theta, ode=model, x0=init_state, t0=[0], t=t, y=y, state_name=['S', 'E', 'I', 'Q', 'R'], target_param = ['delta2', 'p1', 'alpha', 'beta']) res = minimize(fun=objFH.cost, jac=objFH.sensitivity, x0=theta, bounds = bounds, options={'disp': True})
class TestSIRDiscreteEstimate(TestCase): def setUp(self): # initial values N = 2362205.0 self.x0 = [N, 3.0, 0.0] self.t = np.linspace(0, 150, 100).astype('float64') # params param_eval = [('beta', 0.5), ('gamma', 1.0 / 3.0), ('N', N)] state_list = ['S', 'I', 'R'] param_list = ['beta', 'gamma', 'N'] transition_list = [ Transition(origin='S', destination='I', equation='beta * S * I/N', transition_type=TransitionType.T), Transition(origin='I', destination='R', equation='gamma * I', transition_type=TransitionType.T) ] # initialize the model self.ode = DeterministicOde(state_list, param_list, transition=transition_list) self.ode.parameters = param_eval self.ode.initial_values = (self.x0, self.t[0]) # Standard. Find the solution. self.solution = self.ode.integrate(self.t[1::]) # initial value self.theta = np.array([0.4, 0.3]) # constraints EPSILON = np.sqrt(np.finfo(np.float).eps) self.box_bounds = [(EPSILON, 2), (EPSILON, 2)] self.target = np.array([0.5, 1.0 / 3.0]) def test_SIR_Estimate_PoissonLoss_1TargetState(self): obj = PoissonLoss(self.theta, self.ode, self.x0, self.t[0], self.t[1::], np.round(self.solution[1::, 2]), 'R', target_param=['beta', 'gamma']) res = scipy.optimize.minimize(fun=obj.cost, jac=obj.sensitivity, x0=self.theta, method='L-BFGS-B', bounds=self.box_bounds) self.assertTrue(np.allclose(res['x'], self.target)) def test_SIR_Estimate_PoissonLoss_2TargetState(self): # note that we need to round the observations to integer for it # to make sense obj = PoissonLoss(self.theta, self.ode, self.x0, self.t[0], self.t[1::], np.round(self.solution[1::, 1:3]), ['I', 'R'], target_param=['beta', 'gamma']) res = scipy.optimize.minimize(fun=obj.cost, jac=obj.sensitivity, x0=self.theta, method='L-BFGS-B', bounds=self.box_bounds) self.assertTrue(np.allclose(res['x'], self.target))
Transition(origin='Hospitalized', equation='sigma_H * Hospitalized', transition_type=TransitionType.D) ] derived_parameters = [ ('lda', 'beta * ((Infected + (eta*Hospitalized))/N)'), ('N', 'Susceptible_lr + Susceptible_hr + Exposed + Infected + Hospitalized + Recovered' ) ] # Build the model system model_system = DeterministicOde(states, parameters, transition=transitions, birth_death=births_deaths, derived_param=derived_parameters) model_system.print_ode() # Create the timeline t = numpy.linspace(0, 200, 200) # Set the inital values for the states model_system.initial_values = ([1000000, 20000, 15, 10, 0, 0], t[0]) liberia_parameters = { 'phi_H': 1.6, 'sigma_I': 0.1, 'sigma_H': 0.5, 'theta_I': 0.1,
def ODE_model(N0): """This more complicated model can be used to fit for detected cases and hospitalisations (essentially the same thing). compartments are currently gamma distributed (shape=2). Will want to consider shape=3 distributions also. args: N0: Population returns: model: a pygom ODEmodel object """ states = ['S', # susceptible 'E0', 'E1',# incubating 'E2', 'I', # infectious (mild symptoms) 'Id', # infectious detected 'Iu', # infectious undetected 'Id_cum', #Storing cummulative infections 'R',] # Removed (recoveries + fatalities) parameters = ['beta', # infectivity of symptomatic cases 'alpha0', 'alpha1', 'alpha2',# incubation period (time between being infected and developing symptoms) 'gamma', # hospitalisation 'kappa', 'delta1', 'delta2', # proportion of hospitalised/detected cases 'N0'] # population transitions = [Transition(origin='S', destination='E0', equation='beta*S*(I+Iu+Id)/(N0)', transition_type=TransitionType.T), # transmission from symptomatic cases Transition(origin='E0', destination='E1', equation='E0/alpha0', transition_type=TransitionType.T), Transition(origin='E1', destination='E2', equation='E1/alpha1', transition_type=TransitionType.T), # latent to mild symptoms Transition(origin='E2', destination='I', equation='E2/alpha2', transition_type=TransitionType.T), # latent to mild symptoms Transition(origin='I', destination='Id', equation='kappa*I/gamma', transition_type=TransitionType.T), # mild symptoms to severe symptoms/hospitalisation Transition(origin='I', destination='Iu', equation='(1-kappa)*I/gamma', transition_type=TransitionType.T), # mild symptoms to undetected Transition(origin='Id', destination='R', equation='Id/delta1', transition_type=TransitionType.T), Transition(origin='Iu', destination='R', equation='Iu/delta2', transition_type=TransitionType.T)] births = Transition(origin='Id_cum', equation='kappa*I/gamma', transition_type=TransitionType.B) model = DeterministicOde(states, parameters, transition=transitions, birth_death=births) model.parameters = {'beta':1.3, 'alpha0':2.1, 'alpha1':2, 'alpha2':1.1, 'gamma': 7, 'kappa':0.1, 'delta1':14, 'delta2':14, 'N0':N0} return model