`sample_posterior_predictive` fails for one-row X and for multi-output BART
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
- Domain
- machine-learning
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 withshape=(n_classes, n)) raisesTypeError: 'numpy.int64' object is not iterablefor 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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pymc-devs/pymc-bart
-
BART does not check that split_prior and split_rules have one entry per column of XPossibly taken @leninworld claimed this 3 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 1/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 4/5 3-5 days Newbie friendliness 68/100
-
Expand Quantile regression exampleMay be free again @kushagra240 claimed this 66 days ago, and no pull request is open. Opendocumentation good first issue
Difficulty 3/5 1-2 days Newbie friendliness 65/100
All issues in pymc-devs/pymc-bart
Similar issues
-
claude-subscription-directsdk: tool calls with raw control characters in string values are droppedOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
NousResearch/hermes-official-plugins#538 ·
Maintainers usually reply within 3 days
-
bug(qdiffusion): EnergyModel.score_visible_logits computes biased visible/hidden on-ratios when solver returns variable solution counts per candidatePossibly taken @houzhongyu claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
qboson/kaiwu-pytorch-plugin#2133 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
MakazhanAlpamys/Soup#3415 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days