Math
Helper functions for doing analytical and/or numerical calculations.
get_asymptotic_growth_rate
Get the asymptotic per timestep growth rate for a renewal process with a given value of \(\mathcal{R}\) and a given discrete generation interval probability mass vector.
This function computes that growth rate finding the dominant eigenvalue of the renewal process's Leslie matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
R
|
float
|
The reproduction number of the renewal process |
required |
generation_interval_pmf
|
ArrayLike
|
The discrete generation interval probability mass vector of the renewal process |
required |
Returns:
Type | Description |
---|---|
float
|
The asymptotic growth rate of the renewal process, as a jax float. |
Source code in pyrenew/math.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
|
get_asymptotic_growth_rate_and_age_dist
get_asymptotic_growth_rate_and_age_dist(
R: float, generation_interval_pmf: ArrayLike
) -> tuple[float, ArrayLike]
Get the asymptotic per-timestep growth rate of the renewal process (the dominant eigenvalue of its Leslie matrix) and the associated stable age distribution (a normalized eigenvector associated to that eigenvalue).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
R
|
float
|
The reproduction number of the renewal process |
required |
generation_interval_pmf
|
ArrayLike
|
The discrete generation interval probability mass vector of the renewal process |
required |
Returns:
Type | Description |
---|---|
tuple[float, ArrayLike]
|
A tuple consisting of the asymptotic growth rate of the process, as jax float, and the stable age distribution of the process, as a jax array probability vector of the same shape as the generation interval probability vector. |
Raises:
Type | Description |
---|---|
ValueError
|
If an age distribution vector with non-zero imaginary part is produced. |
Source code in pyrenew/math.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
get_leslie_matrix
Create the Leslie matrix corresponding to a basic renewal process with the given \(\mathcal{R}\) value and discrete generation interval pmf vector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
R
|
float
|
The reproduction number of the renewal process |
required |
generation_interval_pmf
|
ArrayLike
|
The discrete generation interval probability mass vector of the renewal process |
required |
Returns:
Type | Description |
---|---|
ArrayLike
|
The Leslie matrix for the renewal process, as a jax array. |
Source code in pyrenew/math.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
|
get_stable_age_distribution
Get the stable age distribution for a renewal process with a given value of R and a given discrete generation interval probability mass vector.
This function computes that stable age distribution by finding and then normalizing an eigenvector associated to the dominant eigenvalue of the renewal process's Leslie matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
R
|
float
|
The reproduction number of the renewal process |
required |
generation_interval_pmf
|
ArrayLike
|
The discrete generation interval probability mass vector of the renewal process |
required |
Returns:
Type | Description |
---|---|
ArrayLike
|
The stable age distribution for the process, as a jax array probability vector of the same shape as the generation interval probability vector. |
Source code in pyrenew/math.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
integrate_discrete
Integrate (de-difference) the differenced process,
obtaining the process values \(X(t=0), X(t=1), ..., X(t)\)
from the \(n^{th}\) differences and a set of
initial process / difference values
\(X(t=0), X^1(t=1), X^2(t=2), ..., X^{(n-1)}(t=n-1)\),
where \(X^k(t)\) is the value of the \(n^{th}\)
difference at index \(t\) of the process,
obtaining a sequence of length equal to the length of
the provided highest_order_diff_vals
vector plus
the order of the process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
init_diff_vals
|
ArrayLike
|
Values of \(X(t=0), X^1(t=1), X^2(t=2), ..., X^{(n-1)}(t=n-1)\). |
required |
highest_order_diff_vals
|
ArrayLike
|
Array of differences at the highest order of differencing, i.e. the order of the overall process, starting with \(X^{n}(t=n)\) |
required |
Returns:
Type | Description |
---|---|
ArrayLike
|
The integrated (de-differenced) sequence of values, of length n_diffs + order, where n_diffs is the number of highest_order_diff_vals and order is the order of the process. |
Source code in pyrenew/math.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
|
neg_MGF
Compute the negative moment generating function (MGF)
for a given rate r
and weights w
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r
|
float
|
The rate parameter. |
required |
w
|
ArrayLike
|
An array of weights. |
required |
Returns:
Type | Description |
---|---|
float
|
The value of the negative MGF evaluated at |
Notes
For a finite discrete random variable \(X\) supported on the first \(n\) positive integers (\(\{1, 2, ..., n \}\)), the moment generating function (MGF) \(M_+(r)\) is defined as the expected value of \(\exp(rX)\). Similarly, the negative moment generating function \(M_-(r)\) is the expected value of \(\exp(-rX)\). So if we represent the PMF of \(X\) as a "weights" vector \(w\) of length \(n\), the negative MGF \(M_-(r)\) is given by:
Source code in pyrenew/math.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
neg_MGF_del_r
Compute the value of the partial deriative of
pyrenew.math.neg_MGF
with respect to r
evaluated at a particular r
and w
pair.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r
|
float
|
The rate parameter. |
required |
w
|
ArrayLike
|
An array of weights. |
required |
Returns:
Type | Description |
---|---|
float
|
The value of the partial derivative evaluated at |
Source code in pyrenew/math.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
r_approx_from_R
Get the approximate asymptotic geometric growth rate r
for a renewal process with a fixed reproduction number R
and discrete generation interval PMF g
.
Uses Newton's method with a fixed number of steps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
R
|
float
|
The reproduction number |
required |
g
|
ArrayLike
|
The probability mass function of the generation interval. |
required |
n_newton_steps
|
int
|
Number of steps to take when performing Newton's method. |
required |
Returns:
Type | Description |
---|---|
float
|
The approximate value of |
Notes
For a fixed value of \(\mathcal{R}\), a renewal process has an asymptotic geometric growth rate \(r\) that satisfies
where \(M_-(r)\) is the negative moment generating function
for a random variable \(\tau\) representing the (discrete)
generation interval. See pyrenew.math.neg_MGF
for details.
We obtain a value for \(r\) via approximate numerical solution of this implicit equation.
We first make an initial guess based on the mean generation interval \(\bar{\tau} = \mathbb{E}(\tau)\):
We then refine this approximation by applying Newton's method for a fixed number of steps.
Source code in pyrenew/math.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|