/
pomp.h
320 lines (285 loc) · 7.72 KB
/
pomp.h
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// -*- C++ -*-
// Header for the C API for pomp.
// Documentation: https://kingaa.github.io/pomp/C_API.html
//! \mainpage C source codes for **pomp**
//!
//! These codes are part of **pomp**, an **R** package for partially observed Markov processes, described in a [2016 *Journal of Statistical Software* paper](https://doi.org/10.18637/jss.v069.i12).
//!
//! See the [package homepage](https://kingaa.github.io/pomp/) and the [manual](https://kingaa.github.io/manuals/pomp/) for more information.
//!
//! The **pomp** C API is [documented here](https://kingaa.github.io/pomp/C_API.html).
//!
#ifndef _POMP_H_
#define _POMP_H_
#include <R.h>
#include <Rmath.h>
#include <Rdefines.h>
#define err(...) Rf_errorcall(R_NilValue,__VA_ARGS__)
#define warn(...) Rf_warningcall(R_NilValue,__VA_ARGS__)
typedef
void bspline_basis_eval_deriv_t
(double x, double *knots, int degree,
int nbasis, int deriv, double *y);
typedef
void periodic_bspline_basis_eval_deriv_t
(double x, double period, int degree,
int nbasis, int deriv, double *y);
typedef const SEXP get_userdata_t (const char *name);
typedef const int *get_userdata_int_t (const char *name);
typedef const double *get_userdata_double_t (const char *name);
typedef
void pomp_rinit
(double *x, const double *p, double t0,
const int *stateindex, const int *parindex,
const int *covindex, const double *covars);
typedef
void pomp_dinit
(double *lik, const double *x, const double *p,
double t0, const int *stateindex, const int *parindex,
const int *covindex, const double *covars);
typedef
double pomp_ssa_rate_fn
(int event, double t, const double *x,
const double *p, const int *stateindex,
const int *parindex, const int *covindex,
const double *covars);
typedef
void pomp_onestep_sim
(double *x, const double *p,
const int *stateindex, const int *parindex,
const int *covindex, const double *covars,
double t, double dt);
typedef
void pomp_dprocess
(double *loglik, const double *x1, const double *x2,
double t1, double t2, const double *p,
const int *stateindex, const int *parindex,
const int *covindex, const double *covars);
typedef
void pomp_skeleton
(double *f, const double *x, const double *p,
const int *stateindex, const int *parindex,
const int *covindex, const double *covars, double t);
typedef
void pomp_rmeasure
(double *y, const double *x, const double *p,
const int *obsindex, const int *stateindex,
const int *parindex, const int *covindex,
const double *covars, double t);
typedef
void pomp_dmeasure
(double *lik, const double *y, const double *x,
const double *p, int give_log, const int *obsindex,
const int *stateindex, const int *parindex,
const int *covindex, const double *covars, double t);
typedef
void pomp_emeasure
(double *f, const double *x, const double *p,
const int *obsindex, const int *stateindex,
const int *parindex, const int *covindex,
const double *covars, double t);
typedef
void pomp_vmeasure
(double *f, const double *x, const double *p,
const int *vmatindex, const int *stateindex,
const int *parindex, const int *covindex,
const double *covars, double t);
typedef
void pomp_rprior
(double *p, const int *parindex);
typedef
void pomp_dprior
(double *lik, const double *p, int give_log, const int *parindex);
typedef
void pomp_transform
(double *pt, const double *p, const int *parindex);
static R_INLINE
double logit (double p) {
return log(p/(1.0-p));
}
static R_INLINE
double expit (double x) {
return 1.0/(1.0+exp(-x));
}
static R_INLINE
double rgammawn
(double sigma, double dt)
{
double sigmasq;
sigmasq = sigma*sigma;
return (sigmasq > 0) ? rgamma(dt/sigmasq,sigmasq) : dt;
}
static R_INLINE
void reulermultinom
(int m, double size, const double *rate, double dt, double *trans)
{
double p = 0.0;
int j, k;
if ( !R_FINITE(size) || size < 0.0 || floor(size+0.5) != size ||
!R_FINITE(dt) || dt < 0.0) {
for (k = 0; k < m; k++) trans[k] = R_NaReal;
warn("in 'reulermultinom': NAs produced.");
return;
}
for (k = 0; k < m; k++) {
if (!R_FINITE(rate[k]) || rate[k] < 0.0) {
for (j = 0; j < m; j++) trans[j] = R_NaReal;
warn("in 'reulermultinom': NAs produced.");
return;
}
p += rate[k]; // total event rate
}
if (p > 0.0) {
size = rbinom(size,1-exp(-p*dt)); // total number of events
m -= 1;
for (k = 0; k < m; k++) {
if (rate[k] > p) p = rate[k];
trans[k] = ((size > 0) && (p > 0)) ? rbinom(size,rate[k]/p) : 0;
size -= trans[k];
p -= rate[k];
}
trans[m] = size;
} else {
for (k = 0; k < m; k++) trans[k] = 0.0;
}
}
static R_INLINE
double deulermultinom
(int m, double size, const double *rate, double dt, double *trans, int give_log)
{
double p = 0.0;
double n = 0.0;
double ff = 0.0;
int k;
if ((dt < 0.0) || (size < 0.0) || (floor(size+0.5) != size)) {
warn("in 'deulermultinom': NaNs produced.");
return R_NaN;
}
for (k = 0; k < m; k++) {
if (rate[k] < 0.0) {
warn("in 'deulermultinom': NaNs produced.");
return R_NaN;
}
if (trans[k] < 0.0) {
ff = (give_log) ? R_NegInf: 0.0;
return ff;
}
p += rate[k]; // total event rate
n += trans[k]; // total number of events
}
if (n > size) {
ff = (give_log) ? R_NegInf: 0.0;
return ff;
}
ff = dbinom(n,size,1-exp(-p*dt),1); // total number of events
m -= 1;
for (k = 0; k < m; k++) {
if ((n > 0) && (p > 0)) {
if (rate[k] > p) p = rate[k];
ff += dbinom(trans[k],n,rate[k]/p,1);
}
n -= trans[k];
p -= rate[k];
}
ff = (give_log) ? ff : exp(ff);
return ff;
}
static R_INLINE
double dmultinom
(int m, const double *prob, double *x, int give_log)
{
double p = 0.0;
double n = 0.0;
double ff = 0.0;
int k;
for (k = 0; k < m; k++) {
if (prob[k] < 0.0) {
warn("in 'dmultinom': NaNs produced.");
return R_NaN;
}
if ((x[k] < 0.0) || (floor(x[k]+0.5) != x[k])) {
ff = (give_log) ? R_NegInf: 0.0;
return ff;
}
p += prob[k]; // sum of probabilities
n += x[k]; // total number of events
}
for (k = 0; k < m; k++) {
if ((n > 0) && (p > 0)) {
if (prob[k] > p) p = prob[k];
ff += dbinom(x[k],n,prob[k]/p,1);
} else if (x[k] < 0.0) {
ff = R_NegInf;
return ff;
}
n -= x[k];
p -= prob[k];
}
ff = (give_log) ? ff : exp(ff);
return ff;
}
static R_INLINE
void to_log_barycentric
(double *xt, const double *x, int n)
{
double sum;
int i;
for (i = 0, sum = 0.0; i < n; i++) sum += x[i];
for (i = 0; i < n; i++) xt[i] = log(x[i]/sum);
}
static R_INLINE
void from_log_barycentric
(double *xt, const double *x, int n)
{
double sum;
int i;
for (i = 0, sum = 0.0; i < n; i++) sum += (xt[i] = exp(x[i]));
for (i = 0; i < n; i++) xt[i] /= sum;
}
static R_INLINE
double dot_product
(int n, const double *x, const double *y)
{
double p = 0.0;
for (int j = 0; j < n; j++) p += x[j]*y[j];
return p;
}
static R_INLINE
double exp2geom_rate_correction
(double R, double dt)
{
return (dt > 0) ? log1p(R*dt)/dt : R;
}
static R_INLINE
double rbetabinom
(double size, double prob, double theta)
{
return rbinom(size,rbeta(prob*theta,(1.0-prob)*theta));
}
static R_INLINE
double dbetabinom
(double x, double size, double prob, double theta, int give_log)
{
double a = theta*prob;
double b = theta*(1.0-prob);
double f = lchoose(size,x)-lbeta(a,b)+lbeta(a+x,b+size-x);
return (give_log) ? f : exp(f);
}
static R_INLINE
double rbetanbinom
(double mu, double size, double theta)
{
double p = size/(size+mu);
return rnbinom(size,rbeta(p*theta,(1.0-p)*theta));
}
static R_INLINE
double dbetanbinom
(double x, double mu, double size, double theta, int give_log)
{
double p = size/(size+mu);
double a = theta*p;
double b = theta*(1.0-p);
double f = lchoose(size+x-1,size-1)-lbeta(a,b)+lbeta(a+size,b+x);
return (give_log) ? f : exp(f);
}
#endif