@@ -15,7 +15,7 @@ are used --- if they do not, the behavior is undefined.
1515The basis of Stan's execution is the evaluation of a log probability
1616function (specifically, a log probability density function) for a given
1717set 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
1919statements. Statements may be grouped
2020into sequences and into for-each loops. In addition, Stan allows
2121local 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
248248data are fixed before the log density is evaluated. The total log
249249probability is initialized to zero. Next, any log Jacobian
250250adjustments 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).
252253Distribution statements and log probability increment statements may add to the log
253254density in the model block. A log probability increment statement
254255directly increments the log density with the value of an expression as
@@ -361,14 +362,51 @@ including arrays of vectors and matrices. For container arguments,
361362their sum will be added to the total log density.
362363
363364
365+ ## Increment log density with a change of variables adjustment
366+
367+
368+ A variant of the ` target += ` statement described above is the
369+ ` jacobian += ` statement. This can be used in the transformed parameters block
370+ or in functions ending with ` _jacobian ` to mimic the log Jacobian
371+ adjustments accrued by built-in variable transforms.
372+
373+ Similarly to those implemented for the built-in transforms, these Jacobian adjustment
374+ may be skipped for maximum likelihood estimation via optimization.
375+
376+ For example, here is a program which recreates the existing
377+ [ ` <upper=x> ` transform] ( transforms.qmd#upper-bounded-scalar ) on real numbers:
378+
379+ ``` stan
380+ functions {
381+ real upper_bound_jacobian(real x, real ub) {
382+ jacobian += x;
383+ return ub - exp(x);
384+ }
385+ }
386+ data {
387+ real ub;
388+ }
389+ parameters {
390+ real b_raw;
391+ }
392+ transformed parameters {
393+ real b = upper_bound_jacobian(b_raw, ub);
394+ }
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+ }
400+ ```
401+
364402### Accessing the log density {-}
365403
366- To access accumulated log density up to the current execution point,
404+ To access the accumulated log density up to the current execution point,
367405the function ` target() ` may be used.
368406
369407## Sampling statements {#sampling-statements.section}
370408
371- The term "sampling statement" has been replaced with
409+ The term "sampling statement" has been replaced with
372410[ distribution statement] ( #distribution-statements.section ) .
373411
374412## Distribution statements {#distribution-statements.section}
@@ -432,7 +470,7 @@ terms in the model block. Equivalently, each $\sim$ statement
432470corresponds to a multiplicative factor in the unnormalized posterior
433471density.
434472
435- Distribution statements (` ~ ` ) accept only built-in or user-defined
473+ Distribution statements (` ~ ` ) accept only built-in or user-defined
436474distributions on the
437475right side. The left side of a distribution statement may be data,
438476parameter, or a complex expression, but the evaluated type needs to
@@ -452,8 +490,8 @@ target += normal_lpdf(sigma | 0, 1);
452490```
453491
454492Stan models can mix distribution statements and log probability
455- increment statements. Although statistical models
456- are usually defined with distributions in the literature,
493+ increment statements. Although statistical models
494+ are usually defined with distributions in the literature,
457495there are several scenarios in which we may want to code the log
458496likelihood or parts of it directly, for example, due to computational
459497efficiency (e.g. censored data model) or coding language limitations
@@ -485,7 +523,7 @@ target += dist_lpmf(y | theta1, ..., thetaN);
485523
486524This will be well formed if and only if `dist_lpdf(y | theta1,
487525 ..., thetaN)` or ` dist_lpmf(y | theta1, ..., thetaN)` is a
488- well-formed expression of type ` real ` . User defined distributions
526+ well-formed expression of type ` real ` . User defined distributions
489527can be defined in functions block by using function names ending with ` _lpdf ` .
490528
491529
@@ -881,7 +919,7 @@ The equivalent code for a vectorized truncation depends on which of the
881919variables are non-scalars (arrays, vectors, etc.):
882920
8839211 . If the variate ` y ` is the only non-scalar, the result is the same as
884- described in the above sections, but the ` lcdf ` /` lccdf ` calculation is
922+ described in the above sections, but the ` lcdf ` /` lccdf ` calculation is
885923 multiplied by ` size(y) ` .
886924
8879252 . If the other arguments to the distribution are non-scalars, then the
0 commit comments