phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
sirs_pomp.c
Go to the documentation of this file.
1#include "pomplink.h"
2#include "internal.h"
3
4static const int nrate = 3;
5
6#define Beta (__p[__parindex[0]])
7#define gamma (__p[__parindex[1]])
8#define psi (__p[__parindex[2]])
9#define chi (__p[__parindex[3]])
10#define omega (__p[__parindex[4]])
11#define S0 (__p[__parindex[5]])
12#define I0 (__p[__parindex[6]])
13#define R0 (__p[__parindex[7]])
14#define POP (__p[__parindex[8]])
15#define S (__x[__stateindex[0]])
16#define I (__x[__stateindex[1]])
17#define R (__x[__stateindex[2]])
18#define ll (__x[__stateindex[3]])
19#define ellI (__x[__stateindex[4]])
20#define node (__x[__stateindex[5]])
21
22#define EVENT_RATES \
23 event_rates(__x,__p,t, \
24 __stateindex,__parindex,__covindex, \
25 __covars,rate,&decay) \
26
27static double event_rates
28(
29 double *__x,
30 const double *__p,
31 double t,
32 const int *__stateindex,
33 const int *__parindex,
34 const int *__covindex,
35 const double *__covars,
36 double *rate,
37 double *decay
38 ) {
39 double event_rate = 0;
40 double alpha, disc;
41 *decay = 0;
42 assert(I >= ellI);
43 assert(ellI >= 0);
44 assert(S >= 0);
45 // 0: transmission with saturation 0 or 1
46 alpha = Beta*S*I/POP;
47 disc = (I > 0) ? ellI*(ellI-1)/I/(I+1) : 1;
48 event_rate += (*rate = alpha*(1-disc)); rate++;
49 *decay += alpha*disc;
50 // 1: recovery
51 alpha = gamma*I;
52 if (I > ellI) {
53 event_rate += (*rate = alpha); rate++;
54 } else {
55 *rate = 0; rate++;
56 *decay += alpha;
57 }
58 // 2: loss of immunity
59 alpha = omega*R;
60 event_rate += (*rate = alpha); rate++;
61 // sampling
62 alpha = (psi+chi)*I;
63 *decay += alpha;
64 assert(R_FINITE(event_rate));
65 return event_rate;
66}
67
70(
71 double *__x,
72 const double *__p,
73 double t,
74 const int *__stateindex,
75 const int *__parindex,
76 const int *__covindex,
77 const double *__covars
78 ){
79 double m = POP/(S0+I0+R0);
80 S = nearbyint(S0*m);
81 I = nearbyint(I0*m);
82 R = nearbyint(R0*m);
83 ll = 0;
84 ellI = 0;
85 node = 0;
86}
87
92(
93 double *__x,
94 const double *__p,
95 const int *__stateindex,
96 const int *__parindex,
97 const int *__covindex,
98 const double *__covars,
99 double t,
100 double dt
101 ){
102 double tstep = 0.0, tmax = t + dt;
103 const int *nodetype = get_userdata_int("nodetype");
104 const int *sat = get_userdata_int("saturation");
105
106 int parent = (int) nearbyint(node);
107
108#ifndef NDEBUG
109 int nnode = *get_userdata_int("nnode");
110 assert(parent>=0);
111 assert(parent<=nnode);
112#endif
113
114 ll = 0;
115
116 // singular portion of filter equation
117 switch (nodetype[parent]) {
118 default: // non-genealogical event #nocov
119 break; // #nocov
120 case 0: // root
121 if (I - ellI > 0)
122 ellI += 1;
123 else {
124 ellI += 1;
125 I += 1;
126 ll += R_NegInf;
127 }
128 break;
129 case 1: // sample
130 assert(I >= ellI);
131 assert(ellI >= 0);
132 if (sat[parent] == 1) {
133 ll += log(psi);
134 } else if (sat[parent] == 0) {
135 if (psi+chi > 0) {
136 ll += log((psi+chi)*I);
137 if (unif_rand() < psi/(psi+chi)) { // non-destructive sample
138 ellI -= 1;
139 ll += log(1-ellI/I);
140 } else { // destructive sample
141 ellI -= 1; I -= 1; R += 1;
142 }
143 } else {
144 ll += R_NegInf;
145 ellI -= 1; I -= 1; R += 1;
146 }
147 } else {
148 assert(0); // #nocov
149 ll += R_NegInf; // #nocov
150 }
151 break;
152 case 2: // branch point s=(1,1)
153 assert(S >= 0);
154 assert(I >= 0);
155 assert(ellI > 0);
156 assert(sat[parent]==2);
157 ll += (I > 0 && I >= ellI) ? log(Beta*S*I/POP) : R_NegInf;
158 S -= 1; I += 1;
159 ellI += 1;
160 ll -= log(I*(I-1)/2);
161 S = (S > 0) ? S : 0;
162 break;
163 }
164
165 if (tmax > t) {
166
167 // take Gillespie steps to the end of the interval:
168 int event;
169 double decay = 0;
170 double rate[nrate];
171
172 double event_rate = EVENT_RATES;
173 tstep = exp_rand()/event_rate;
174
175 while (t + tstep < tmax) {
176 event = rcateg(event_rate,rate,nrate);
177 assert(event>=0 && event<nrate);
178 ll -= decay*tstep;
179 switch (event) {
180 case 0: // transmission
181 S -= 1; I += 1;
182 break;
183 case 1: // recovery
184 I -= 1; R += 1;
185 break;
186 case 2: // loss of immunity
187 R -= 1; S += 1;
188 break;
189 default: // #nocov
190 assert(0); // #nocov
191 break; // #nocov
192 }
193 t += tstep;
194 event_rate = EVENT_RATES;
195 tstep = exp_rand()/event_rate;
196 }
197 tstep = tmax - t;
198 ll -= decay*tstep;
199 }
200 node += 1;
201}
202
203# define lik (__lik[0])
204
207(
208 double *__lik,
209 const double *__y,
210 const double *__x,
211 const double *__p,
212 int give_log,
213 const int *__obsindex,
214 const int *__stateindex,
215 const int *__parindex,
216 const int *__covindex,
217 const double *__covars,
218 double t
219 ){
220 assert(!ISNAN(ll));
221 lik = (give_log) ? ll : exp(ll);
222}
#define POP
Definition bdei_pomp.c:30
#define I
Definition bdei_pomp.c:34
#define I0
Definition bdei_pomp.c:32
#define ellI
Definition bdei_pomp.c:38
#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
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 psi
Definition lbdp_pomp.c:6
#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
void sirs_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 sirs_pomp.c:207
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 *decay)
Definition sirs_pomp.c:28
void sirs_rinit(double *__x, const double *__p, double t, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars)
Latent-state initializer (rinit).
Definition sirs_pomp.c:70
void sirs_gill(double *__x, const double *__p, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars, double t, double dt)
Definition sirs_pomp.c:92