1

我正在尝试使用 pymc3 创建一个相对简单的分层贝叶斯模型。但是,我不断收到错误消息。代码是:

import numpy as np
import pymc3 as pm

# Example data.
ncond = 4
nSubj = 40
trials = 64

N = np.repeat([trials], (ncond * nSubj))
z = np.array([45, 63, 58, 64, 58, 63, 51, 60, 59, 47, 63, 61, 60, 51, 59, 45,
61, 59, 60, 58, 63, 56, 63, 64, 64, 60, 64, 62, 49, 64, 64, 58, 64, 52, 64, 64,
64, 62, 64, 61, 59, 59, 55, 62, 51, 58, 55, 54, 59, 57, 58, 60, 54, 42, 59, 57,
59, 53, 53, 42, 59, 57, 29, 36, 51, 64, 60, 54, 54, 38, 61, 60, 61, 60, 62, 55,
38, 43, 58, 60, 44, 44, 32, 56, 43, 36, 38, 48, 32, 40, 40, 34, 45, 42, 41, 32,
48, 36, 29, 37, 53, 55, 50, 47, 46, 44, 50, 56, 58, 42, 58, 54, 57, 54, 51, 49,
52, 51, 49, 51, 46, 46, 42, 49, 46, 56, 42, 53, 55, 51, 55, 49, 53, 55, 40, 46,
56, 47, 54, 54, 42, 34, 35, 41, 48, 46, 39, 55, 30, 49, 27, 51, 41, 36, 45, 41,
53, 32, 43, 33])
condition = np.repeat([0,1,2,3], nSubj)


# Specify the model in PyMC
with pm.Model() as model:
    # define the hyperparameters
    kappa = pm.Gamma('kappa', 1, 0.1)
    mu = pm.Beta('mu', 1, 1, shape=ncond)
    # define the prior
    theta = pm.Beta('theta', mu[condition] * kappa, (1 - mu[condition]) * kappa, shape=len(z))
    # define the likelihood
    y = pm.Binomial('y', p=theta, n=N, observed=z)
    start = pm.find_MAP()
    step1 = pm.Metropolis([mu])
    step2 = pm.Metropolis([theta])
    step3 = pm.NUTS([kappa])
#    samplers = [pm.Metropolis([rv]) for rv in model.unobserved_RVs]
    trace = pm.sample(10000, [step1, step2, step3], start=start, progressbar=False)

但是我一直收到关于 find_MAP() 函数的错误。谁能向我解释这个错误?

    ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-c4e40b20fd5c> in <module>()
     38     # define the likelihood
     39     y = pm.Binomial('y', p=theta, n=N, observed=z)
---> 40     start = pm.find_MAP()
     41     step1 = pm.Metropolis([mu])
     42     step2 = pm.Metropolis([theta])

/Library/Python/2.7/site-packages/pymc3/tuning/starting.pyc in find_MAP(start, vars, fmin, return_raw, disp, model, *args, **kwargs)
    124                          "density. 2) your distribution logp's are " +
    125                          "properly specified. Specific issues: \n" +
--> 126                          specific_errors)
    127     mx = {v.name: np.floor(mx[v.name]) if v.dtype in discrete_types else
    128           mx[v.name] for v in model.vars}

ValueError: Optimization error: max, logp or dlogp at max have non-finite values. Some values may be outside of distribution support. max: {'mu': array([ 14.66440283,  67.64627608,  36.73991444,  -2.27146408]), 'theta': array([ -3.28858534e+02,   1.49945045e+02,  -7.02150417e+01,
         9.97352990e-01,  -7.02150417e+01,   1.49945045e+02,
        -1.50195789e+02,   5.63428376e+00,  -9.29002815e+01,
        -2.70445580e+02,   1.49945045e+02,   1.03986058e+02,
         5.63428376e+00,  -1.50195789e+02,  -9.29002815e+01,
        -3.28858534e+02,   1.03986058e+02,  -9.29002815e+01,
         5.63428376e+00,  -7.02150417e+01,   1.49945045e+02,
        -5.57301509e+01,   1.49945045e+02,   1.01682005e+00,
         9.62368437e-01,   5.63428376e+00,   9.95206871e-01,
         5.88827819e+01,  -2.08511670e+02,  -7.51688112e+00,
         9.54524715e+00,  -7.02150417e+01,   1.02401342e+00,
        -1.24386722e+02,   1.00553699e+00,   1.00113792e+00,
         1.05862921e+00,   5.88827819e+01,   9.96530636e-01,
         1.03986058e+02,  -1.66473577e+02,  -1.66473577e+02,
         1.21070195e+02,   7.06961277e+01,   1.51469750e+02,
        -1.01158381e+02,   1.21070195e+02,   1.47085125e+02,
        -1.66473577e+02,  -5.55202522e-01,  -1.01158381e+02,
         5.37160225e+01,   1.47085125e+02,  -3.00112781e+01,
        -1.66473577e+02,  -5.55202522e-01,  -1.66473577e+02,
         1.58340576e+02,   1.58340575e+02,  -3.00112781e+01,
        -1.66473577e+02,  -5.55202521e-01,  -1.87830813e+01,
        -7.63325604e+01,   1.51469750e+02,   6.56687525e+02,
         5.37160224e+01,   1.47085125e+02,   1.47085125e+02,
        -7.30815598e+01,   5.20052822e+02,   5.37160224e+01,
         5.20052822e+02,   5.37160224e+01,   7.06961278e+01,
         1.21070195e+02,  -7.30815598e+01,  -1.20799887e+01,
        -1.01158381e+02,   5.37160224e+01,   2.55349034e+01,
         2.55349034e+01,  -1.42715299e+02,   3.77924839e+01,
         4.84747986e+00,  -1.07962425e+02,  -8.33028412e+01,
         1.00157336e+02,  -1.42715299e+02,  -5.22680519e+01,
        -5.22680519e+01,  -1.27123074e+02,   4.61912845e+01,
        -1.52905863e+01,  -3.44347435e+01,  -1.42715299e+02,
         1.00157336e+02,  -1.07962425e+02,  -1.65836239e+02,
        -9.64018880e+01,   1.06636770e+02,   6.59333401e+01,
         1.19873182e+02,   8.44063494e+01,   6.60981540e+01,
         2.55349034e+01,   1.19873182e+02,   3.77924839e+01,
        -3.45990714e+01,  -1.52905863e+01,  -3.45990714e+01,
         8.91977361e+01,   5.07494024e+00,   8.91977361e+01,
         1.21864768e+02,   1.12323534e+02,   1.17572851e+02,
         1.21864768e+02,   1.12323534e+02,   1.21864768e+02,
         6.29159569e+01,   6.29159569e+01,  -1.64948241e+01,
         1.05932671e+02,   6.29159569e+01,   1.85689051e+01,
        -1.64948241e+01,   9.30151996e+01,   4.81456147e+01,
         1.12276366e+02,   4.81456147e+01,   1.05932671e+02,
         9.30151996e+01,   4.81456147e+01,  -5.32061084e+01,
         6.29159569e+01,   1.85689051e+01,   8.03463229e+01,
         7.34110211e+01,   7.34110211e+01,  -1.64948241e+01,
        -1.29160807e+02,  -1.19831995e+02,  -3.54559256e+01,
         9.50334978e+01,   6.29159569e+01,  -6.95343458e+01,
         4.81456147e+01,  -1.61402820e+02,   1.05932671e+02,
        -1.92150490e+02,   1.12276366e+02,  -3.54559256e+01,
        -1.09378401e+02,   4.37180227e+01,  -3.54559256e+01,
         9.30151996e+01,  -1.45569868e+02,   3.34881430e+00,
        -1.37632194e+02]), 'kappa': array(1399.2675885517883)} logp: array(-inf) dlogp: array([         nan,          nan,          nan,          nan,
                nan,  -0.        ,   0.        ,  -0.        ,
        64.16985826,  -0.        ,   0.        ,  -0.        ,
         0.        ,  -0.        ,  -0.        ,   0.        ,
         0.        ,   0.        ,  -0.        ,  -0.        ,
        -0.        ,   0.        ,  -0.        ,   0.        ,
        -0.        ,   0.        ,  -0.        ,   0.        ,
         0.        ,  66.50259666,   0.        ,  64.30823767,
         0.        ,  -0.        ,  -0.        ,   0.        ,
        -0.        ,   0.        ,  -0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,  64.22281229,
         0.        ,  -0.        ,  -0.        ,   0.        ,
         0.        ,   0.        ,  -0.        ,   0.        ,
         0.        ,  -0.        ,  -0.        ,  -0.        ,
         0.        ,   0.        ,  -0.        ,  -0.        ,
        -0.        ,  -0.        ,   0.        ,   0.        ,
        -0.        ,  -0.        ,  -0.        ,  -0.        ,
        -0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,  -0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,  -0.        ,  -0.        ,  -0.        ,
         0.        ,   0.        ,   0.        ,  -0.        ,
         0.        ,   0.        ,  -0.        ,  -0.        ,
         0.        ,  -0.        ,  -0.        ,  -0.        ,
        -0.        ,   0.        ,  -0.        ,  -0.        ,
        -0.        ,   0.        ,  -0.        ,  -0.        ,
        -0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,  -0.        ,  -0.        ,  -0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,   0.        ])Check that 1) you don't have hierarchical parameters, these will lead to points with infinite density. 2) your distribution logp's are properly specified. Specific issues: 
mu.logp bad: -inf
theta.dlogp bad at idx: (array([160, 161, 162, 163, 164]),) with values: [ nan  nan  nan  nan  nan]
theta.logp bad: -inf
4

1 回答 1

2

该错误意味着优化算法已完成,但返回的值没有任何意义。通常这是因为最大值没有很好地定义。

但是,这实际上对我来说很好。你有什么版本?

我有 python 2.7、最新的 pymc3、theano 0.7.0、scipy 0.13

于 2015-05-23T04:12:15.397 回答