You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: vignettes/C_API.Rmd
+79-18
Original file line number
Diff line number
Diff line change
@@ -65,8 +65,52 @@ The return value is the probability or log probability (as requested).
65
65
66
66
### Euler-multinomial distribution
67
67
68
-
#### Simulate Euler-multinomial transitions
68
+
The Euler multinomial approximation of a continuous-time, stochastic compartmental model is as follows.
69
+
Suppose a compartment has occupancy $N_t$ at time $t$ and that there are $K$ ways of exiting the compartment, with per capita rates (hazards) $\mu_1,\dots,\mu_K$, respectively.
70
+
71
+
-----
72
+
73
+
```{r compartment-diagram,echo=FALSE,purl=FALSE,out.width="200px",out.height="200px",dpi=100,fig.height=3,fig.width=3,fig.align="center",fig.cap="**Diagram:** *A single compartment within a compartmental model. Here, there are $K=2$ paths out of the compartment.*"}
To make the Euler multinomial approximation, we approximate the total exit rate as constant over a small interval $[t,t+\Delta{t})$.
90
+
Let the random variable $\Delta{n_k}$, $k=1,\dots,K$, be the number that exit by path $k$ in this time interval and $\Delta{n_0}$ be the number that remain.
91
+
Under this assumption, the vector of numbers of exits, $(\Delta{n_{0}},\Delta{n_{1}},\dots,\Delta{n_{K}})$ is multinomially distributed with size $N_t$ and probabilities $(p_k)_{k=0}^K$, where
By way of shorthand, we say that $\Delta{n}=(\Delta{n_k})_{k=1}^K$ is *Euler-multinomially distributed* with size $N_t$, rates $\mu=(\mu_k)_{k=1}^K$, and time-step $\Delta{t}$ and we write
The **pomp** C API provides three functions that relate to the Euler-multinomial distribution.
99
+
Their descriptions follow.
100
+
101
+
#### Simulate an Euler-multinomial random variable
102
+
103
+
The `reulermultinom` function draws a random sample from this distribution.
104
+
Using the notation above, one has to pack the $K$ rates $\mu_1,\dots,\mu_K$ into contiguous memory locations and retrieve the results in (a different set of) contiguous memory locations.
105
+
For example, if `rate` is a [pointer](https://www.tutorialspoint.com/cprogramming/c_pointers) to $K$ contiguous memory locations holding the rates and `dn` is a pointer to $K$ contiguous memory locations ready to hold the results, then
106
+
```{c eval=FALSE}
107
+
reulermultinom(K,N,rate,dt,dn);
108
+
```
109
+
will result in a random sample from the Euler multinomial distribution (with timestep dt) being stored in `dn[0]`, ..., `dn[K-1]`.
110
+
In the foregoing, we've assumed that the quantities $N_t$ and $K$ are stored in the integer variables `N` and `K`, respectively, and that the double precision variable `dt` holds the timestep.
69
111
112
+
<aid="reulermultinom-prototype"></a>
113
+
The prototype is:
70
114
71
115
```{c eval=FALSE}
72
116
void reulermultinom(int m, double size, const double *rate,
@@ -81,26 +125,21 @@ Input:
81
125
-`dt`, a positive real number, is the duration of time interval.
82
126
-`trans` is a pointer to the vector that will hold the random deviate.
83
127
128
+
Output:
129
+
84
130
On return, `trans[0]`, ..., `trans[m-1]` will be the numbers of individuals making each of the respective transitions.
85
131
86
-
See [`?reulermultinom`](https://kingaa.github.io/manuals/pomp/html/eulermultinom.html)and [FAQ 3.6](./FAQ.html#eulermultinomial-approximation)for more on the Euler-multinomial distributions.
132
+
See [`?reulermultinom`](https://kingaa.github.io/manuals/pomp/html/eulermultinom.html) for more on the Euler-multinomial distributions.
87
133
88
134
**NB:**`reulermultinom` does not call `GetRNGstate()` or `PutRNGstate()` internally.
89
135
This must be done by the calling program.
90
136
But note that when `reulermultinom` is called inside a **pomp** rprocess, there is no need to call either `GetRNGState()` or `PutRNGState()`;
91
137
this is handled by **pomp**.
92
138
93
-
#### Expectation of an Euler-multinomial random variable
94
-
95
-
```{c eval=FALSE}
96
-
void eeulermultinom(int m, double size, const double *rate,
97
-
double dt, double *trans);
98
-
```
99
-
100
-
The parameters `m`, `size`, `rate`, and `dt` have the same meaning as above.
101
-
After a call to `eeulermultinom`, `trans` points to an array of `double`s holding the *expected values* of the Euler-multinomial random variables.
139
+
#### Probability distribution of an Euler-multinomial random variable
102
140
103
-
#### Compute probabilities of Euler-multinomial transitions
141
+
If $\Delta{n} \sim \mathrm{Eulermultinom}\left(N_t,\mu,\Delta{t}\right)$, then the probability it takes a specific value can be computed using the C function `deulermultinom`.
142
+
Its prototype is:
104
143
105
144
```{c eval=FALSE}
106
145
double deulermultinom(int m, double size, const double *rate,
@@ -115,13 +154,35 @@ Input:
115
154
-`dt`, a positive real number, is the duration of time interval.
116
155
-`trans` is pointer to vector containing the data, which are numbers of individuals making the respective transitions.
117
156
-`give_log` is an integer:
118
-
- `give_log=1` if log probability is desired;
119
-
- `give_log=0` if probability is desired.
157
+
- `give_log=0` requests that the probability be returned.
158
+
- `give_log=1` requests that the log probability to be returned.
159
+
160
+
Output:
120
161
121
162
The value returned is the probability or log probability (as requested).
122
163
123
-
See [`?deulermultinom`](https://kingaa.github.io/manuals/pomp/html/eulermultinom.html) and [FAQ 3.6](./FAQ.html#eulermultinomial-approximation) for more on the Euler-multinomial distributions.
164
+
See also [`?deulermultinom`](https://kingaa.github.io/manuals/pomp/html/eulermultinom.html).
124
165
166
+
#### Expectation of an Euler-multinomial random variable
167
+
168
+
If $\Delta{n} \sim \mathrm{Eulermultinom}\left(N_t,\mu,\Delta{t}\right)$, then the expectation of its $i$-th component is
169
+
$$\mathbb{E}\left[\Delta{n}_i\right]=p_k N_t,$$
170
+
where $p_k$ is as [defined above](#eulermultinomial-definition).
171
+
The C function `eeulermultinom` computes this.
172
+
Its prototype is:
173
+
174
+
```{c eval=FALSE}
175
+
void eeulermultinom(int m, double size, const double *rate,
176
+
double dt, double *trans);
177
+
```
178
+
179
+
Input:
180
+
181
+
The parameters `m`, `size`, `rate`, and `dt` have the same meaning [as above](#reulermultinom-prototype).
182
+
183
+
Output:
184
+
185
+
After a call to `eeulermultinom`, `trans` points to an array of `double`s holding the *expected values* of the Euler-multinomial random variables.
0 commit comments