Arbitrary Deterministic variable

Hi Team, apologies if this question was already asked but all the related discussions I have found made use of Theano tensors and I am not sure that this is still the recommended approach.

Question is how can I construct ad-hoc Deterministic variable where the calculation uses functions not directly available in pymc.math.

In my case I am just trying to evaluate a Deterministic bincount variable:


import pymc as pm
import numpy as np

def bincount(input):
    return np.bincount(input.eval())

with pm.Model() as model:
    p = pm.Uniform('p', lower=0, upper=1)
    samples = pm.Bernoulli('samples_0', p=p, size=100)

    sum_value = pm.Deterministic('sum_value', pm.math.sum(samples))
    bin_count_value = pm.Deterministic('bin_count_value', bincount(samples))

→ sum_value works well, but bin_count_value breaks with error message:
var = var.copy(model.name_for(name))
ValueError: order must be one of ‘C’, ‘F’, ‘A’, or ‘K’ (got ‘bin_count_value’)

Thank you

Can you use pytensor’s bincount?

Thanks yes that would work in that case, I wasn’t aware of the extra_ops.
Is there an alternative to this approach for more complicated calculation? I.e. should we always find a way to express the calculation in the form of a sequence of pytensor operations?

Yes. This shouldn’t be too bad though, most of the numpy functions exist with the very same name in pytensor

Got it thank you!