Skip to content

AttributeError when sampling with PyMC model using conditional probabilities #7667

Open
@ricardoV94

Description

@ricardoV94

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?

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

    Issue actions