首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用具有自定义对数概率的MCMC及其矩阵的求解

如何使用具有自定义对数概率的MCMC及其矩阵的求解
EN

Stack Overflow用户
提问于 2019-03-27 04:34:16
回答 1查看 67关注 0票数 1

代码是用PyMC3编写的,但这是一个一般性问题。我想找出哪个矩阵(变量的组合)给我的概率最高。取每个元素的迹线的平均值是没有意义的,因为它们相互依赖。

下面是一个简单的例子;为简单起见,代码使用向量而不是矩阵。目标是找到一个长度为2的向量,其中每个值都在0和1之间,因此总和为1。

代码语言:javascript
复制
import numpy as np
import theano
import theano.tensor as tt
import pymc3 as mc

# define a theano Op for our likelihood function
class LogLike_Matrix(tt.Op):
    itypes = [tt.dvector] # expects a vector of parameter values when called
    otypes = [tt.dscalar] # outputs a single scalar value (the log likelihood)

    def __init__(self, loglike):
        self.likelihood = loglike        # the log-p function

    def perform(self, node, inputs, outputs):
        # the method that is used when calling the Op
        theta, = inputs  # this will contain my variables

        # call the log-likelihood function
        logl = self.likelihood(theta)

        outputs[0][0] = np.array(logl) # output the log-likelihood

def logLikelihood_Matrix(data):
    """
        We want sum(data) = 1
    """
    p = 1-np.abs(np.sum(data)-1)
    return np.log(p)

logl_matrix = LogLike_Matrix(logLikelihood_Matrix)

# use PyMC3 to sampler from log-likelihood
with mc.Model():
    """
        Data will be sampled randomly with uniform distribution
        because the log-p doesn't work on it
    """
    data_matrix = mc.Uniform('data_matrix', shape=(2), lower=0.0, upper=1.0)

    # convert m and c to a tensor vector
    theta = tt.as_tensor_variable(data_matrix)

    # use a DensityDist (use a lamdba function to "call" the Op)
    mc.DensityDist('likelihood_matrix', lambda v: logl_matrix(v), observed={'v': theta})

    trace_matrix = mc.sample(5000, tune=100, discard_tuned_samples=True)
EN

回答 1

Stack Overflow用户

发布于 2019-04-02 01:05:33

如果您只需要最高似然参数值,那么您需要最大后验概率(MAP)估计,该估计可以使用pymc3.find_MAP()获得(有关方法的详细信息,请参阅starting.py )。如果你期望多模态后验,那么你可能需要用不同的初始化重复运行它,并选择获得最大logp值的那个,但这仍然只会增加找到全局最优的机会,尽管不能保证它。

应该注意,在高参数维度,MAP估计通常不是典型集合的一部分,即,它不代表将导致观测数据的典型参数值。Michael Betancourt在A Conceptual Introduction to Hamiltonian Monte Carlo中讨论了这一点。完全贝叶斯方法是使用posterior predictive distributions,它有效地平均所有高似然参数配置,而不是使用参数的单点估计。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55365787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档