You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The next generation matrix $K$ has entries $K_{ij}$. Every infection in group $i$ in this generation will give rise to $K_{ij}$ infections in group $j$ in the next generation.
7
11
8
12
In this example, there are four groups. The core group transmits many infections to themselves and to travelers, who in turn transfer many infections to the core group (but not to themselves). Children and the general public don't transmit very much.
9
13
10
-
```{r}
14
+
```{python}
11
15
# high and low transmission rates
12
-
hi <- 3.0
13
-
lo <- 0.5
14
-
15
-
K <- matrix(c(
16
-
hi, lo, hi, lo, # core
17
-
lo, lo, lo, lo, # kids
18
-
hi, lo, lo, lo, # travelers
19
-
lo, lo, lo, lo # general
20
-
), nrow = 4, ncol = 4)
16
+
hi = 3.0
17
+
lo = 0.5
18
+
19
+
K = np.array(
20
+
[
21
+
[hi, lo, hi, lo], # core
22
+
[lo, lo, lo, lo], # children
23
+
[hi, lo, lo, lo], # travelers
24
+
[lo, lo, lo, lo], # general
25
+
]
26
+
)
21
27
```
22
28
23
29
Given a length-4 vector of infections $x$, representing the number of infections in each group in some generation, the next generation will have $Kx$ infections in each group.
24
30
25
31
By definition, $v$ and $\lambda$ are an eigenvector and eigenvalue of $K$ if $Kv = \lambda v$:
26
32
27
-
```{r}
33
+
```{python}
28
34
# do the eigenvalue analysis
29
-
e <- eigen(K)
35
+
e = np.linalg.eig(K)
30
36
# which eigenvalue is the dominant one?
31
-
i <- which.max(e$values)
32
-
dominant_eigenvalue <- e$values[i]
37
+
i = np.argmax(np.abs(e.eigenvalues))
38
+
dominant_eigenvalue = e.eigenvalues[i]
33
39
# get the corresponding eigenvector
34
-
v <- e$vectors[, i]
40
+
v = e.eigenvectors[:, i]
35
41
36
42
# note that this vector is L2-normalized
37
43
# (I need all.equal because there is some machine precision limitation)
0 commit comments