phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 n0   (__p[__parindex[3]])
 
#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

◆ ell

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

Definition at line 10 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:18

Definition at line 13 of file lbdp_pomp.c.

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

◆ lambda

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

Definition at line 4 of file lbdp_pomp.c.

◆ lik

#define lik   (__lik[0])

Definition at line 159 of file lbdp_pomp.c.

◆ ll

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

Definition at line 9 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 8 of file lbdp_pomp.c.

◆ n0

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

Definition at line 7 of file lbdp_pomp.c.

◆ node

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

Definition at line 11 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 17 of file lbdp_pomp.c.

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

◆ 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 162 of file lbdp_pomp.c.

175 {
176 assert(!ISNAN(ll));
177 lik = (give_log) ? ll : exp(ll);
178}
#define lik
Definition lbdp_pomp.c:159
#define ll
Definition lbdp_pomp.c:9

◆ 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 71 of file lbdp_pomp.c.

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

61 {
62 n = nearbyint(n0);
63 ll = 0;
64 ell = 0;
65 node = 0;
66}
#define n0
Definition lbdp_pomp.c:7