This is an old version, view current version.

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)=logu1u.

The inverse of the log odds function is the logistic sigmoid, defined for v(,) by

logit1(v)=11+exp(v).

The derivative of the logistic sigmoid is

ddylogit1(y)=logit1(y)(1logit1(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(Xaba).

Lower and upper bounds inverse transform

The inverse of this transform is

X=a+(ba)logit1(Y).

Absolute derivative of the lower and upper bounds inverse transform

The absolute derivative of the inverse transform is given by

|ddy(a+(ba)logit1(y))|=(ba)logit1(y)(1logit1(y)).

Therefore, the density of the transformed variable Y is

pY(y)=pX(a+(ba)logit1(y))(ba)logit1(y)(1logit1(y)).

Despite the apparent complexity of this expression, most of the terms are repeated and thus only need to be evaluated once. Most importantly, logit1(y) only needs to be evaluated once, so there is only one call to exp(y).