使用PyMC3进行增量模型更新

使用PyMC3进行增量模型更新

本文介绍了使用PyMC3进行增量模型更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在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.

但是据我了解,贝叶斯模型也意味着能够更新信念.在pyMC3中可以吗?我在哪里可以找到信息?

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?

谢谢:)

推荐答案

按照@ChrisFonnesbeck的建议,我写了一个有关增量优先更新的小型教程笔记本.可以在这里找到:

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

基本上,您需要将后验样本包装到自定义的Continuous类中,该类从它们中计算出KDE.下面的代码就是这样做的:

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

然后,通过调用带有参数名称和前一次迭代后的跟踪样本的from_posterior函数来定义模型参数的先验(例如alpha):

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

这篇关于使用PyMC3进行增量模型更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:23