Skip to content

Commit 765d11d

Browse files
Merge pull request #894 from stan-dev/fix-655
fix triangle distribution equation
2 parents 756612a + 10cdfb7 commit 765d11d

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/stan-users-guide/custom-probability.qmd

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,44 @@ the total log probability. The rest of the chapter provides examples.
1212

1313
### Triangle distribution {-}
1414

15-
A simple example is the triangle distribution,
15+
A simple example is the symmetric triangle distribution,
1616
whose density is shaped like an isosceles triangle with corners at
1717
specified bounds and height determined by the constraint that a
1818
density integrate to 1. If $\alpha \in \mathbb{R}$ and $\beta \in \mathbb{R}$
1919
are the bounds, with $\alpha < \beta$, then $y \in (\alpha,\beta)$ has
20-
a density defined as follows.
20+
a density defined as
2121
$$
2222
\textsf{triangle}(y \mid \alpha,\beta)
2323
=
24-
\frac{2}{\beta - \alpha}
25-
\
26-
\left(
27-
1 -
28-
\left|
29-
y - \frac{\alpha + \beta}{\beta - \alpha}
30-
\right|
31-
\right)
24+
\frac{1}{(\beta - \alpha)^2}
25+
\cdot
26+
\textrm{min}(y - \alpha, \beta - y).
3227
$$
3328

34-
If $\alpha = -1$, $\beta = 1$, and $y \in (-1,1)$, this reduces to
29+
The general form of triangle can be coded in Stan as follows.
30+
31+
```stan
32+
data {
33+
real alpha;
34+
real<lower=alpha> beta;
35+
}
36+
parameters {
37+
real<lower=alpha, upper=beta> y;
38+
}
39+
model {
40+
target += -2 * log(beta - alpha)
41+
+ log(fmin(y - alpha, beta - y));
42+
}
43+
```
44+
45+
Because the bounds are specified as data here, the term `-2 * log(beta
46+
- alpha)` could be dropped from the log density. If either of the
47+
bounds depends on a parameter, then this term must be included.
48+
49+
If $\alpha = -1$, $\beta = 1$, and $y \in (-1,1)$, then
50+
`fmin(y - alpha, beta - y)` is `fmin(y + 1, -1 - y)`, which is `fmin(y + 1, -(y + 1))`, which reduces to `1 - abs(y)`. Therfore, the density, dropping constants, reduces to
3551
$$
36-
\textsf{triangle}(y \mid -1,1) = 1 - |y|.
52+
\textsf{triangle}(y \mid -1, 1) \propto 1 - |y|.
3753
$$
3854
Consider the following Stan implementation of
3955
$\textsf{triangle}(-1,1)$ for sampling.
@@ -49,11 +65,11 @@ model {
4965
```
5066

5167
The single scalar parameter `y` is declared as lying in the
52-
interval `(-1,1)`. The total log probability is
68+
interval `(-1, 1)`. The total log probability is
5369
incremented with the joint log probability of all parameters, i.e.,
54-
$\log \mathsf{Triangle}(y \mid -1,1)$. This value is coded in Stan as
70+
$\log \mathsf{Triangle}(y \mid -1, 1)$. This value is coded in Stan as
5571
`log1m(abs(y))`. The function `log1m` is defined so
56-
that `log1m(x)` has the same value as $\log(1-x)$, but the
72+
that `log1m(x)` has the same value as $\log(1 - x)$, but the
5773
computation is faster, more accurate, and more stable.
5874

5975
The constrained type `real<lower=-1, upper=1>` declared for `y` is
@@ -65,7 +81,7 @@ explored values of `y` outside of $(-1,1)$.
6581

6682
Now suppose the log probability function were extended to all of
6783
$\mathbb{R}$ as follows by defining the probability to be `log(0.0)`,
68-
i.e., $-\infty$, for values outside of $(-1,1)$.
84+
i.e., $-\infty$, for values outside of $(-1, 1)$.
6985

7086
```stan
7187
target += log(fmax(0.0,1 - abs(y)));

0 commit comments

Comments
 (0)