def test_stochastic(self): # Tests the following system simulating jumps # A + A -> C # A + B -> D # \emptyset -> A # \emptyset -> B stateList = ['A', 'B', 'C', 'D'] paramList = ['k1', 'k2', 'k3', 'k4'] transitionList = [ Transition(origState=('A','A'), destState='C', equation='A * (A - 1) * k1', transitionType=TransitionType.T), Transition(origState=('A','B'), destState='D', equation='A * B * k2', transitionType=TransitionType.T) ] # our birth and deaths birthDeathList = [ Transition(origState='A', equation='k3', transitionType=TransitionType.B), Transition(origState='B', equation='k4', transitionType=TransitionType.B) ] ode = SimulateOdeModel(stateList, paramList, birthDeathList=birthDeathList, transitionList=transitionList) x0 = [0,0,0,0] t = numpy.linspace(0, 100, 100) ode.setParameters(paramEval).setInitialValue(x0, t[0]) simX, simT = ode.simulateJump(t, 5, full_output=True)
def test_hard(self): # the SLIARD model is considered to be hard because a state can # go to multiple state. This is not as hard as the SEIHFR model # below. stateList = ['S', 'L', 'I', 'A', 'R', 'D'] paramList = [ 'beta', 'p', 'kappa', 'alpha', 'f', 'delta', 'epsilon', 'N' ] odeList = [ Transition('S', '- beta * S/N * ( I + delta * A)', 'ODE'), Transition('L', 'beta * S/N * (I + delta * A) - kappa * L', 'ODE'), Transition('I', 'p * kappa * L - alpha * I', 'ODE'), Transition('A', '(1-p) * kappa * L - epsilon * A', 'ODE'), Transition('R', 'f * alpha * I + epsilon * A', 'ODE'), Transition('D', '(1-f) * alpha * I', 'ODE') ] ode = SimulateOdeModel(stateList, paramList, odeList=odeList) ode2 = ode.returnObjWithTransitionsAndBD() diffEqZero = map(lambda x: x == 0, sympy.simplify(ode.getOde() - ode2.getOde())) if numpy.any(numpy.array(list(diffEqZero)) == False): raise Exception("Hard: SLIARD Decomposition failed")
def test_simple(self): ode1 = Transition('S','-beta*S*I', 'ode') ode2 = Transition('I','beta*S*I - gamma * I', 'ode') ode3 = Transition('R','gamma*I', 'ode') stateList = ['S','I','R'] paramList = ['beta','gamma'] ode = SimulateOdeModel(stateList, paramList, odeList=[ode1,ode2,ode3]) ode2 = ode.returnObjWithTransitionsAndBD() diffEqZero = map(lambda x: x==0, sympy.simplify(ode.getOde() - ode2.getOde())) if numpy.any(numpy.array(diffEqZero) == False): raise Exception("Simple: SIR Decomposition failed")
def test_simulateParam2(self): ''' Stochastic ode under the interpretation that the parameters follow some sort of distribution. In this case, a function handle which has the same name as R ''' t0 = 0 # the initial state, normalized to zero one x0 = [1, 1.27e-6, 0] # set the time sequence that we would like to observe t = numpy.linspace(0, 150, 100) # Standard. Find the solution. ode = common_models.SIR() ode.setParameters([0.5, 1.0 / 3.0]) ode.setInitialValue(x0, t0) solutionReference = ode.integrate(t[1::], full_output=False) # now we need to define our ode explicitly stateList = ['S', 'I', 'R'] paramList = ['beta', 'gamma'] transitionList = [ Transition(origState='S', destState='I', equation='beta*S*I', transitionType=TransitionType.T), Transition(origState='I', destState='R', equation='gamma*I', transitionType=TransitionType.T) ] # our stochastic version odeS = SimulateOdeModel(stateList, paramList, transitionList=transitionList) # define our parameters in terms of two gamma distributions # where the expected values are the same as before [0.5,1.0/3.0] d = dict() d['beta'] = (rgamma, {'shape': 100.0, 'rate': 200.0}) d['gamma'] = (rgamma, (100.0, 300.0)) odeS.setParameters(d).setInitialValue(x0, t0) # now we generate the solutions solutionDiff = odeS.simulateParam(t[1::], 1000) - solutionReference # test :) if numpy.any(abs(solutionDiff) >= 0.2): raise Exception("Possible problem with simulating the parameters")
def test_simple(self): ode1 = Transition('S', '-beta*S*I', 'ode') ode2 = Transition('I', 'beta*S*I - gamma * I', 'ode') ode3 = Transition('R', 'gamma*I', 'ode') stateList = ['S', 'I', 'R'] paramList = ['beta', 'gamma'] ode = SimulateOdeModel(stateList, paramList, odeList=[ode1, ode2, ode3]) ode2 = ode.returnObjWithTransitionsAndBD() diffEqZero = map(lambda x: x == 0, sympy.simplify(ode.getOde() - ode2.getOde())) if numpy.any(numpy.array(list(diffEqZero)) == False): raise Exception("Simple: SIR Decomposition failed")
def test_derived_param(self): # the derived parameters are treated separately when compared to the # normal parameters and the odes ode = common_models.Legrand_Ebola_SEIHFR() odeList = [ Transition( 'S', '-(beta_I * S * I + beta_H_Time * S * H + beta_F_Time * S * F)' ), Transition( 'E', '(beta_I * S * I + beta_H_Time * S * H + beta_F_Time * S * F)-alpha * E' ), Transition( 'I', '-gamma_I * (1 - theta_1) * (1 - delta_1) * I - gamma_D * (1 - theta_1) * delta_1 * I - gamma_H * theta_1 * I + alpha * E' ), Transition( 'H', 'gamma_H * theta_1 * I - gamma_DH * delta_2 * H - gamma_IH * (1 - delta_2) * H' ), Transition( 'F', '- gamma_F * F + gamma_DH * delta_2 * H + gamma_D * (1 - theta_1) * delta_1 * I' ), Transition( 'R', 'gamma_I * (1 - theta_1) * (1 - delta_1) * I + gamma_F * F + gamma_IH * (1 - delta_2) * H' ), Transition('tau', '1') ] ode1 = SimulateOdeModel(ode._stateList, ode._paramList, ode._derivedParamEqn, odeList=odeList) ode2 = ode1.returnObjWithTransitionsAndBD() diffEqZero = map(lambda x: x == 0, sympy.simplify(ode.getOde() - ode2.getOde())) if numpy.any(numpy.array(list(diffEqZero)) == False): raise Exception("FAILED!")
def test_simulateParam2(self): ''' Stochastic ode under the interpretation that the parameters follow some sort of distribution. In this case, a function handle which has the same name as R ''' t0 = 0 # the initial state, normalized to zero one x0 = [1,1.27e-6,0] # set the time sequence that we would like to observe t = numpy.linspace(0, 150, 100) # Standard. Find the solution. ode = common_models.SIR() ode.setParameters([0.5,1.0/3.0]) ode.setInitialValue(x0,t0) solutionReference = ode.integrate(t[1::],full_output=False) # now we need to define our ode explicitly stateList = ['S','I','R'] paramList = ['beta','gamma'] transitionList = [ Transition(origState='S',destState='I',equation='beta * S * I',transitionType=TransitionType.T), Transition(origState='I',destState='R',equation='gamma * I',transitionType=TransitionType.T) ] # our stochastic version odeS = SimulateOdeModel(stateList, paramList, transitionList=transitionList) # define our parameters in terms of two gamma distributions # where the expected values are the same as before [0.5,1.0/3.0] d = dict() d['beta'] = (rgamma,{'shape':100.0,'rate':200.0}) d['gamma'] = (rgamma,(100.0,300.0)) odeS.setParameters(d).setInitialValue(x0,t0) # now we generate the solutions solutionDiff = odeS.simulateParam(t[1::],1000) - solutionReference # test :) if numpy.any(abs(solutionDiff)>=0.2): raise Exception("Possible problem with simulating the parameters")
def test_derived_param(self): # the derived parameters are treated separately when compared to the # normal parametes and the odes ode = common_models.Legrand_Ebola_SEIHFR() odeList = [ Transition('S', '-(beta_I * S * I + beta_H_Time * S * H + beta_F_Time * S * F)'), Transition('E', '(beta_I * S * I + beta_H_Time * S * H + beta_F_Time * S * F)-alpha * E'), Transition('I','-gamma_I * (1 - theta_1) * (1 - delta_1) * I - gamma_D * (1 - theta_1) * delta_1 * I - gamma_H * theta_1 * I + alpha * E'), Transition('H', 'gamma_H * theta_1 * I - gamma_DH * delta_2 * H - gamma_IH * (1 - delta_2) * H'), Transition('F','- gamma_F * F + gamma_DH * delta_2 * H + gamma_D * (1 - theta_1) * delta_1 * I'), Transition('R', 'gamma_I * (1 - theta_1) * (1 - delta_1) * I + gamma_F * F + gamma_IH * (1 - delta_2) * H'), Transition('tau', '1') ] ode1 = SimulateOdeModel(ode._stateList, ode._paramList, ode._derivedParamEqn, odeList=odeList) ode2 = ode1.returnObjWithTransitionsAndBD() diffEqZero = map(lambda x: x==0, sympy.simplify(ode.getOde() - ode2.getOde())) if numpy.any(numpy.array(diffEqZero) == False): raise Exception("FAILED!")
def test_hard(self): # the SLIARD model is considered to be hard because a state can # go to multiple state. This is not as hard as the SEIHFR model # below. stateList = ['S', 'L','I','A','R','D'] paramList = ['beta','p','kappa','alpha','f','delta','epsilon', 'N'] odeList = [ Transition('S', '- beta * S/N * ( I + delta * A)', 'ODE'), Transition('L', 'beta * S/N * (I + delta * A) - kappa * L', 'ODE'), Transition('I', 'p * kappa * L - alpha * I', 'ODE'), Transition('A', '(1-p) * kappa * L - epsilon * A', 'ODE'), Transition('R', 'f * alpha * I + epsilon * A', 'ODE'), Transition('D', '(1-f) * alpha * I', 'ODE') ] ode = SimulateOdeModel(stateList, paramList, odeList=odeList) ode2 = ode.returnObjWithTransitionsAndBD() diffEqZero = map(lambda x: x==0, sympy.simplify(ode.getOde() - ode2.getOde())) if numpy.any(numpy.array(diffEqZero) == False): raise Exception("Hard: SLIARD Decomposition failed")
def test_stochastic(self): # Tests the following system simulating jumps # A + A -> C # A + B -> D # \emptyset -> A # \emptyset -> B stateList = ['A', 'B', 'C', 'D'] paramList = ['k1', 'k2', 'k3', 'k4'] transitionList = [ Transition(origState=('A', 'A'), destState='C', equation='A * (A - 1) * k1', transitionType=TransitionType.T), Transition(origState=('A', 'B'), destState='D', equation='A * B * k2', transitionType=TransitionType.T) ] # our birth and deaths birthDeathList = [ Transition(origState='A', equation='k3', transitionType=TransitionType.B), Transition(origState='B', equation='k4', transitionType=TransitionType.B) ] ode = SimulateOdeModel(stateList, paramList, birthDeathList=birthDeathList, transitionList=transitionList) x0 = [0, 0, 0, 0] t = numpy.linspace(0, 100, 100) ode.setParameters(paramEval).setInitialValue(x0, t[0]) simX, simT = ode.simulateJump(t, 5, full_output=True)
def test_bd(self): stateList = ['S', 'I', 'R'] paramList = ['beta', 'gamma', 'B', 'mu'] odeList = [ Transition(origState='S', equation='-beta * S * I + B - mu * S', transitionType=TransitionType.ODE), Transition(origState='I', equation='beta * S * I - gamma * I - mu * I', transitionType=TransitionType.ODE), Transition(origState='R', destState='R', equation='gamma * I', transitionType=TransitionType.ODE) ] ode = SimulateOdeModel(stateList, paramList, odeList=odeList) ode2 = ode.returnObjWithTransitionsAndBD() diffEqZero = map(lambda x: x==0, sympy.simplify(ode.getOde() - ode2.getOde())) if numpy.any(numpy.array(diffEqZero) == False): raise Exception("Birth Death: SIR+BD Decomposition failed")
def test_bd(self): stateList = ['S', 'I', 'R'] paramList = ['beta', 'gamma', 'B', 'mu'] odeList = [ Transition(origState='S', equation='-beta * S * I + B - mu * S', transitionType=TransitionType.ODE), Transition(origState='I', equation='beta * S * I - gamma * I - mu * I', transitionType=TransitionType.ODE), Transition(origState='R', destState='R', equation='gamma * I', transitionType=TransitionType.ODE) ] ode = SimulateOdeModel(stateList, paramList, odeList=odeList) ode2 = ode.returnObjWithTransitionsAndBD() diffEqZero = map(lambda x: x == 0, sympy.simplify(ode.getOde() - ode2.getOde())) if numpy.any(numpy.array(list(diffEqZero)) == False): raise Exception("Birth Death: SIR+BD Decomposition failed")
def test_SimulateCTMC(self): ''' Stochastic ode under the interpretation that we have a continuous time Markov chain as the underlying process ''' #x0 = [1,1.27e-6,0] # original x0 = [2362206.0, 3.0, 0.0] t = numpy.linspace(0, 250, 50) stateList = ['S', 'I', 'R'] paramList = ['beta', 'gamma', 'N'] transitionList = [ Transition(origState='S', destState='I', equation='beta*S*I/N', transitionType=TransitionType.T), Transition(origState='I', destState='R', equation='gamma*I', transitionType=TransitionType.T) ] # initialize the model odeS = SimulateOdeModel(stateList, paramList, transitionList=transitionList) odeS.setParameters([0.5, 1.0 / 3.0, x0[0]]).setInitialValue(x0, t[0]) solution = odeS.integrate(t[1::]) odeS.transitionMean(x0, t[0]) odeS.transitionVar(x0, t[0]) odeS.transitionMean(solution[10, :], t[10]) odeS.transitionVar(solution[10, :], t[10]) simX, simT = odeS.simulateJump(250, 3, full_output=True)
def test_SimulateCTMC(self): ''' Stochastic ode under the interpretation that we have a continuous time Markov chain as the underlying process ''' #x0 = [1,1.27e-6,0] # original x0 = [2362206.0, 3.0, 0.0] t = numpy.linspace(0, 250, 50) stateList = ['S','I','R'] paramList = ['beta','gamma','N'] transitionList = [ Transition(origState='S',destState='I',equation='beta * S * I/N',transitionType=TransitionType.T), Transition(origState='I',destState='R',equation='gamma * I',transitionType=TransitionType.T) ] # initialize the model odeS = SimulateOdeModel(stateList, paramList, transitionList=transitionList) odeS.setParameters([0.5, 1.0/3.0, x0[0]]).setInitialValue(x0, t[0]) solution = odeS.integrate(t[1::]) odeS.transitionMean(x0,t[0]) odeS.transitionVar(x0,t[0]) odeS.transitionMean(solution[10,:],t[10]) odeS.transitionVar(solution[10,:],t[10]) simX,simT = odeS.simulateJump(250, 3, full_output=True)