phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
si2r_pomp.c File Reference
#include "pomplink.h"
#include "internal.h"
Include dependency graph for si2r_pomp.c:

Go to the source code of this file.

Macros

#define Low   1
 
#define High   2
 
#define Beta   (__p[__parindex[0]])
 
#define kappa   (__p[__parindex[1]])
 
#define gamma   (__p[__parindex[2]])
 
#define omega   (__p[__parindex[3]])
 
#define chi   (__p[__parindex[4]])
 
#define etaL   (__p[__parindex[5]])
 
#define etaH   (__p[__parindex[6]])
 
#define POP   (__p[__parindex[7]])
 
#define S0   (__p[__parindex[8]])
 
#define IL0   (__p[__parindex[9]])
 
#define IH0   (__p[__parindex[10]])
 
#define R0   (__p[__parindex[11]])
 
#define S   (__x[__stateindex[0]])
 
#define IL   (__x[__stateindex[1]])
 
#define IH   (__x[__stateindex[2]])
 
#define R   (__x[__stateindex[3]])
 
#define ll   (__x[__stateindex[4]])
 
#define node   (__x[__stateindex[5]])
 
#define ellL   (__x[__stateindex[6]])
 
#define ellH   (__x[__stateindex[7]])
 
#define COLOR   (__x[__stateindex[8]])
 
#define EVENT_RATES
 
#define lik   (__lik[0])
 

Functions

static int random_choice (double n)
 
static void change_color (double *color, int nsample, int n, int from, int to)
 
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)
 
void si2rs_rinit (double *__x, const double *__p, double t0, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars)
 
void si2rs_gill (double *__x, const double *__p, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars, double t, double dt)
 
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).
 

Variables

static const int nrate = 10
 

Macro Definition Documentation

◆ Beta

#define Beta   (__p[__parindex[0]])

Definition at line 26 of file si2r_pomp.c.

◆ chi

#define chi   (__p[__parindex[4]])

Definition at line 30 of file si2r_pomp.c.

◆ COLOR

#define COLOR   (__x[__stateindex[8]])

Definition at line 46 of file si2r_pomp.c.

◆ ellH

#define ellH   (__x[__stateindex[7]])

Definition at line 45 of file si2r_pomp.c.

◆ ellL

#define ellL   (__x[__stateindex[6]])

Definition at line 44 of file si2r_pomp.c.

◆ etaH

#define etaH   (__p[__parindex[6]])

Definition at line 32 of file si2r_pomp.c.

◆ etaL

#define etaL   (__p[__parindex[5]])

Definition at line 31 of file si2r_pomp.c.

◆ EVENT_RATES

#define EVENT_RATES
Value:
event_rates(__x,__p,t, \
__stateindex,__parindex,__covindex, \
__covars,rate,logpi,&decay) \
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 bdei_pomp.c:47

Definition at line 48 of file si2r_pomp.c.

48#define EVENT_RATES \
49 event_rates(__x,__p,t, \
50 __stateindex,__parindex,__covindex, \
51 __covars,rate,logpi,&decay) \
52

◆ gamma

#define gamma   (__p[__parindex[2]])

Definition at line 28 of file si2r_pomp.c.

◆ High

#define High   2

Definition at line 5 of file si2r_pomp.c.

◆ IH

#define IH   (__x[__stateindex[2]])

Definition at line 40 of file si2r_pomp.c.

◆ IH0

#define IH0   (__p[__parindex[10]])

Definition at line 36 of file si2r_pomp.c.

◆ IL

#define IL   (__x[__stateindex[1]])

Definition at line 39 of file si2r_pomp.c.

◆ IL0

#define IL0   (__p[__parindex[9]])

Definition at line 35 of file si2r_pomp.c.

◆ kappa

#define kappa   (__p[__parindex[1]])

Definition at line 27 of file si2r_pomp.c.

◆ lik

#define lik   (__lik[0])

Definition at line 400 of file si2r_pomp.c.

◆ ll

#define ll   (__x[__stateindex[4]])

Definition at line 42 of file si2r_pomp.c.

◆ Low

#define Low   1

Definition at line 4 of file si2r_pomp.c.

◆ node

#define node   (__x[__stateindex[5]])

Definition at line 43 of file si2r_pomp.c.

◆ omega

#define omega   (__p[__parindex[3]])

Definition at line 29 of file si2r_pomp.c.

◆ POP

#define POP   (__p[__parindex[7]])

Definition at line 33 of file si2r_pomp.c.

◆ R

#define R   (__x[__stateindex[3]])

Definition at line 41 of file si2r_pomp.c.

◆ R0

#define R0   (__p[__parindex[11]])

Definition at line 37 of file si2r_pomp.c.

◆ S

#define S   (__x[__stateindex[0]])

Definition at line 38 of file si2r_pomp.c.

◆ S0

#define S0   (__p[__parindex[8]])

Definition at line 34 of file si2r_pomp.c.

Function Documentation

◆ change_color()

static void change_color ( double * color,
int nsample,
int n,
int from,
int to )
static

Definition at line 13 of file si2r_pomp.c.

14 {
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}
SEXP nsample(TYPE &X)
Definition generics.h:12
#define n
Definition lbdp_pomp.c:9
Here is the call graph for this function:
Here is the caller graph for this function:

◆ event_rates()

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 )
static

Definition at line 53 of file si2r_pomp.c.

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}
#define POP
Definition bdei_pomp.c:30
#define chi
Definition bdei_pomp.c:29
#define S
Definition bdss_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
#define IH
Definition si2r_pomp.c:40
#define ellL
Definition si2r_pomp.c:44
#define IL
Definition si2r_pomp.c:39
#define etaL
Definition si2r_pomp.c:31

◆ random_choice()

static int random_choice ( double n)
inlinestatic

Definition at line 9 of file si2r_pomp.c.

9 {
10 return floor(R_unif_index(n));
11}
Here is the caller graph for this function:

◆ si2rs_dmeas()

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 at line 403 of file si2r_pomp.c.

416 {
417 assert(!ISNAN(ll));
418 lik = (give_log) ? ll : exp(ll);
419}
#define lik
Definition bdei_pomp.c:317
#define ll
Definition bdei_pomp.c:35

◆ si2rs_gill()

void si2rs_gill ( double * __x,
const double * __p,
const int * __stateindex,
const int * __parindex,
const int * __covindex,
const double * __covars,
double t,
double dt )

Simulator for the latent-state process (rprocess).

This is the Gillespie algorithm applied to the solution of the filter equation for the SI2RS process.

Definition at line 183 of file si2r_pomp.c.

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}
#define COLOR
Definition bdei_pomp.c:39
static const int nrate
Definition bdei_pomp.c:7
#define EVENT_RATES
Definition bdei_pomp.c:41
#define node
Definition bdei_pomp.c:36
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 void change_color(double *color, int nsample, int n, int from, int to)
Definition si2r_pomp.c:13
#define High
Definition si2r_pomp.c:5
static int random_choice(double n)
Definition si2r_pomp.c:9
#define Low
Definition si2r_pomp.c:4
Here is the call graph for this function:

◆ si2rs_rinit()

void si2rs_rinit ( double * __x,
const double * __p,
double t0,
const int * __stateindex,
const int * __parindex,
const int * __covindex,
const double * __covars )

Latent-state initializer (rinit component).

The state variables include S, IL, IH, R plus 'ellL' and 'ellH' (numbers of L- and H-deme lineages), the accumulated weight ('ll'), the current node number ('node'), and the coloring of each lineage ('COLOR').

Definition at line 158 of file si2r_pomp.c.

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}
#define S0
Definition bdss_pomp.c:34
#define R0
Definition seirs_pomp.c:36
#define IL0
Definition si2r_pomp.c:35
#define IH0
Definition si2r_pomp.c:36

Variable Documentation

◆ nrate

const int nrate = 10
static

Definition at line 7 of file si2r_pomp.c.