phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
lbdp_pomp.c File Reference
#include "pomplink.h"
#include "internal.h"
Include dependency graph for lbdp_pomp.c:

Go to the source code of this file.

Macros

#define lambda   (__p[__parindex[0]])
 
#define mu   (__p[__parindex[1]])
 
#define psi   (__p[__parindex[2]])
 
#define chi   (__p[__parindex[3]])
 
#define n0   (__p[__parindex[4]])
 
#define n   (__x[__stateindex[0]])
 
#define ll   (__x[__stateindex[1]])
 
#define ell   (__x[__stateindex[2]])
 
#define node   (__x[__stateindex[3]])
 
#define EVENT_RATES
 
#define lik   (__lik[0])
 

Functions

static double event_rates (double *__x, const double *__p, double t, const int *__stateindex, const int *__parindex, double *rate, double *penalty)
 
void lbdp_rinit (double *__x, const double *__p, double t, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars)
 Latent-state initializer (rinit).
 
void lbdp_gill (double *__x, const double *__p, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars, double t, double dt)
 
void lbdp_dmeas (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)
 Measurement model likelihood (dmeasure).
 

Macro Definition Documentation

◆ chi

#define chi   (__p[__parindex[3]])

Definition at line 7 of file lbdp_pomp.c.

◆ ell

#define ell   (__x[__stateindex[2]])

Definition at line 11 of file lbdp_pomp.c.

◆ EVENT_RATES

#define EVENT_RATES
Value:
event_rates(__x,__p,t, \
__stateindex,__parindex,rate,&penalty) \
static double event_rates(double *__x, const double *__p, double t, const int *__stateindex, const int *__parindex, double *rate, double *penalty)
Definition lbdp_pomp.c:19

Definition at line 14 of file lbdp_pomp.c.

14#define EVENT_RATES \
15 event_rates(__x,__p,t, \
16 __stateindex,__parindex,rate,&penalty) \
17

◆ lambda

#define lambda   (__p[__parindex[0]])

Definition at line 4 of file lbdp_pomp.c.

◆ lik

#define lik   (__lik[0])

Definition at line 165 of file lbdp_pomp.c.

◆ ll

#define ll   (__x[__stateindex[1]])

Definition at line 10 of file lbdp_pomp.c.

◆ mu

#define mu   (__p[__parindex[1]])

Definition at line 5 of file lbdp_pomp.c.

◆ n

#define n   (__x[__stateindex[0]])

Definition at line 9 of file lbdp_pomp.c.

◆ n0

#define n0   (__p[__parindex[4]])

Definition at line 8 of file lbdp_pomp.c.

◆ node

#define node   (__x[__stateindex[3]])

Definition at line 12 of file lbdp_pomp.c.

◆ psi

#define psi   (__p[__parindex[2]])

Definition at line 6 of file lbdp_pomp.c.

Function Documentation

◆ event_rates()

static double event_rates ( double * __x,
const double * __p,
double t,
const int * __stateindex,
const int * __parindex,
double * rate,
double * penalty )
static

Definition at line 18 of file lbdp_pomp.c.

27 {
28 double event_rate = 0;
29 double alpha, disc;
30 *penalty = 0;
31 assert(n >= ell);
32 assert(ell >= 0);
33 // birth with saturation 0 or 1
34 alpha = lambda*n;
35 disc = (n > 0) ? ell*(ell-1)/n/(n+1) : 1;
36 event_rate += (*rate = alpha*(1-disc)); rate++;
37 *penalty += alpha*disc;
38 // death
39 alpha = mu*n;
40 if (n > ell) {
41 event_rate += (*rate = alpha); rate++;
42 } else {
43 *rate = 0; rate++;
44 *penalty += alpha;
45 }
46 // sampling
47 *penalty += (psi+chi)*n;
48 assert(R_FINITE(event_rate));
49 return event_rate;
50}
#define mu
Definition lbdp_pomp.c:5
#define chi
Definition lbdp_pomp.c:7
#define ell
Definition lbdp_pomp.c:11
#define n
Definition lbdp_pomp.c:9
#define lambda
Definition lbdp_pomp.c:4
#define psi
Definition lbdp_pomp.c:6
Here is the caller graph for this function:

◆ lbdp_dmeas()

void lbdp_dmeas ( 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 )

Measurement model likelihood (dmeasure).

Definition at line 168 of file lbdp_pomp.c.

181 {
182 assert(!ISNAN(ll));
183 lik = (give_log) ? ll : exp(ll);
184}
#define lik
Definition lbdp_pomp.c:165
#define ll
Definition lbdp_pomp.c:10

◆ lbdp_gill()

void lbdp_gill ( double * __x,
const double * __p,
const int * __stateindex,
const int * __parindex,
const int * __covindex,
const double * __covars,
double t,
double dt )

Latent-state process simulator (rprocess).

This integrates the filter equation.

Definition at line 73 of file lbdp_pomp.c.

83 {
84 double tstep = 0.0, tmax = t + dt;
85 const int *nodetype = get_userdata_int("nodetype");
86 const int *sat = get_userdata_int("saturation");
87 int parent = (int) nearbyint(node);
88
89#ifndef NDEBUG
90 int nnode = *get_userdata_int("nnode");
91 assert(parent>=0);
92 assert(parent<=nnode);
93#endif
94
95 ll = 0;
96
97 // singular portion of filter equation
98 switch (nodetype[parent]) {
99 default: // non-genealogical event #nocov
100 break; // #nocov
101 case 0: // root
102 ell += 1;
103 break;
104 case 1: // sample
105 assert(n >= ell);
106 assert(ell >= 0);
107 if (sat[parent] == 1) { // s=1
108 ll += log(psi);
109 } else if (sat[parent] == 0) { // s=0
110 ell -= 1;
111 double drate = chi*n;
112 double trate = drate+psi*(n-ell);
113 ll += (trate > 0) ? log(trate) : R_NegInf;
114 if (trate > 0 && unif_rand() < drate/trate) n -= 1;
115 } else {
116 assert(0); // #nocov
117 ll += R_NegInf; // #nocov
118 }
119 break;
120 case 2: // branch point s=2
121 ll = 0;
122 assert(n >= 0);
123 assert(ell > 0);
124 assert(sat[parent]==2);
125 n += 1; ell += 1;
126 ll += log(2*lambda/n);
127 break;
128 }
129
130 if (tmax > t) {
131
132 // Gillespie steps:
133 int event;
134 double penalty = 0;
135 double rate[2];
136
137 double event_rate = EVENT_RATES;
138 tstep = exp_rand()/event_rate;
139
140 while (t + tstep < tmax) {
141 event = rcateg(event_rate,rate,2);
142 assert(event>=0 && event<2);
143 ll -= penalty*tstep;
144 switch (event) {
145 case 0: // birth
146 n += 1;
147 break;
148 case 1: // death
149 n -= 1;
150 break;
151 default: // #nocov
152 assert(0); // #nocov
153 break; // #nocov
154 }
155 t += tstep;
156 event_rate = EVENT_RATES;
157 tstep = exp_rand()/event_rate;
158 }
159 tstep = tmax - t;
160 ll -= penalty*tstep;
161 }
162 node += 1;
163}
get_userdata_int_t * get_userdata_int
Definition init.c:7
static int rcateg(double erate, double *rate, int nrate)
Definition internal.h:85
#define EVENT_RATES
Definition lbdp_pomp.c:14
#define node
Definition lbdp_pomp.c:12
Here is the call graph for this function:

◆ lbdp_rinit()

void lbdp_rinit ( double * __x,
const double * __p,
double t,
const int * __stateindex,
const int * __parindex,
const int * __covindex,
const double * __covars )

Latent-state initializer (rinit).

Definition at line 53 of file lbdp_pomp.c.

62 {
63 n = nearbyint(n0);
64 assert(n>=0); // #nocov
65 ll = 0;
66 ell = 0;
67 node = 0;
68}
#define n0
Definition lbdp_pomp.c:8