phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
si2r_pomp.c
Go to the documentation of this file.
1#include "pomplink.h"
2#include "internal.h"
3
4#define Low 1
5#define High 2
6
7static const int nrate = 10;
8
9static inline int random_choice (double n) {
10 return floor(R_unif_index(n));
11}
12
13static void change_color (double *color, int nsample,
14 int n, int from, int to) {
15 int i = -1;
16 while (n >= 0 && i < nsample) {
17 i++;
18 if (!ISNA(color[i]) && nearbyint(color[i]) == from) n--;
19 }
20 assert(i < nsample);
21 assert(n == -1);
22 assert(nearbyint(color[i]) == from);
23 color[i] = to;
24}
25
26#define Beta (__p[__parindex[0]])
27#define kappa (__p[__parindex[1]])
28#define gamma (__p[__parindex[2]])
29#define omega (__p[__parindex[3]])
30#define chi (__p[__parindex[4]])
31#define etaL (__p[__parindex[5]])
32#define etaH (__p[__parindex[6]])
33#define POP (__p[__parindex[7]])
34#define S0 (__p[__parindex[8]])
35#define IL0 (__p[__parindex[9]])
36#define IH0 (__p[__parindex[10]])
37#define R0 (__p[__parindex[11]])
38#define S (__x[__stateindex[0]])
39#define IL (__x[__stateindex[1]])
40#define IH (__x[__stateindex[2]])
41#define R (__x[__stateindex[3]])
42#define ll (__x[__stateindex[4]])
43#define node (__x[__stateindex[5]])
44#define ellL (__x[__stateindex[6]])
45#define ellH (__x[__stateindex[7]])
46#define COLOR (__x[__stateindex[8]])
47
48#define EVENT_RATES \
49 event_rates(__x,__p,t, \
50 __stateindex,__parindex,__covindex, \
51 __covars,rate,logpi,&decay) \
52
53static double event_rates
54(
55 double *__x,
56 const double *__p,
57 double t,
58 const int *__stateindex,
59 const int *__parindex,
60 const int *__covindex,
61 const double *__covars,
62 double *rate,
63 double *logpi,
64 double *decay
65 ) {
66 double event_rate = 0;
67 double alpha, pi;
68 *decay = 0;
69 assert(R_FINITE(event_rate));
70 // 0: TL, s=(0,0) or s=(1,0)
71 assert(S>=0 && IL>=0);
72 alpha = (POP > 0) ? Beta*S*IL/POP : 0;
73 assert(IL >= ellL);
74 pi = 1;
75 event_rate += (*rate = alpha*pi); rate++;
76 *logpi = log(pi); logpi++;
77 assert(R_FINITE(event_rate));
78 // 1: TH, s=(0,0)
79 assert(S>=0 && IH>=0);
80 alpha = (POP > 0) ? kappa*Beta*S*IH/POP : 0;
81 assert(IH >= ellH);
82 pi = (IH > 0) ? 1-0.5*ellH/IH : 1;
83 event_rate += (*rate = alpha*pi); rate++;
84 *logpi = log(pi); logpi++;
85 assert(R_FINITE(event_rate));
86 // 2: TH, s=(1,0)
87 pi = 1-pi;
88 event_rate += (*rate = alpha*pi); rate++;
89 *logpi = log(pi/ellH); logpi++;
90 assert(R_FINITE(event_rate));
91 // 3: L, s=(0,0)
92 assert(IL>=0);
93 alpha = etaL*IL;
94 assert(IL >= ellL);
95 pi = (IL > 0) ? 1-ellL/IL : 1;
96 event_rate += (*rate = alpha*pi); rate++;
97 *logpi = log(pi); logpi++;
98 assert(R_FINITE(event_rate));
99 // 4: L, s=(0,1)
100 pi = 1-pi;
101 event_rate += (*rate = alpha*pi); rate++;
102 *logpi = log(pi/ellL); logpi++;
103 assert(R_FINITE(event_rate));
104 // 5: H, s=(0,0)
105 assert(IH>=0);
106 alpha = etaH*IH;
107 assert(IH >= ellH);
108 pi = (IH > 0) ? 1-ellH/IH : 1;
109 event_rate += (*rate = alpha*pi); rate++;
110 *logpi = log(pi); logpi++;
111 assert(R_FINITE(event_rate));
112 // 6: H, s=(1,0)
113 pi = 1-pi;
114 event_rate += (*rate = alpha*pi); rate++;
115 *logpi = log(pi/ellH); logpi++;
116 assert(R_FINITE(event_rate));
117 // 7: RL
118 assert(IL>=0);
119 alpha = gamma*IL;
120 if (IL > ellL) {
121 event_rate += (*rate = alpha); rate++;
122 *logpi = 0; logpi++;
123 } else {
124 *rate = 0; rate++;
125 *logpi = 0; logpi++;
126 *decay += alpha;
127 }
128 assert(R_FINITE(event_rate));
129 // 8: RH
130 assert(IH>=0);
131 alpha = gamma*IH;
132 if (IH > ellH) {
133 event_rate += (*rate = alpha); rate++;
134 *logpi = 0; logpi++;
135 } else {
136 *rate = 0; rate++;
137 *logpi = 0; logpi++;
138 *decay += alpha;
139 }
140 assert(R_FINITE(event_rate));
141 // 9: W
142 event_rate += (*rate = omega*R); rate++;
143 *logpi = 0; logpi++;
144 // sampling
145 *decay += chi*(IL+IH);
146 assert(R_FINITE(event_rate));
147 return event_rate;
148}
149
157(
158 double *__x,
159 const double *__p,
160 double t0,
161 const int *__stateindex,
162 const int *__parindex,
163 const int *__covindex,
164 const double *__covars
165 ){
166 double adj = POP/(S0+IL0+IH0+R0);
167 S = nearbyint(S0*adj);
168 IL = nearbyint(IL0*adj);
169 IH = nearbyint(IH0*adj);
170 R = nearbyint(R0*adj);
171 ellL = 0;
172 ellH = 0;
173 ll = 0;
174 node = 0;
175}
176
182(
183 double *__x,
184 const double *__p,
185 const int *__stateindex,
186 const int *__parindex,
187 const int *__covindex,
188 const double *__covars,
189 double t,
190 double dt
191 ){
192 double tstep = 0.0, tmax = t + dt;
193 double *color = &COLOR;
194 const int nsample = *get_userdata_int("nsample");
195 const int *nodetype = get_userdata_int("nodetype");
196 const int *lineage = get_userdata_int("lineage");
197 const int *index = get_userdata_int("index");
198 const int *child = get_userdata_int("child");
199
200 int parent = (int) nearbyint(node);
201
202#ifndef NDEBUG
203 const int *sat = get_userdata_int("saturation");
204 int nnode = *get_userdata_int("nnode");
205 assert(parent>=0);
206 assert(parent<=nnode);
207#endif
208
209 int parlin = lineage[parent];
210 int parcol = color[parlin];
211 assert(parlin >= 0 && parlin < nsample);
212
213 ll = 0;
214
215 // singular portion of filter equation
216 switch (nodetype[parent]) {
217 default: // non-genealogical event #nocov
218 break; // #nocov
219 case 0: // root
220 // color lineages by sampling without replacement
221 assert(sat[parent]==1);
222 int c = child[index[parent]];
223 assert(lineage[parent]==lineage[c]);
224 if (IL-ellL + IH-ellH > 0) {
225 double x = (IL-ellL)/(IL-ellL + IH-ellH);
226 if (unif_rand() < x) { // lineage is put into Low deme
227 color[lineage[c]] = Low;
228 ellL += 1;
229 ll -= log(x);
230 } else { // lineage is put into High deme
231 color[lineage[c]] = High;
232 ellH += 1;
233 ll -= log(1-x);
234 }
235 assert(!ISNAN(ll));
236 } else { // more roots than infectives
237 ll += R_NegInf; // this is incompatible with the genealogy
238 // the following keeps the state valid
239 color[lineage[c]] = Low;
240 ellL += 1; IL += 1;
241 }
242 break;
243 case 1: // sample
244 assert(sat[parent]==0);
245 if (parcol == Low) {
246 assert(ellL>=1 && IL >= ellL);
247 ll += log(chi*IL);
248 ellL -= 1; IL -= 1;
249 } else if (parcol == High) {
250 assert(ellH>=1 && IH >= ellH);
251 ll += log(chi*IH);
252 ellH -= 1; IH -= 1;
253 } else {
254 assert(0); // #nocov
255 }
256 color[parlin] = R_NaReal;
257 break;
258 case 2:
259 assert(sat[parent]==2);
260 int c1 = child[index[parent]];
261 int c2 = child[index[parent]+1];
262 assert(c1 != c2);
263 assert(lineage[c1] != lineage[c2]);
264 assert(lineage[c1] != parlin || lineage[c2] != parlin);
265 assert(lineage[c1] == parlin || lineage[c2] == parlin);
266 if (parcol == Low) {
267 assert(ellL >= 1 && IL >= ellL);
268 if (S >= 1 && POP > 0) {
269 ll += log(Beta*S*IL/POP);
270 S -= 1; IL += 1;
271 ellL += 1;
272 color[lineage[c1]] = Low;
273 color[lineage[c2]] = Low;
274 } else {
275 ll += R_NegInf;
276 IL += 1; ellL += 1;
277 color[lineage[c1]] = Low;
278 color[lineage[c2]] = Low;
279 }
280 } else if (parcol == High) {
281 assert(ellH >= 1 && IH >= ellH);
282 if (S>=1 && POP > 0) {
283 ll += log(kappa*Beta*S*IH/POP);
284 S -= 1; IL += 1;
285 ellL += 1;
286 if (unif_rand() < 0.5) {
287 color[lineage[c1]] = Low;
288 color[lineage[c2]] = High;
289 } else {
290 color[lineage[c1]] = High;
291 color[lineage[c2]] = Low;
292 }
293 ll -= log(0.5);
294 assert(!ISNAN(ll));
295 } else {
296 ll += R_NegInf;
297 IL += 1; ellL += 1;
298 color[lineage[c1]] = Low;
299 color[lineage[c2]] = High;
300 }
301 } else {
302 assert(0); // #nocov
303 }
304 break;
305 }
306
307 // continuous portion of filter equation:
308 // take Gillespie steps to the end of the interval
309 if (tmax > t && R_FINITE(ll)) {
310
311 double rate[nrate], logpi[nrate];
312 int event;
313 double event_rate = 0;
314 double decay = 0;
315
316 event_rate = EVENT_RATES;
317 tstep = exp_rand()/event_rate;
318
319 while (t + tstep < tmax) {
320 event = rcateg(event_rate,rate,nrate);
321 assert(event>=0 && event<nrate);
322 ll -= decay*tstep + logpi[event];
323 switch (event) {
324 case 0: // TL, s=(0,0) or s=(1,0)
325 assert(S>=1 && IL>=1);
326 S -= 1; IL += 1;
327 ll += log(1-ellL*(ellL-1)/IL/(IL-1));
328 break;
329 case 1: // TH, s = (0,0)
330 assert(S>=1 && IH >= 1);
331 S -= 1; IL += 1;
332 ll += log(1-ellL/IL);
333 break;
334 case 2: // TH, s=(1,0)
335 assert(S>=1 && IH >= 1);
336 S -= 1; IL += 1;
338 ellH -= 1; ellL += 1;
339 ll += log(1-ellH/IH/IL);
340 break;
341 case 3: // L, s=(0,0)
342 assert(IL>=1);
343 IL -= 1; IH += 1;
344 ll += log(1-ellH/IH);
345 break;
346 case 4: // L, s=(0,1)
347 assert(IL>=1);
349 ellL -= 1; ellH += 1;
350 IL -= 1; IH += 1;
351 ll -= log(IH);
352 break;
353 case 5: // H, s=(0,0)
354 assert(IH>=1);
355 IL += 1; IH -= 1;
356 ll += log(1-ellL/IL);
357 break;
358 case 6: // H, s=(1,0)
359 assert(IH>=1);
361 ellL += 1; ellH -= 1;
362 IL += 1; IH -= 1;
363 ll -= log(IL);
364 break;
365 case 7: // RL
366 assert(IL>=1);
367 IL -= 1; R += 1;
368 break;
369 case 8: // RH
370 assert(IH>=1);
371 IH -= 1; R += 1;
372 break;
373 case 9: // W
374 assert(R>=1);
375 R -= 1; S += 1;
376 break;
377 default: // #nocov
378 assert(0); // #nocov
379 ll += R_NegInf; // #nocov
380 break; // #nocov
381 }
382
383 ellL = nearbyint(ellL);
384 ellH = nearbyint(ellH);
385
386 t += tstep;
387 event_rate = EVENT_RATES;
388 tstep = exp_rand()/event_rate;
389
390 }
391 tstep = tmax - t;
392 ll -= decay*tstep;
393 }
394 node += 1;
395}
396
397# define lik (__lik[0])
398
401(
402 double *__lik,
403 const double *__y,
404 const double *__x,
405 const double *__p,
406 int give_log,
407 const int *__obsindex,
408 const int *__stateindex,
409 const int *__parindex,
410 const int *__covindex,
411 const double *__covars,
412 double t
413 ) {
414 assert(!ISNAN(ll));
415 lik = (give_log) ? ll : exp(ll);
416}
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:85
#define chi
Definition lbdp_pomp.c:7
#define lik
Definition lbdp_pomp.c:165
#define n
Definition lbdp_pomp.c:9
#define ll
Definition lbdp_pomp.c:10
#define EVENT_RATES
Definition lbdp_pomp.c:14
#define node
Definition lbdp_pomp.c:12
#define POP
Definition seirs_pomp.c:37
#define R0
Definition seirs_pomp.c:36
#define gamma
Definition seirs_pomp.c:29
#define COLOR
Definition seirs_pomp.c:46
#define R
Definition seirs_pomp.c:41
#define S0
Definition seirs_pomp.c:33
#define Beta
Definition seirs_pomp.c:27
static const int nrate
Definition seirs_pomp.c:7
#define omega
Definition seirs_pomp.c:32
#define S
Definition seirs_pomp.c:38
#define kappa
Definition si2r_pomp.c:27
#define etaH
Definition si2r_pomp.c:32
#define ellH
Definition si2r_pomp.c:45
void si2rs_rinit(double *__x, const double *__p, double t0, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars)
Definition si2r_pomp.c:157
static void change_color(double *color, int nsample, int n, int from, int to)
Definition si2r_pomp.c:13
#define IL0
Definition si2r_pomp.c:35
#define High
Definition si2r_pomp.c:5
void si2rs_gill(double *__x, const double *__p, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars, double t, double dt)
Definition si2r_pomp.c:182
#define IH
Definition si2r_pomp.c:40
#define ellL
Definition si2r_pomp.c:44
static int random_choice(double n)
Definition si2r_pomp.c:9
#define Low
Definition si2r_pomp.c:4
void si2rs_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 si2r_pomp.c:401
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 *decay)
Definition si2r_pomp.c:54
#define IL
Definition si2r_pomp.c:39
#define etaL
Definition si2r_pomp.c:31
#define IH0
Definition si2r_pomp.c:36