# Difference in lkj\_corr\_cholesky\_lpdf and lkj\_corr\_lpdf outputs with same corresponding inputs

**URL:** <https://discourse.mc-stan.org/t/difference-in-lkj-corr-cholesky-lpdf-and-lkj-corr-lpdf-outputs-with-same-corresponding-inputs/12934>\
**Category:** General\
**Created:** [February 2, 2020, 9:15pm UTC](https://discourse.mc-stan.org/t/difference-in-lkj-corr-cholesky-lpdf-and-lkj-corr-lpdf-outputs-with-same-corresponding-inputs/12934 "2020-02-02T21:15:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ybryan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ybryan/32/2103_2.png) [@ybryan](https://discourse.mc-stan.org/u/ybryan)\
**Post date:** [February 2, 2020, 9:15pm UTC](https://discourse.mc-stan.org/t/difference-in-lkj-corr-cholesky-lpdf-and-lkj-corr-lpdf-outputs-with-same-corresponding-inputs/12934/1 "2020-02-02T21:15:25Z")

</div>

Hello,

I am starting to get into multivariate modelling and was playing with the above mentioned functions by exposing them in RStan and found consistent differences in the lpdf’s with the “same” (L\_Omega or Omega) inputs. Is this a significant difference?

```
functions {
  real lkj_cor_lpdf(matrix y, real eta) {
    return lkj_corr_lpdf(y | eta);
  }
  matrix lkj_cor_rng(int K, real eta) {
    return lkj_corr_rng(K, eta);
  }
  real lkj_cor_cholesky_lpdf(matrix L, real eta) {
    return lkj_corr_cholesky_lpdf(L | eta);
  }
  matrix lkj_cor_cholesky_rng(int K, real eta) {
    return lkj_corr_cholesky_rng(K, eta);
  }
  real st_cauchy_rng(real mu, real sigma) {
    return cauchy_rng(mu, sigma);
  }
  matrix corr_matrix_cholesky(matrix L_Omega) {
    return multiply_lower_tri_self_transpose(L_Omega);
  }
}

```

```
expose_stan_functions(lkj)

```

```
eta <- 0.7
L_Omega <- lkj_cor_cholesky_rng(5, eta)
L_Sigma <- replicate(5, abs(st_cauchy_rng(0, 0.5)))
Omega <- corr_matrix_cholesky(L_Omega)

all.equal(Omega, L_Omega %*% t(L_Omega))
lkj_cor_cholesky_lpdf(L_Omega, eta)
lkj_cor_lpdf(Omega, eta)

```

```
[1] TRUE
[1] -4.491901
[1] -2.382085

```

Also, when using eta = 1, the `lkj_corr_lpdf` function never changes?  
RStan 2.19.2

---

<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:** [February 2, 2020, 10:09pm UTC](https://discourse.mc-stan.org/t/difference-in-lkj-corr-cholesky-lpdf-and-lkj-corr-lpdf-outputs-with-same-corresponding-inputs/12934/2 "2020-02-02T22:09:01Z")

</div>

> [@ybryan](#):
>
> I am starting to get into multivariate modelling and was playing with the above mentioned functions by exposing them in RStan and found consistent differences in the lpdf’s with the “same” (L\_Omega or Omega) inputs. Is this a significant difference?

You should input `Omega` into `lkj_corr_lpdf` and input `L` into `lkj_corr_cholesky_lpdf`. In other words, `lkj_corr_lpdf` is the log-density of a correlation matrix under the LKJ distriution and `lkj_corr_cholesky_lpdf` is the log-density of the Cholesky factor of a correlation that has a LKJ distribution.

> [@ybryan](#):
>
> Also, when using eta = 1, the `lkj_corr_lpdf` function never changes?

That is because the density of a LKJ distribution is constant when \eta = 1, although that is not so in the case of `lkj_corr_cholesky_lpdf(L | eta)` due to the Jacobian term.

---

<div class="post-metadata">

**Author:** ![ybryan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ybryan/32/2103_2.png) [@ybryan](https://discourse.mc-stan.org/u/ybryan)\
**Post date:** [February 2, 2020, 11:22pm UTC](https://discourse.mc-stan.org/t/difference-in-lkj-corr-cholesky-lpdf-and-lkj-corr-lpdf-outputs-with-same-corresponding-inputs/12934/3 "2020-02-02T23:22:50Z")

</div>

Thanks for the \eta=1 explanation.

> [@bgoodri](#):
>
> You should input `Omega` into `lkj_corr_lpdf` and input `L` into `lkj_corr_cholesky_lpdf` . In other words, `lkj_corr_lpdf` is the log-density of a correlation matrix under the LKJ distriution and `lkj_corr_cholesky_lpdf` is the log-density of the Cholesky factor of a correlation that has a LKJ distribution.

I thought that was what I was doing here?

```no-highlight
lkj_cor_cholesky_lpdf(L_Omega, eta) // stan::lkj_corr_cholesky_lpdf
lkj_cor_lpdf(Omega, eta) // stan::lkj_corr_lpdf

```

---

<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:** [February 3, 2020, 4:32am UTC](https://discourse.mc-stan.org/t/difference-in-lkj-corr-cholesky-lpdf-and-lkj-corr-lpdf-outputs-with-same-corresponding-inputs/12934/4 "2020-02-03T04:32:15Z")

</div>

Yes, that is right.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [February 13, 2020, 9:49pm UTC](https://discourse.mc-stan.org/t/difference-in-lkj-corr-cholesky-lpdf-and-lkj-corr-lpdf-outputs-with-same-corresponding-inputs/12934/5 "2020-02-13T21:49:39Z")

</div>

> [@bgoodri](#):
>
> You should input `Omega` into `lkj_corr_lpdf` and input `L` into `lkj_corr_cholesky_lpdf` .

There’s a Jacobian in play here for the transform. You get different answers for the same reason that

```no-highlight
lognormal(a | mu, sigma) != normal(log(a) | u, sigma)

```

The [Stan reference manual chapter on constraint transforms](https://mc-stan.org/docs/2_22/reference-manual/cholesky-factors-of-correlation-matrices-1.html) walks through the Jacobian because it’s what we use for encoding covariance matrices.

---

<div class="post-metadata">

**Author:** ![ybryan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ybryan/32/2103_2.png) [@ybryan](https://discourse.mc-stan.org/u/ybryan)\
**Post date:** [February 13, 2020, 10:20pm UTC](https://discourse.mc-stan.org/t/difference-in-lkj-corr-cholesky-lpdf-and-lkj-corr-lpdf-outputs-with-same-corresponding-inputs/12934/6 "2020-02-13T22:20:08Z")

</div>

Thanks!

I am now in a rabbit-hole of understanding Jacobians transforms/adjustments. I learned about Jacobians in school but didn’t bother to think it was important in applied stats.
