pomp
Inference for partially observed Markov processes
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pomp.h
Go to the documentation of this file.
1// -*- C++ -*-
2// Header for the C API for pomp.
3// Documentation: https://kingaa.github.io/pomp/C_API.html
4
13
14#ifndef _POMP_H_
15#define _POMP_H_
16
17#include <R.h>
18#include <Rmath.h>
19#include <Rdefines.h>
20
21#define err(...) Rf_errorcall(R_NilValue,__VA_ARGS__)
22#define warn(...) Rf_warningcall(R_NilValue,__VA_ARGS__)
23
24typedef
26(double x, double *knots, int degree,
27 int nbasis, int deriv, double *y);
28
29typedef
31(double x, double period, int degree,
32 int nbasis, int deriv, double *y);
33
34typedef const SEXP get_userdata_t (const char *name);
35typedef const int *get_userdata_int_t (const char *name);
36typedef const double *get_userdata_double_t (const char *name);
37
38typedef
40(double *x, const double *p, double t0,
41 const int *stateindex, const int *parindex,
42 const int *covindex, const double *covars);
43
44typedef
46(double *lik, const double *x, const double *p,
47 double t0, const int *stateindex, const int *parindex,
48 const int *covindex, const double *covars);
49
50typedef
52(int event, double t, const double *x,
53 const double *p, const int *stateindex,
54 const int *parindex, const int *covindex,
55 const double *covars);
56
57typedef
59(double *x, const double *p,
60 const int *stateindex, const int *parindex,
61 const int *covindex, const double *covars,
62 double t, double dt);
63
64typedef
66(double *loglik, const double *x1, const double *x2,
67 double t1, double t2, const double *p,
68 const int *stateindex, const int *parindex,
69 const int *covindex, const double *covars);
70
71typedef
73(double *f, const double *x, const double *p,
74 const int *stateindex, const int *parindex,
75 const int *covindex, const double *covars, double t);
76
77typedef
79(double *y, const double *x, const double *p,
80 const int *obsindex, const int *stateindex,
81 const int *parindex, const int *covindex,
82 const double *covars, double t);
83
84typedef
86(double *lik, const double *y, const double *x,
87 const double *p, int give_log, const int *obsindex,
88 const int *stateindex, const int *parindex,
89 const int *covindex, const double *covars, double t);
90
91typedef
93(double *f, const double *x, const double *p,
94 const int *obsindex, const int *stateindex,
95 const int *parindex, const int *covindex,
96 const double *covars, double t);
97
98typedef
100(double *f, const double *x, const double *p,
101 const int *vmatindex, const int *stateindex,
102 const int *parindex, const int *covindex,
103 const double *covars, double t);
104
105typedef
107(double *p, const int *parindex);
108
109typedef
111(double *lik, const double *p, int give_log, const int *parindex);
112
113typedef
115(double *pt, const double *p, const int *parindex);
116
117static R_INLINE
118double logit (double p) {
119 return log(p/(1.0-p));
120}
121
122static R_INLINE
123double expit (double x) {
124 return 1.0/(1.0+exp(-x));
125}
126
127static R_INLINE
129(double sigma, double dt)
130{
131 double sigmasq;
132 sigmasq = sigma*sigma;
133 return (sigmasq > 0) ? rgamma(dt/sigmasq,sigmasq) : dt;
134}
135
136static R_INLINE
138(int m, double size, const double *rate, double dt, double *trans)
139{
140 double lambda = 0.0;
141 int j, k;
142 if (!R_FINITE(size) || size < 0.0 || !R_FINITE(dt) || dt < 0.0) {
143 for (k = 0; k < m; k++) trans[k] = R_NaReal;
144 warn("in 'eeulermultinom': NAs produced.");
145 return;
146 }
147 for (k = 0; k < m; k++) {
148 if (!R_FINITE(rate[k]) || rate[k] < 0.0) {
149 for (j = 0; j < m; j++) trans[j] = R_NaReal;
150 warn("in 'eeulermultinom': NAs produced.");
151 return;
152 }
153 lambda += rate[k];
154 }
155 if (lambda > 0.0) {
156 size = size*(1-exp(-lambda*dt));
157 for (k = 0; k < m; k++)
158 trans[k] = size*rate[k]/lambda;
159 } else {
160 for (k = 0; k < m; k++) trans[k] = 0.0;
161 }
162}
163
164static R_INLINE
166(int m, double size, const double *rate, double dt, double *trans)
167{
168 double p = 0.0;
169 int j, k;
170 if ( !R_FINITE(size) || size < 0.0 || floor(size+0.5) != size ||
171 !R_FINITE(dt) || dt < 0.0) {
172 for (k = 0; k < m; k++) trans[k] = R_NaReal;
173 warn("in 'reulermultinom': NAs produced.");
174 return;
175 }
176 for (k = 0; k < m; k++) {
177 if (!R_FINITE(rate[k]) || rate[k] < 0.0) {
178 for (j = 0; j < m; j++) trans[j] = R_NaReal;
179 warn("in 'reulermultinom': NAs produced.");
180 return;
181 }
182 p += rate[k]; // total event rate
183 }
184 if (p > 0.0) {
185 size = rbinom(size,1-exp(-p*dt)); // total number of events
186 m -= 1;
187 for (k = 0; k < m; k++) {
188 if (rate[k] > p) p = rate[k];
189 trans[k] = ((size > 0) && (p > 0)) ? rbinom(size,rate[k]/p) : 0;
190 size -= trans[k];
191 p -= rate[k];
192 }
193 trans[m] = size;
194 } else {
195 for (k = 0; k < m; k++) trans[k] = 0.0;
196 }
197}
198
199static R_INLINE
201(int m, double size, const double *rate, double dt, double *trans, int give_log)
202{
203 double p = 0.0;
204 double n = 0.0;
205 double ff = 0.0;
206 int k;
207 if ((dt < 0.0) || (size < 0.0) || (floor(size+0.5) != size)) {
208 warn("in 'deulermultinom': NaNs produced.");
209 return R_NaN;
210 }
211 for (k = 0; k < m; k++) {
212 if (rate[k] < 0.0) {
213 warn("in 'deulermultinom': NaNs produced.");
214 return R_NaN;
215 }
216 if (trans[k] < 0.0) {
217 ff = (give_log) ? R_NegInf: 0.0;
218 return ff;
219 }
220 p += rate[k]; // total event rate
221 n += trans[k]; // total number of events
222 }
223 if (n > size) {
224 ff = (give_log) ? R_NegInf: 0.0;
225 return ff;
226 }
227 ff = dbinom(n,size,1-exp(-p*dt),1); // total number of events
228 m -= 1;
229 for (k = 0; k < m; k++) {
230 if ((n > 0) && (p > 0)) {
231 if (rate[k] > p) p = rate[k];
232 ff += dbinom(trans[k],n,rate[k]/p,1);
233 }
234 n -= trans[k];
235 p -= rate[k];
236 }
237 ff = (give_log) ? ff : exp(ff);
238 return ff;
239}
240
241static R_INLINE
243(int m, const double *prob, double *x, int give_log)
244{
245 double p = 0.0;
246 double n = 0.0;
247 double ff = 0.0;
248 int k;
249 for (k = 0; k < m; k++) {
250 if (prob[k] < 0.0) {
251 warn("in 'dmultinom': NaNs produced.");
252 return R_NaN;
253 }
254 if ((x[k] < 0.0) || (floor(x[k]+0.5) != x[k])) {
255 ff = (give_log) ? R_NegInf: 0.0;
256 return ff;
257 }
258 p += prob[k]; // sum of probabilities
259 n += x[k]; // total number of events
260 }
261 for (k = 0; k < m; k++) {
262 if ((n > 0) && (p > 0)) {
263 if (prob[k] > p) p = prob[k];
264 ff += dbinom(x[k],n,prob[k]/p,1);
265 } else if (x[k] < 0.0) {
266 ff = R_NegInf;
267 return ff;
268 }
269 n -= x[k];
270 p -= prob[k];
271 }
272 ff = (give_log) ? ff : exp(ff);
273 return ff;
274}
275
276static R_INLINE
278(double *xt, const double *x, int n)
279{
280 double sum;
281 int i;
282 for (i = 0, sum = 0.0; i < n; i++) sum += x[i];
283 for (i = 0; i < n; i++) xt[i] = log(x[i]/sum);
284}
285
286static R_INLINE
288(double *xt, const double *x, int n)
289{
290 double sum;
291 int i;
292 for (i = 0, sum = 0.0; i < n; i++) sum += (xt[i] = exp(x[i]));
293 for (i = 0; i < n; i++) xt[i] /= sum;
294}
295
296static R_INLINE
298(int n, const double *x, const double *y)
299{
300 double p = 0.0;
301 for (int j = 0; j < n; j++) p += x[j]*y[j];
302 return p;
303}
304
305static R_INLINE
307(double R, double dt)
308{
309 return (dt > 0) ? log1p(R*dt)/dt : R;
310}
311
312static R_INLINE
314(double size, double prob, double theta)
315{
316 return rbinom(size,rbeta(prob*theta,(1.0-prob)*theta));
317}
318
319static R_INLINE
321(double x, double size, double prob, double theta, int give_log)
322{
323 double a = theta*prob;
324 double b = theta*(1.0-prob);
325 double f = lchoose(size,x)-lbeta(a,b)+lbeta(a+x,b+size-x);
326 return (give_log) ? f : exp(f);
327}
328
329static R_INLINE
331(double mu, double size, double theta)
332{
333 double p = size/(size+mu);
334 return rnbinom(size,rbeta(p*theta,(1.0-p)*theta));
335}
336
337static R_INLINE
339(double x, double mu, double size, double theta, int give_log)
340{
341 double p = size/(size+mu);
342 double a = theta*p;
343 double b = theta*(1.0-p);
344 double f = lchoose(size+x-1,size-1)-lbeta(a,b)+lbeta(a+size,b+x);
345 return (give_log) ? f : exp(f);
346}
347
348#endif
#define R
Definition gompertz.c:8
#define sigma
Definition gompertz.c:93
static R_INLINE double dbetanbinom(double x, double mu, double size, double theta, int give_log)
Definition pomp.h:339
static R_INLINE double deulermultinom(int m, double size, const double *rate, double dt, double *trans, int give_log)
Definition pomp.h:201
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)
Definition pomp.h:46
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)
Definition pomp.h:73
const double * get_userdata_double_t(const char *name)
Definition pomp.h:36
static R_INLINE void eeulermultinom(int m, double size, const double *rate, double dt, double *trans)
Definition pomp.h:138
static R_INLINE double rbetabinom(double size, double prob, double theta)
Definition pomp.h:314
static R_INLINE void from_log_barycentric(double *xt, const double *x, int n)
Definition pomp.h:288
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)
Definition pomp.h:93
void pomp_dprior(double *lik, const double *p, int give_log, const int *parindex)
Definition pomp.h:111
const SEXP get_userdata_t(const char *name)
Definition pomp.h:34
static R_INLINE double logit(double p)
Definition pomp.h:118
static R_INLINE double exp2geom_rate_correction(double R, double dt)
Definition pomp.h:307
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)
Definition pomp.h:86
static R_INLINE double rgammawn(double sigma, double dt)
Definition pomp.h:129
static R_INLINE double rbetanbinom(double mu, double size, double theta)
Definition pomp.h:331
static R_INLINE double expit(double x)
Definition pomp.h:123
static R_INLINE void to_log_barycentric(double *xt, const double *x, int n)
Definition pomp.h:278
static R_INLINE double dbetabinom(double x, double size, double prob, double theta, int give_log)
Definition pomp.h:321
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)
Definition pomp.h:52
#define warn(...)
Definition pomp.h:22
void pomp_rinit(double *x, const double *p, double t0, const int *stateindex, const int *parindex, const int *covindex, const double *covars)
Definition pomp.h:40
void pomp_rprior(double *p, const int *parindex)
Definition pomp.h:107
void bspline_basis_eval_deriv_t(double x, double *knots, int degree, int nbasis, int deriv, double *y)
Definition pomp.h:26
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)
Definition pomp.h:79
static R_INLINE double dmultinom(int m, const double *prob, double *x, int give_log)
Definition pomp.h:243
void periodic_bspline_basis_eval_deriv_t(double x, double period, int degree, int nbasis, int deriv, double *y)
Definition pomp.h:31
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)
Definition pomp.h:66
static R_INLINE double dot_product(int n, const double *x, const double *y)
Definition pomp.h:298
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)
Definition pomp.h:59
const int * get_userdata_int_t(const char *name)
Definition pomp.h:35
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)
Definition pomp.h:100
void pomp_transform(double *pt, const double *p, const int *parindex)
Definition pomp.h:115
static R_INLINE void reulermultinom(int m, double size, const double *rate, double dt, double *trans)
Definition pomp.h:166