Skip to content

Commit 03abb88

Browse files
committed
Make an explicit expression for new infections in the model implementation
1 parent 44d9faf commit 03abb88

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/penn_chime/models.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,17 @@ def sir(
102102
s: float, i: float, r: float, beta: float, gamma: float, n: float
103103
) -> Tuple[float, float, float]:
104104
"""The SIR model, one time step."""
105-
s_n = (-beta * s * i) + s
106-
i_n = (beta * s * i - gamma * i) + i
105+
106+
potential_new_infections = beta * s * i
107+
# Can't infect more people than there are left to become infected
108+
new_infections = min(s, potential_new_infections)
109+
110+
s_n = s - new_infections
111+
i_n = (new_infections - gamma * i) + i
107112
r_n = gamma * i + r
108-
if s_n < 0.0:
109-
s_n = 0.0
113+
114+
assert s_n >= 0.0
115+
110116
if i_n < 0.0:
111117
i_n = 0.0
112118
if r_n < 0.0:

tests/test_app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def test_sir():
163163

164164
with pytest.raises(TypeError) as error:
165165
sir(100, 1, 0, "beta", 0.5, 1)
166-
assert str(error.value) == "bad operand type for unary -: 'str'"
166+
assert str(error.value) == "'<' not supported between instances of 'str' and 'int'"
167167

168168
with pytest.raises(TypeError) as error:
169169
sir(100, 1, 0, 0.2, "gamma", 1)

0 commit comments

Comments
 (0)