pymc-devs / pymc-examples

Examples of PyMC models, including a library of Jupyter notebooks.
https://www.pymc.io/projects/examples/en/latest/
MIT License
280 stars 243 forks source link

nb: case studies, spline (error) #514

Open reshamas opened 1 year ago

reshamas commented 1 year ago

Notebook title: Splines Notebook url: https://www.pymc.io/projects/examples/en/latest/case_studies/spline.html

Issue description

Code:

COORDS = {"splines": np.arange(B.shape[1])}
with pm.Model(coords=COORDS) as spline_model:
    a = pm.Normal("a", 100, 5)
    w = pm.Normal("w", mu=0, sigma=3, size=B.shape[1], dims="splines")
    mu = pm.Deterministic("mu", a + pm.math.dot(np.asarray(B, order="F"), w.T))
    sigma = pm.Exponential("sigma", 1)
    D = pm.Normal("D", mu=mu, sigma=sigma, observed=blossom_data.doy, dims="obs")

Error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[13], line 7
      5 mu = pm.Deterministic("mu", a + pm.math.dot(np.asarray(B, order="F"), w.T))
      6 sigma = pm.Exponential("sigma", 1)
----> 7 D = pm.Normal("D", mu=mu, sigma=sigma, observed=blossom_data.doy, dims="obs")

File ~/miniforge3/envs/pymc-dev/lib/python3.11/site-packages/pymc/distributions/distribution.py:304, in Distribution.__new__(cls, name, rng, dims, initval, observed, total_size, transform, *args, **kwargs)
    302 if kwargs.get("size") is None and kwargs.get("shape") is None:
    303     if dims is not None:
--> 304         kwargs["shape"] = shape_from_dims(dims, model)
    305     elif observed is not None:
    306         kwargs["shape"] = tuple(observed.shape)

File ~/miniforge3/envs/pymc-dev/lib/python3.11/site-packages/pymc/distributions/shape_utils.py:507, in shape_from_dims(dims, model)
    505 unknowndim_dims = set(dims) - set(model.dim_lengths)
    506 if unknowndim_dims:
--> 507     raise KeyError(
    508         f"Dimensions {unknowndim_dims} are unknown to the model and cannot be used to specify a `shape`."
    509     )
    511 return tuple(model.dim_lengths[dname] for dname in dims)

KeyError: "Dimensions {'obs'} are unknown to the model and cannot be used to specify a `shape`."

Note that this issue tracker is about the contents in the notebooks, if the notebook is instead triggering a bug or error in pymc, please report to https://github.com/pymc-devs/pymc/issues instead

Expected output

If applicable, describe what should happen instead.

Proposed solution

If applicable, explain possible solutions and workarounds.

OriolAbril commented 1 year ago

I think the pteferred fix would be adding B as constant data and label the dimensions there (so we wouldn't even need coords anymore).