Simulator for the latent-state process (rprocess).
This is the Gillespie algorithm applied to the solution of the filter equation for the SI2RS process.
191 {
192 double tstep = 0.0, tmax = t + dt;
193 double *color = &
COLOR;
199
200 int parent = (int) nearbyint(
node);
201
202#ifndef NDEBUG
205 assert(parent>=0);
206 assert(parent<=nnode);
207#endif
208
209 int parlin = lineage[parent];
210 int parcol = color[parlin];
211 assert(parlin >= 0 && parlin <
nsample);
212
214
215
216 switch (nodetype[parent]) {
217 default:
218 break;
219 case 0:
220
221 assert(sat[parent]==1);
222 int c = child[index[parent]];
223 assert(lineage[parent]==lineage[c]);
226 if (unif_rand() < x) {
227 color[lineage[c]] =
Low;
230 } else {
231 color[lineage[c]] =
High;
234 }
236 } else {
238
239 color[lineage[c]] =
Low;
241 }
242 break;
243 case 1:
244 assert(sat[parent]==0);
249 }
else if (parcol ==
High) {
253 } else {
254 assert(0);
255 }
256 color[parlin] = R_NaReal;
257 break;
258 case 2:
259 assert(sat[parent]==2);
260 int c1 = child[index[parent]];
261 int c2 = child[index[parent]+1];
262 assert(c1 != c2);
263 assert(lineage[c1] != lineage[c2]);
264 assert(lineage[c1] != parlin || lineage[c2] != parlin);
265 assert(lineage[c1] == parlin || lineage[c2] == parlin);
268 if (
S >= 1 &&
POP > 0) {
272 color[lineage[c1]] =
Low;
273 color[lineage[c2]] =
Low;
274 } else {
277 color[lineage[c1]] =
Low;
278 color[lineage[c2]] =
Low;
279 }
280 }
else if (parcol ==
High) {
282 if (
S>=1 &&
POP > 0) {
286 if (unif_rand() < 0.5) {
287 color[lineage[c1]] =
Low;
288 color[lineage[c2]] =
High;
289 } else {
290 color[lineage[c1]] =
High;
291 color[lineage[c2]] =
Low;
292 }
295 } else {
298 color[lineage[c1]] =
Low;
299 color[lineage[c2]] =
High;
300 }
301 } else {
302 assert(0);
303 }
304 break;
305 }
306
307
308
309 if (tmax > t && R_FINITE(
ll)) {
310
312 int event;
313 double event_rate = 0;
314 double decay = 0;
315
317 tstep = exp_rand()/event_rate;
318
319 while (t + tstep < tmax) {
321 assert(event>=0 && event<
nrate);
322 ll -= decay*tstep + logpi[event];
323 switch (event) {
324 case 0:
325 assert(
S>=1 &&
IL>=1);
328 break;
329 case 1:
330 assert(
S>=1 &&
IH >= 1);
333 break;
334 case 2:
335 assert(
S>=1 &&
IH >= 1);
340 break;
341 case 3:
345 break;
346 case 4:
352 break;
353 case 5:
357 break;
358 case 6:
364 break;
365 case 7:
368 break;
369 case 8:
372 break;
373 case 9:
376 break;
377 default:
378 assert(0);
380 break;
381 }
382
385
386 t += tstep;
388 tstep = exp_rand()/event_rate;
389
390 }
391 tstep = tmax - t;
393 }
395}
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)