Skip to content

Commit 4e8ca56

Browse files
Merge branch 'master' into avehtari-patch-1
2 parents 6a2e556 + fe5f556 commit 4e8ca56

File tree

9 files changed

+247
-116
lines changed

9 files changed

+247
-116
lines changed

src/bibtex/all.bib

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,4 +1867,30 @@ @article{Magnusson+etal:2024:posteriordb
18671867
author={Magnusson, M{\aa}ns and Torgander, Jakob and B{\"u}rkner, Paul-Christian and Zhang, Lu and Carpenter, Bob and Vehtari, Aki},
18681868
journal={arXiv preprint arXiv:2407.04967},
18691869
year={2024}
1870+
1871+
@article{egozcue+etal:2003,
1872+
title={Isometric logratio transformations for compositional data analysis},
1873+
author={Egozcue, Juan Jos{\'e} and Pawlowsky-Glahn, Vera and Mateu-Figueras, Gl{\`o}ria and Barcelo-Vidal, Carles},
1874+
journal={Mathematical Geology},
1875+
volume={35},
1876+
number={3},
1877+
pages={279--300},
1878+
year={2003}
1879+
}
1880+
1881+
@book{filzmoser+etal:2018,
1882+
title={Geometrical properties of compositional data},
1883+
author={Filzmoser, Peter and Hron, Karel and Templ, Matthias},
1884+
booktitle={Applied Compositional Data Analysis: With Worked Examples in R},
1885+
pages={35--68},
1886+
year={2018},
1887+
publisher={Springer}
1888+
}
1889+
1890+
@misc{seyboldt:2024,
1891+
author="Seyboldt, Adrian",
1892+
title="Add ZeroSumNormal distribution",
1893+
note="pyro-ppl GitHub repository issue \#1751",
1894+
year = "2024",
1895+
url ="https://github.com/pyro-ppl/numpyro/pull/1751#issuecomment-1980569811"
18701896
}

src/functions-reference/real-valued_basic_functions.qmd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,10 @@ calculations, but the result is likely to be reduced acceptance
783783
probabilities and less efficient sampling.
784784

785785
The rounding functions cannot be used as indices to arrays because
786-
they return real values. Stan may introduce integer-valued versions
787-
of these in the future, but as of now, there is no good workaround.
786+
they return real values. For operations over `data` or in the
787+
`generated quantities` block, the
788+
[`to_int()` function](integer-valued_basic_functions.qmd#casting-functions)
789+
can be used.
788790

789791
<!-- R; floor; (T x); -->
790792
\index{{\tt \bfseries floor }!{\tt (T x): R}|hyperpage}
@@ -1636,7 +1638,8 @@ proportion theta, defined by \begin{eqnarray*}
16361638
Calculates the log mixture density given `thetas`,
16371639
mixing proportions which should be between 0 and 1 and sum to 1,
16381640
and `lps`, log densities.
1639-
These two containers must have the same length.
1641+
The `lps` variable must be either a 1-d container of the same
1642+
length as `thetas`, or an array of such.
16401643

16411644
\begin{eqnarray*}
16421645
\mathrm{log\_mix}(\theta, \lambda)

src/reference-manual/expressions.qmd

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ any of the following.
149149

150150
```
151151
int, real, complex, vector, simplex, unit_vector,
152-
ordered, positive_ordered, row_vector, matrix,
153-
cholesky_factor_corr, cholesky_factor_cov,
154-
corr_matrix, cov_matrix, array
152+
sum_to_zero_vector, ordered, positive_ordered,
153+
row_vector, matrix, cholesky_factor_corr,
154+
cholesky_factor_cov, corr_matrix, cov_matrix, array
155155
```
156156

157157
The following built in functions are also reserved and
@@ -810,9 +810,9 @@ In addition to single integer indexes, as described in
810810
[the language indexing section](#language-indexing.section), Stan supports multiple indexing.
811811
Multiple indexes can be integer arrays of indexes, lower
812812
bounds, upper bounds, lower and upper bounds, or simply shorthand for
813-
all of the indexes. If the upper bound is smaller than the lower bound,
814-
the range is empty (unlike, e.g., in R). The upper bound and lower bound can be
815-
expressions that evaluate to integer. A complete list of index types is
813+
all of the indexes. If the upper bound is smaller than the lower bound,
814+
the range is empty (unlike, e.g., in R). The upper bound and lower bound can be
815+
expressions that evaluate to integer. A complete list of index types is
816816
given in the following table.
817817

818818
##### Indexing Options Table {- #index-types-table}
@@ -1078,6 +1078,7 @@ the following table shows the mapping from types to their primitive types.
10781078
| `vector` | `vector` |
10791079
| `simplex` | `vector` |
10801080
| `unit_vector` | `vector` |
1081+
| `sum_to_zero_vector` | `vector` |
10811082
| `ordered` | `vector` |
10821083
| `positive_ordered` | `vector` |
10831084
| `row_vector` | `row_vector` |
@@ -1378,7 +1379,7 @@ model {
13781379
}
13791380
```
13801381

1381-
Algebraically,
1382+
Algebraically,
13821383
[the distribution statement](statements.qmd#distribution-statements.section)
13831384
in the model could be reduced to
13841385

src/reference-manual/grammar.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
<identifier> ::= IDENTIFIER
2626
| TRUNCATE
27+
| JACOBIAN
2728

2829
<decl_identifier> ::= <identifier>
2930
| <reserved_word>
@@ -57,10 +58,13 @@
5758
| POSITIVEORDERED
5859
| SIMPLEX
5960
| UNITVECTOR
61+
| SUMTOZERO
6062
| CHOLESKYFACTORCORR
6163
| CHOLESKYFACTORCOV
6264
| CORRMATRIX
6365
| COVMATRIX
66+
| STOCHASTICCOLUMNMATRIX
67+
| STOCHASTICROWMATRIX
6468
| PRINT
6569
| REJECT
6670
| FATAL_ERROR
@@ -165,11 +169,16 @@
165169
| POSITIVEORDERED LBRACK <expression> RBRACK
166170
| SIMPLEX LBRACK <expression> RBRACK
167171
| UNITVECTOR LBRACK <expression> RBRACK
172+
| SUMTOZERO LBRACK <expression> RBRACK
168173
| CHOLESKYFACTORCORR LBRACK <expression> RBRACK
169174
| CHOLESKYFACTORCOV LBRACK <expression> [COMMA <expression>]
170175
RBRACK
171176
| CORRMATRIX LBRACK <expression> RBRACK
172177
| COVMATRIX LBRACK <expression> RBRACK
178+
| STOCHASTICCOLUMNMATRIX LBRACK <expression> COMMA
179+
<expression> RBRACK
180+
| STOCHASTICROWMATRIX LBRACK <expression> COMMA <expression>
181+
RBRACK
173182

174183
<type_constraint> ::= [LABRACK <range> RABRACK]
175184
| LABRACK <offset_mult> RABRACK

src/reference-manual/syntax.qmd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ The raw output is available [here](https://raw.githubusercontent.com/stan-dev/do
113113
```
114114
<identifier> ::= IDENTIFIER
115115
| TRUNCATE
116+
| JACOBIAN
116117
117118
<decl_identifier> ::= <identifier>
118119
@@ -175,11 +176,16 @@ The raw output is available [here](https://raw.githubusercontent.com/stan-dev/do
175176
| POSITIVEORDERED LBRACK <expression> RBRACK
176177
| SIMPLEX LBRACK <expression> RBRACK
177178
| UNITVECTOR LBRACK <expression> RBRACK
179+
| SUMTOZERO LBRACK <expression> RBRACK
178180
| CHOLESKYFACTORCORR LBRACK <expression> RBRACK
179181
| CHOLESKYFACTORCOV LBRACK <expression> [COMMA <expression>]
180182
RBRACK
181183
| CORRMATRIX LBRACK <expression> RBRACK
182184
| COVMATRIX LBRACK <expression> RBRACK
185+
| STOCHASTICCOLUMNMATRIX LBRACK <expression> COMMA
186+
<expression> RBRACK
187+
| STOCHASTICROWMATRIX LBRACK <expression> COMMA <expression>
188+
RBRACK
183189
184190
<type_constraint> ::= [LABRACK <range> RABRACK]
185191
| LABRACK <offset_mult> RABRACK

src/reference-manual/transforms.qmd

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ constrained to be ordered, positive ordered, or simplexes. Matrices
1515
may be constrained to be correlation matrices or covariance matrices.
1616
This chapter provides a definition of the transforms used for each
1717
type 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

2121
Stan 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

467550
Variables constrained to the unit simplex show up in multivariate

src/reference-manual/types.qmd

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ vector<lower=-1, upper=1>[3] rho;
117117
```
118118

119119
There are also special data types for structured vectors and
120-
matrices. There are four constrained vector data types, `simplex`
120+
matrices. There are five constrained vector data types, `simplex`
121121
for unit simplexes, `unit_vector` for unit-length vectors,
122-
`ordered` for ordered vectors of scalars and
122+
`sum_to_zero_vector` for vectors that sum to zero,
123+
`ordered` for ordered vectors of scalars, and
123124
`positive_ordered` for vectors of positive ordered
124125
scalars. There are specialized matrix data types `corr_matrix`
125126
and `cov_matrix` for correlation matrices (symmetric, positive
@@ -692,6 +693,26 @@ unit vectors, this is only done up to a statically specified accuracy
692693
threshold $\epsilon$ to account for errors arising from floating-point
693694
imprecision.
694695

696+
### Vectors that sum to zero {-}
697+
698+
A zero-sum vector is constrained such that the
699+
sum of its elements is always $0$. These are sometimes useful
700+
for resolving identifiability issues in regression models.
701+
While the underlying vector has only $N - 1$ degrees of freedom,
702+
zero sum vectors are declared with their full dimensionality.
703+
For instance, `beta` is declared to be a zero-sum $5$-vector (4 DoF) by
704+
705+
```stan
706+
sum_to_zero_vector[5] beta;
707+
```
708+
709+
Zero sum vectors are implemented as vectors and may be assigned to other
710+
vectors and vice-versa. Zero sum vector variables, like other constrained
711+
variables, are validated to ensure that they are indeed unit length; for
712+
zero sum vectors, this is only done up to a statically specified accuracy
713+
threshold $\epsilon$ to account for errors arising from floating-point
714+
imprecision.
715+
695716
### Ordered vectors {-}
696717

697718
An ordered vector type in Stan represents a vector whose entries are
@@ -1636,6 +1657,8 @@ dimensions `matrix[K, K]` types.
16361657
+---------------------+-------------------------+----------------------------------------------+
16371658
| | | `unit_vector[N]` |
16381659
+---------------------+-------------------------+----------------------------------------------+
1660+
| | | `sum_to_zero_vector[N]` |
1661+
+---------------------+-------------------------+----------------------------------------------+
16391662
| `row_vector` | `row_vector[N]` | `row_vector[N]` |
16401663
+---------------------+-------------------------+----------------------------------------------+
16411664
| | | `row_vector[N]<lower=L>` |
@@ -1700,6 +1723,8 @@ dimensions `matrix[K, K]` types.
17001723
+---------------------+-------------------------+----------------------------------------------+
17011724
| | | `array[M] unit_vector[N]` |
17021725
+---------------------+-------------------------+----------------------------------------------+
1726+
| | | `array[M] sum_to_zero_vector[N]` |
1727+
+---------------------+-------------------------+----------------------------------------------+
17031728

17041729
<a name="id:constrained-types.figure"></a>
17051730

0 commit comments

Comments
 (0)