예제 #1
0
    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)
예제 #2
0
    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)
예제 #3
0
    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::])
예제 #4
0
    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::])
예제 #5
0
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))
    'tau': 0.16,
    'Rec_rate': 1.7,
    'p': 0.2,
    'beta': 0.371,
    'mu': (1 / 63) / 365,
    'eta': 0.7,
}

sierra_leone_parameters = {
    'phi_H': 1.6,
    'sigma_I': 0.1,
    'sigma_H': 0.5,
    'theta_I': 0.1,
    'theta_H': 0.2,
    'alpha': 0.1,
    'tau': 0.16,
    'Rec_rate': 1.7,
    'p': 0.2,
    'beta': 0.371,
    'mu': (1 / 63) / 365,
    'eta': 0.7,
}

model_system.parameters = liberia_parameters
solution, output = model_system.integrate(t[1::], full_output=True)
model_system.plot()

model_system.parameters = sierra_leone_parameters
solution, output = model_system.integrate(t[1::], full_output=True)
model_system.plot()