![]() |
Helpful Stan Functions
|
Functions | |
real | multi_normal_cholesky_truncated_lpdf (vector u, vector mu, matrix L, vector lb, vector ub, vector lb_ind, vector ub_ind) |
vector | multi_normal_cholesky_truncated_rng (vector u, vector mu, matrix L, vector lb, vector ub, vector lb_ind, vector ub_ind) |
void | multi_normal_cholesky_truncated_lp (vector u, vector mu, matrix L, vector lb, vector ub, vector lb_ind, vector ub_ind) |
The truncated multivariate normal with mean vector \( \mu \) and variance-covariance matrix \( \Sigma \). Additionally, \( L \) is the Cholesky factor defined by \( \text{Chol}(\Sigma) = LL^T \).
The following derivation first appeared in an unpublished manuscript by Ben Goodrich, circa 2017.
Let a \( k \)-vector random variable \( \mathbf{y} \) be distributed multivariate normal then
\[ \mathbf{y} \stackrel{d}{=} \mathbf{\mu} + \mathbf{Lz(u)}, \]
where \( \mathbf{u} \) is a vector of uniform variates on \([0, 1]\) and \( \mathbf{z(u)} = \Phi^{-1}(\mathbf{u}) \).
Using the above definition, we may partition the standard uniform line so that the constaints are satisfied, however, we must supply a Jacobian adjustment due to the transformation of a uniform variate from \([0, 1]\) to the constrained space.
Given realiziations of the uniform variate \( \mathbf{u} = u_1, \ldots, u_k \) and a \( k \)-array of length-2 vector bounds
\[ \mathbf{b[lb, ub]} = [lb_1, ub_1], \ldots, [lb_k, ub_k], \]
we constrain \( \mathbf{u} \) to lie within the given bounds. That is, for each \( k \) a lower and upper bound must satisfy \( u_k^* = \Phi(b_k - \mu_k + L_{k, 1:k-1} z_{k - 1}) \). The random variate \( \mathbf{u} \) is then constrained to fall within those bounds. The new uniform variate is constrained to lie within the given bounds \( v_k \sim \mathcal{U}(u_k^*[1], u_k^*[2]) \) by
\[ v_k = u_k^*[1] + (u_k^*[2] - u_k^*[1]) u_k. \]
This implies that \( \frac{\partial u_k}{\partial v_k} = (u_k^*[2] - u_k^*[1]) \) and the absolute value of the log Jacobian is \(\ln(u_k^*[2] - u_k^*[1]) \).
The final realizations of the truncated multivariate normal vector is given by
\[ \mathbf{r} = \mu + L \Phi^{-1}(\mathbf{v}). \]
void multi_normal_cholesky_truncated_lp | ( | vector | u, |
vector | mu, | ||
matrix | L, | ||
vector | lb, | ||
vector | ub, | ||
vector | lb_ind, | ||
vector | ub_ind | ||
) |
Truncated Multivariate Increment Log-Probability
u | Vector number on [0,1], not checked but function will return NaN |
mu | Vector |
L | Cholesky Factor Corr |
lb | Vector of lower bounds |
ub | Vector of upper bounds |
lb_ind | Vector indicator if there is an lower bound |
ub_ind | Vector indicator if there is an upper bound |
real multi_normal_cholesky_truncated_lpdf | ( | vector | u, |
vector | mu, | ||
matrix | L, | ||
vector | lb, | ||
vector | ub, | ||
vector | lb_ind, | ||
vector | ub_ind | ||
) |
Truncated Multivariate Log Probability Density
u | Vector number on [0,1], not checked but function will return NaN |
mu | Vector |
L | Cholesky Factor Corr |
lb | Vector of lower bounds |
ub | Vector of upper bounds |
lb_ind | Vector indicator if there is an lower bound |
ub_ind | Vector indicator if there is an upper bound |
vector multi_normal_cholesky_truncated_rng | ( | vector | u, |
vector | mu, | ||
matrix | L, | ||
vector | lb, | ||
vector | ub, | ||
vector | lb_ind, | ||
vector | ub_ind | ||
) |
Truncated Multivariate Random Number Generator
u | Vector number on [0,1], not checked but function will return NaN |
mu | Vector |
L | Cholesky Factor Corr |
lb | Vector of lower bounds |
ub | Vector of upper bounds |
lb_ind | Vector indicator if there is an lower bound |
ub_ind | Vector indicator if there is an upper bound |