How does Stan sample correlation matrices?

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

Those details are in the Users guide here:


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.


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 :)

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:

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


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?



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.

1 Like

Thanks for your response!

From this post (here) 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