# How does Stan sample correlation matrices?

**URL:** <https://discourse.mc-stan.org/t/how-does-stan-sample-correlation-matrices/30537>\
**Category:** General\
**Created:** [February 26, 2023, 2:48am UTC](https://discourse.mc-stan.org/t/how-does-stan-sample-correlation-matrices/30537 "2023-02-26T02:48:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![CerulloE](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cerulloe/32/15968_2.png) [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Post date:** [February 26, 2023, 2:48am UTC](https://discourse.mc-stan.org/t/how-does-stan-sample-correlation-matrices/30537/1 "2023-02-26T02:48:37Z")

</div>

I was wondering if anybody has the specific algorithm details for how Stan samples correlation matrices - particularly how it ensures positive-definiteness

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [February 26, 2023, 4:47pm UTC](https://discourse.mc-stan.org/t/how-does-stan-sample-correlation-matrices/30537/2 "2023-02-26T16:47:12Z")

</div>

Those details are in the Users guide here:

> **[10.9 Correlation matrices | Stan Reference Manual](https://mc-stan.org/docs/reference-manual/correlation-matrix-transform.html)**
>
> Stan reference manual specifying the syntax and semantics of the Stan programming language.

---

<div class="post-metadata">

**Author:** ![spinkney](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@spinkney](https://discourse.mc-stan.org/u/spinkney)\
**Post date:** [February 26, 2023, 7:40pm UTC](https://discourse.mc-stan.org/t/how-does-stan-sample-correlation-matrices/30537/3 "2023-02-26T19:40:26Z")

</div>

It’s in the LKJ paper but it’s tough to parse out how to write this algorithm. I’m still impressed how Ben was able to grok the algorithm and write it in Stan.

---

<div class="post-metadata">

**Author:** ![CerulloE](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cerulloe/32/15968_2.png) [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Post date:** [February 26, 2023, 8:51pm UTC](https://discourse.mc-stan.org/t/how-does-stan-sample-correlation-matrices/30537/4 "2023-02-26T20:51:04Z")

</div>

Thanks @spinkney and @jsocolar - the section in the manual which @jsocolar linked too (which is the same method as the LKJ paper you reference, I think) doesnt seem like it would be too difficult to implement, as it clearly spells out how to map from the unconstrained parameters (which we can use in the HMC leapfrog integrator to update the estimates) to the correlations and vice-versa? I havent had a chance to attempt to implement it yet though so ill see how it goes and let you know :)

---

<div class="post-metadata">

**Author:** ![nspencer](https://avatars.discourse-cdn.com/v4/letter/n/bbe5ce/32.png) [@nspencer](https://discourse.mc-stan.org/u/nspencer)\
**Post date:** [March 1, 2023, 4:26pm UTC](https://discourse.mc-stan.org/t/how-does-stan-sample-correlation-matrices/30537/5 "2023-03-01T16:26:54Z")

</div>

Hi @CerulloE,

Just in case you’re planning on using the implementation as laid out in the Users guide, I want to point out that Section 10.9 of the reference manual contains several mistakes. I described them here:

> [@Possible Error in the Correlation Matrices (10.9) Section of the Stan Manual](https://discourse.mc-stan.org/t/possible-error-in-the-correlation-matrices-10-9-section-of-the-stan-manual/29671):
>
> After spending some time playing around with implementing a modification of the LKJ prior, I believe there are errors in the statement of the “manageable expression” for W provided in the stan manual at the following link: The definition provided seems to assume that z has ones along the diagonal, instead of the zeros shown in the definition above. Otherwise, all diagonal entries other than the first row will be forced to 0. Also, I believe the final line in the piecewise statement is missin…

I submitted a pull request with fixes to github, but it doesn’t seem to have been pushed to the online version yet.

---

<div class="post-metadata">

**Author:** ![CerulloE](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cerulloe/32/15968_2.png) [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Post date:** [March 1, 2023, 9:56pm UTC](https://discourse.mc-stan.org/t/how-does-stan-sample-correlation-matrices/30537/6 "2023-03-01T21:56:24Z")

</div>

Hi @nspencer ,

Thanks for pointing this out. I noticed that the “manageable expression” also wasnt correct but assumed I coded it wrong

Is this part of that section correct?

![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/3/37e2bb7f5a37a92f93c39617d5c76da00c9e11bf.png)

Thanks

---

<div class="post-metadata">

**Author:** ![nspencer](https://avatars.discourse-cdn.com/v4/letter/n/bbe5ce/32.png) [@nspencer](https://discourse.mc-stan.org/u/nspencer)\
**Post date:** [March 1, 2023, 10:10pm UTC](https://discourse.mc-stan.org/t/how-does-stan-sample-correlation-matrices/30537/7 "2023-03-01T22:10:45Z")

</div>

Hi CerulloE,

I think that part you screen-shotted is still fine. If you want my updated version of the “manageable expression”, you can find it on the latest version of the manual on github.

> <https://github.com/stan-dev/docs/commit/7b1d3149fa1d47565195cd4e065d673a5859aaff>
>
> Error and fix suggested here on the stan forums.
> 
> https://discourse.mc-stan.or…g/t/possible-error-in-the-correlation-matrices-10-9-section-of-the-stan-manual/29671

---

<div class="post-metadata">

**Author:** ![CerulloE](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cerulloe/32/15968_2.png) [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Post date:** [March 2, 2023, 1:15pm UTC](https://discourse.mc-stan.org/t/how-does-stan-sample-correlation-matrices/30537/8 "2023-03-02T13:15:39Z")

</div>

Thanks for your response!

From this post ([here](https://discourse.mc-stan.org/t/lkj-inverse-transform-for-correlation-matrices-incorrect-documentation/19312/14)) it looks like the Stan math library implements a different method than that given in the user guide - is this still the case now? shouldn’t the user guide match/be up at date with the method Stan uses? apparently they result in different correlation matrices
