Skip to content

AttributeError when sampling with PyMC model using conditional probabilities #7667

@ricardoV94

Description

@ricardoV94
Member

Discussed in #7618

Originally posted by yk4978 December 18, 2024

I am encountering an error when trying to sample from a PyMC model. In the model, I am defining a set of Bernoulli variables with conditional probabilities based on another parent Bernoulli variable A. The model definition itself works fine, but when I attempt to sample and calculate the posterior, I get the following error:
AttributeError: 'Scratchpad' object has no attribute 'ufunc'

Here is the code I am using:

import pymc as pm

n = 40
with pm.Model() as model:
    A = pm.Bernoulli('A', p=0.001)
    for i in range(n):
        if i < n // 2:
            pm.Bernoulli(f"A{i}", p=pm.math.switch(A, 0.6, 0.01), observed=1)
        else:
            pm.Bernoulli(f"A{i}", p=pm.math.switch(A, 0.6, 0.01))
    trace = pm.sample(1000, return_inferencedata=False, cores=1)

probs = sum(trace['A']) / len(trace['A'])
print(f"probs: {probs}")

Can anyone help me identify what might be causing this error and suggest a possible solution?

Activity

ricardoV94

ricardoV94 commented on Jan 31, 2025

@ricardoV94
MemberAuthor

@yk4978 I've seen that when PyTensor fails to fuse long graphs. Did you see any warnings prior to the failure?

How did you install pymc? And can you try on the latest version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ricardoV94

        Issue actions

          AttributeError when sampling with PyMC model using conditional probabilities · Issue #7667 · pymc-devs/pymc