Deterministic RVs with slicing (converting from pymc 2 to 3)

I’m trying to convert a model from Bayesian Methods For Hackers Ch. 1 from pymc 2 to 3. The relevant piece of code in the original is:

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

observation = pm.Poisson("obs", lambda_, value=count_data, observed=True)

I’m not sure how to convert this to pymc3. I can perform “basic” deterministic math on RVs directly (because of operator overloading), but it doesn’t look like slicing was overloaded, so if I write:

  out = np.zeros(n_count_data)
  out[:tau] = lambda_1

I get ‘TypeError: slice indices must be integers or None or have an index method’

Advice welcome.



Ah, I found my own answer (it was easy to find once I saw that BM4H exists in both pymc2 and pymc3 versions). The relevant bit is pm.math.switch.

Welcome Rif :wink: I am glad that you finally get your hand dirty and play with some PyMC3.