Migrating to Pymc4 with example from PYMC docs

Prior and Posterior Predictive Checks — PyMC 4.4.0 documentation
I am a novice starting pymc4. can you help with the following what would be considered a simple example from the pymc docs for learners.

Running the following introductory code for learning about pymc 4 generates an error:
import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pymc as pm
import xarray as xr
from scipy.special import expit as logistic
print(f"Runing on PyMC v{pm.version}")

Runing on PyMC v4.3.0

RANDOM_SEED = 58
rng = np.random.default_rng(RANDOM_SEED)

def standardize(series):
“”“Standardize a pandas series”“”
return (series - series.mean()) / series.std()
Generate some sample data:
N = 100

true_a, true_b, predictor = 0.5, 3.0, rng.normal(loc=2, scale=6, size=N)
true_mu = true_a + true_b * predictor
true_sd = 2.0

outcome = rng.normal(loc=true_mu, scale=true_sd, size=N)

f"{predictor.mean():.2f}, {predictor.std():.2f}, {outcome.mean():.2f}, {outcome.std():.2f}"

predictor_scaled = standardize(predictor)
outcome_scaled = standardize(outcome)

f"{predictor_scaled.mean():.2f}, {predictor_scaled.std():.2f}, {outcome_scaled.mean():.2f}, {outcome_scaled.std():.2f}"

with pm.Model() as model_1:
a = pm.Normal(“a”, 0.0, 0.5)
b = pm.Normal(“b”, 0.0, 1.0)

mu = a + b * predictor_scaled
sigma = pm.Exponential("sigma", 1.0)

pm.Normal("obs", mu=mu, sigma=sigma, observed=outcome_scaled)
pm.sample(1000, tune=2000, random_seed=rng)

az.plot_trace(idata);
pm.model_to_graphviz(model_1)

with model_1:
pm.sample_posterior_predictive(idata)

Generates the following error:
“different number of dimensions on data and dims: 3 vs 2”

Runs with pymc3 but gives above error on pymc4.
why is the error occurring and what exactly in simple terms does it mean?
Is there an easy way to correct dimensions/data conflict in pymc4?
Thanks in advance for your help.
DD

I got your example to work without changing anything - take a look at this Colab notebook and make sure it runs for you.

Also, I’m not sure what pm.version was supposed to do - there isn’t an attribute as such AFAIK. You can check the version number for most packages with the .__version__ attribute.

Thank you for successfully running the code in the Colab notebook. It indicated to me that my recent install of the latest pymc was faulty. I was running PYMC3 and tried to install the new PYMC over it which I suspect caused a conflict. Thus, I uninstalled Anaconda first - then I reinstalled Anaconda and added ‘pip install pymc’ and ran the sample code from the pymc docs (simple regression example for beginners migrating from pymc3) which immediately ran perfectly.
pm.__version__4.4.0

The original code in its entirety can be found here:
[Introductory Overview of PyMC — PyMC dev documentation: (Introductory Overview of PyMC — PyMC dev documentation)

So, I am just left with 2 queries:

  1. How do I, now run, previously created pymc3 code?
    Can I run pymc programs based on Aesare and pymc3 programs based on Theano in a Jupiter notebook. Is this achieved by setting up separate enviroments in Anaconda install or am I over thinking it: with some code like:

conda create -c conda-forge -n pymc_env “pymc>=4”
conda activate pymc_env_4
and then:
conda create -c conda-forge -n pymc_env “pymc>=3”
conda activate pymc_env_3

would this be the correct approach?
2) I am looking forward to converting previously written and tested pymc3 code to pymc but would be greatly helped if I could find easy beginner samples besides the examples given in the official pymc docs: PyMC Example Gallery — PyMC example gallery (PyMC Example Gallery — PyMC example gallery).
I am particularly interested in simple examples to learn from such as: comparing group means or proportions, linear and logistic regression and being able to swap out the data for new data using the pm.data container.
The resources given in the pymc docs, books etc appear to be written for pymc3 or ported to pymc3 from R etc.
Is there any books for the newest pymc or ported to the new pymc. or other beginner git hub notebooks to learn the code for the above topics?
Thank you again for your time and expertise.
DD

Close, but you may want to make sure you give the environments different names. I also think (but others may want to confirm) that you try to install pymc3 instead of “pymc>=3”.

The resources given in the pymc docs, books etc appear to be written for pymc3 or ported to pymc3 from R etc.

Yup, there’s some updates that should go in.

Thanks a million Dd