pomp
Inference for partially observed Markov processes
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
simulate.c
Go to the documentation of this file.
1// -*- C++ -*-
2
3#include <Rdefines.h>
4#include "internal.h"
5
6SEXP do_simulate (SEXP object, SEXP params, SEXP nsim, SEXP rettype, SEXP gnsi)
7{
8
9 SEXP t0, times, x0, x, y;
10 SEXP ans = R_NilValue, ans_names = R_NilValue;
11 SEXP simnames;
12 int return_type = *(INTEGER(rettype)); // 0 = array, 1 = pomps
13
14 if (LENGTH(nsim) != 1) err("'nsim' must be a single integer"); // #nocov
15
16 PROTECT(params = as_matrix(params));
17
18 PROTECT(t0 = GET_SLOT(object,install("t0")));
19 PROTECT(times = GET_SLOT(object,install("times")));
20
21 // initialize the simulations
22 PROTECT(x0 = do_rinit(object,params,t0,nsim,gnsi));
23 PROTECT(simnames = GET_COLNAMES(GET_DIMNAMES(x0)));
24
25 // call 'rprocess' to simulate state process
26 PROTECT(x = do_rprocess(object,x0,t0,times,params,gnsi));
27
28 // call 'rmeasure' to simulate the measurement process
29 PROTECT(y = do_rmeasure(object,x,times,params,gnsi));
30
31 setcolnames(x,simnames);
32 setcolnames(y,simnames);
33
34 int nprotect = 7;
35
36 switch (return_type) {
37
38 case 0: // return a list of arrays
39
40 PROTECT(ans = NEW_LIST(2));
41 PROTECT(ans_names = NEW_CHARACTER(2));
42 nprotect += 2;
43 SET_STRING_ELT(ans_names,0,mkChar("states"));
44 SET_STRING_ELT(ans_names,1,mkChar("obs"));
45 SET_NAMES(ans,ans_names);
46 SET_ELEMENT(ans,0,x);
47 SET_ELEMENT(ans,1,y);
48
49 break;
50
51 case 1: default:
52
53 // a list to hold the pomp objects
54 {
55 SEXP pp, xx, yy, po;
56 const int *xdim;
57 int nvar, npar, nobs, nsim, ntim, nparsets;
58 int dim[2], i, j, k;
59
60 PROTECT(po = duplicate(object));
61 SET_SLOT(po,install("t0"),t0);
62 SET_SLOT(po,install("times"),times);
63
64 xdim = INTEGER(GET_DIM(x));
65 nvar = xdim[0]; nsim = xdim[1]; ntim = xdim[2];
66
67 xdim = INTEGER(GET_DIM(y));
68 nobs = xdim[0]; // second dimensions of 'x' and 'y' must agree
69
70 xdim = INTEGER(GET_DIM(params));
71 npar = xdim[0]; nparsets = xdim[1];
72
73 dim[0] = nvar; dim[1] = ntim;
74 PROTECT(xx = makearray(2,dim));
75 setrownames(xx,GET_ROWNAMES(GET_DIMNAMES(x)),2);
76 SET_SLOT(po,install("states"),xx);
77
78 dim[0] = nobs; dim[1] = ntim;
79 PROTECT(yy = makearray(2,dim));
80 setrownames(yy,GET_ROWNAMES(GET_DIMNAMES(y)),2);
81 SET_SLOT(po,install("data"),yy);
82
83 PROTECT(pp = NEW_NUMERIC(npar));
84 SET_NAMES(pp,GET_ROWNAMES(GET_DIMNAMES(params)));
85 SET_SLOT(po,install("params"),pp);
86
87 PROTECT(ans = NEW_LIST(nsim));
88 SET_NAMES(ans,simnames);
89
90 nprotect += 5;
91
92 for (k = 0; k < nsim; k++) {
93
94 SEXP po2;
95 double *xs = REAL(x), *ys = REAL(y), *ps = REAL(params);
96 double *xt, *yt, *pt;
97
98 PROTECT(po2 = duplicate(po));
99 xt = REAL(GET_SLOT(po2,install("states")));
100 yt = REAL(GET_SLOT(po2,install("data")));
101 pt = REAL(GET_SLOT(po2,install("params")));
102
103 memcpy(pt,ps+npar*(k%nparsets),npar*sizeof(double));
104
105 // copy x[,k,] and y[,k,] into po2
106 for (j = 0; j < ntim; j++) {
107 for (i = 0; i < nvar; i++, xt++) *xt = xs[i+nvar*(k+nsim*j)];
108 for (i = 0; i < nobs; i++, yt++) *yt = ys[i+nobs*(k+nsim*j)];
109 }
110
111 SET_ELEMENT(ans,k,po2);
112 UNPROTECT(1);
113
114 }
115
116 break;
117
118 }
119
120 }
121
122 UNPROTECT(nprotect);
123 return ans;
124
125}
SEXP do_rmeasure(SEXP, SEXP, SEXP, SEXP, SEXP)
Definition rmeasure.c:98
SEXP do_rinit(SEXP, SEXP, SEXP, SEXP, SEXP)
Definition rinit.c:91
SEXP do_rprocess(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP)
Definition rprocess.c:25
#define err(...)
Definition pomp.h:21
static R_INLINE void setrownames(SEXP x, SEXP names, int rank)
static R_INLINE void setcolnames(SEXP x, SEXP names)
static R_INLINE SEXP makearray(int rank, const int *dim)
static R_INLINE SEXP as_matrix(SEXP x)
SEXP do_simulate(SEXP object, SEXP params, SEXP nsim, SEXP rettype, SEXP gnsi)
Definition simulate.c:6
SEXP params
Definition trajectory.c:128