-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstan_grf.stan
70 lines (56 loc) · 1.59 KB
/
stan_grf.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
data {
int<lower=0> N;
int<lower=0> dia0;
int<lower=0> pop;
vector[N] dia;
vector[N] defs;
vector[N] casos_obs;
}
parameters {
real<lower = 0, upper = 1> letalidad;
real<lower = 0, upper = 1000> casos_0;
vector<lower = 0, upper = .5>[N] r0;
real<lower = 0> sigma_delta_r0;
real<lower = 0> sigma_obs;
}
transformed parameters {
vector[N] casos;
vector[N] contagios;
contagios[1] = casos_0 * r0[1];
casos[1] = casos_0 + contagios[1];
for (i in 2:N) {
real contagiadores = 0;
for (j in max(1, i - 27):(i-1)) {
contagiadores = contagiadores + contagios[j];
}
// print("i: ", i);
// print("r0: ", r0[i]);
// print("contagios: ", contagios[i-1]);
// print("contagiadores: ", contagiadores);
contagios[i] = fmax(0, (1 - casos[i-1] / pop)) * contagiadores * r0[i];
casos[i] = casos[i-1] + contagios[i];
}
}
model {
// prior
letalidad ~ normal(0.01, 0.01);
sigma_delta_r0 ~ normal(0, .001);
sigma_obs ~ normal(0, .25);
// first iteration
r0[1] ~ normal(.03, .02);
for (i in 2:N)
r0[i] ~ normal(r0[i-1], sigma_delta_r0);
// only people catching coronavirus 22 days ago can die
for (i in 1:N) {
// normal approximation to binomial
if (i > 7) {
real en_riesgo = 0;
for (j in max(1, i - 22):(i-6)) {
en_riesgo = en_riesgo + contagios[j];
}
defs[i] ~ normal(letalidad * en_riesgo, sqrt(letalidad * en_riesgo));
}
// observation error grows with the number of existing cases
casos_obs[i] ~ normal(casos[i], casos[i] * sigma_obs);
}
}