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,&penalty) \
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 *penalty
38 ) {
39 double event_rate = 0;
40 double alpha, disc;
41 *penalty = 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 *penalty += 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 *penalty += 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 *penalty += 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 ellI += 1;
122 break;
123 case 1: // sample
124 assert(I >= ellI);
125 assert(ellI >= 0);
126 if (sat[parent] == 1) {
127 ll += log(psi);
128 } else if (sat[parent] == 0) {
129 ellI -= 1;
130 ll += log(psi+chi);
131 if (psi+chi <= 0)
132 ll += R_NegInf;
133 else if (unif_rand() < psi/(psi+chi)) { // non-destructive sample
134 ll += log(I-ellI);
135 } else { // destructive sample
136 ll += log(I);
137 I -= 1;
138 }
139 } else {
140 assert(0); // #nocov
141 ll += R_NegInf; // #nocov
142 }
143 break;
144 case 2: // branch point s=(1,1)
145 assert(S >= 0);
146 assert(I >= 0);
147 assert(ellI > 0);
148 assert(sat[parent]==2);
149 ll += (I > 0 && I >= ellI) ? log(Beta*S*I/POP) : R_NegInf;
150 S -= 1; I += 1;
151 ellI += 1;
152 ll -= log(I*(I-1)/2);
153 S = (S > 0) ? S : 0;
154 break;
155 }
156
157 if (tmax > t) {
158
159 // take Gillespie steps to the end of the interval:
160 int event;
161 double penalty = 0;
162 double rate[nrate];
163
164 double event_rate = EVENT_RATES;
165 tstep = exp_rand()/event_rate;
166
167 while (t + tstep < tmax) {
168 event = rcateg(event_rate,rate,nrate);
169 assert(event>=0 && event<nrate);
170 ll -= penalty*tstep;
171 switch (event) {
172 case 0: // transmission
173 S -= 1; I += 1;
174 break;
175 case 1: // recovery
176 I -= 1; R += 1;
177 break;
178 case 2: // loss of immunity
179 R -= 1; S += 1;
180 break;
181 default: // #nocov
182 assert(0); // #nocov
183 break; // #nocov
184 }
185 t += tstep;
186 event_rate = EVENT_RATES;
187 tstep = exp_rand()/event_rate;
188 }
189 tstep = tmax - t;
190 ll -= penalty*tstep;
191 }
192 node += 1;
193}
194
195# define lik (__lik[0])
196
199(
200 double *__lik,
201 const double *__y,
202 const double *__x,
203 const double *__p,
204 int give_log,
205 const int *__obsindex,
206 const int *__stateindex,
207 const int *__parindex,
208 const int *__covindex,
209 const double *__covars,
210 double t
211 ){
212 assert(!ISNAN(ll));
213 lik = (give_log) ? ll : exp(ll);
214}
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 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 POP
Definition seirs_pomp.c:37
#define R0
Definition seirs_pomp.c:36
#define gamma
Definition seirs_pomp.c:29
#define R
Definition seirs_pomp.c:41
#define S0
Definition seirs_pomp.c:33
#define I
Definition seirs_pomp.c:40
#define Beta
Definition seirs_pomp.c:27
#define I0
Definition seirs_pomp.c:35
#define ellI
Definition seirs_pomp.c:45
static const int nrate
Definition seirs_pomp.c:7
#define omega
Definition seirs_pomp.c:32
#define S
Definition seirs_pomp.c:38
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:199
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 *penalty)
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