Skip to content

Commit e8d1813

Browse files
committed
initial doc for embedded laplace.
1 parent ba3557f commit e8d1813

File tree

1 file changed

+173
-48
lines changed

1 file changed

+173
-48
lines changed

src/functions-reference/embedded_laplace.qmd

Lines changed: 173 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ $p(\phi \mid y)$ using one of Stan's algorithms.
3030

3131
To obtain posterior draws for $\theta$, we generate samples from the Laplace
3232
approximation to $p(\theta \mid y, \phi)$ in `generated quantities`.
33+
The process of iteratively drawing from $p(\phi \mid y)$ (say, with MCMC) and
34+
then $p(\theta \mid y, \phi)$ produces samples from the joint posterior
35+
$p(\theta, \phi \mid y)$.
36+
3337

3438
## Specifying the likelihood function
3539

36-
The first step is to write down a function in the `functions` block which
37-
returns `\log p(y \mid \theta, \phi)`. There are a few constraints on this
38-
function:
40+
The first step to use the embedded Laplace approximation is to write down a
41+
function in the `functions` block which returns `\log p(y \mid \theta, \phi)`.
42+
There are a few constraints on this function:
3943

4044
* The function return type must be `real`
4145

@@ -148,9 +152,7 @@ target += laplace_margina_tol(function ll_function, tupple (...), vector theta_0
148152

149153
In `generated quantities`, it is possible to draw samples from the Laplace
150154
approximation of $p(\theta \mid \phi, y)$ using `laplace_latent_rng`.
151-
The process of iteratively drawing from $p(\phi \mid y)$ (say, with MCMC) and
152-
then $p(\theta \mid y, \phi)$ produces samples from the joint posterior
153-
$p(\theta, \phi \mid y)$. The signature for `laplace_latent_rng` follows closely
155+
The signature for `laplace_latent_rng` follows closely
154156
the signature for `laplace_marginal`:
155157
```
156158
vector theta =
@@ -166,70 +168,193 @@ vector theta =
166168
int solver, int max_steps_linesearch);
167169
```
168170

169-
## Built-in likelihood functions for the embedded Laplace
170-
171-
Stan supports a narrow menu of built-in likelihood functions. These wrappers
172-
exist for the user's convenience but are not more computationally efficient
173-
than specifying log likelihoods in the `functions` block.
171+
## Built-in likelihood functions
174172

175-
[...]
173+
Stan supports certain built-in likelihood functions. This selection is currently
174+
narrow and expected to grow. The built-in functions exist for the user's
175+
convenience but are not more computationally efficient than specifying log
176+
likelihoods in the `functions` block.
176177

178+
### Poisson likelihood with log link
177179

178-
## Draw approximate samples for out-of-sample latent variables.
179-
180-
In many applications, it is of interest to draw latent variables for
181-
in-sample and out-of-sample predictions. We respectively denote these latent
182-
variables $\theta$ and $\theta^*$. In a latent Gaussian model,
183-
$(\theta, \theta^*)$ jointly follow a prior multivariate normal distribution:
180+
Consider a count data, which each observed count $y_i$ associated with a group
181+
$g(i)$ and a corresponding latent variable $\theta_{g(i)}$. The likelihood is
184182
$$
185-
\theta, \theta^* \sim \text{MultiNormal}(0, {\bf K}(\phi)),
183+
p(y \mid \theta, \phi) = \prod_i\text{Poisson} (y_i \mid \exp(\theta_{g(i)})).
186184
$$
187-
where $\bf K$ designates the joint covariance matrix over $\theta, \theta^*$.
185+
The arguments required to compute this likelihood are:
188186

189-
We can break $\bf K$ into three components,
190-
$$
191-
{\bf K} = \begin{bmatrix}
192-
K & \\
193-
K^* & K^{**}
194-
\end{bmatrix},
195-
$$
196-
where $K$ is the prior covariance matrix for $\theta$, $K^{**}$ the prior
197-
covariance matrix for $\theta^*$, and $K^*$ the covariance matrix between
198-
$\theta$ and $\theta^*$.
187+
* `y`: an array of counts.
188+
* `y_index`: an array whose $i^\text{th}$ element indicates to which
189+
group the $i^\text{th}$ observation belongs to.
199190

200-
Stan supports the case where $\theta$ is associated with an in-sample
201-
covariate $X$ and $\theta^*$ with an out-of-sample covariate $X^*$.
202-
Furthermore, the covariance function is written in such a way that
203-
$$
204-
K = f(..., X, X), \ \ K^{**} = f(..., X^*, X^*), \ \ K^* = f(..., X, X^*),
205-
$$
206-
as is typically the case in Gaussian process models.
191+
The signatures for the embedded Laplace approximation function with a Poisson
192+
likelihood are
193+
```
194+
real laplace_marginal_poisson_log_lpmf(array[] int y | array[] int y_index,
195+
vector theta0, function K_function, (...));
207196
197+
real laplace_marginal_tol_poisson_log_lpmf(array[] int y | array[] int y_index,
198+
vector theta0, function K_function, (...),
199+
real tol, int max_steps, int hessian_block_size,
200+
int solver, int max_steps_linesearch);
208201
202+
vector laplace_latent_poisson_log_rng(array[] int y, array[] int y_index,
203+
vector theta0, function K_function, (...));
209204
205+
vector laplace_latent_tol_poisson_log_rng(array[] int y, array[] int y_index,
206+
vector theta0, function K_function, (...),
207+
real tol, int max_steps, int hessian_block_size,
208+
int solver, int max_steps_linesearch);
209+
```
210210

211211

212-
The
213-
function `laplace_latent_rng` produces samples from the Laplace approximation
214-
and admits nearly the same arguments as `laplace_marginal`. A key difference
215-
is that
212+
A similar built-in likelihood lets users specify an offset $x_i \in \mathbb R^+$
213+
to the rate parameter of the Poisson. The likelihood is then,
214+
$$
215+
p(y \mid \theta, \phi) = \prod_i\text{Poisson} (y_i \mid \exp(\theta_{g(i)}) x_i).
216+
$$
217+
The signatures for this function are:
216218
```
217-
vector laplace_latent_rng(function ll_function, tupple (...), vector theta_0,
218-
function K_function, tupple (...));
219+
real laplace_marginal_poisson2_log_lpmf(array[] int y | array[] int y_index,
220+
vector x, vector theta0,
221+
function K_function, (...));
222+
223+
real laplace_marginal_tol_poisson2_log_lpmf(array[] int y | array[] int y_index,
224+
vector x, vector theta0,
225+
function K_function, (...),
226+
real tol, int max_steps, int hessian_block_size,
227+
int solver, int max_steps_linesearch);
228+
229+
vector laplace_latent_poisson2_log_rng(array[] int y, array[] int y_index,
230+
vector x, vector theta0,
231+
function K_function, (...));
232+
233+
vector laplace_latent_tol_poisson2_log_rng(array[] int y, array[] int y_index,
234+
vector x, vector theta0,
235+
function K_function, (...),
236+
real tol, int max_steps, int hessian_block_size,
237+
int solver, int max_steps_linesearch);
219238
```
220239

221240

241+
### Negative Binomial likelihood with log link
222242

243+
The negative Bionomial generalizes the Poisson likelihood function by
244+
introducing the dispersion parameter $\eta$. The likelihood is then
245+
$$
246+
p(y \mid \theta, \phi) = \prod_i\text{NegBinomial2} (y_i \mid \exp(\theta_{g(i)}), \eta).
247+
$$
248+
Here we use the alternative paramererization implemented in Stan, meaning that
249+
$$
250+
\mathbb E(y_i) = \exp (\theta_{g(i)}), \ \ \text{Var}(y_i) = \mathbb E(y_i) + \frac{(\mathbb E(y_i))^2}{\eta}.
251+
$$
252+
The arguments for the likelihood function are:
223253

254+
* `y`: the observed counts
255+
* `y_index`: an array whose $i^\text{th}$ element indicates to which
256+
group the $i^\text{th}$ observation belongs to.
257+
* `eta`: the overdispersion parameter.
224258

259+
The function signatures for the embedded Laplace approximation with a negative
260+
Binomial likelihood are
261+
```
262+
real laplace_marginal_neg_binomial_2_log_lpmf(array[] int y |
263+
array[] int y_index, real eta, vector theta0,
264+
function K_function, (...));
265+
266+
real laplace_marginal_tol_neg_binomial_2_log_lpmf(array[] int y |
267+
array[] int y_index, real eta, vector theta0,
268+
function K_function, (...),
269+
real tol, int max_steps, int hessian_block_size,
270+
int solver, int max_steps_linesearch);
271+
272+
vector laplace_latent_neg_binomial_2_log_rng(array[] int y,
273+
array[] int y_index, real eta, vector theta0,
274+
function K_function, (...));
275+
276+
vector laplace_latent_tol_neg_binomial_2_log_rng(array[] int y,
277+
array[] int y_index, real eta, vector theta0,
278+
function K_function, (...),
279+
real tol, int max_steps, int hessian_block_size,
280+
int solver, int max_steps_linesearch);
281+
```
225282

283+
### Bernoulli likelihood with logit link
226284

285+
For a binary outcome $y_i \in \{0, 1\}$, the likelihood is
286+
$$
287+
p(y \mid \theta, \phi) = \prod_i\text{Bernoulli} (y_i \mid \text{logit}^{-1}(\theta_{g(i)})).
288+
$$
289+
The arguments of the likelihood function are:
227290

291+
* `y`: the observed counts
292+
* `y_index`: an array whose $i^\text{th}$ element indicates to which
293+
group the $i^\text{th}$ observation belongs to.
228294

295+
The function signatures for the embedded Laplace approximation with a Bernoulli likelihood are
296+
```
297+
real laplace_marginal_bernoulli_logit_lpmf(array[] int y |
298+
array[] int y_index, real eta, vector theta0,
299+
function K_function, (...));
300+
301+
real laplace_marginal_tol_bernoulli_logit_lpmf(array[] int y |
302+
array[] int y_index, real eta, vector theta0,
303+
function K_function, (...),
304+
real tol, int max_steps, int hessian_block_size,
305+
int solver, int max_steps_linesearch);
306+
307+
vector laplace_latent_bernoulli_logit_rng(array[] int y,
308+
array[] int y_index, real eta, vector theta0,
309+
function K_function, (...));
310+
311+
vector laplace_latent_tol_bernoulli_logit_rng(array[] int y,
312+
array[] int y_index, real eta, vector theta0,
313+
function K_function, (...),
314+
real tol, int max_steps, int hessian_block_size,
315+
int solver, int max_steps_linesearch);
316+
```
229317

230-
231-
232-
233-
234-
318+
<!-- ## Draw approximate samples for out-of-sample latent variables. -->
319+
320+
<!-- In many applications, it is of interest to draw latent variables for -->
321+
<!-- in-sample and out-of-sample predictions. We respectively denote these latent -->
322+
<!-- variables $\theta$ and $\theta^*$. In a latent Gaussian model, -->
323+
<!-- $(\theta, \theta^*)$ jointly follow a prior multivariate normal distribution: -->
324+
<!-- $$ -->
325+
<!-- \theta, \theta^* \sim \text{MultiNormal}(0, {\bf K}(\phi)), -->
326+
<!-- $$ -->
327+
<!-- where $\bf K$ designates the joint covariance matrix over $\theta, \theta^*$. -->
328+
329+
<!-- We can break $\bf K$ into three components, -->
330+
<!-- $$ -->
331+
<!-- {\bf K} = \begin{bmatrix} -->
332+
<!-- K & \\ -->
333+
<!-- K^* & K^{**} -->
334+
<!-- \end{bmatrix}, -->
335+
<!-- $$ -->
336+
<!-- where $K$ is the prior covariance matrix for $\theta$, $K^{**}$ the prior -->
337+
<!-- covariance matrix for $\theta^*$, and $K^*$ the covariance matrix between -->
338+
<!-- $\theta$ and $\theta^*$. -->
339+
340+
<!-- Stan supports the case where $\theta$ is associated with an in-sample -->
341+
<!-- covariate $X$ and $\theta^*$ with an out-of-sample covariate $X^*$. -->
342+
<!-- Furthermore, the covariance function is written in such a way that -->
343+
<!-- $$ -->
344+
<!-- K = f(..., X, X), \ \ K^{**} = f(..., X^*, X^*), \ \ K^* = f(..., X, X^*), -->
345+
<!-- $$ -->
346+
<!-- as is typically the case in Gaussian process models. -->
347+
348+
349+
350+
351+
352+
<!-- The -->
353+
<!-- function `laplace_latent_rng` produces samples from the Laplace approximation -->
354+
<!-- and admits nearly the same arguments as `laplace_marginal`. A key difference -->
355+
<!-- is that -->
356+
<!-- ``` -->
357+
<!-- vector laplace_latent_rng(function ll_function, tupple (...), vector theta_0, -->
358+
<!-- function K_function, tupple (...)); -->
359+
<!-- ``` -->
235360

0 commit comments

Comments
 (0)