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 \in (0,1)\) by

\[ \mathrm{logit}(u) = \log \frac{u}{1 - u}. \]

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

\[ \mathrm{logit}^{-1}(v) = \frac{1}{1 + \exp(-v)}. \]

The derivative of the logistic sigmoid is

\[ \frac{d}{dy} \mathrm{logit}^{-1}(y) = \mathrm{logit}^{-1}(y) \cdot \left( 1 - \mathrm{logit}^{-1}(y) \right). \]

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 = \mathrm{logit} \left( \frac{X - a}{b - a} \right). \]

Lower and Upper Bounds Inverse Transform

The inverse of this transform is

\[ X = a + (b - a) \cdot \mathrm{logit}^{-1}(Y). \]

Absolute Derivative of the Lower and Upper Bounds Inverse Transform

The absolute derivative of the inverse transform is given by

\[ \left| \frac{d}{dy} \left( a + (b - a) \cdot \mathrm{logit}^{-1}(y) \right) \right| = (b - a) \cdot \mathrm{logit}^{-1}(y) \cdot \left( 1 - \mathrm{logit}^{-1}(y) \right). \]

Therefore, the density of the transformed variable \(Y\) is

\[ p_Y(y) = p_X \! \left( a + (b - a) \cdot \mathrm{logit}^{-1}(y) \right) \cdot (b - a) \cdot \mathrm{logit}^{-1}(y) \cdot \left( 1 - \mathrm{logit}^{-1}(y) \right). \]

Despite the apparent complexity of this expression, most of the terms are repeated and thus only need to be evaluated once. Most importantly, \(\mathrm{logit}^{-1}(y)\) only needs to be evaluated once, so there is only one call to \(\exp(-y)\).