# LKJ inverse transform for correlation matrices (incorrect documentation?)

**URL:** <https://discourse.mc-stan.org/t/lkj-inverse-transform-for-correlation-matrices-incorrect-documentation/19312>\
**Category:** Developers\
**Created:** [November 16, 2020, 2:18pm UTC](https://discourse.mc-stan.org/t/lkj-inverse-transform-for-correlation-matrices-incorrect-documentation/19312 "2020-11-16T14:18:22Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [November 21, 2020, 2:22am UTC](https://discourse.mc-stan.org/t/lkj-inverse-transform-for-correlation-matrices-incorrect-documentation/19312/14 "2020-11-21T02:22:12Z")

</div>

That is all correct and basically the motivation for why we have promoted using the Cholesky factor of a correlation matrix rather than the correlation matrix itself (also things are more numerically stable).

If \boldsymbol{\Sigma} =\mathbf{L} \mathbf{L}^\top is a correlation matrix and \mathbf{L} is its lower-triangular Cholesky factor, then as @bbbales2 mentioned, its rows must have unit length in order for the diagonal elements of \boldsymbol{\Sigma} to be 1. Since the i-th row of \mathbf{L} has unit-length, its elements can be expressed in unit-[hyperspherical coordinates](https://en.wikipedia.org/wiki/N-sphere#Spherical_coordinates) (i.e. in terms of \theta\_{ij} \in \left(0,\pi\right)) as  
\begin{eqnarray\*} L\left(\boldsymbol{\theta}\right)\_{ij} & = & \begin{cases} 1 & 1=i=j\\ \prod\_{k=1}^{j-1}\sin\left(\theta\_{kj}\right) & 1\<i=j\\ \cos\left(\theta\_{i1}\right) & 1=i\<j\\ \cos\left(\theta\_{ij}\right)\prod\_{k=1}^{j-1}\sin\left(\theta\_{kj}\right) & 1\<i\<j\\ 0 & i\> j \end{cases} \end{eqnarray\*}  
Let \omega\_{ij} = \cos\left(\theta\_{ij}\right) be a partial correlation on a canonical vine, then 0 \< \sin\left(\theta\_{ij}\right) = \sqrt{1 - \cos\left(\theta\_{ij}\right)^2} = \sqrt{1 - \omega\_{ij}^2}. Then, the elements of the Cholesky factor in terms of partial correlations on a canonical vine become  
\begin{eqnarray\*} L\left(\boldsymbol{\omega}\right)\_{ij} & = & \begin{cases} 1 & 1=i=j\\ \prod\_{k=1}^{j-1}\sqrt{1 - \omega\_{kj}^2} & 1\<i=j\\ \omega\_{i1} & 1=i\<j\\ \omega\_{ij}\prod\_{k=1}^{j-1}\sqrt{1 - \omega\_{kj}^2} & 1\<i\<j\\ 0 & i \> j \end{cases} \end{eqnarray\*}

That is what the code at

> <https://github.com/stan-dev/math/blob/develop/stan/math/prim/fun/cholesky_corr_constrain.hpp#L17>

  
does and the documentation butchers. It sort of remains to be shown that \omega\_{ij} “is” a partial correlation on a canonical vine, but you can get a hint that is true from the relation in the LKJ paper that \left|\boldsymbol{\Sigma}\right| = \prod\_{i = 1}^{K - 1} \prod\_{j = i + 1}^K \left(1 - \omega\_{ij}^2\right), which is also equal to 1 \times \prod\_{j = 2}^K L\left(\boldsymbol{\omega}\right)\_{jj}^2 = 1 \times \prod\_{j = 2}^K \prod\_{k=1}^{j-1}\left(1 - \omega\_{kj}^2\right) since all {K \choose 2} elements of \boldsymbol{\omega} appear exactly once in both expressions.

---

_[View the full topic](https://discourse.mc-stan.org/t/lkj-inverse-transform-for-correlation-matrices-incorrect-documentation/19312)._
