### Describe the issue:
LKJCorr backwards sampling is currently broken and does… not properly constrain values to a positive semidefinite matrix early enough. This leads to compilation randomly failing when providing it as a covariance matrix for MvNormal and other similar multivariate distributions. The failure is inherently random, but it gets increasingly likely with larger n values and fails almost always with n>=10 and can in theory be seen with n as low as 3.
Error message is informative, as it is easy to verify the matrix reported in error has negative eigenvalues, proving it is not PSD.
This only affects the backwards sampling (i.e. logp computations), with .eval() and predictive sampling working as they should.
### Reproduceable code example:
```python
import pymc as pm
import pytensor.tensor as pt
import numpy as np
n, eta = 10, 1
# Turn LKJCorr output to a proper square corr matrix
def corr_to_mat(corr_values, n):
corr = pt.zeros((n, n))
corr = pt.set_subtensor(corr[np.triu_indices(n, k=1)], corr_values)
return corr + corr.T + pt.identity_like(corr)
# Generate Data
corr = corr_to_mat(pm.LKJCorr.dist(n=n, eta=eta),n)
y = pm.draw(pm.MvNormal.dist(mu=0, cov=corr), 100)
# PyMC Model
with pm.Model() as m:
corr_values = pm.LKJCorr('corr_values', n=n, eta=eta)
corr = corr_to_mat(corr_values,n)
y_hat = pm.MvNormal('y_hat', mu=0, cov=corr, observed=y)
idata = pm.sample()
```
### Error message:
```shell
Traceback (most recent call last):
File "/home/velochy/pymc/test3.py", line 22, in <module>
idata = pm.sample()
^^^^^^^^^^^
File "/home/velochy/pymc/pymc/sampling/mcmc.py", line 744, in sample
model.check_start_vals(ip)
File "/home/velochy/pymc/pymc/model/core.py", line 1660, in check_start_vals
raise SamplingError(
pymc.exceptions.SamplingError: Initial evaluation of model at starting point failed!
Starting values:
{'corr_values_interval__': array([ 0.96546568, 0.55627918, -0.52092415, -0.79551073, -0.04416735,
-0.61208579, -0.04889871, -0.62181685, 0.13621748, 0.32908589,
-0.38471896, -0.89088229, -0.1326561 , -0.18828583, 0.26747631,
-0.47067211, 0.51480876, -0.56323033, -0.3513053 , 0.30480191,
-0.36865109, 0.1479414 , -0.66890036, 0.98484518, -0.85993115,
0.48094929, -0.04547197, -0.07743477, -0.3381161 , 0.12565052,
-0.06450642, -0.69698417, 0.80246982, -0.67958155, -0.29175309,
-0.02445876, -0.7858127 , 0.17738997, -0.87779485, -0.27196011,
-0.52231265, 0.46271978, 0.0810375 , 0.84458683, -0.72285369])}
Logp initial evaluation results:
{'corr_values': -inf, 'y_hat': -inf}
You can call `model.debug()` for more details.
```
### PyMC version information:
pymc 5.10.3
### Context for the issue:
This arose in discussion with @ricardoV94 and @jessegrabowski in https://discourse.pymc.io/t/using-lkjcorr-together-with-mvnormal/13606/29 . Thread has more useful context