9

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 :)

wbadart
  • 2,583
  • 1
  • 13
  • 27
Christian
  • 1,341
  • 1
  • 16
  • 35
  • 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](https://gitter.im/pymc-devs/pymc) – aloctavodia Nov 29 '16 at 17:59
  • 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 Nov 29 '16 at 19:12
  • 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 Nov 30 '16 at 05:59
  • @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 Feb 21 '17 at 22:38
  • 1
    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 Feb 22 '17 at 23:55
  • In my opinion this would be one of the most basic use cases of a Bayesian framework in python. But maybe I just not really have any idea about Bayesians ;) – Christian Feb 23 '17 at 13:04

1 Answers1

13

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'])
David Brochart
  • 825
  • 9
  • 17
  • 2
    the notebook from the answer lives here now: https://github.com/pymc-devs/pymc-examples/blob/main/examples/pymc3_howto/updating_priors.ipynb – Christian Apr 06 '21 at 08:25