Dear all, I would like to kindly ask you for your support. I have tried to perform the inference with covariance matrix which is not hardcoded as matrix for n variables as the input and I failed. I am using the PyMC v5.3.0. Honestly, I have no idea how to solve it. Please find the code bellow which I used:
n = 3 # number of variables
data = np.random.randn(5, n) # example data with 5 observations and n variables
with pm.Model() as pearson_model_n:
# Priors for the mean and standard deviation of each variable are weakly informative
mean = pm.Normal('mean', mu=0, sigma=10, shape=n)
sigma = pm.HalfNormal('sigma', sigma=10, shape=n)
# Prior for the correlation matrix
packed_L = pm.LKJCholeskyCov('packed_L', n=n, eta=2., sd_dist=pm.Exponential.dist(1.))
L = pm.expand_packed_triangular(n, packed_L)
cov = pm.Deterministic('cov', L.dot(L.T))
# Calculate the Pearson correlation matrix
corr = pm.Deterministic('corr', cov / np.outer(sigma, sigma))
# Define the multivariate normal distribution
y_pred_n = pm.MvNormal('y_pred', mu=mean, cov=cov, observed=data)
# Perform Bayesian inference
trace_pn = pm.sample(draws=2000, chains=4, tune=1000, random_seed=42)
Here is the error I recieved:
AttributeError Traceback (most recent call last)
Cell In[1011], line 11
9 # Prior for the correlation matrix
10 packed_L = pm.LKJCholeskyCov('packed_L', n=n, eta=2., sd_dist=pm.Exponential.dist(1.))
---> 11 L = pm.expand_packed_triangular(n, packed_L)
12 cov = pm.Deterministic('cov', L.dot(L.T))
14 # Calculate the Pearson correlation matrix
File c:\Users\uknown\anaconda3\envs\pymc202305\Lib\site-packages\pymc\math.py:432, in expand_packed_triangular(n, packed, lower, diagonal_only)
409 def expand_packed_triangular(n, packed, lower=True, diagonal_only=False):
410 r"""Convert a packed triangular matrix into a two dimensional array.
411
412 Triangular matrices can be stored with better space efficiency by
(...)
430 If true, return only the diagonal of the matrix.
431 """
--> 432 if packed.ndim != 1:
433 raise ValueError("Packed triangular is not one dimensional.")
434 if not isinstance(n, int):
AttributeError: 'tuple' object has no attribute 'ndim'
I checked the dimension/type of my input:
data shape: (5, 3)
data type: <class ânumpy.ndarrayâ>