Hello, I am having difficulty implementing a mixture model (two distributions). On each of n trials I have 1000 observations. For each trial, I want the model to assume that the observations originate from a mixture of a von mises distribution and a uniform distribution. However, the mu of the von mises distribution must come from one of four possible uniform priors, where the selected prior varies according to the trial.
Below is my attempt at using a categorical distribution to determine which of the four priors to use, but I run into problems indexing mus in this example. (“idx” is a 1d array of length 1000 x ntrials that specifies what trial each observation belongs to, and “X” is a 1d array of observations that is also length 1000 x ntrials)
with pm.Model() as model: mus = [pm.Uniform('mu_%d' % i, lower = (2*np.pi)/4*i - np.pi, upper = ((2*np.pi)/4*i + (2*np.pi)/4) - np.pi) for i in range(4)] mu_p = pm.Dirichlet('mu_p',a=np.ones(4),shape=(ntrials,4)) cat = pm.Categorical('cat',p=mu_p,shape=ntrials) p = pm.Dirichlet('p',a=np.ones(2),shape=(ntrials,2)) dists = [ pm.VonMises.dist(mu=mus[cat[idx]], kappa=np.ones(1000)), pm.Uniform.dist(lower=np.full(1000,-np.pi), upper=np.full(1000,np.pi)), ] y = pm.Mixture('y', w=p, comp_dists=dists, observed=X)
Any help is greatly appreciated, I have looked into other posts on the forum already and made attempts such as marginalization but I have repeatedly run into problems.
Edit: The error is the mus[cat[idx]] part, it leads to an error: “TypeError: index must be integers.”