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;
158
159 int parent = (int) nearbyint(
node);
160
161#ifndef NDEBUG
163 assert(parent>=0);
164 assert(parent<=nnode);
165#endif
166
167 int parlin = lineage[parent];
168 int parcol = color[parlin];
169 assert(parlin >= 0 && parlin <
nsample);
170
171
172 switch (nodetype[parent]) {
173 default:
174 break;
175 case 0:
177
178 assert(sat[parent]==1);
179 int c = child[index[parent]];
180 assert(lineage[parent]==lineage[c]);
183 if (unif_rand() < x) {
184 color[lineage[c]] = 0;
187 } else {
188 color[lineage[c]] = 1;
191 }
192 } else {
194
195 if (unif_rand() < 0.5) {
196 color[lineage[c]] = 0;
198
199 } else {
200 color[lineage[c]] = 1;
202
203 }
204 }
205 break;
206 case 1:
208
209 if (parcol != 1) {
211 color[parlin] = 1;
212
215 }
216 if (sat[parent] == 1) {
217 int c = child[index[parent]];
218 color[lineage[c]] = 1;
220 } else if (sat[parent] == 0) {
223 } else {
224 assert(0);
226 }
227 color[parlin] = R_NaReal;
228 break;
229 case 2:
231
232 if (parcol != 1) {
234 color[parlin] = 1;
235
238 }
239 assert(sat[parent]==2);
240 ll += (
S > 0 &&
I > 0) ? log(
Beta*
S/
N/(
E+1)) : R_NegInf;
244 int c1 = child[index[parent]];
245 int c2 = child[index[parent]+1];
246 assert(c1 != c2);
247 assert(lineage[c1] != lineage[c2]);
248 assert(lineage[c1] != parlin || lineage[c2] != parlin);
249 assert(lineage[c1] == parlin || lineage[c2] == parlin);
250 if (unif_rand() < 0.5) {
251 color[lineage[c1]] = 0;
252 color[lineage[c2]] = 1;
253 } else {
254 color[lineage[c1]] = 1;
255 color[lineage[c2]] = 0;
256 }
258 break;
259 }
260
261
262
263 if (tmax > t) {
264
266 int event;
267 double event_rate = 0;
268 double penalty = 0;
269
271 tstep = exp_rand()/event_rate;
272
273 while (t + tstep < tmax) {
275 assert(event>=0 && event<
nrate);
276 ll -= penalty*tstep + logpi[event];
277 switch (event) {
278 case 0:
283 break;
284 case 1:
289 break;
290 case 2:
297 break;
298 case 3:
303 break;
304 case 4:
311 break;
312 case 5:
316 break;
317 case 6:
321 break;
322 default:
323 assert(0);
325 break;
326 }
327
330
331 t += tstep;
333 tstep = exp_rand()/event_rate;
334
335 }
336 tstep = tmax - t;
338 }
340}
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)