forked from kingaa/pomp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmeasure.c
276 lines (207 loc) · 7.08 KB
/
dmeasure.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// dear emacs, please treat this as -*- C++ -*-
#include <R.h>
#include <Rmath.h>
#include <Rdefines.h>
#include "internal.h"
static R_INLINE SEXP add_args
(
SEXP args, SEXP Onames, SEXP Snames,
SEXP Pnames, SEXP Cnames, SEXP log
) {
SEXP var;
int v;
// we construct the call from end to beginning
// 'log', covariates, parameter, states, observables, then time
// 'log' is a needed argument
PROTECT(log = AS_LOGICAL(log));
PROTECT(args = VectorToPairList(args));
PROTECT(args = LCONS(log,args));
SET_TAG(args,install("log"));
// Covariates
for (v = LENGTH(Cnames)-1; v >= 0; v--) {
var = NEW_NUMERIC(1);
args = LCONS(var,args);
UNPROTECT(1);
PROTECT(args);
SET_TAG(args,installChar(STRING_ELT(Cnames,v)));
}
// Parameters
for (v = LENGTH(Pnames)-1; v >= 0; v--) {
var = NEW_NUMERIC(1);
args = LCONS(var,args);
UNPROTECT(1);
PROTECT(args);
SET_TAG(args,installChar(STRING_ELT(Pnames,v)));
}
// Latent state variables
for (v = LENGTH(Snames)-1; v >= 0; v--) {
var = NEW_NUMERIC(1);
args = LCONS(var,args);
UNPROTECT(1);
PROTECT(args);
SET_TAG(args,installChar(STRING_ELT(Snames,v)));
}
// Observables
for (v = LENGTH(Onames)-1; v >= 0; v--) {
var = NEW_NUMERIC(1);
args = LCONS(var,args);
UNPROTECT(1);
PROTECT(args);
SET_TAG(args,installChar(STRING_ELT(Onames,v)));
}
// Time
var = NEW_NUMERIC(1);
args = LCONS(var,args);
UNPROTECT(1);
PROTECT(args);
SET_TAG(args,install("t"));
UNPROTECT(3);
return args;
}
static R_INLINE SEXP eval_call (
SEXP fn, SEXP args,
double *t,
double *y, int nobs,
double *x, int nvar,
double *p, int npar,
double *c, int ncov)
{
SEXP var = args, ans, ob;
int v;
*(REAL(CAR(var))) = *t; var = CDR(var);
for (v = 0; v < nobs; v++, y++, var=CDR(var)) *(REAL(CAR(var))) = *y;
for (v = 0; v < nvar; v++, x++, var=CDR(var)) *(REAL(CAR(var))) = *x;
for (v = 0; v < npar; v++, p++, var=CDR(var)) *(REAL(CAR(var))) = *p;
for (v = 0; v < ncov; v++, c++, var=CDR(var)) *(REAL(CAR(var))) = *c;
PROTECT(ob = LCONS(fn,args));
PROTECT(ans = eval(ob,CLOENV(fn)));
UNPROTECT(2);
return ans;
}
static R_INLINE SEXP ret_array (int nreps, int ntimes) {
int dim[2] = {nreps, ntimes};
const char *dimnm[2] = {".id","time"};
SEXP F;
PROTECT(F = makearray(2,dim));
fixdimnames(F,dimnm,2);
UNPROTECT(1);
return F;
}
SEXP do_dmeasure (SEXP object, SEXP y, SEXP x, SEXP times, SEXP params, SEXP log, SEXP gnsi)
{
pompfunmode mode = undef;
int ntimes, nvars, npars, ncovars, nreps, nrepsx, nrepsp, nobs;
SEXP Snames, Pnames, Cnames, Onames;
SEXP cvec, pompfun;
SEXP fn, args, ans;
SEXP F;
int *dim;
lookup_table_t covariate_table;
double *cov;
PROTECT(times = AS_NUMERIC(times));
ntimes = length(times);
if (ntimes < 1) err("length('times') = 0, no work to do.");
PROTECT(y = as_matrix(y));
dim = INTEGER(GET_DIM(y));
nobs = dim[0];
if (ntimes != dim[1])
err("length of 'times' and 2nd dimension of 'y' do not agree.");
PROTECT(x = as_state_array(x));
dim = INTEGER(GET_DIM(x));
nvars = dim[0]; nrepsx = dim[1];
if (ntimes != dim[2])
err("length of 'times' and 3rd dimension of 'x' do not agree.");
PROTECT(params = as_matrix(params));
dim = INTEGER(GET_DIM(params));
npars = dim[0]; nrepsp = dim[1];
nreps = (nrepsp > nrepsx) ? nrepsp : nrepsx;
if ((nreps % nrepsp != 0) || (nreps % nrepsx != 0))
err("larger number of replicates is not a multiple of smaller.");
PROTECT(Onames = GET_ROWNAMES(GET_DIMNAMES(y)));
PROTECT(Snames = GET_ROWNAMES(GET_DIMNAMES(x)));
PROTECT(Pnames = GET_ROWNAMES(GET_DIMNAMES(params)));
PROTECT(Cnames = get_covariate_names(GET_SLOT(object,install("covar"))));
// set up the covariate table
covariate_table = make_covariate_table(GET_SLOT(object,install("covar")),&ncovars);
PROTECT(cvec = NEW_NUMERIC(ncovars));
cov = REAL(cvec);
// extract the user-defined function
PROTECT(pompfun = GET_SLOT(object,install("dmeasure")));
PROTECT(fn = pomp_fun_handler(pompfun,gnsi,&mode,Snames,Pnames,Onames,Cnames));
// extract 'userdata' as pairlist
PROTECT(args = GET_SLOT(object,install("userdata")));
// create array to store results
PROTECT(F = ret_array(nreps,ntimes));
int nprotect = 13;
switch (mode) {
case Rfun: {
double *ys = REAL(y), *xs = REAL(x), *ps = REAL(params), *time = REAL(times);
double *ft = REAL(F);
int j, k;
// build argument list
PROTECT(args = add_args(args,Onames,Snames,Pnames,Cnames,log)); nprotect++;
for (k = 0; k < ntimes; k++, time++, ys += nobs) { // loop over times
R_CheckUserInterrupt(); // check for user interrupt
table_lookup(&covariate_table,*time,cov); // interpolate the covariates
for (j = 0; j < nreps; j++, ft++) { // loop over replicates
// evaluate the call
PROTECT(
ans = eval_call(
fn,args,
time,
ys,nobs,
xs+nvars*((j%nrepsx)+nrepsx*k),nvars,
ps+npars*(j%nrepsp),npars,
cov,ncovars
)
);
if (k == 0 && j == 0 && LENGTH(ans) != 1)
err("user 'dmeasure' returns a vector of length %d when it should return a scalar.",LENGTH(ans));
*ft = *(REAL(AS_NUMERIC(ans)));
UNPROTECT(1);
}
}
}
break;
case native: case regNative: {
int *oidx, *sidx, *pidx, *cidx;
int give_log;
pomp_dmeasure *ff = NULL;
double *yp = REAL(y), *xs = REAL(x), *ps = REAL(params), *time = REAL(times);
double *ft = REAL(F);
double *xp, *pp;
int j, k;
// extract state, parameter, covariate, observable indices
sidx = INTEGER(GET_SLOT(pompfun,install("stateindex")));
pidx = INTEGER(GET_SLOT(pompfun,install("paramindex")));
oidx = INTEGER(GET_SLOT(pompfun,install("obsindex")));
cidx = INTEGER(GET_SLOT(pompfun,install("covarindex")));
give_log = *(INTEGER(AS_INTEGER(log)));
// address of native routine
*((void **) (&ff)) = R_ExternalPtrAddr(fn);
for (k = 0; k < ntimes; k++, time++, yp += nobs) { // loop over times
R_CheckUserInterrupt(); // check for user interrupt
// interpolate the covar functions for the covariates
table_lookup(&covariate_table,*time,cov);
for (j = 0; j < nreps; j++, ft++) { // loop over replicates
xp = &xs[nvars*((j%nrepsx)+nrepsx*k)];
pp = &ps[npars*(j%nrepsp)];
(*ff)(ft,yp,xp,pp,give_log,oidx,sidx,pidx,cidx,cov,*time);
}
}
}
break;
default: {
double *ft = REAL(F);
int j, k;
for (k = 0; k < ntimes; k++) { // loop over times
for (j = 0; j < nreps; j++, ft++) { // loop over replicates
*ft = R_NaReal;
}
}
warn("'dmeasure' unspecified: likelihood undefined.");
}
}
UNPROTECT(nprotect);
return F;
}