Hi, as reported initially in this thread (Dynamic shaping, "round" function, JAX, and a "few" more questions - #21 by caclav), the code below, which is basically identical to the following example (LKJ Cholesky Covariance Priors for Multivariate Normal Models — PyMC example gallery), returns an error when the sampler’s inivals
parameter is initialized to model.initial_point()
.
import numpy as np
import pymc as pm
# configuration
RANDOM_SEED = 12345
rng = np.random.default_rng(RANDOM_SEED)
N = 10000
# true parameters
mu_actual = np.array([1.0, -2.0])
sigmas_actual = np.array([0.7, 1.5])
Rho_actual = np.matrix([[1.0, -0.4], [-0.4, 1.0]])
Sigma_actual = np.diag(sigmas_actual) * Rho_actual * np.diag(sigmas_actual)
# simulated data generation
x = rng.multivariate_normal(mu_actual, Sigma_actual, size=N)
# PyMC Model definition
coords = {"axis": ["y", "z"],
"axis_bis": ["y", "z"],
"obs_id": np.arange(N)}
with pm.Model(coords=coords,) as model:
chol, corr, stds = pm.LKJCholeskyCov(
"chol", n=2, eta=2.0, sd_dist=pm.Exponential.dist(1.0, shape=2)
)
cov = pm.Deterministic("cov", chol.dot(chol.T), dims=("axis", "axis_bis"))
mu = pm.Normal("mu", 0.0, sigma=1.5, dims="axis")
obs = pm.MvNormal("obs", mu, chol=chol, observed=x, dims=("obs_id", "axis"))
init_pts = model.initial_point()
if False: # AN ERROR IS RAISED IN THIS CASE
pass
else: # THE ISSUE DOESN'T HAPPEN IN THIS CASE
del init_pts["chol_cholesky-cov-packed__"]
# line below facultative/optional
# init_pts['chol'] = np.array([0.70931675, -0.61463295, 1.36559879])
# NUTS MCMC sampling and inference - THE ERROR HAPPENS HERE!
with model:
idata = pm.sample(initvals=init_pts,
idata_kwargs={"dims": {"chol_stds": ["axis"],
"chol_corr": ["axis", "axis_bis"]}},
)
The error is:
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Traceback (most recent call last):
File ~/InstProg/anaconda3/envs/myenv/lib/python3.10/site-packages/spyder_kernels/py3compat.py:356 in compat_exec
exec(code, globals, locals)
File ~/Bureau/detector_work/IPC/_bug_test.py:49
idata = pm.sample(initvals=init_pts,
File ~/InstProg/anaconda3/envs/myenv/lib/python3.10/site-packages/pymc/sampling/mcmc.py:682 in sample
initial_points, step = init_nuts(
File ~/InstProg/anaconda3/envs/myenv/lib/python3.10/site-packages/pymc/sampling/mcmc.py:1327 in init_nuts
initial_points = _init_jitter(
File ~/InstProg/anaconda3/envs/myenv/lib/python3.10/site-packages/pymc/sampling/mcmc.py:1204 in _init_jitter
ipfns = make_initial_point_fns_per_chain(
File ~/InstProg/anaconda3/envs/myenv/lib/python3.10/site-packages/pymc/initial_point.py:86 in make_initial_point_fns_per_chain
make_initial_point_fn(
File ~/InstProg/anaconda3/envs/myenv/lib/python3.10/site-packages/pymc/initial_point.py:134 in make_initial_point_fn
sdict_overrides = convert_str_to_rv_dict(model, overrides or {})
File ~/InstProg/anaconda3/envs/myenv/lib/python3.10/site-packages/pymc/initial_point.py:47 in convert_str_to_rv_dict
initvals[rv] = model.rvs_to_transforms[rv].backward(initval, *rv.owner.inputs)
File ~/InstProg/anaconda3/envs/myenv/lib/python3.10/site-packages/pymc/distributions/transforms.py:157 in backward
return pt.set_subtensor(value[..., self.diag_idxs], pt.exp(value[..., self.diag_idxs]))
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
I hope I haven’t made any stupid mistakes in this code sample and that I won’t waste your time.
((by the way, I think that in the example mentioned - which is extremely useful for a project I’m working on! - the rng_seeder
parameter passed as an input parameter to pm.Model
is obsolete in the latest versions of PyMC.))
Thank you for your help!