phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
seirs_pomp.c
Go to the documentation of this file.
1#include "pomplink.h"
2#include "internal.h"
3
4static const int nrate = 7;
5
6static inline int random_choice (double n) {
7 return floor(R_unif_index(n));
8}
9
10static void change_color (double *color, int nsample,
11 int n, int from, int to) {
12 int i = -1;
13 while (n >= 0 && i < nsample) {
14 i++;
15 if (!ISNA(color[i]) && nearbyint(color[i]) == from) n--;
16 }
17 assert(i < nsample);
18 assert(n == -1);
19 assert(nearbyint(color[i]) == from);
20 color[i] = to;
21}
22
23#define Beta (__p[__parindex[0]])
24#define sigma (__p[__parindex[1]])
25#define gamma (__p[__parindex[2]])
26#define psi (__p[__parindex[3]])
27#define omega (__p[__parindex[4]])
28#define S0 (__p[__parindex[5]])
29#define E0 (__p[__parindex[6]])
30#define I0 (__p[__parindex[7]])
31#define R0 (__p[__parindex[8]])
32#define N (__p[__parindex[9]])
33#define S (__x[__stateindex[0]])
34#define E (__x[__stateindex[1]])
35#define I (__x[__stateindex[2]])
36#define R (__x[__stateindex[3]])
37#define ll (__x[__stateindex[4]])
38#define node (__x[__stateindex[5]])
39#define ellE (__x[__stateindex[6]])
40#define ellI (__x[__stateindex[7]])
41#define COLOR (__x[__stateindex[8]])
42
43#define EVENT_RATES \
44 event_rates(__x,__p,t, \
45 __stateindex,__parindex,__covindex, \
46 __covars,rate,logpi,&penalty) \
47
48static double event_rates
49(
50 double *__x,
51 const double *__p,
52 double t,
53 const int *__stateindex,
54 const int *__parindex,
55 const int *__covindex,
56 const double *__covars,
57 double *rate,
58 double *logpi,
59 double *penalty
60 ) {
61 double event_rate = 0;
62 double alpha, pi;
63 *penalty = 0;
64 // 0: transmission, s=(0,0)
65 assert(S>=0 && I>=0);
66 alpha = (N > 0) ? Beta*S*I/N : 0;
67 pi = (I > 0) ? 1-ellI/I : 0;
68 assert(I >= ellI);
69 event_rate += (*rate = alpha*pi); rate++;
70 *logpi = log(pi); logpi++;
71 // 1: transmission, s=(0,1)
72 pi = (1-pi)/2;
73 event_rate += (*rate = alpha*pi); rate++;
74 *logpi = log(pi)-log(ellI); logpi++;
75 // 2: transmission, s=(1,0)
76 event_rate += (*rate = alpha*pi); rate++;
77 *logpi = log(pi)-log(ellI); logpi++;
78 // 3: progression, s=(0,0)
79 assert(E>=0);
80 alpha = sigma*E;
81 pi = (E > 0) ? ellE/E : 1;
82 assert(E >= ellE);
83 event_rate += (*rate = alpha*(1-pi)); rate++;
84 *logpi = log(1-pi); logpi++;
85 // 4: progression, s=(0,1)
86 event_rate += (*rate = alpha*pi); rate++;
87 *logpi = log(pi)-log(ellE); logpi++;
88 // 5: recovery
89 assert(I>=0);
90 alpha = gamma*I;
91 if (I > ellI) {
92 event_rate += (*rate = alpha); rate++;
93 *logpi = 0; logpi++;
94 } else {
95 *rate = 0; rate++;
96 *logpi = 0; logpi++;
97 *penalty += alpha;
98 }
99 // 6: waning
100 event_rate += (*rate = omega*R); rate++;
101 *logpi = 0; logpi++;
102 // sampling (Q = 0)
103 *penalty += psi*I;
104 assert(R_FINITE(event_rate));
105 return event_rate;
106}
107
115(
116 double *__x,
117 const double *__p,
118 double t0,
119 const int *__stateindex,
120 const int *__parindex,
121 const int *__covindex,
122 const double *__covars
123 ){
124 double adj = N/(S0+E0+I0+R0);
125 S = nearbyint(S0*adj);
126 E = nearbyint(E0*adj);
127 I = nearbyint(I0*adj);
128 R = nearbyint(R0*adj);
129 ellE = 0;
130 ellI = 0;
131 ll = 0;
132 node = 0;
133}
134
140(
141 double *__x,
142 const double *__p,
143 const int *__stateindex,
144 const int *__parindex,
145 const int *__covindex,
146 const double *__covars,
147 double t,
148 double dt
149 ){
150 double tstep = 0.0, tmax = t + dt;
151 double *color = &COLOR;
152 const int nsample = *get_userdata_int("nsample");
153 const int *nodetype = get_userdata_int("nodetype");
154 const int *nodedeme = get_userdata_int("deme");
155 const int *lineage = get_userdata_int("lineage");
156 const int *sat = get_userdata_int("saturation");
157 const int *index = get_userdata_int("index");
158 const int *child = get_userdata_int("child");
159
160 int parent = (int) nearbyint(node);
161
162#ifndef NDEBUG
163 int nnode = *get_userdata_int("nnode");
164 assert(parent>=0);
165 assert(parent<=nnode);
166#endif
167
168 int parlin = lineage[parent];
169 int parcol = color[parlin];
170 int deme = nodedeme[parent];
171 assert(parlin >= 0 && parlin < nsample);
172
173 ll = 0;
174
175 // singular portion of filter equation
176 switch (nodetype[parent]) {
177 default: // non-genealogical event
178 break;
179 case 0: // root
180 // color lineages by sampling without replacement
181 assert(sat[parent]==1);
182 int c = child[index[parent]];
183 assert(lineage[parent]==lineage[c]);
184 if (E-ellE + I-ellI > 0) {
185 double x = (E-ellE)/(E-ellE + I-ellI);
186 if (unif_rand() < x) { // lineage is put into E deme
187 color[lineage[c]] = 0;
188 ellE += 1;
189 ll -= log(x);
190 } else { // lineage is put into I deme
191 color[lineage[c]] = 1;
192 ellI += 1;
193 ll -= log(1-x);
194 }
195 } else { // more roots than infectives
196 ll += R_NegInf; // this is incompatible with the genealogy
197 // the following keeps the state valid
198 if (unif_rand() < 0.5) { // lineage is put into E deme
199 color[lineage[c]] = 0;
200 ellE += 1; E += 1;
201 // ll -= log(0.5);
202 } else { // lineage is put into I deme
203 color[lineage[c]] = 1;
204 ellI += 1; I += 1;
205 // ll -= log(0.5);
206 }
207 }
208 break;
209 case 1: // sample
210 // If parent is not in deme I, likelihood = 0.
211 assert(deme==1);
212 if (parcol != deme) {
213 ll += R_NegInf;
214 color[parlin] = 1;
215 // the following keeps the state valid
216 ellE -= 1; ellI += 1;
217 E -= 1; I += 1;
218 }
219 if (sat[parent] == 1) { // s=(0,1)
220 int c = child[index[parent]];
221 color[lineage[c]] = 1;
222 ll += log(psi);
223 } else if (sat[parent] == 0) { // s=(0,0)
224 ellI -= 1;
225 ll += log(psi*(I-ellI));
226 } else {
227 assert(0); // #nocov
228 ll += R_NegInf; // #nocov
229 }
230 color[parlin] = R_NaReal;
231 break;
232 case 2: // branch point s=(1,1)
233 // If parent is not in deme I, likelihood = 0.
234 if (parcol != 1) {
235 ll += R_NegInf;
236 color[parlin] = 1;
237 // the following keeps the state valid
238 ellE -= 1; ellI += 1;
239 E -= 1; I += 1;
240 }
241 assert(sat[parent]==2);
242 ll += (S > 0 && I > 0) ? log(Beta*S/N/(E+1)) : R_NegInf;
243 S -= 1; E += 1;
244 ellE += 1;
245 S = (S > 0) ? S : 0;
246 int c1 = child[index[parent]];
247 int c2 = child[index[parent]+1];
248 assert(c1 != c2);
249 assert(lineage[c1] != lineage[c2]);
250 assert(lineage[c1] != parlin || lineage[c2] != parlin);
251 assert(lineage[c1] == parlin || lineage[c2] == parlin);
252 if (unif_rand() < 0.5) {
253 color[lineage[c1]] = 0;
254 color[lineage[c2]] = 1;
255 } else {
256 color[lineage[c1]] = 1;
257 color[lineage[c2]] = 0;
258 }
259 ll -= log(0.5);
260 break;
261 }
262
263 // continuous portion of filter equation:
264 // take Gillespie steps to the end of the interval
265 if (tmax > t) {
266
267 double rate[nrate], logpi[nrate];
268 int event;
269 double event_rate = 0;
270 double penalty = 0;
271
272 event_rate = EVENT_RATES;
273 tstep = exp_rand()/event_rate;
274
275 while (t + tstep < tmax) {
276 event = rcateg(event_rate,rate,nrate);
277 assert(event>=0 && event<nrate);
278 ll -= penalty*tstep + logpi[event];
279 switch (event) {
280 case 0: // transmission, s=(0,0)
281 assert(S>=1);
282 S -= 1; E += 1;
283 ll += log(1-ellI/I)+log(1-ellE/E);
284 assert(!ISNAN(ll));
285 break;
286 case 1: // transmission, s=(0,1)
287 assert(S>=1);
288 S -= 1; E += 1;
289 ll += log(1-ellE/E)-log(I);
290 assert(!ISNAN(ll));
291 break;
292 case 2: // transmission, s=(1,0)
293 assert(S>=1);
295 ellE += 1; ellI -= 1;
296 S -= 1; E += 1;
297 ll += log(1-ellI/I)-log(E);
298 assert(!ISNAN(ll));
299 break;
300 case 3: // progression, s=(0,0)
301 assert(E>=1);
302 E -= 1; I += 1;
303 ll += log(1-ellI/I);
304 assert(!ISNAN(ll));
305 break;
306 case 4: // progression, s=(0,1)
307 assert(E>=1);
309 ellE -= 1; ellI += 1;
310 E -= 1; I += 1;
311 ll -= log(I);
312 assert(!ISNAN(ll));
313 break;
314 case 5: // recovery
315 assert(I>=1);
316 I -= 1; R += 1;
317 assert(!ISNAN(ll));
318 break;
319 case 6: // waning
320 assert(R>=1);
321 R -= 1; S += 1;
322 assert(!ISNAN(ll));
323 break;
324 default: // #nocov
325 assert(0); // #nocov
326 ll += R_NegInf; // #nocov
327 break; // #nocov
328 }
329
330 ellE = nearbyint(ellE);
331 ellI = nearbyint(ellI);
332
333 t += tstep;
334 event_rate = EVENT_RATES;
335 tstep = exp_rand()/event_rate;
336
337 }
338 tstep = tmax - t;
339 ll -= penalty*tstep;
340 }
341 node += 1;
342}
343
344# define lik (__lik[0])
345
348(
349 double *__lik,
350 const double *__y,
351 const double *__x,
352 const double *__p,
353 int give_log,
354 const int *__obsindex,
355 const int *__stateindex,
356 const int *__parindex,
357 const int *__covindex,
358 const double *__covars,
359 double t
360 ) {
361 assert(!ISNAN(ll));
362 lik = (give_log) ? ll : exp(ll);
363}
SEXP nsample(TYPE &X)
Definition generics.h:12
get_userdata_int_t * get_userdata_int
Definition init.c:7
static int rcateg(double erate, double *rate, int nrate)
Definition internal.h:76
static const int deme
Definition lbdp.cc:7
#define lik
Definition lbdp_pomp.c:160
#define n
Definition lbdp_pomp.c:8
#define ll
Definition lbdp_pomp.c:9
#define psi
Definition lbdp_pomp.c:6
#define EVENT_RATES
Definition lbdp_pomp.c:13
#define node
Definition lbdp_pomp.c:11
#define N
Definition seirs_pomp.c:32
#define ellE
Definition seirs_pomp.c:39
#define E
Definition seirs_pomp.c:34
static void change_color(double *color, int nsample, int n, int from, int to)
Definition seirs_pomp.c:10
#define R0
Definition seirs_pomp.c:31
#define gamma
Definition seirs_pomp.c:25
#define COLOR
Definition seirs_pomp.c:41
static double event_rates(double *__x, const double *__p, double t, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars, double *rate, double *logpi, double *penalty)
Definition seirs_pomp.c:49
static int random_choice(double n)
Definition seirs_pomp.c:6
#define R
Definition seirs_pomp.c:36
#define S0
Definition seirs_pomp.c:28
#define I
Definition seirs_pomp.c:35
#define sigma
Definition seirs_pomp.c:24
#define Beta
Definition seirs_pomp.c:23
#define I0
Definition seirs_pomp.c:30
#define E0
Definition seirs_pomp.c:29
#define ellI
Definition seirs_pomp.c:40
void seirs_gill(double *__x, const double *__p, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars, double t, double dt)
Definition seirs_pomp.c:140
void seirs_dmeas(double *__lik, const double *__y, const double *__x, const double *__p, int give_log, const int *__obsindex, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars, double t)
Measurement model likelihood (dmeasure).
Definition seirs_pomp.c:348
static const int nrate
Definition seirs_pomp.c:4
void seirs_rinit(double *__x, const double *__p, double t0, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars)
Definition seirs_pomp.c:115
#define omega
Definition seirs_pomp.c:27
#define S
Definition seirs_pomp.c:33