Skip to content

Commit 051a291

Browse files
committed
Merge branch 'master' of https://github.com/stan-dev/docs
2 parents a5f598c + 82c0051 commit 051a291

18 files changed

+406
-160
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/functions-reference/functions_index.qmd linguist-generated=true

.github/ISSUE_TEMPLATE.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,3 @@ Describe the issue as clearly as possible.
88

99
#### Additional Information:
1010
Provide any additional information here.
11-
12-
#### Current Version:
13-
v2.18.0

src/bibtex/all.bib

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,3 +1845,52 @@ @article{Timonen+etal:2023:ODE-PSIS
18451845
pages = {e614}
18461846
}
18471847

1848+
@article{Vehtari+etal:2024:PSIS,
1849+
author = {Aki Vehtari and Daniel Simpson and Andrew Gelman and Yuling Yao and Jonah Gabry},
1850+
title = {Pareto smoothed importance sampling},
1851+
journal = {Journal of Machine Learning Research},
1852+
year = {2024},
1853+
volume = {25},
1854+
number = {72},
1855+
pages = {1--58}
1856+
}
1857+
1858+
@article{Gelman:etal:2020:workflow,
1859+
title={Bayesian workflow},
1860+
author={Gelman, Andrew and Vehtari, Aki and Simpson, Daniel and Margossian, Charles C and Carpenter, Bob and Yao, Yuling and Kennedy, Lauren and Gabry, Jonah and B{\"u}rkner, Paul-Christian and Modr{\'a}k, Martin},
1861+
journal={arXiv preprint arXiv:2011.01808},
1862+
year={2020}
1863+
}
1864+
1865+
@article{Magnusson+etal:2024:posteriordb,
1866+
title={posteriordb: Testing, benchmarking and developing {Bayesian} inference algorithms},
1867+
author={Magnusson, M{\aa}ns and Torgander, Jakob and B{\"u}rkner, Paul-Christian and Zhang, Lu and Carpenter, Bob and Vehtari, Aki},
1868+
journal={arXiv preprint arXiv:2407.04967},
1869+
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"
1896+
}

src/cmdstan-guide/installation.qmd

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ Python bindings to CmdStan seamlessly. Additionally, it provides the
2727
command `cmdstan_model` to activate the CmdStan makefile from anywhere.
2828

2929
_Note_: This requires that conda has been installed already on your machine.
30-
You can either install [miniconda](https://docs.conda.io/en/latest/miniconda.html), a free, minimal installer for conda
31-
or you can get the full [Anaconda](https://docs.continuum.io/anaconda/) system
32-
which provides graphical installer wizards for [MacOS](https://www.anaconda.com/products/distribution#macos)
33-
and [Windows](https://www.anaconda.com/products/distribution#windows) users.
30+
We recommend using the [miniforge](https://github.com/conda-forge/miniforge)
31+
distribution.
3432

3533
We recommend installing CmdStan in a new conda environment:
3634

@@ -228,7 +226,7 @@ On Linux and macOS:
228226
229227
# default output written to file `output.csv`,
230228
# default num_samples is 1000, output file should have approx. 1050 lines
231-
> ls -l output.csv
229+
> wc -l output.csv
232230
233231
# run the `bin/stansummary utility to summarize parameter estimates
234232
> bin/stansummary output.csv
@@ -353,36 +351,39 @@ or cluster administrator to install these tools for you.
353351

354352
#### MacOS
355353

356-
To install a C++ development
357-
environment on a Mac, use Apple's Xcode development environment
358-
https://developer.apple.com/xcode/.
359-
360-
From the [Xcode home page](https://developer.apple.com/xcode/)
361-
`View in Mac App Store`.
362-
363-
- From the App Store, click `Install`, enter an Apple ID, and wait
364-
for Xcode to finish installing.
365-
- Open the Xcode application, click top-level menu `Preferences`,
366-
click top-row button `Downloads`, click button for
367-
`Components`, click on the `Install` button to the right of
368-
the `Command Line Tools` entry, then wait for it to finish
369-
installing.
370-
- Click the top-level menu item `Xcode`, then click item `Quit
371-
Xcode` to quit.
372-
373-
To test, open the Terminal application and enter:
354+
To check if you already already have an appropriate toolchain
355+
installed, open the Terminal application and enter:
374356
```
375357
clang++ --version
376358
make --version
377359
```
378360

379-
If you have installed XCode, but don't have `make`, you can install the
380-
XCode command-line tools via command:
361+
If either of these commands prints the message
362+
`command not found`, you will need to install Xcode's
363+
command line tools.
364+
365+
Open the Terminal application and enter:
366+
381367
```
382368
xcode-select --install
383369
```
384370

371+
Select "Install" in the window that opens.
372+
373+
After the installation completes, you can double check that
374+
installation was successful by reopening the Terminal and
375+
running:
376+
```
377+
clang++ --version
378+
make --version
379+
```
380+
381+
You can read more about Xcode on its site:
382+
[https://developer.apple.com/xcode/](https://developer.apple.com/xcode/)
385383

384+
We don't recommend trying to use the GNU C++ compiler, available via Homebrew,
385+
based on the number of reports of installation difficulties from Mac users on GitHub
386+
as well as the Stan forums.
386387

387388
#### Windows {#windows}
388389

src/functions-reference/functions_index.qmd

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/functions-reference/real-valued_basic_functions.qmd

Lines changed: 29 additions & 7 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}
@@ -1601,8 +1603,12 @@ The logarithm of one minus the natural exponentiation of x
16011603
Return the natural logarithm of the difference of the natural
16021604
exponentiation of x and the natural exponentiation of y. \begin{equation*}
16031605
\mathrm{log\_diff\_exp}(x,y) = \begin{cases} \log(\exp(x)-\exp(y)) &
1604-
\text{if } x > y \\[6pt] \textrm{NaN} & \text{otherwise} \end{cases}
1606+
\text{if } +\infty > x \ge y \\[6pt]
1607+
\textrm{NaN} & \text{otherwise} \end{cases}
16051608
\end{equation*}
1609+
1610+
When x is equal to y, `log_diff_exp(x, y)` returns $-\infty$, consistent with `log(0)` returning $-\infty$. This includes the case in which x and y are both equal to $-\infty$, which corresponds to `log(0 - 0)` because `exp(negative_infinity())` returns 0.
1611+
16061612
{{< since 2.0 >}}
16071613

16081614
<!-- R; log_diff_exp; (T1 x, T2 y); -->
@@ -1624,11 +1630,27 @@ proportion theta, defined by \begin{eqnarray*}
16241630
\lambda_1, \ \log(1 - \theta) + \lambda_2\right). \end{eqnarray*}
16251631
{{< since 2.6 >}}
16261632

1627-
<!-- R; log_mix; (T1 theta, T2 lp1, T3 lp2); -->
1628-
\index{{\tt \bfseries log\_mix }!{\tt (T1 theta, T2 lp1, T3 lp2): real}|hyperpage}
1633+
<!-- R; log_mix; (T1 thetas, T2 lps); -->
1634+
\index{{\tt \bfseries log\_mix }!{\tt (T1 thetas, T2 lps): real}|hyperpage}
1635+
1636+
`R` **`log_mix`**`(T1 thetas, T2 lps)`<br>\newline
1637+
1638+
Calculates the log mixture density given `thetas`,
1639+
mixing proportions which should be between 0 and 1 and sum to 1,
1640+
and `lps`, log densities.
1641+
The `lps` variable must be either a 1-d container of the same
1642+
length as `thetas`, or an array of such.
1643+
1644+
\begin{eqnarray*}
1645+
\mathrm{log\_mix}(\theta, \lambda)
1646+
& = & \log \!\left( \sum_{n=1}^N \theta_n * \exp(\lambda_n) \right) \\[3pt]
1647+
& = & \mathrm{log\_sum\_exp}\!\left(\log(\theta) + \lambda\right).
1648+
\end{eqnarray*}
1649+
1650+
This is a generalization of the above signature of three arguments to
1651+
more than two densities.
1652+
For example, `log_mix(lambda, lp1, lp2) == log_mix({lambda, 1 - lambda}, {lp1, lp2})`.
16291653

1630-
`R` **`log_mix`**`(T1 theta, T2 lp1, T3 lp2)`<br>\newline
1631-
Vectorized implementation of the `log_mix` function
16321654
{{< since 2.26 >}}
16331655

16341656
<!-- R; log_sum_exp; (T1 x, T2 y); -->

src/functions-reference/unbounded_discrete_distributions.qmd

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,3 +586,36 @@ The log Poisson probability mass of `y` given the log-rate `alpha + x * beta`.
586586
The log Poisson probability mass of `y` given the log-rate `alpha + x * beta`
587587
dropping constant additive terms.
588588
{{< since 2.25 >}}
589+
590+
## Beta negative binomial distribution {#beta-neg-binomial}
591+
592+
### Probability mass function
593+
594+
If $r \in \mathbb{R}^+$, $\alpha \in \mathbb{R}^+$, and $\beta \in \mathbb{R}^+$, then for $n \in \mathbb{N}$, \begin{equation*}
595+
\text{BetaNegBinomial}(n|r,\alpha,\beta) = \frac {\Gamma (n+r )}{n!\;\Gamma (r )}
596+
\frac {\mathrm {B} (\beta+n,\alpha +r )}{\mathrm {B} (\beta,\alpha )}. \end{equation*}
597+
598+
### Distribution statement
599+
600+
`n ~ ` **`beta_neg_binomial`**`(r,alpha,beta)`
601+
602+
Increment target log probability density with `beta_neg_binomial_lupmf(n | r, alpha, beta)`.
603+
{{< since 2.36 >}}
604+
<!-- real; beta_neg_binomial ~; -->
605+
\index{{\tt \bfseries beta\_neg\_binomial }!sampling statement|hyperpage}
606+
607+
### Stan functions
608+
609+
<!-- real; beta_neg_binomial_lpmf; (ints n | reals r, reals alpha, reals beta); -->
610+
\index{{\tt \bfseries beta\_neg\_binomial\_lpmf }!{\tt (ints n \textbar\ reals r, reals alpha, reals beta): real}|hyperpage}
611+
612+
`real` **`beta_neg_binomial_lpmf`**`(ints n | reals r, reals alpha, reals beta)`<br>\newline
613+
The log beta negative binomial probability mass of `n` given parameters `r`, `alpha` and `beta`.
614+
{{< since 2.36 >}}
615+
616+
<!-- real; beta_neg_binomial_lupmf; (ints n | reals r, reals alpha, reals beta); -->
617+
\index{{\tt \bfseries beta\_neg\_binomial\_lupmf }!{\tt (ints n \textbar\ reals r, reals alpha, reals beta): real}|hyperpage}
618+
619+
`real` **`beta_neg_binomial_lupmf`**`(ints n | reals r, reals alpha, reals beta)`<br>\newline
620+
The log beta negative binomial probability mass of `n` given parameters `r`, `alpha` and `beta` dropping constant additive terms.
621+
{{< since 2.36 >}}

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/pathfinder.qmd

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pagetitle: Pathfinder
44

55
# Pathfinder
66

7-
Stan supports the Pathfinder algorithm @zhang_pathfinder:2022.
7+
Stan supports the Pathfinder algorithm [@zhang_pathfinder:2022].
88
Pathfinder is a variational method for approximately
99
sampling from differentiable log densities. Starting from a random
1010
initialization, Pathfinder locates normal approximations to the target
@@ -22,6 +22,31 @@ the problem of L-BFGS getting stuck at local optima or in saddle points on plate
2222
Compared to ADVI and short dynamic HMC runs, Pathfinder
2323
requires one to two orders of magnitude fewer log density and gradient
2424
evaluations, with greater reductions for more challenging posteriors.
25-
While the evaluations in @zhang_pathfinder:2022 found that
26-
single-path and multi-path Pathfinder outperform ADVI for most of the models in the PosteriorDB evaluation set,
25+
While the evaluations by @zhang_pathfinder:2022 found that
26+
single-path and multi-path Pathfinder outperform ADVI for most of the models in the PosteriorDB [@Magnusson+etal:2024:posteriordb] evaluation set,
2727
we recognize the need for further experiments on a wider range of models.
28+
29+
## Diagnosing Pathfinder
30+
31+
Pathfinder diagnoses the accuracy of the approximation by computing the density ratio of the true posterior and
32+
the approximation and using Pareto-$\hat{k}$ diagnostic [@Vehtari+etal:2024:PSIS] to assess whether these ratios can
33+
be used to improve the approximation via resampling. The
34+
normalization for the posterior can be estimated reliably [@Vehtari+etal:2024:PSIS, Section 3], which is the
35+
first requirement for reliable resampling. If estimated Pareto-$\hat{k}$ for the ratios is smaller than 0.7,
36+
there is still need to further diagnose reliability of importance sampling estimate for all quantities of interest [@Vehtari+etal:2024:PSIS, Section 2.2]. If estimated Pareto-$\hat{k}$ is larger than 0.7, then the
37+
estimate for the normalization is unreliable and any Monte Carlo estimate may have a big error. The resampled draws
38+
can still contain some useful information about the location and shape of the posterior which can be used in early
39+
parts of Bayesian workflow [@Gelman:etal:2020:workflow].
40+
41+
## Using Pathfinder for initializing MCMC
42+
43+
If estimated Pareto-$\hat{k}$ for the ratios is smaller than 0.7, the resampled posterior draws are almost as
44+
good for initializing MCMC as would independent draws from the posterior be. If estimated Pareto-$\hat{k}$ for the
45+
ratios is larger than 0.7, the Pathfinder draws are not reliable for posterior inference directly, but they are still
46+
very likely better for initializing MCMC than random draws from an arbitrary pre-defined distribution (e.g. uniform from
47+
-2 to 2 used by Stan by default). If Pareto-$\hat{k}$ is larger than 0.7, it is likely that one of the ratios is much bigger
48+
than others and the default resampling with replacement would produce copies of one unique draw. For initializing several
49+
Markov chains, it is better to use resampling without replacement to guarantee unique initialization for each chain. At the
50+
moment Stan allows turning off the resampling completely, and then the resampling without replacement can be done outside of
51+
Stan.
52+

0 commit comments

Comments
 (0)