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) or s=(0,1)
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 alpha = omega*R;
143 event_rate += (*rate = alpha); rate++;
144 *logpi = 0; logpi++;
145 // sampling
146 alpha = chi*(IL+IH);
147 *decay += alpha;
148 assert(R_FINITE(event_rate));
149 return event_rate;
150}
151
159(
160 double *__x,
161 const double *__p,
162 double t0,
163 const int *__stateindex,
164 const int *__parindex,
165 const int *__covindex,
166 const double *__covars
167 ){
168 double adj = POP/(S0+IL0+IH0+R0);
169 S = nearbyint(S0*adj);
170 IL = nearbyint(IL0*adj);
171 IH = nearbyint(IH0*adj);
172 R = nearbyint(R0*adj);
173 ellL = 0;
174 ellH = 0;
175 ll = 0;
176 node = 0;
177}
178
184(
185 double *__x,
186 const double *__p,
187 const int *__stateindex,
188 const int *__parindex,
189 const int *__covindex,
190 const double *__covars,
191 double t,
192 double dt
193 ){
194 double tstep = 0.0, tmax = t + dt;
195 double *color = &COLOR;
196 const int nsample = *get_userdata_int("nsample");
197 const int *nodetype = get_userdata_int("nodetype");
198 const int *lineage = get_userdata_int("lineage");
199 const int *index = get_userdata_int("index");
200 const int *child = get_userdata_int("child");
201
202 int parent = (int) nearbyint(node);
203
204#ifndef NDEBUG
205 const int *sat = get_userdata_int("saturation");
206 int nnode = *get_userdata_int("nnode");
207 assert(parent>=0);
208 assert(parent<=nnode);
209#endif
210
211 int parlin = lineage[parent];
212 int parcol = color[parlin];
213 assert(parlin >= 0 && parlin < nsample);
214
215 ll = 0;
216
217 // singular portion of filter equation
218 switch (nodetype[parent]) {
219 default: // non-genealogical event #nocov
220 break; // #nocov
221 case 0: // root
222 // color lineages by sampling without replacement
223 assert(sat[parent]==1);
224 int c = child[index[parent]];
225 assert(lineage[parent]==lineage[c]);
226 if (IL-ellL + IH-ellH > 0) {
227 double x = (IL-ellL)/(IL-ellL + IH-ellH);
228 if (unif_rand() < x) { // lineage is put into Low deme
229 color[lineage[c]] = Low;
230 ellL += 1;
231 ll -= log(x);
232 } else { // lineage is put into High deme
233 color[lineage[c]] = High;
234 ellH += 1;
235 ll -= log(1-x);
236 }
237 assert(!ISNAN(ll));
238 } else { // more roots than infectives
239 ll += R_NegInf; // this is incompatible with the genealogy
240 // the following keeps the state valid
241 color[lineage[c]] = Low;
242 ellL += 1; IL += 1;
243 }
244 break;
245 case 1: // sample
246 assert(sat[parent]==0);
247 if (parcol == Low) {
248 assert(ellL>=1 && IL >= ellL);
249 ll += log(chi*IL);
250 ellL -= 1; IL -= 1; R += 1;
251 } else if (parcol == High) {
252 assert(ellH>=1 && IH >= ellH);
253 ll += log(chi*IH);
254 ellH -= 1; IH -= 1; R += 1;
255 } else {
256 assert(0); // #nocov
257 }
258 color[parlin] = R_NaReal;
259 break;
260 case 2:
261 assert(sat[parent]==2);
262 int c1 = child[index[parent]];
263 int c2 = child[index[parent]+1];
264 assert(c1 != c2);
265 assert(lineage[c1] != lineage[c2]);
266 assert(lineage[c1] != parlin || lineage[c2] != parlin);
267 assert(lineage[c1] == parlin || lineage[c2] == parlin);
268 if (parcol == Low) {
269 assert(ellL >= 1 && IL >= ellL);
270 if (S >= 1 && POP > 0) {
271 ll += log(Beta*S*IL/POP);
272 S -= 1; IL += 1;
273 ellL += 1;
274 color[lineage[c1]] = Low;
275 color[lineage[c2]] = Low;
276 ll -= log(IL*(IL-1)/2);
277 } else {
278 ll += R_NegInf;
279 IL += 1; ellL += 1;
280 color[lineage[c1]] = Low;
281 color[lineage[c2]] = Low;
282 }
283 } else if (parcol == High) {
284 assert(ellH >= 1 && IH >= ellH);
285 if (S>=1 && POP > 0) {
286 ll += log(kappa*Beta*S*IH/POP);
287 S -= 1; IL += 1;
288 ellL += 1;
289 if (unif_rand() < 0.5) {
290 color[lineage[c1]] = Low;
291 color[lineage[c2]] = High;
292 } else {
293 color[lineage[c1]] = High;
294 color[lineage[c2]] = Low;
295 }
296 ll -= log(IL*IH/2);
297 assert(!ISNAN(ll));
298 } else {
299 ll += R_NegInf;
300 IL += 1; ellL += 1;
301 color[lineage[c1]] = Low;
302 color[lineage[c2]] = High;
303 }
304 } else {
305 assert(0); // #nocov
306 }
307 break;
308 }
309
310 // continuous portion of filter equation:
311 // take Gillespie steps to the end of the interval
312 if (tmax > t && R_FINITE(ll)) {
313
314 double rate[nrate], logpi[nrate];
315 int event;
316 double event_rate = 0;
317 double decay = 0;
318
319 event_rate = EVENT_RATES;
320 tstep = exp_rand()/event_rate;
321
322 while (t + tstep < tmax) {
323 event = rcateg(event_rate,rate,nrate);
324 assert(event>=0 && event<nrate);
325 ll -= decay*tstep + logpi[event];
326 switch (event) {
327 case 0: // TL, s=(0,0) or s=(1,0)
328 assert(S>=1 && IL>=1);
329 S -= 1; IL += 1;
330 ll += log(1-ellL*(ellL-1)/IL/(IL-1));
331 break;
332 case 1: // TH, s=(0,0) or s=(0,1)
333 assert(S>=1 && IH >= 1);
334 S -= 1; IL += 1;
335 ll += log(1-ellL/IL);
336 break;
337 case 2: // TH, s=(1,0)
338 assert(S>=1 && IH >= 1);
339 S -= 1; IL += 1;
341 ellH -= 1; ellL += 1;
342 ll += log((1-ellH/IH)/IL);
343 break;
344 case 3: // L, s=(0,0)
345 assert(IL>=1);
346 IL -= 1; IH += 1;
347 ll += log(1-ellH/IH);
348 break;
349 case 4: // L, s=(0,1)
350 assert(IL>=1);
352 ellL -= 1; ellH += 1;
353 IL -= 1; IH += 1;
354 ll -= log(IH);
355 break;
356 case 5: // H, s=(0,0)
357 assert(IH>=1);
358 IL += 1; IH -= 1;
359 ll += log(1-ellL/IL);
360 break;
361 case 6: // H, s=(1,0)
362 assert(IH>=1);
364 ellL += 1; ellH -= 1;
365 IL += 1; IH -= 1;
366 ll -= log(IL);
367 break;
368 case 7: // RL
369 assert(IL>=1);
370 IL -= 1; R += 1;
371 break;
372 case 8: // RH
373 assert(IH>=1);
374 IH -= 1; R += 1;
375 break;
376 case 9: // W
377 assert(R>=1);
378 R -= 1; S += 1;
379 break;
380 default: // #nocov
381 assert(0); // #nocov
382 ll += R_NegInf; // #nocov
383 break; // #nocov
384 }
385
386 ellL = nearbyint(ellL);
387 ellH = nearbyint(ellH);
388
389 t += tstep;
390 event_rate = EVENT_RATES;
391 tstep = exp_rand()/event_rate;
392
393 }
394 tstep = tmax - t;
395 ll -= decay*tstep;
396 }
397 node += 1;
398}
399
400# define lik (__lik[0])
401
404(
405 double *__lik,
406 const double *__y,
407 const double *__x,
408 const double *__p,
409 int give_log,
410 const int *__obsindex,
411 const int *__stateindex,
412 const int *__parindex,
413 const int *__covindex,
414 const double *__covars,
415 double t
416 ) {
417 assert(!ISNAN(ll));
418 lik = (give_log) ? ll : exp(ll);
419}
#define POP
Definition bdei_pomp.c:30
#define COLOR
Definition bdei_pomp.c:39
#define chi
Definition bdei_pomp.c:29
static const int nrate
Definition bdei_pomp.c:7
#define lik
Definition bdei_pomp.c:317
#define ll
Definition bdei_pomp.c:35
#define EVENT_RATES
Definition bdei_pomp.c:41
#define node
Definition bdei_pomp.c:36
#define S0
Definition bdss_pomp.c:34
#define S
Definition bdss_pomp.c:36
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 n
Definition lbdp_pomp.c:9
#define R0
Definition seirs_pomp.c:36
#define gamma
Definition seirs_pomp.c:29
#define R
Definition seirs_pomp.c:41
#define Beta
Definition seirs_pomp.c:27
#define omega
Definition seirs_pomp.c:32
#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:159
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:184
#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:404
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