10.4 Lower and upper bounded scalar
For lower and upper-bounded variables, Stan uses a scaled and translated log-odds transform.
Log odds and the logistic sigmoid
The log-odds function is defined for u∈(0,1) by
logit(u)=logu1−u.
The inverse of the log odds function is the logistic sigmoid, defined for v∈(−∞,∞) by
logit−1(v)=11+exp(−v).
The derivative of the logistic sigmoid is
ddylogit−1(y)=logit−1(y)⋅(1−logit−1(y)).
Lower and upper bounds transform
For variables constrained to be in the open interval (a,b), Stan uses a scaled and translated log-odds transform. If variable X is declared to have lower bound a and upper bound b, then it is transformed to a new variable Y, where
Y=logit(X−ab−a).
Absolute derivative of the lower and upper bounds inverse transform
The absolute derivative of the inverse transform is given by
|ddy(a+(b−a)⋅logit−1(y))|=(b−a)⋅logit−1(y)⋅(1−logit−1(y)).
Therefore, the density of the transformed variable Y is
pY(y)=pX(a+(b−a)⋅logit−1(y))⋅(b−a)⋅logit−1(y)⋅(1−logit−1(y)).
Despite the apparent complexity of this expression, most of the terms are repeated and thus only need to be evaluated once. Most importantly, logit−1(y) only needs to be evaluated once, so there is only one call to exp(−y).