@@ -15,7 +15,7 @@ constrained to be ordered, positive ordered, or simplexes. Matrices
1515may be constrained to be correlation matrices or covariance matrices.
1616This chapter provides a definition of the transforms used for each
1717type of variable. For examples of how to declare and define these
18- variables in a Stan program, see section
18+ variables in a Stan program, see section
1919[ Variable declaration] ( types.qmd#variable-declaration.section ) .
2020
2121Stan converts models to C++ classes which define probability
@@ -39,7 +39,7 @@ the exact arithmetic:
3939 rounded to the boundary. This may cause unexpected warnings or
4040 errors, if in other parts of the code the boundary value is
4141 invalid. For example, we may observe floating-point value 0 for
42- a variance parameter that has been declared to be larger than 0.
42+ a variance parameter that has been declared to be larger than 0.
4343 See more about [Floating point Arithmetic in Stan user's guide](../stan-users-guide/floating-point.qmd)).
4444 - CmdStan stores the output to CSV files with 6 significant digits
4545 accuracy by default, but the constraints are checked with 8
@@ -462,6 +462,89 @@ p_Y(y)
462462$$
463463
464464
465+
466+ ## Zero sum vector
467+
468+ Vectors that are constrained to sum to zero are useful for, among
469+ other things, additive varying effects, such as varying slopes or
470+ intercepts in a regression model (e.g., for income deciles).
471+
472+ A zero sum $K$-vector $x \in \mathbb{R}^K$ satisfies the constraint
473+ $$
474+ \sum_{k=1}^K x_k = 0.
475+ $$
476+
477+ For the transform, Stan uses the first part of an isometric log ratio
478+ transform; see [ @egozcue +etal:2003] for the basic definitions and
479+ Chapter 3 of [ @filzmoser +etal:2018] for the pivot coordinate version
480+ used here. Stan uses the isometric log ratio transform because it
481+ induces a geometry with zero correlation among the dimensions, making
482+ it easier for HMC to explore than simpler alternatives such as setting
483+ the final element to the negative sum of the first elements; see, e.g.,
484+ [ @seyboldt :2024] .
485+
486+
487+
488+
489+ ### Zero sum transform {-}
490+
491+ The (unconstraining) transform is defined iteratively. Given an $x \in
492+ \mathbb{R}^{N + 1}$ that sums to zero (i.e., $\sum_ {n=1}^{N+1} x_n =
493+ 0$), the transform proceeds as follows to produce an unconstrained $y
494+ \in \mathbb{R}^N$.
495+
496+ The transform is initialized by setting
497+ $$
498+ S_N = 0
499+ $$
500+ and
501+ $$
502+ y_N = -x_{N + 1} \cdot \frac{\sqrt{N \cdot (N + 1)}{N}}.
503+ $$
504+ The for each $n$ from $N - 1$ down to $1$, let
505+ $$
506+ w_{n + 1} = \frac{y_{n + 1}}{\sqrt{(n + 1) \cdot (n + 2)}},
507+ $$
508+ $$
509+ S_n = S_{n + 1} + w_{n + 1},
510+ $$
511+ and
512+ $$
513+ y_n = (S_n - x_{n + 1}) \cdot \frac{\sqrt{n \cdot (n + 1)}}{n}.
514+ $$
515+
516+ ### Zero sum inverse transform {-}
517+
518+ The inverse (constraining) transform follows the isometric logratio tranform.
519+ It maps an unconstrained vector $y \in \mathbb{R}^N$ to a zero-sum vector $x \in
520+ \mathbb{R}^{N + 1}$ such that
521+ $$
522+ \sum_{n=1}^{N + 1} x_n = 0.
523+ $$
524+ The values are defined inductively, starting with
525+ $$
526+ x_1 = \sum_{n=1}^N \frac{y_n}{\sqrt{n \cdot (n + 1)}}
527+ $$
528+ and then setting
529+ $$
530+ x_{n + 1} = \sum_{i = n + 1}^N \frac{\sqrt{y_i}}{\sqrt{i \cdot (i + 1)}}
531+ - n \cdot \frac{y_n}{\sqrt{n \cdot (n + 1)}}.
532+ $$
533+ for $n \in 1{:}N$.
534+
535+ The definition is such that
536+ $$
537+ \sum_{n = 1}^{N + 1} x_n = 0
538+ $$
539+ by construction, because each of the terms added to $x_ {n}$ is then
540+ subtracted from $x_ {n + 1}$ the number of times it shows up in earlier terms.
541+
542+ ### Absolute Jacobian determinant of the zero sum inverse transform {-}
543+
544+ The inverse transform is a linear operation, leading to a constant Jacobian
545+ determinant which is therefore not included.
546+
547+
465548## Unit simplex {#simplex-transform.section}
466549
467550Variables constrained to the unit simplex show up in multivariate
0 commit comments