# Bayesian correlation coefficient

**URL:** <https://discourse.pymc.io/t/bayesian-correlation-coefficient/12108>\
**Category:** v5\
**Tags:** theano\
**Created:** [May 12, 2023, 12:49pm UTC](https://discourse.pymc.io/t/bayesian-correlation-coefficient/12108 "2023-05-12T12:49:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrKevinDC](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mrkevindc/32/6481_2.png) [@MrKevinDC](https://discourse.pymc.io/u/MrKevinDC)\
**Post date:** [May 12, 2023, 12:49pm UTC](https://discourse.pymc.io/t/bayesian-correlation-coefficient/12108/1 "2023-05-12T12:49:52Z")

</div>

Hi, I was attempting to infer Pearson-like correlation coefficients using [this PYMC3-based notebook](https://nbviewer.org/github/sebp/bayesian-correlation/blob/master/bayesian_correlation_pymc3.ipynb) as basis, but I cannot figure out how to initiate the precision matrix for PYMC5. The core code is this:

```auto
import theano.tensor as T

def precision(sigma, rho):
    C = T.alloc(rho, 2, 2)
    C = T.fill_diagonal(C, 1.)
    S = T.diag(sigma)
    return T.nlinalg.matrix_inverse(S.dot(C).dot(S))

def analyze(data):
    with pm.Model() as model:
        # priors might be adapted here to be less flat
        mu = pm.Normal('mu', mu=0., tau=0.000001, shape=2, testval=np.mean(data, axis=1))
        sigma = pm.Uniform('sigma', lower=1e-6, upper=1000., shape=2, testval=np.std(data, axis=1))
        rho = pm.Uniform('r', lower=-1., upper=1., testval=0)

        prec = pm.Deterministic('prec', precision(sigma, rho))
        mult_n = pm.MvNormal('mult_n', mu=mu, tau=prec, observed=data.T)

    return model

```

I will try to figure out how in the meantime and might answer myself, I think it is the type of functionality someone else might find this useful.

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [May 13, 2023, 5:56am UTC](https://discourse.pymc.io/t/bayesian-correlation-coefficient/12108/2 "2023-05-13T05:56:11Z")

</div>

The advice is usually to use a LKJCholeskyCov as a prior, because every other form of covariance prior tends to sample poorly.

There are a couple of examples in the gallery that use it.

This one is still in PyMC v4 but should be similar with the latest release: [LKJ Cholesky Covariance Priors for Multivariate Normal Models — PyMC example gallery](https://www.pymc.io/projects/examples/en/latest/case_studies/LKJ.html)

---

<div class="post-metadata">

**Author:** ![MrKevinDC](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mrkevindc/32/6481_2.png) [@MrKevinDC](https://discourse.pymc.io/u/MrKevinDC)\
**Post date:** [May 15, 2023, 9:17am UTC](https://discourse.pymc.io/t/bayesian-correlation-coefficient/12108/3 "2023-05-15T09:17:11Z")

</div>

Thank so much for referring me to that link, I was unaware of this. Thanks @ricardoV94 ! Marking as solved as well

---

<div class="post-metadata">

**Author:** ![Ingmar](https://avatars.discourse-cdn.com/v4/letter/i/cc9497/32.png) [@Ingmar](https://discourse.pymc.io/u/Ingmar)\
**Post date:** [March 11, 2024, 7:20pm UTC](https://discourse.pymc.io/t/bayesian-correlation-coefficient/12108/4 "2024-03-11T19:20:15Z")

</div>

Hello, I have a related question and hope it is ok to ask it here. Is there a good way to get a posterior distribution for a Pearson’s correlation coefficient, if I don’t want to assume normality for the variables?
