You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/stan-users-guide/custom-probability.qmd
+32-16Lines changed: 32 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -12,28 +12,44 @@ the total log probability. The rest of the chapter provides examples.
12
12
13
13
### Triangle distribution {-}
14
14
15
-
A simple example is the triangle distribution,
15
+
A simple example is the symmetric triangle distribution,
16
16
whose density is shaped like an isosceles triangle with corners at
17
17
specified bounds and height determined by the constraint that a
18
18
density integrate to 1. If $\alpha \in \mathbb{R}$ and $\beta \in \mathbb{R}$
19
19
are the bounds, with $\alpha < \beta$, then $y \in (\alpha,\beta)$ has
20
-
a density defined as follows.
20
+
a density defined as
21
21
$$
22
22
\textsf{triangle}(y \mid \alpha,\beta)
23
23
=
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).
32
27
$$
33
28
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
35
51
$$
36
-
\textsf{triangle}(y \mid -1,1) = 1 - |y|.
52
+
\textsf{triangle}(y \mid -1,1) \propto 1 - |y|.
37
53
$$
38
54
Consider the following Stan implementation of
39
55
$\textsf{triangle}(-1,1)$ for sampling.
@@ -49,11 +65,11 @@ model {
49
65
```
50
66
51
67
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
53
69
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
55
71
`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
57
73
computation is faster, more accurate, and more stable.
58
74
59
75
The constrained type `real<lower=-1, upper=1>` declared for `y` is
@@ -65,7 +81,7 @@ explored values of `y` outside of $(-1,1)$.
65
81
66
82
Now suppose the log probability function were extended to all of
67
83
$\mathbb{R}$ as follows by defining the probability to be `log(0.0)`,
0 commit comments