phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
lbdp_pomp.c
Go to the documentation of this file.
1#include "pomplink.h"
2#include "internal.h"
3
4#define lambda (__p[__parindex[0]])
5#define mu (__p[__parindex[1]])
6#define psi (__p[__parindex[2]])
7#define chi (__p[__parindex[3]])
8#define n0 (__p[__parindex[4]])
9#define n (__x[__stateindex[0]])
10#define ll (__x[__stateindex[1]])
11#define ell (__x[__stateindex[2]])
12#define node (__x[__stateindex[3]])
13
14#define EVENT_RATES \
15 event_rates(__x,__p,t, \
16 __stateindex,__parindex,rate,&penalty) \
17
18static double event_rates
19(
20 double *__x,
21 const double *__p,
22 double t,
23 const int *__stateindex,
24 const int *__parindex,
25 double *rate,
26 double *penalty
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}
51
54(
55 double *__x,
56 const double *__p,
57 double t,
58 const int *__stateindex,
59 const int *__parindex,
60 const int *__covindex,
61 const double *__covars
62 ){
63 n = nearbyint(n0);
64 ll = 0;
65 ell = 0;
66 node = 0;
67}
68
73(
74 double *__x,
75 const double *__p,
76 const int *__stateindex,
77 const int *__parindex,
78 const int *__covindex,
79 const double *__covars,
80 double t,
81 double dt
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}
163
164# define lik (__lik[0])
165
168(
169 double *__lik,
170 const double *__y,
171 const double *__x,
172 const double *__p,
173 int give_log,
174 const int *__obsindex,
175 const int *__stateindex,
176 const int *__parindex,
177 const int *__covindex,
178 const double *__covars,
179 double t
180 ) {
181 assert(!ISNAN(ll));
182 lik = (give_log) ? ll : exp(ll);
183}
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 mu
Definition lbdp_pomp.c:5
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 lbdp_pomp.c:54
#define n0
Definition lbdp_pomp.c:8
void lbdp_gill(double *__x, const double *__p, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars, double t, double dt)
Definition lbdp_pomp.c:73
#define chi
Definition lbdp_pomp.c:7
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
#define ell
Definition lbdp_pomp.c:11
#define lik
Definition lbdp_pomp.c:164
#define n
Definition lbdp_pomp.c:9
#define ll
Definition lbdp_pomp.c:10
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 lbdp_pomp.c:168
#define lambda
Definition lbdp_pomp.c:4
#define psi
Definition lbdp_pomp.c:6
#define EVENT_RATES
Definition lbdp_pomp.c:14
#define node
Definition lbdp_pomp.c:12