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 164 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 167 of file lbdp_pomp.c.

180 {
181 assert(!ISNAN(ll));
182 lik = (give_log) ? ll : exp(ll);
183}
#define lik
Definition lbdp_pomp.c:164
#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 72 of file lbdp_pomp.c.

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