I am new to PyMC3 and I have been attempting to create a mixture of independent Poisson's using the following code:
import pymc3 as pm
import numpy as np
from pymc3.distributions.continuous import Uniform
from pymc3.distributions.discrete import Poisson
from pymc3.distributions.mixture import Mixture
from pymc3 import Deterministic
# Bayesian non-parametric histogram comparison
def bayesian_nphist(bins_1, bins_2):
# n equals m anyway
m = len(bins_1)
n = len(bins_2)
with pm.Model():
beta = Uniform('beta', lower=0, upper=np.mean(np.append(bins_1, bins_2)) * 2, shape=m)
eta = Uniform('eta', lower=0, upper=np.mean(bins_1) * 2, shape=m)
gamma = Uniform('gamma', lower=0, upper=np.mean(bins_2) * 2, shape=n)
pi_1 = Uniform('pi_1', lower=0, upper=1)
pi_2 = Deterministic('pi_2', 1 - pi_1)
m1 = Poisson.dist(mu=beta, shape=[m, 1])
n1 = Poisson.dist(mu=beta, shape=[n, 1])
m2 = Poisson.dist(mu=eta, shape=[m, 1])
n2 = Poisson.dist(mu=gamma, shape=[n, 1])
Mixture('mixture_M', [pi_1, pi_2], [m1, m2], observed=bins_1)
Mixture('mixture_N', [pi_1, pi_2], [n1, n2], observed=bins_2)
start = pm.find_MAP()
step = pm.NUTS(state=start)
trace = pm.sample(5000, step=step, progressbar=True)
pm.traceplot(trace)
Basically, this is a non-parametric way of comparing histograms, using the fact that a multinomial distribution is a non-parametric model for a histogram, and that multinomial can be modelled as a bunch of independent Poisson variables.
Somehow I managed to get this error:
ValueError: Input dimension mis-match. (input[0].shape[1] = 2, input[1].shape[1] = 14)
on line: Mixture('mixture_M', [pi_1, pi_2], [m1, m2], observed=bins_1)
when running with bins_1 and bins_2 as (14, 1) integer arrays, so n=m=14. I printed out the shapes of different variables and I believe they match. Have I misunderstood how Mixture in PYMC3 works? Thanks in advance!