Simulator for the latent-state process (rprocess).
This is the Gillespie algorithm applied to the solution of the filter equation for the SI2RS process.
193 {
194 double tstep = 0.0, tmax = t + dt;
195 double *color = &
COLOR;
201
202 int parent = (int) nearbyint(
node);
203
204#ifndef NDEBUG
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
216
217
218 switch (nodetype[parent]) {
219 default:
220 break;
221 case 0:
222
223 assert(sat[parent]==1);
224 int c = child[index[parent]];
225 assert(lineage[parent]==lineage[c]);
228 if (unif_rand() < x) {
229 color[lineage[c]] =
Low;
232 } else {
233 color[lineage[c]] =
High;
236 }
238 } else {
240
241 color[lineage[c]] =
Low;
243 }
244 break;
245 case 1:
246 assert(sat[parent]==0);
251 }
else if (parcol ==
High) {
255 } else {
256 assert(0);
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);
270 if (
S >= 1 &&
POP > 0) {
274 color[lineage[c1]] =
Low;
275 color[lineage[c2]] =
Low;
277 } else {
280 color[lineage[c1]] =
Low;
281 color[lineage[c2]] =
Low;
282 }
283 }
else if (parcol ==
High) {
285 if (
S>=1 &&
POP > 0) {
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 }
298 } else {
301 color[lineage[c1]] =
Low;
302 color[lineage[c2]] =
High;
303 }
304 } else {
305 assert(0);
306 }
307 break;
308 }
309
310
311
312 if (tmax > t && R_FINITE(
ll)) {
313
315 int event;
316 double event_rate = 0;
317 double decay = 0;
318
320 tstep = exp_rand()/event_rate;
321
322 while (t + tstep < tmax) {
324 assert(event>=0 && event<
nrate);
325 ll -= decay*tstep + logpi[event];
326 switch (event) {
327 case 0:
328 assert(
S>=1 &&
IL>=1);
331 break;
332 case 1:
333 assert(
S>=1 &&
IH >= 1);
336 break;
337 case 2:
338 assert(
S>=1 &&
IH >= 1);
343 break;
344 case 3:
348 break;
349 case 4:
355 break;
356 case 5:
360 break;
361 case 6:
367 break;
368 case 7:
371 break;
372 case 8:
375 break;
376 case 9:
379 break;
380 default:
381 assert(0);
383 break;
384 }
385
388
389 t += tstep;
391 tstep = exp_rand()/event_rate;
392
393 }
394 tstep = tmax - t;
396 }
398}
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)