Example #1
0
    def test_stochastic_well_mixed(self):

        S, E, I, R = sympy.symbols("S E I R")

        N = 75000
        tmax = 100
        model = SymbolicEpiModel([S, E, I, R], N)
        model.set_processes([
            (S, I, 2, E, I),
            (I, 1, R),
            (E, 1, I),
        ])
        model.set_initial_conditions({S: N - 100, I: 100})

        tt = np.linspace(0, tmax, 10)
        result_int = model.integrate(tt)

        t, result_sim = model.simulate(tmax,
                                       sampling_dt=1,
                                       return_compartments=[S, R])

        for c, res in result_sim.items():
            #print(c, np.abs(1-res[-1]/result_int[c][-1]))
            #print(c, np.abs(1-res[-1]/result_sim[c][-1]))
            assert (np.abs(1 - res[-1] / result_int[c][-1]) < 0.05)
Example #2
0
    def test_adding_linear_rates(self):

        S, E, I, R = sympy.symbols("S E I R")
        epi = SymbolicEpiModel([S, E, I, R])

        epi.set_processes([
            (E, 1, I),
        ])

        epi.add_transition_processes([
            (I, 1, R),
        ])
        linear_rates = [E, I]
        linear_events = [Matrix([[0, -1, +1, 0]]), Matrix([[0, 0, -1, +1]])]
        for r0, r1 in zip(linear_rates, epi.linear_rate_functions):
            assert (r0 == r1)
        for e0, e1 in zip(linear_events, epi.linear_event_updates):
            assert (all([_e0 == _e1 for _e0, _e1 in zip(e0, e1)]))
Example #3
0
    def test_changing_population_size(self):

        A, B, C, t = sympy.symbols("A B C t")
        epi = SymbolicEpiModel([A, B, C],
                               10,
                               correct_for_dynamical_population_size=True)
        epi.set_initial_conditions({A: 5, B: 5})
        epi.set_processes([
            (A, B, 1, C),
        ], allow_nonzero_column_sums=True)

        dydt = epi.dydt()
        assert (dydt[0] == -1 * A * B / (A + B + C))

        _, res = epi.simulate(1e9)
        assert (res[C][-1] == 5)

        epi.set_processes([
            (None, 1 + sympy.log(1 + t), A),
            (A, 1 + sympy.log(1 + t), B),
            (B, 1 + sympy.log(1 + t), None),
        ],
                          allow_nonzero_column_sums=True)

        rates, comp_changes = epi.get_numerical_event_and_rate_functions()
        _, res = epi.simulate(200, sampling_dt=0.05)

        vals = np.concatenate([res[A][_ > 10], res[B][_ > 10]])
        rv = poisson(vals.mean())
        measured, bins = np.histogram(vals,
                                      bins=np.arange(10) - 0.5,
                                      density=True)
        theory = [
            rv.pmf(i) for i in range(0,
                                     len(bins) - 1) if measured[i] > 0
        ]
        experi = [
            measured[i] for i in range(0,
                                       len(bins) - 1) if measured[i] > 0
        ]
        # make sure the kullback-leibler divergence is below some threshold
        assert (entropy(theory, experi) < 2e-3)
        assert (np.median(res[A]) == 1)
Example #4
0
    def test_basic_analytics(self):
        S, I, R, eta, rho, omega, t = symbols("S I R eta rho omega, t")

        SIRS = SymbolicEpiModel([S, I, R])

        SIRS.set_processes([
            #### transmission process ####
            # S + I (eta)-> I + I
            (S, I, eta, I, I),

            #### transition processes ####
            # I (rho)-> R
            # R (omega)-> S
            (I, rho, R),
            (R, omega, S),
        ])

        odes = SIRS.ODEs()

        expected = [
            Eq(Derivative(S, t), -I * S * eta + R * omega),
            Eq(Derivative(I, t), I * (S * eta - rho)),
            Eq(Derivative(R, t), I * rho - R * omega)
        ]

        assert (all([got == exp for got, exp in zip(odes, expected)]))

        fixed_points = SIRS.find_fixed_points()
        expected = FiniteSet((S, 0, 0), (rho / eta, R * omega / rho, R))

        assert (all([got == exp for got, exp in zip(fixed_points, expected)]))

        J = SIRS.jacobian()
        expected = Matrix([[-I * eta, -S * eta, omega],
                           [I * eta, S * eta - rho, 0], [0, rho, -omega]])
        N = SIRS.N_comp
        assert (all(
            [J[i, j] == expected[i, j] for i in range(N) for j in range(N)]))

        eig = SIRS.get_eigenvalues_at_disease_free_state()
        expected = {-omega: 1, eta - rho: 1, 0: 1}

        assert (all([v == expected[k] for k, v in eig.items()]))
Example #5
0
    def test_adding_quadratic_processes(self):

        S, E, I, A, R, rho = sympy.symbols("S E I A R rho")
        epi = SymbolicEpiModel([S, E, I, A, R])

        epi.set_processes([
            (S, I, rho, I, E),
        ])
        epi.add_transmission_processes([
            (S, A, rho, A, E),
        ])
        quadratic_rates = [I * S * rho, S * A * rho]
        quadratic_events = [
            Matrix([[-1, +1, 0, 0, 0]]),
            Matrix([[-1, +1, 0, 0, 0]])
        ]
        for r0, r1 in zip(quadratic_rates, epi.quadratic_rate_functions):
            assert (r0 == r1)
        for e0, e1 in zip(quadratic_events, epi.quadratic_event_updates):
            assert (all([_e0 == _e1 for _e0, _e1 in zip(e0, e1)]))
Example #6
0
    SIS = SymbolicEpiModel([S, I])
    SIS.add_transmission_processes([
        (I, S, eta, I, I),
    ])
    SIS.add_transition_processes([
        (I, rho, S),
    ])

    print(SIS.find_fixed_points())

    print(SIS.get_eigenvalues_at_fixed_point({S: 1}))

    print("==========")
    SIS = SymbolicEpiModel([S, I])
    SIS.set_processes([
        (I, S, eta / (1 - I), I, I),
        (I, rho, S),
    ])
    print(SIS.jacobian())
    print(SIS.get_eigenvalues_at_disease_free_state())

    N = sympy.symbols("N")
    epi = SymbolicSIRSModel(eta, rho, omega, initial_population_size=N)
    print()
    print(epi.ODEs())
    print(epi.find_fixed_points())

    print("==========")
    x = sympy.symbols("x")
    SIS = SymbolicEpiModel([x, I])
    SIS.set_processes([
        (I, x, eta / (1 - I), I, I),