Bayesian Methods for Hackers - Running in PyMC 4

Hi everyone,
While I am progressing in self-educating me about PyMC, I have been using Osvaldo Martin´s Bayesian Analysis with Python -2nd edition book together with Cameron Davidson-Pilon´s book: Bayesian Methods for Hackers which together with some on-line tutorials.

Some limitations I am facing, is most of the code in the books above were written either in PyMC 3 or even in some PyMC2 (for Davidson-Pilon´s book) versions. Anyhow, I am doing my best to translate the codes to PyMC4 after forking the github.

I need a little help with this one, which looks like was writen for PyMC2:

I updated the initialization syntax of the model to a context manager:

with pm.Model() as texting_model:
  lambda_1 = pm.Exponential("lambda_1", alpha)
  lambda_2 = pm.Exponential("lambda_2", alpha)
  tau = pm.DiscreteUniform("tau", lower=0, upper=n_count_data)

but the likelihood is defined through a step function named lambda_ according to:

def lambda_(tau=tau, lambda_1=lambda_1, lambda_2=lambda_2):
    out = np.zeros(n_count_data)
    out[:tau] = lambda_1  # lambda before tau is lambda1
    out[tau:] = lambda_2  # lambda after (and including) tau is lambda2
    return out

When I pass the data to the model through a likelihood, which calls the step function lambda_

with texting_model:
  observation = pm.Poisson("obs", mu=lambda_, value=count_data, observed=True)

PyMC4 return an error as mu cannot be defined by a function:

TypeError: float() argument must be a string or a number, not ‘function’

Help is appreciated here.

1 Like

Is the pymc v3 version of that model found here? It looks like all the chapters are available in both v2 and v3, though I am not sure they are all complete.

1 Like

There’s also a WIP port here for PyMC 4: Shift to the pymc current version by miemiekurisu · Pull Request #548 · CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers · GitHub