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 \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)$$.