简体   繁体   English

使用 PyMC3 进行增量模型更新

[英]Incremental model update with PyMC3

Is it possible to incrementally update a model in pyMC3.是否可以在 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?这在pyMC3中可能吗? Where can I find info in this?我在哪里可以找到这方面的信息?

Thank you :)谢谢:)

Following @ChrisFonnesbeck's advice, I wrote a small tutorial notebook about incremental prior updating.按照@ChrisFonnesbeck 的建议,我写了一个关于增量更新前的小型教程笔记本。 It can be found here:可以在这里找到:

https://github.com/pymc-devs/pymc3/blob/master/docs/source/notebooks/updating_priors.ipynb 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.基本上,您需要将后验样本包装在一个自定义的连续类中,该类从它们计算 KDE。 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:然后,您通过使用参数名称和来自上一次迭代后验样本的跟踪样本调用from_posterior函数来定义模型参数的先验(比如alpha ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM