1

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?

Community
  • 1
  • 1
zaxtax
  • 113
  • 3

1 Answers1

3

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.

Chris Fonnesbeck
  • 4,143
  • 4
  • 29
  • 30
  • When I add testval=1 to Multinomial, I get: ValueError: ('Input dimension mis-match. (input[0].shape[0] = 500, input[1].shape[0] = 3)', Elemwise{mul,no_inplace}(category, ), [Elemwise{mul,no_inplace}.0]) – zaxtax Dec 17 '13 at 03:18
  • I don't think an integer is the appropriate test value for a Multinomial (should be a vector of size k). At any rate, this is something we need to fix. – Chris Fonnesbeck Dec 18 '13 at 16:19