8
votes

Is it possible to incrementally update a model in pyMC3. I can currently find no information on this. All documentation is always working with a priori known data.

But in my understanding, a Bayesian model also means being able to update a belief. Is this possible in pyMC3? Where can I find info in this?

Thank you :)

1
AFAIK, this is not implemented in PyMC3. Nevertheless you can do this manually, just use some data and prior, use PyMC3 to update to compute the posterior and then use the posterior as prior. You may want to ask this question here - aloctavodia
No, the major constraint is that after each update, you'd have to convert your posteriors to priors, before incorporating the next batch of data. - Chris Fonnesbeck
To clarify: I want to build a model that predicts when certain events are happening. Whenever an actual event occurs I want to update my belief. Can I do this in pymc3? As far as I understood the library mostly supports MCMC and therefore does not really work with simple Bayesian updates or is it? I basically was trying to use the library because I can build complex models where for example multiple of my events share certain knowledge. - Christian
@ChrisFonnesbeck By "convert the posteriors to priors", do you mean using something like kernel density estimation and wrapping it with a Continuous subclass? - David Brochart
It's not always easy to do the conversion without loss of information, unless its a simple conjugate problem. It would be nice to be able to use a histogram or kde directly as a probability distribution; that would be the easiest, I guess. Note that the kde would inevitably mean loss of information in the transition. - Chris Fonnesbeck

1 Answers

13
votes

Following @ChrisFonnesbeck's advice, I wrote a small tutorial notebook about incremental prior updating. It can be found here:

https://github.com/pymc-devs/pymc3/blob/master/docs/source/notebooks/updating_priors.ipynb

Basically, you need to wrap your posterior samples in a custom Continuous class that computes the KDE from them. The following code does just that:

def from_posterior(param, samples):

    class FromPosterior(Continuous):

        def __init__(self, *args, **kwargs):
            self.logp = logp
            super(FromPosterior, self).__init__(*args, **kwargs)

    smin, smax = np.min(samples), np.max(samples)
    x = np.linspace(smin, smax, 100)
    y = stats.gaussian_kde(samples)(x)
    y0 = np.min(y) / 10 # what was never sampled should have a small probability but not 0

    @as_op(itypes=[tt.dscalar], otypes=[tt.dscalar])
    def logp(value):
        # Interpolates from observed values
        return np.array(np.log(np.interp(value, x, y, left=y0, right=y0)))

    return FromPosterior(param, testval=np.median(samples))

Then you define the prior of your model parameter (say alpha) by calling the from_posterior function with the parameter name and the trace samples from the posterior of the previous iteration:

alpha = from_posterior('alpha', trace['alpha'])