Search code examples
pythontime-seriespymc3

How to update observations over time in PyMC3?


I'm in the process of teaching myself PyMC3 and I was replicating this changepoint detection example. I want to extend the example by sampling the posterior of the 2 different Poisson rate parameters over time. How would I go along doing this?

To give a bit more detail: if I have the time-series y from the example. I want to first sample with observations y[0:2], then y[0:3], y[0:4], ... , y[0:100]. My goal is to plot the distribution of mu0 and mu1 (the Poisson rate parameters) over time 2..100. Obviously, I should be able to do this by creating around 98 different models and sampling independently from each of one. However, this seems rather inefficient and I'm wondering if there is a better way of just updating the model with new observations and continuing the trace?

I couldn't find anything related in the documentation or online, so I tried just replacing the variables but then I get a "ValueError: Variable name changepoint already exists.".

TLDR; Is there an efficient way to update observations over time and keep on sampling from the previous trace with these new observations?


Solution

  • Did you try wrapping y with a theano.shared an update the y value at each step? Something like:

    yshared = theano.shared(y[0:2])
    with pm.Model() as m:
        # setup model
        y_obs = pm.SOMEDISTRIBUTION('',..., observed=yshared)
    # inference
    with m:
        trace.append(pm.sample())
    # update value
    yshared.set_value(y[0:3])
    # inference again
    with m:
        trace.append(pm.sample())
    

    If you have more question, please post to https://discourse.pymc.io with your model and (simulated) data. We check and answer on our discourse much more regularly.