Pergunta

I am attempting to port the gaussian mixture model as defined in: How to model a mixture of 3 Normals in PyMC? over to pymc3

Code

import numpy as np
from pymc import Model, Gamma, Normal, Dirichlet
from pymc import Multinomial
from pymc import sample, Metropolis

k = 3
ndata = 500

v = np.random.randint(0, k, ndata)
data = ((v == 0)*(50 + np.random.randn(ndata))
        + (v == 1)*(-50 + np.random.randn(ndata))
        + (v == 2)*np.random.randn(ndata))

model = Model()

with model:
    dd = Dirichlet('dd', k=k, a=1, shape=k)
    precs = Gamma('precs', alpha=0.1, beta=0.1, shape=k)
    means = Normal('means', 0, 0.001, shape=k)
    category = Multinomial('category',
                           n=1,
                           p=dd,
                           shape=ndata)

    points = Normal('obs',
                    means[category],
                    precs[category],
                    observed=data)
    tr = sample(3000, step=Metropolis())

I'm getting the following code error:

AttributeError: <pymc.quickclass.Multinomial object at 0x4804210> has no default value to use, checked for: ['mode'] pass testval argument or provide one of these.

What am I doing wrong?

Foi útil?

Solução

This is because there were no initial values passed for the variables in the model. Usually this is no problem because the model just takes the mean/median/mode of each distribution and uses those. The multinomial is difficult because the mean usually gives values outside the support (i.e. non-integer values) and the mode is difficult to compute.

The short-term solution is to supply initial values at least for the multinomial. I will file an issue on the bug tracker for this to figure out what to do long-term.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top