21#define err(...) Rf_errorcall(R_NilValue,__VA_ARGS__)
22#define warn(...) Rf_warningcall(R_NilValue,__VA_ARGS__)
26(
double x,
double *knots,
int degree,
27 int nbasis,
int deriv,
double *y);
31(
double x,
double period,
int degree,
32 int nbasis,
int deriv,
double *y);
40(
double *x,
const double *p,
double t0,
41 const int *stateindex,
const int *parindex,
42 const int *covindex,
const double *covars);
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);
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);
59(
double *x,
const double *p,
60 const int *stateindex,
const int *parindex,
61 const int *covindex,
const double *covars,
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);
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);
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);
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);
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);
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);
107(
double *p,
const int *parindex);
111(
double *lik,
const double *p,
int give_log,
const int *parindex);
115(
double *pt,
const double *p,
const int *parindex);
119 return log(p/(1.0-p));
124 return 1.0/(1.0+exp(-x));
129(
double sigma,
double dt)
133 return (sigmasq > 0) ? rgamma(dt/sigmasq,sigmasq) : dt;
138(
int m,
double size,
const double *rate,
double dt,
double *trans)
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.");
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.");
156 size = size*(1-exp(-lambda*dt));
157 for (k = 0; k < m; k++)
158 trans[k] = size*rate[k]/lambda;
160 for (k = 0; k < m; k++) trans[k] = 0.0;
166(
int m,
double size,
const double *rate,
double dt,
double *trans)
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.");
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.");
185 size = rbinom(size,1-exp(-p*dt));
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;
195 for (k = 0; k < m; k++) trans[k] = 0.0;
201(
int m,
double size,
const double *rate,
double dt,
double *trans,
int give_log)
207 if ((dt < 0.0) || (size < 0.0) || (floor(size+0.5) != size)) {
208 warn(
"in 'deulermultinom': NaNs produced.");
211 for (k = 0; k < m; k++) {
213 warn(
"in 'deulermultinom': NaNs produced.");
216 if (trans[k] < 0.0) {
217 ff = (give_log) ? R_NegInf: 0.0;
224 ff = (give_log) ? R_NegInf: 0.0;
227 ff = dbinom(n,size,1-exp(-p*dt),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);
237 ff = (give_log) ? ff : exp(ff);
243(
int m,
const double *prob,
double *x,
int give_log)
249 for (k = 0; k < m; k++) {
251 warn(
"in 'dmultinom': NaNs produced.");
254 if ((x[k] < 0.0) || (floor(x[k]+0.5) != x[k])) {
255 ff = (give_log) ? R_NegInf: 0.0;
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) {
272 ff = (give_log) ? ff : exp(ff);
278(
double *xt,
const double *x,
int n)
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);
288(
double *xt,
const double *x,
int n)
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;
298(
int n,
const double *x,
const double *y)
301 for (
int j = 0; j < n; j++) p += x[j]*y[j];
309 return (dt > 0) ? log1p(
R*dt)/dt :
R;
314(
double size,
double prob,
double theta)
316 return rbinom(size,rbeta(prob*theta,(1.0-prob)*theta));
321(
double x,
double size,
double prob,
double theta,
int give_log)
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);
331(
double mu,
double size,
double theta)
333 double p = size/(size+mu);
334 return rnbinom(size,rbeta(p*theta,(1.0-p)*theta));
339(
double x,
double mu,
double size,
double theta,
int give_log)
341 double p = size/(size+mu);
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);
static R_INLINE double dbetanbinom(double x, double mu, double size, double theta, int give_log)
static R_INLINE double deulermultinom(int m, double size, const double *rate, double dt, double *trans, int give_log)
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)
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)
const double * get_userdata_double_t(const char *name)
static R_INLINE void eeulermultinom(int m, double size, const double *rate, double dt, double *trans)
static R_INLINE double rbetabinom(double size, double prob, double theta)
static R_INLINE void from_log_barycentric(double *xt, const double *x, int n)
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)
void pomp_dprior(double *lik, const double *p, int give_log, const int *parindex)
const SEXP get_userdata_t(const char *name)
static R_INLINE double logit(double p)
static R_INLINE double exp2geom_rate_correction(double R, double dt)
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)
static R_INLINE double rgammawn(double sigma, double dt)
static R_INLINE double rbetanbinom(double mu, double size, double theta)
static R_INLINE double expit(double x)
static R_INLINE void to_log_barycentric(double *xt, const double *x, int n)
static R_INLINE double dbetabinom(double x, double size, double prob, double theta, int give_log)
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)
void pomp_rinit(double *x, const double *p, double t0, const int *stateindex, const int *parindex, const int *covindex, const double *covars)
void pomp_rprior(double *p, const int *parindex)
void bspline_basis_eval_deriv_t(double x, double *knots, int degree, int nbasis, int deriv, double *y)
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)
static R_INLINE double dmultinom(int m, const double *prob, double *x, int give_log)
void periodic_bspline_basis_eval_deriv_t(double x, double period, int degree, int nbasis, int deriv, double *y)
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)
static R_INLINE double dot_product(int n, const double *x, const double *y)
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)
const int * get_userdata_int_t(const char *name)
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)
void pomp_transform(double *pt, const double *p, const int *parindex)
static R_INLINE void reulermultinom(int m, double size, const double *rate, double dt, double *trans)