pymc-devs / pymc

Bayesian Modeling and Probabilistic Programming in Python
https://docs.pymc.io/
Other
8.64k stars 1.99k forks source link

Introductory example for Arbitrary Distributions #6279

Open M-Wieler opened 1 year ago

M-Wieler commented 1 year ago

Arbitrary distributions: (https://docs.pymc.io/en/latest/learn/core_notebooks/pymc_overview.html):

Issue description

I just copied the following code from the web to try it out,

import pymc as pm
import aesara.tensor as at

class BetaRV(at.random.op.RandomVariable):
    name = "beta"
    ndim_supp = 0
    ndims_params = []
    dtype = "floatX"

    @classmethod
    def rng_fn(cls, rng, size):
        raise NotImplementedError("Cannot sample from beta variable")

beta = BetaRV()

class Beta(pm.Continuous):

    rv_op = beta

    @classmethod
    def dist(cls, mu=0, **kwargs):
        mu = at.as_tensor_variable(mu)
        return super().dist([mu], **kwargs)

    def logp(self, value):
        mu = self.mu
        return beta_logp(value - mu)

def beta_logp(value):
    return -1.5 * at.log(1 + (value) ** 2)

but when trying to sample from this model with

with pm.Model() as model:
    beta = Beta("beta", mu=0)
    data = pm.sample()

I get the error:

Traceback (most recent call last):

  File "C:\Users\matth\AppData\Local\Temp\ipykernel_6768\417332797.py", line 3, in <cell line: 1>
    data = pm.sample()

  File "C:\Users\matth\AppData\Roaming\Python\Python39\site-packages\pymc\sampling.py", line 535, in sample
    step = assign_step_methods(model, step, methods=pm.STEP_METHODS, step_kwargs=kwargs)

  File "C:\Users\matth\AppData\Roaming\Python\Python39\site-packages\pymc\sampling.py", line 207, in assign_step_methods
    model_logp = model.logp()

  File "C:\Users\matth\AppData\Roaming\Python\Python39\site-packages\pymc\model.py", line 744, in logp
    rv_logps = joint_logp(list(rv_values.keys()), rv_values, sum=False, jacobian=jacobian)

  File "C:\Users\matth\AppData\Roaming\Python\Python39\site-packages\pymc\distributions\logprob.py", line 237, in joint_logp
    temp_logp_var_dict = factorized_joint_logprob(

  File "C:\Users\matth\AppData\Roaming\Python\Python39\site-packages\aeppl\joint_logprob.py", line 151, in factorized_joint_logprob
    q_logprob_vars = _logprob(

  File "C:\Users\matth\miniconda3\envs\sipy_local\lib\functools.py", line 888, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)

  File "C:\Users\matth\AppData\Roaming\Python\Python39\site-packages\pymc\distributions\distribution.py", line 123, in logp
    return class_logp(value, *dist_params)

  File "C:\Users\matth\AppData\Local\Temp\ipykernel_6768\880349476.py", line 24, in logp
    mu = self.mu

AttributeError: 'TensorVariable' object has no attribute 'mu'

Proposed solution

It seems like to logp function does not get a class instance as first input self. Any help is welcome.

OriolAbril commented 1 year ago

Thanks for reporting, tagging @ricardoV94 @fonnesbeck

I'll also move the issue to pymc repo where this notebook is stored