Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laplace approximation not handling non-scalar parameters #376

Open
dar326 opened this issue Sep 3, 2024 · 5 comments
Open

Laplace approximation not handling non-scalar parameters #376

dar326 opened this issue Sep 3, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@dar326
Copy link

dar326 commented Sep 3, 2024

I have been trying to utilize the new laplace function for estimating the posterior distribution using a quadratic approximation. It seems that there is an issue when trying to use non-scalar parameters. This is a slight modification of the example that is provided in the documentation for the function:

y = np.array([2642, 3503, 4358]*10)
cat = np.array([0]*15 + [1]*15)
with pm.Model() as m:
    logsigma = pm.Uniform("logsigma", 1, 100)
    mu = pm.Uniform("mu", -10000, 10000, shape=2)
    yobs = pm.Normal("y", mu=mu[cat], sigma=pm.math.exp(logsigma), observed=y)
    idata = laplace([mu, logsigma], model=m)

The following error is generated:

TypeError: ('Wrong number of dimensions: expected 1, got 0 with shape ().', 'Container name "mu"')

Interestingly, this error occurs even when the shape is not greater than 1 and no indexing is performed:

y = np.array([2642, 3503, 4358]*10)
with pm.Model() as m:
    logsigma = pm.Uniform("logsigma", 1, 100)
    mu = pm.Uniform("mu", -10000, 10000, shape=1)
    yobs = pm.Normal("y", mu=mu, sigma=pm.math.exp(logsigma), observed=y)
    idata = laplace([mu, logsigma], model=m)

This seems to indicate that there is an issue with setting the shape parameter to any value.

@ricardoV94 ricardoV94 added the bug Something isn't working label Oct 4, 2024
@ricardoV94
Copy link
Member

ricardoV94 commented Oct 4, 2024

I can confirm this is a bug. The code assumes all parameters to be scalars in multiple places. First this line would fail with 2d parameters:

mean = np.concatenate([np.atleast_1d(map[v.name]) for v in vars])

Which should be instead mean = np.concatenate([np.atleast_1d(map[v.name].ravel()) for v in vars])

Then in this loop we are assuming there is a single entry per variable (with chain, draws batch):

data_vars[str(var)] = xr.DataArray(samples[:, :, i], dims=("chain", "draw"))

This later leads to the error reported above.

Instead we should slice the portion corresponding to each variable, and reshape it to the original shape. There should be some utilities in the PyMC codebase to help with that. As a bonus we may want to recover the core dims, although I think the code would work without them.

@ricardoV94
Copy link
Member

CC @carsten-j

@carsten-j
Copy link
Contributor

@ricardoV94 I will try to take at look at this sometime next week.

@carsten-j
Copy link
Contributor

@ricardoV94 I am not sure that I understand how to fix this bug.

@jessegrabowski
Copy link
Member

I think this can be fixed by the new code in #385

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants