Skip to content

Commit 1dcaf78

Browse files
committed
Doc clarifications
1 parent c89010b commit 1dcaf78

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

src/reference-manual/statements.qmd

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ are used --- if they do not, the behavior is undefined.
1515
The basis of Stan's execution is the evaluation of a log probability
1616
function (specifically, a log probability density function) for a given
1717
set of (real-valued) parameters. Log probability functions can be
18-
constructed by using distribution statements and log probability increment
18+
constructed by using distribution statements and log probability increment
1919
statements. Statements may be grouped
2020
into sequences and into for-each loops. In addition, Stan allows
2121
local variables to be declared in blocks and also allows an empty
@@ -248,7 +248,8 @@ of the posterior up to an additive constant. Data and transformed
248248
data are fixed before the log density is evaluated. The total log
249249
probability is initialized to zero. Next, any log Jacobian
250250
adjustments accrued by the variable constraints are added to the log
251-
density (the Jacobian adjustment may be skipped for optimization).
251+
density (the Jacobian adjustment may be skipped for maximum likelihood estimation
252+
via optimization).
252253
Distribution statements and log probability increment statements may add to the log
253254
density in the model block. A log probability increment statement
254255
directly increments the log density with the value of an expression as
@@ -370,9 +371,9 @@ or in functions ending with `_jacobian` to mimic the log Jacobian
370371
adjustments accrued by built-in variable transforms.
371372

372373
Similarly to those implemented for the built-in transforms, these Jacobian adjustment
373-
may be skipped for optimization.
374+
may be skipped for maximum likelihood estimation via optimization.
374375

375-
For example, here is a program which re-creates the existing
376+
For example, here is a program which recreates the existing
376377
[`<upper=x>` transform](transforms.qmd#upper-bounded-scalar) on real numbers:
377378

378379
```stan
@@ -391,16 +392,21 @@ parameters {
391392
transformed parameters {
392393
real b = upper_bound_jacobian(b_raw, ub);
393394
}
395+
model {
396+
// use b as if it was declared `real<upper=ub> b;` in parameters
397+
// e.g.
398+
// b ~ lognormal(0, 1);
399+
}
394400
```
395401

396402
### Accessing the log density {-}
397403

398-
To access accumulated log density up to the current execution point,
404+
To access the accumulated log density up to the current execution point,
399405
the function `target()` may be used.
400406

401407
## Sampling statements {#sampling-statements.section}
402408

403-
The term "sampling statement" has been replaced with
409+
The term "sampling statement" has been replaced with
404410
[distribution statement](#distribution-statements.section).
405411

406412
## Distribution statements {#distribution-statements.section}
@@ -464,7 +470,7 @@ terms in the model block. Equivalently, each $\sim$ statement
464470
corresponds to a multiplicative factor in the unnormalized posterior
465471
density.
466472

467-
Distribution statements (`~`) accept only built-in or user-defined
473+
Distribution statements (`~`) accept only built-in or user-defined
468474
distributions on the
469475
right side. The left side of a distribution statement may be data,
470476
parameter, or a complex expression, but the evaluated type needs to
@@ -484,8 +490,8 @@ target += normal_lpdf(sigma | 0, 1);
484490
```
485491

486492
Stan models can mix distribution statements and log probability
487-
increment statements. Although statistical models
488-
are usually defined with distributions in the literature,
493+
increment statements. Although statistical models
494+
are usually defined with distributions in the literature,
489495
there are several scenarios in which we may want to code the log
490496
likelihood or parts of it directly, for example, due to computational
491497
efficiency (e.g. censored data model) or coding language limitations
@@ -517,7 +523,7 @@ target += dist_lpmf(y | theta1, ..., thetaN);
517523

518524
This will be well formed if and only if `dist_lpdf(y | theta1,
519525
..., thetaN)` or `dist_lpmf(y | theta1, ..., thetaN)` is a
520-
well-formed expression of type `real`. User defined distributions
526+
well-formed expression of type `real`. User defined distributions
521527
can be defined in functions block by using function names ending with `_lpdf`.
522528

523529

@@ -913,7 +919,7 @@ The equivalent code for a vectorized truncation depends on which of the
913919
variables are non-scalars (arrays, vectors, etc.):
914920

915921
1. If the variate `y` is the only non-scalar, the result is the same as
916-
described in the above sections, but the `lcdf`/`lccdf` calculation is
922+
described in the above sections, but the `lcdf`/`lccdf` calculation is
917923
multiplied by `size(y)`.
918924

919925
2. If the other arguments to the distribution are non-scalars, then the

src/reference-manual/user-functions.qmd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ arguments to produce an expression, which has a value when executed.
9595
### Functions as statements {-}
9696

9797
Functions with void return types may be applied to arguments and used
98-
as [statements.qmd](statements).
98+
as [statements.qmd](statements).
9999
These act like distribution statements or print
100100
statements. Such uses are only appropriate for functions that act
101101
through side effects, such as incrementing the log probability
@@ -161,7 +161,11 @@ used in place of parameterized distributions on the right side of
161161
Functions of certain types are restricted on scope of usage.
162162
Functions whose names end in `_lp` assume access to the log
163163
probability accumulator and are only available in the transformed
164-
parameter and model blocks.
164+
parameters and model blocks.
165+
166+
Functions whose name end in `_jacobian` assume access to the log
167+
probability accumulator may only be used within the transformed parameters
168+
block.
165169

166170
Functions whose names end in `_rng`
167171
assume access to the random number generator and may only be used
@@ -293,8 +297,8 @@ a function elsewhere results in a compile-time error.
293297

294298
### Log probability access in functions {-}
295299

296-
Functions that include
297-
[statements.qmd#distribution-statements.section](distribution statements) or
300+
Functions that include
301+
[statements.qmd#distribution-statements.section](distribution statements) or
298302
[statements.qmd#increment-log-prob.section](log probability increment statements)
299303
must have a name that ends in `_lp`.
300304
Attempts to use distribution statements or increment log probability

src/stan-users-guide/user-functions.qmd

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,20 @@ Functions whose names end in `_jacobian` can use the
293293
`jacobian +=` statement. This can be used to implement a custom
294294
change of variables for arbitrary parameters.
295295

296-
For example, here is a program which re-creates the built-in
296+
For example, this function recreates the built-in
297297
`<upper=x>` transform on real numbers:
298+
```stan
299+
real upper_bound_jacobian(real x, real ub) {
300+
jacobian += x;
301+
return ub - exp(x);
302+
}
303+
```
304+
305+
It can be used as a replacement for `real<lower=ub>` as follows:
298306

299307
```stan
300308
functions {
301-
real upper_bound_jacobian(real x, real ub) {
302-
jacobian += x;
303-
return ub - exp(x);
304-
}
309+
// upper_bound_jacobian as above
305310
}
306311
data {
307312
real ub;
@@ -312,6 +317,10 @@ parameters {
312317
transformed parameters {
313318
real b = upper_bound_jacobian(b_raw, ub);
314319
}
320+
model {
321+
b ~ lognormal(0, 1);
322+
// ...
323+
}
315324
```
316325

317326
## Functions acting as random number generators

0 commit comments

Comments
 (0)