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