-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculations.R
128 lines (106 loc) · 2.74 KB
/
calculations.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
library(deSolve)
library(cubature)
expectation <- function(fn) {
expectation <- integrate(
\(x) x * fn(x),
0,
50,
abs.tol = 1e-4,
rel.tol = 1e-4
)
expectation$value
}
multi_forward_gt <- function(m, strain) {
# effective S
fn.S <- \(t) m$S.AB(t) + x * m[[paste("S.", strain, sep="")]](t)
fn.beta <- \(t) m$params[[strain]](0)$beta
fn.g0 <- \(tau) m[[paste("g.0.", strain, sep="")]](tau)
function(t, tau) {
num <- fn.g0(tau) * fn.beta(t + tau) * fn.S(t + tau)
denom <- integrate(
\(sigma) fn.g0(sigma) * fn.beta(t + sigma) * fn.S(t + sigma),
0,
Inf,
abs.tol = 1e-8
)$value
if (denom <= 0) {
return(0)
}
num/denom
}
}
multi_backward_gt <- function(m, strain) {
# effective S
fn.i <- function(t) {
result <- m[[paste("incidence.", strain, "1", sep="")]](t) + m[[paste("incidence.", strain, "2", sep="")]](t)
# print("START")
# print(t)
# print(result)
}
fn.beta <- \(t) m$params[[strain]](0)$beta
fn.g0 <- \(tau) m[[paste("g.0.", strain, sep="")]](tau)
function(t, tau) {
num <- fn.g0(tau) * fn.i(t-tau)
denom <- integrate(
\(sigma) fn.g0(sigma) * fn.i(t-sigma),
0,
100,
abs.tol = 1e-4,
rel.tol = 1e-4
)$value
if (denom <= 0) {
return(0)
}
num/denom
}
}
# return a function which calculates the intrinsic generation time for a single
# model strain
multi_intrinsic_gt <- function(m, strain) {
function(tau) {
# this is just a hypoexponential distribution
sigma.A <- m$params[[strain]](0)[["sigma"]]
gamma.A <- m$params[[strain]](0)[["gamma"]]
(sigma.A * gamma.A) * (exp(-gamma.A * tau) - exp(-sigma.A * tau)) / (sigma.A - gamma.A)
}
}
# return a function which calculates the intrinsic generation time for a single
# model strain
single_intrinsic_gt <- function(m) {
function(tau) {
# this is just a hypoexponential distribution
sigma <- m$params[["sigma"]]
gamma <- m$params[["gamma"]]
(sigma * gamma) * (exp(-gamma * tau) - exp(-sigma * tau)) / (sigma - gamma)
}
}
# return a function which calculates the forward generation time for a single
# model strain
single_forward_gt <- function(m) {
function(t, tau) {
num <- m$g.0(tau) * m$beta(t + tau) * m$S(t + tau)
denom <- integrate(
\(sigma) m$g.0(sigma) * m$beta(t + sigma) * m$S(t + sigma),
0,
Inf
)$value
if (denom <= 0) {
return(0)
}
num/denom
}
}
single_backward_gt <- function(m) {
function(t, tau) {
num <- m$g.0(tau) * m$i(t - tau)
denom <- integrate(
\(sigma) m$g.0(sigma) * m$i(t - sigma),
0,
t
)$value
if (denom <= 0) {
return(0)
}
num/denom
}
}