02
SIR compartments and biological transfers
This lesson focuses only on compartments and transfers. We use whole observed infection and recovery events to understand how one biological movement changes two compartment counts. Rates, differential equations and Euler’s method begin in later lessons.
Biological scenario
At the beginning of a short reporting interval, a closed population contains 990 susceptible, 10 infectious and no recovered people.
During the interval, 3 susceptible people become infectious and 1 infectious person recovers. What are the new compartment counts?
The three compartments
| Symbol | Compartment | Meaning |
|---|---|---|
| \(S\) | Susceptible | Can become infected. |
| \(I\) | Infectious | Currently infectious and able to transmit under the model assumptions. |
| \(R\) | Recovered or removed | No longer participates in transmission in the basic SIR model. |
The permitted biological transfers
The arrows show changes of biological state, not physical movement. This basic model permits only \(S\to I\) through infection and \(I\to R\) through recovery.
One event changes two compartments
One infection
\[S\to S-1,\qquad I\to I+1.\]
The same person leaves \(S\) and enters \(I\).
One recovery
\[I\to I-1,\qquad R\to R+1.\]
The same person leaves \(I\) and enters \(R\).
This coordinated change is the foundation of compartment modelling.
Calculate the interval by hand
With 3 infections and 1 recovery:
| Compartment | Before | Change | After |
|---|---|---|---|
| Susceptible | 990 | −3 | 987 |
| Infectious | 10 | +3−1 | 12 |
| Recovered | 0 | +1 | 1 |
| Total | 1,000 | 0 | 1,000 |
Translate the transfers into Python
Step 1: Preserve the before-state
S_before = 990
I_before = 10
R_before = 0
The suffix _before makes the time meaning explicit.
Step 2: Store the observed transfers
new_infections = 3
new_recoveries = 1
These are whole event counts supplied for this interval. This lesson does not yet calculate them from rates.
Step 3: Calculate all after-values from the same before-state
S_after = S_before - new_infections
I_after = I_before + new_infections - new_recoveries
R_after = R_before + new_recoveries
Separate before- and after-names prevent an updated value from being accidentally reused in another calculation.
Run the transfer program
Output
Run the code to see the result.
Why the total is conserved
Every loss from one compartment is a matching gain to another. Therefore, internal transfers change the distribution of people but not the closed population total.
New programming conditions
| Code | Meaning |
|---|---|
or | The combined condition is true when at least one connected condition is true. |
> | Tests whether the value on the left is greater than the value on the right. |
raise ValueError(...) | Stops the program and explains why the supplied transfer is invalid. |
assert N_after == N_before | Checks exact conservation because all values here are integer counts. |
Boundary of this lesson
The values new_infections = 3 and new_recoveries = 1 are supplied observations. We have not yet asked how a deterministic model calculates flows per unit time.
Lesson 3 introduces rate formulas and differential equations. Lesson 6 introduces numerical updating through time.
Try another interval
Change the event counts to 6 infections and 4 recoveries.
- Predict all three after-values.
- Run the code.
- Check that the total remains 1,000.
- Explain why the infectious change is \(+6-4=+2\), not simply +6.