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

Go to the source code of this file.

Macros

#define Beta   (__p[__parindex[0]])
 
#define gamma   (__p[__parindex[1]])
 
#define psi   (__p[__parindex[2]])
 
#define chi   (__p[__parindex[3]])
 
#define omega   (__p[__parindex[4]])
 
#define S0   (__p[__parindex[5]])
 
#define I0   (__p[__parindex[6]])
 
#define R0   (__p[__parindex[7]])
 
#define POP   (__p[__parindex[8]])
 
#define S   (__x[__stateindex[0]])
 
#define I   (__x[__stateindex[1]])
 
#define R   (__x[__stateindex[2]])
 
#define ll   (__x[__stateindex[3]])
 
#define ellI   (__x[__stateindex[4]])
 
#define node   (__x[__stateindex[5]])
 
#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, const int *__covindex, const double *__covars, double *rate, double *penalty)
 
void sirs_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 sirs_gill (double *__x, const double *__p, const int *__stateindex, const int *__parindex, const int *__covindex, const double *__covars, double t, double dt)
 
void sirs_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).
 

Variables

static const int nrate = 3
 

Macro Definition Documentation

◆ Beta

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

Definition at line 6 of file sirs_pomp.c.

◆ chi

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

Definition at line 9 of file sirs_pomp.c.

◆ ellI

#define ellI   (__x[__stateindex[4]])

Definition at line 19 of file sirs_pomp.c.

◆ EVENT_RATES

#define EVENT_RATES
Value:
event_rates(__x,__p,t, \
__stateindex,__parindex,__covindex, \
__covars,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 22 of file sirs_pomp.c.

22#define EVENT_RATES \
23 event_rates(__x,__p,t, \
24 __stateindex,__parindex,__covindex, \
25 __covars,rate,&penalty) \
26

◆ gamma

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

Definition at line 7 of file sirs_pomp.c.

◆ I

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

Definition at line 16 of file sirs_pomp.c.

◆ I0

#define I0   (__p[__parindex[6]])

Definition at line 12 of file sirs_pomp.c.

◆ lik

#define lik   (__lik[0])

Definition at line 195 of file sirs_pomp.c.

◆ ll

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

Definition at line 18 of file sirs_pomp.c.

◆ node

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

Definition at line 20 of file sirs_pomp.c.

◆ omega

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

Definition at line 10 of file sirs_pomp.c.

◆ POP

#define POP   (__p[__parindex[8]])

Definition at line 14 of file sirs_pomp.c.

◆ psi

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

Definition at line 8 of file sirs_pomp.c.

◆ R

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

Definition at line 17 of file sirs_pomp.c.

◆ R0

#define R0   (__p[__parindex[7]])

Definition at line 13 of file sirs_pomp.c.

◆ S

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

Definition at line 15 of file sirs_pomp.c.

◆ S0

#define S0   (__p[__parindex[5]])

Definition at line 11 of file sirs_pomp.c.

Function Documentation

◆ event_rates()

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

Definition at line 27 of file sirs_pomp.c.

38 {
39 double event_rate = 0;
40 double alpha, disc;
41 *penalty = 0;
42 assert(I >= ellI);
43 assert(ellI >= 0);
44 assert(S >= 0);
45 // 0: transmission with saturation 0 or 1
46 alpha = Beta*S*I/POP;
47 disc = (I > 0) ? ellI*(ellI-1)/I/(I+1) : 1;
48 event_rate += (*rate = alpha*(1-disc)); rate++;
49 *penalty += alpha*disc;
50 // 1: recovery
51 alpha = gamma*I;
52 if (I > ellI) {
53 event_rate += (*rate = alpha); rate++;
54 } else {
55 *rate = 0; rate++;
56 *penalty += alpha;
57 }
58 // 2: loss of immunity
59 alpha = omega*R;
60 event_rate += (*rate = alpha); rate++;
61 // sampling
62 alpha = (psi+chi)*I;
63 *penalty += alpha;
64 assert(R_FINITE(event_rate));
65 return event_rate;
66}
#define chi
Definition lbdp_pomp.c:7
#define psi
Definition lbdp_pomp.c:6
#define POP
Definition seirs_pomp.c:37
#define gamma
Definition seirs_pomp.c:29
#define R
Definition seirs_pomp.c:41
#define I
Definition seirs_pomp.c:40
#define Beta
Definition seirs_pomp.c:27
#define ellI
Definition seirs_pomp.c:45
#define omega
Definition seirs_pomp.c:32
#define S
Definition seirs_pomp.c:38

◆ sirs_dmeas()

void sirs_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 198 of file sirs_pomp.c.

211 {
212 assert(!ISNAN(ll));
213 lik = (give_log) ? ll : exp(ll);
214}
#define lik
Definition lbdp_pomp.c:165
#define ll
Definition lbdp_pomp.c:10

◆ sirs_gill()

void sirs_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 91 of file sirs_pomp.c.

101 {
102 double tstep = 0.0, tmax = t + dt;
103 const int *nodetype = get_userdata_int("nodetype");
104 const int *sat = get_userdata_int("saturation");
105
106 int parent = (int) nearbyint(node);
107
108#ifndef NDEBUG
109 int nnode = *get_userdata_int("nnode");
110 assert(parent>=0);
111 assert(parent<=nnode);
112#endif
113
114 ll = 0;
115
116 // singular portion of filter equation
117 switch (nodetype[parent]) {
118 default: // non-genealogical event #nocov
119 break; // #nocov
120 case 0: // root
121 ellI += 1;
122 break;
123 case 1: // sample
124 assert(I >= ellI);
125 assert(ellI >= 0);
126 if (sat[parent] == 1) {
127 ll += log(psi);
128 } else if (sat[parent] == 0) {
129 ellI -= 1;
130 ll += log(psi+chi);
131 if (psi+chi <= 0)
132 ll += R_NegInf;
133 else if (unif_rand() < psi/(psi+chi)) { // non-destructive sample
134 ll += log(I-ellI);
135 } else { // destructive sample
136 ll += log(I);
137 I -= 1;
138 }
139 } else {
140 assert(0); // #nocov
141 ll += R_NegInf; // #nocov
142 }
143 break;
144 case 2: // branch point s=(1,1)
145 assert(S >= 0);
146 assert(I >= 0);
147 assert(ellI > 0);
148 assert(sat[parent]==2);
149 ll += (I > 0 && I >= ellI) ? log(Beta*S*I/POP) : R_NegInf;
150 S -= 1; I += 1;
151 ellI += 1;
152 ll -= log(I*(I-1)/2);
153 S = (S > 0) ? S : 0;
154 break;
155 }
156
157 if (tmax > t) {
158
159 // take Gillespie steps to the end of the interval:
160 int event;
161 double penalty = 0;
162 double rate[nrate];
163
164 double event_rate = EVENT_RATES;
165 tstep = exp_rand()/event_rate;
166
167 while (t + tstep < tmax) {
168 event = rcateg(event_rate,rate,nrate);
169 assert(event>=0 && event<nrate);
170 ll -= penalty*tstep;
171 switch (event) {
172 case 0: // transmission
173 S -= 1; I += 1;
174 break;
175 case 1: // recovery
176 I -= 1; R += 1;
177 break;
178 case 2: // loss of immunity
179 R -= 1; S += 1;
180 break;
181 default: // #nocov
182 assert(0); // #nocov
183 break; // #nocov
184 }
185 t += tstep;
186 event_rate = EVENT_RATES;
187 tstep = exp_rand()/event_rate;
188 }
189 tstep = tmax - t;
190 ll -= penalty*tstep;
191 }
192 node += 1;
193}
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
static const int nrate
Definition seirs_pomp.c:7
Here is the call graph for this function:

◆ sirs_rinit()

void sirs_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 69 of file sirs_pomp.c.

78 {
79 double m = POP/(S0+I0+R0);
80 S = nearbyint(S0*m);
81 I = nearbyint(I0*m);
82 R = nearbyint(R0*m);
83 ll = 0;
84 ellI = 0;
85 node = 0;
86}
#define R0
Definition seirs_pomp.c:36
#define S0
Definition seirs_pomp.c:33
#define I0
Definition seirs_pomp.c:35

Variable Documentation

◆ nrate

const int nrate = 3
static

Definition at line 4 of file sirs_pomp.c.