Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

python - Incremental model update with PyMC3

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...