Skip to content

pymc3 multinomial mixture gets stuck #3069

Closed
@antjoseme

Description

@antjoseme

I am trying use PYMC3 to implement an example where the data comes from a mixture of multinomials. The goal is to infer the underlying state_prob vector (see below). The code runs, but the Metropolis sampler gets stuck at the initial state_prior vector. Also, for some reason I have not been able to get NUTS to work.

import numpy as np
from pymc3 import Model, Multinomial, Dirichlet, Uniform
import pymc3 as pm
import theano.tensor as tt

N = 20
Nstates = 3
Nlabel = Nstates
state_prob = np.array([0.3, 0.1, 0.6]) #need to infer this
state_data = np.random.choice(Nstates, N, p=state_prob)
state_label_tran = np.array([[0.3, 0.2, 0.5],
                               [0.1, 0.5, 0.4],
                               [0.0, 0.05, 0.95]])
label_prob_given_state = state_label_tran[state_data]
def rand_mult(row_p):
    return np.random.multinomial(1, row_p)
label_data = np.apply_along_axis(rand_mult, 1, label_prob_given_state)
#label_data = np.reshape(label_data, (-1,N))[0]
print 'Unobserved state data'
print state_data
print 'Observed label data'
print label_data

with Model() as simple_dag:
    state = Dirichlet('state', np.array([1.1, 1.1, 1.1]), shape=3)
    label_dist = [pm.Multinomial.dist(p=state_label_tran[i], n=1, shape=3) for i in range(3)]
    label = pm.Mixture('label', w=state, comp_dists=label_dist, observed=label_data)
    
with simple_dag:
    trace = pm.sample(1000, cores=2, chains=2, tune=1000, progressbar=True)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions