`sample_posterior_predictive` fails for one-row X and for multi-output BART

Open
#296 0 comments 0 reactions 0 assignees View on GitHub

@RaphaelFakhri is already working on this.

Since Nov 5, 2026.

  • #297 by @RaphaelFakhri — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python

Research direction

Start by reading BARTRV.rng_fn and _sample_posterior, which the issue identifies as involved in the multi-output failure; then trace how the one-row prediction passes through pred.squeeze().T. Reproduce both cases with the provided examples and check the shapes returned for mu and w. Done when posterior predictive sampling succeeds for both cases and returns the expected shapes.

Written by the indexing model from the issue text.

Description

Describe the bug
Since 0.13.0, resampling a BART variable in pm.sample_posterior_predictive fails in two cases:

  • A 1D BART variable with new data that has a single row raises ValueError: Vectorized input 0 has an incompatible shape in axis 0.
  • A multi-output BART variable (for example, shape=(2, n) or a categorical model with shape=(n_classes, n)) raises TypeError: 'numpy.int64' object is not iterable for any number of rows.

BARTRV.rng_fn passes size[0] (the number of outputs) to _sample_posterior as the number of posterior draws. _sample_posterior only accepts a Python int, so the NumPy integer fails. Before the Rust sampler, the same value was the number of outputs and one draw was taken. In the 1D case, pred.squeeze().T also drops the observation axis when X has one row, so the result has shape () instead of (1,).

To Reproduce

import numpy as np
import pymc as pm
import pymc_bart as pmb

rng = np.random.default_rng(0)
X = rng.normal(size=(50, 2))
Y = X[:, 0] + rng.normal(scale=0.1, size=50)

# 1D BART, predict one new row
with pm.Model() as model:
    data_X = pm.Data("data_X", X)
    mu = pmb.BART("mu", data_X, Y, m=10)
    pm.Normal("y", mu, 0.1, observed=Y, shape=data_X.shape[0])
    idata = pm.sample(tune=50, draws=10, chains=2, random_seed=1)
    pm.set_data({"data_X": X[:1]})
    pm.sample_posterior_predictive(idata, sample_vars=["mu", "y"])
    # ValueError: Vectorized input 0 has an incompatible shape in axis 0.

# Multi-output BART, predict three new rows
with pm.Model() as model:
    data_X = pm.Data("data_X", X)
    w = pmb.BART("w", data_X, Y, m=10, shape=(2, data_X.shape[0]))
    pm.Normal("y", w[0], pm.math.abs(w[1]), observed=Y, shape=data_X.shape[0])
    idata = pm.sample(tune=50, draws=10, chains=2, random_seed=1)
    pm.set_data({"data_X": X[:3]})
    pm.sample_posterior_predictive(idata, sample_vars=["w", "y"])
    # TypeError: 'numpy.int64' object is not iterable

Expected behavior
mu has shape (chain, draw, 1) in the first case and w has shape (chain, draw, 2, 3) in the second, as with more rows or a 1D variable.

Additional context
pymc-bart main (0.13.1), bartrs 0.4.x, PyMC 6.4.0, Python 3.13, Linux x86_64.

Dominant language
Python
Stars
160
Forks
36
Avg merge
6d 2h
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pymc-devs/pymc-bart

All issues in pymc-devs/pymc-bart

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.