-
Notifications
You must be signed in to change notification settings - Fork 34
/
fit_infection.stan
127 lines (99 loc) · 3.91 KB
/
fit_infection.stan
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
functions {
/* discretesized version of lognormal distribution */
vector plognormal(real mu, real sigma, int K) {
vector[K] res;
for (k in 1:K)
res[k] = exp(lognormal_lcdf(k | mu, sigma));
return append_row(res[1], res[2:K]-res[1:(K-1)]);
}
/* discretesized version of Weibull distribution */
vector pweibull(real kappa, real theta, int K) {
vector[K] res;
for (k in 1:K)
res[k] = -expm1(-pow(1.0*k/theta, kappa));
return append_row(res[1], res[2:K]-res[1:(K-1)]);
}
/* calculating the convolutions */
// X: first function, Yrev: reversed version of the second function
// K: length of X and Yrev
// the result is the vector of length K-1, because the first element equal zero is omitted
vector convolution(vector X, vector Yrev, int K) {
vector[K-1] res;
res[1] = X[1]*Yrev[K];
for (k in 2:K-1)
res[k] = dot_product(head(X, k), tail(Yrev, k));
return res;
}
// Special form of the convolution with adjusting to the delay
// F: cumulative distribution function of the reporting delay
vector convolution_with_delay(vector X, vector Yrev, vector F, int K) {
vector[K-1] res;
vector[K] Z = X ./ F;
res[1] = F[2]*Z[1]*Yrev[K];
for (k in 3:K)
res[k-1] = F[k]*dot_product(head(Z, k-1), tail(Yrev, k-1));
return res;
}
}
data {
int<lower = 1> K; //number of days
vector<lower = 0>[K] imported_backproj;
vector<lower = 0>[K] domestic_backproj;
int<lower = K> upper_bound;
// serial interval
real<lower = 0> param1_SI;
real<lower = 0> param2_SI;
// reporting delay is given by Weibul distribution
real<lower = 0> param1_delay;
real<lower = 0> param2_delay;
// incubation period
real mu_inc;
real<lower = 0> sigma_inc;
}
transformed data {
vector[K] cases_backproj;
vector[K-1] conv;
vector[K-1] conv_delay_adj;
cases_backproj = imported_backproj + domestic_backproj;
{
// serial interval
vector[K] gt = pweibull(param1_SI, param2_SI, K);
vector[K] gtrev;
vector[upper_bound] IncPeriod = plognormal(mu_inc, sigma_inc, upper_bound);
vector[upper_bound] IncPeriod_inv;
vector[upper_bound] ReportDelay;
vector[upper_bound] conv_report_inc;
vector[upper_bound] F_tmp;
vector[K] F;
for (k in 1:K)
gtrev[k] = gt[K+1-k];
for (k in 1:upper_bound)
IncPeriod_inv[k] = IncPeriod[upper_bound+1-k];
// vector[upper_bound] ReportDelay;
ReportDelay = pweibull(param1_delay, param2_delay, upper_bound);
// convolution of the reporting delay distribution and incubation period
// vector[upper_bound] conv_report_inc;
conv_report_inc = append_row(rep_vector(0,1), convolution(ReportDelay, IncPeriod_inv, upper_bound));
conv_report_inc = cumulative_sum(conv_report_inc);
ReportDelay = cumulative_sum(ReportDelay);
for (k in 1:upper_bound)
F_tmp[k] = ReportDelay[upper_bound+1-k];
F = head(F_tmp, K);
// convolution without adjusting for the delay
conv = convolution(cases_backproj, gtrev, K);
// convolution with adjusting for the delay
conv_delay_adj = convolution_with_delay(cases_backproj, gtrev, F, K);
}
}
parameters {
// effective reproduction number without adjustment to the delay in reporting
vector<lower = 0>[K-1] Rt;
// effective reproduction number with adjustment to the delay in reporting
vector<lower = 0>[K-1] Rt_adj;
}
model {
Rt ~ normal(2.4, 2.0);
Rt_adj ~ normal(2.4, 2.0);
target += gamma_lpdf(tail(domestic_backproj, K-1) | Rt .* conv + 1e-13, 1.0)
+ gamma_lpdf(tail(domestic_backproj, K-1) | Rt_adj .* conv_delay_adj + 1e-13, 1.0);
}