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

Enable passing Y as a SharedVariable to pm.Bart #202

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

AlexAndorra
Copy link
Contributor

Ready for review 🍾

This PR solves the following issue by adding a check in rng_fn of whether Y is a SharedVariable:

# Generate synthetic data
n_samples = 1000
n_features = 10

# Generate feature matrix X with some correlations
X = rng.normal(0, 1, size=(n_samples, n_features))
# Add some correlation between features
X[:, 1] = 0.6 * X[:, 0] + 0.4 * rng.normal(0, 1, n_samples)
X[:, 2] = -0.3 * X[:, 0] + 0.7 * rng.normal(0, 1, n_samples)

# Generate true relationship (non-linear with interactions)
true_y = (
    np.sin(X[:, 0])
    + 0.5 * X[:, 1] ** 2
    + 0.2 * X[:, 0] * X[:, 1]
    + 0.3 * np.exp(X[:, 2])
    + 0.1 * X[:, 3]
)

# Add noise
noise = rng.normal(0, 0.2, n_samples)
y = true_y + noise

with pm.Model() as bart_model:
    X_data = pm.Data("X", X)
    y_data = pm.Data("y", y)

    mu = pmb.BART("mu", X=X_data, Y=y_data, m=50)
    sigma = pm.Exponential("noise_obs", 1)

    _ = pm.Normal(
        "y_obs",
        mu=mu,
        sigma=sigma,
        observed=y_data,
    )

with bart_model:
    idata = pm.sample_prior_predictive()
    idata.extend(pm.sample())
    idata.extend(pm.sample_posterior_predictive(idata))
TypeError: expected a sequence of integers or a single integer, got 'Subtensor{i}.0'
Apply node that caused the error: BART_rv{"(m,n),(m),(),(),() -> (m)"}(RNG(<Generator(PCG64) at 0x70ABEF69B140>), [], X, y, 50, 0.95, 2.0)
Toposort index: 0
Inputs types: [RandomGeneratorType, TensorType(int64, shape=(0,)), TensorType(float64, shape=(None, None)), TensorType(float64, shape=(None,)), TensorType(int8, shape=()), TensorType(float64, shape=()), TensorType(float32, shape=())]
Inputs shapes: ['No shapes', (0,), (24413, 11), (24413,), (), (), ()]
Inputs strides: ['No strides', (0,), (8, 195304), (8,), (), (), ()]
Inputs values: [Generator(PCG64) at 0x70ABEF69B140, array([], dtype=int64), 'not shown', 'not shown', array(50, dtype=int8), array(0.95), array(2., dtype=float32)]
Outputs clients: [[output[3](BART_rv{"(m,n),(m),(),(),() -> (m)"}.0)], [output[0](mu), normal_rv{"(),()->()"}(RNG(<Generator(PCG64) at 0x70ABEF69B220>), MakeVector{dtype='int64'}.0, mu, ExpandDims{axis=0}.0)]]

@AlexAndorra AlexAndorra added the bug Something isn't working label Dec 4, 2024
@AlexAndorra AlexAndorra self-assigned this Dec 4, 2024
@aloctavodia aloctavodia merged commit 3364ed3 into main Dec 5, 2024
4 checks passed
@aloctavodia aloctavodia deleted the enable-y-share-var branch December 5, 2024 00:22
@aloctavodia
Copy link
Member

Thanks @AlexAndorra!

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

Successfully merging this pull request may close these issues.

2 participants