Simulator for the latent-state process (rprocess).
This is the Gillespie algorithm applied to the solution of the filter equation for the SEIRS process.
149 {
150 double tstep = 0.0, tmax = t + dt;
151 double *color = &
COLOR;
159
160 int parent = (int) nearbyint(
node);
161
162#ifndef NDEBUG
164 assert(parent>=0);
165 assert(parent<=nnode);
166#endif
167
168 int parlin = lineage[parent];
169 int parcol = color[parlin];
170 int deme = nodedeme[parent];
171 assert(parlin >= 0 && parlin <
nsample);
172
174
175
176 switch (nodetype[parent]) {
177 default:
178 break;
179 case 0:
180
181 assert(sat[parent]==1);
182 int c = child[index[parent]];
183 assert(lineage[parent]==lineage[c]);
186 if (unif_rand() < x) {
187 color[lineage[c]] = 0;
190 } else {
191 color[lineage[c]] = 1;
194 }
195 } else {
197
198 if (unif_rand() < 0.5) {
199 color[lineage[c]] = 0;
201
202 } else {
203 color[lineage[c]] = 1;
205
206 }
207 }
208 break;
209 case 1:
210
212 if (parcol !=
deme) {
214 color[parlin] = 1;
215
218 }
219 if (sat[parent] == 1) {
220 int c = child[index[parent]];
221 color[lineage[c]] = 1;
223 } else if (sat[parent] == 0) {
226 } else {
227 assert(0);
229 }
230 color[parlin] = R_NaReal;
231 break;
232 case 2:
233
234 if (parcol != 1) {
236 color[parlin] = 1;
237
240 }
241 assert(sat[parent]==2);
242 ll += (
S > 0 &&
I > 0) ? log(
Beta*
S/
N/(
E+1)) : R_NegInf;
246 int c1 = child[index[parent]];
247 int c2 = child[index[parent]+1];
248 assert(c1 != c2);
249 assert(lineage[c1] != lineage[c2]);
250 assert(lineage[c1] != parlin || lineage[c2] != parlin);
251 assert(lineage[c1] == parlin || lineage[c2] == parlin);
252 if (unif_rand() < 0.5) {
253 color[lineage[c1]] = 0;
254 color[lineage[c2]] = 1;
255 } else {
256 color[lineage[c1]] = 1;
257 color[lineage[c2]] = 0;
258 }
260 break;
261 }
262
263
264
265 if (tmax > t) {
266
268 int event;
269 double event_rate = 0;
270 double penalty = 0;
271
273 tstep = exp_rand()/event_rate;
274
275 while (t + tstep < tmax) {
277 assert(event>=0 && event<
nrate);
278 ll -= penalty*tstep + logpi[event];
279 switch (event) {
280 case 0:
285 break;
286 case 1:
291 break;
292 case 2:
299 break;
300 case 3:
305 break;
306 case 4:
313 break;
314 case 5:
318 break;
319 case 6:
323 break;
324 default:
325 assert(0);
327 break;
328 }
329
332
333 t += tstep;
335 tstep = exp_rand()/event_rate;
336
337 }
338 tstep = tmax - t;
340 }
342}
get_userdata_int_t * get_userdata_int
static int rcateg(double erate, double *rate, int nrate)
static void change_color(double *color, int nsample, int n, int from, int to)
static int random_choice(double n)