Simulator for the latent-state process (rprocess).
This is the Gillespie algorithm applied to the solution of the filter equation for the BDSS process.
172 {
173 double tstep = 0.0, tmax = t + dt;
174 double *color = &
COLOR;
181
182 int parent = (int) nearbyint(
node);
183
184#ifndef NDEBUG
186 assert(parent>=0);
187 assert(parent<=nnode);
188#endif
189
190 int parlin = lineage[parent];
191 int parcol = color[parlin];
192 assert(parlin >= 0 && parlin <
nsample);
193
195
196
197 switch (nodetype[parent]) {
198 default:
199 break;
200 case 0:
201 assert(sat[parent]==1);
202 {
203 int c = child[index[parent]];
204 assert(lineage[parent]==lineage[c]);
207 if (unif_rand() < x) {
208 color[lineage[c]] =
Normal;
211 } else {
215 }
216 } else {
218 if (unif_rand() < 0.5) {
219 color[lineage[c]] =
Normal;
221 } else {
224 }
225 }
226 }
227 break;
228 case 1:
229 if (sat[parent] == 0) {
232 ll += (
N > 0) ? log(
chi*
N) : R_NegInf;
236 ll += (
S > 0) ? log(
chi*
S) : R_NegInf;
238 } else {
240 }
241 } else {
242 assert(0);
244 }
245 color[parlin] = R_NaReal;
246 break;
247 case 2:
248 assert(sat[parent]==2);
249 {
250 int c1 = child[index[parent]];
251 int c2 = child[index[parent]+1];
252 assert(c1 != c2);
253 assert(lineage[c1] != lineage[c2]);
254 assert(lineage[c1] != parlin || lineage[c2] != parlin);
255 assert(lineage[c1] == parlin || lineage[c2] == parlin);
256
259 if (lam <= 0) {
ll += R_NegInf;
break; }
261 if (unif_rand() < p_nn) {
262
265 if (
N*(
N-1) <= 0)
ll += R_NegInf;
266 color[lineage[c1]] =
Normal;
267 color[lineage[c2]] =
Normal;
268 } else {
269
272 if (
N*
S <= 0)
ll += R_NegInf;
273 if (unif_rand() < 0.5) {
274 color[lineage[c1]] =
Normal;
276 } else {
278 color[lineage[c2]] =
Normal;
279 }
281 }
284 if (lam <= 0) {
ll += R_NegInf;
break; }
286 if (unif_rand() < p_ss) {
287
290 if (
S*(
S-1) <= 0)
ll += R_NegInf;
293 } else {
294
297 if (
N*
S <= 0)
ll += R_NegInf;
298 if (unif_rand() < 0.5) {
299 color[lineage[c1]] =
Normal;
301 } else {
303 color[lineage[c2]] =
Normal;
304 }
306 }
307 } else {
309 }
310 }
311 break;
312 }
313
314
315 if (tmax > t && R_FINITE(
ll)) {
316
318 int event;
319 double event_rate = 0;
320 double penalty = 0;
321
323 tstep = exp_rand()/event_rate;
324
325 while (t + tstep < tmax) {
327 assert(event>=0 && event<
nrate);
328 ll -= penalty*tstep + logpi[event];
329 switch (event) {
330 case 0:
333 break;
334 case 1:
338 break;
339 case 2:
345 break;
346 case 3:
349 break;
350 case 4:
354 break;
355 case 5:
361 break;
362 case 6:
365 break;
366 case 7:
369 break;
370 default:
371 assert(0);
372 break;
373 }
374
377
378 t += tstep;
380 tstep = exp_rand()/event_rate;
381
382 }
383 tstep = tmax - t;
385 }
387}
static void change_color(double *color, int nsample, int n, int from, int to)
static int random_choice(double n)
get_userdata_int_t * get_userdata_int
static int rcateg(double erate, double *rate, int nrate)