Last active
October 3, 2016 05:09
-
-
Save zaxtax/7968670 to your computer and use it in GitHub Desktop.
Mixture modeling example in pymc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from pymc import Model, Gamma, Normal, Dirichlet | |
from pymc import Categorical | |
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)) | |
with Model() as model: | |
dd = Dirichlet('dd', a=np.array([1., 1., 1.]), shape=k) | |
precs = Gamma('precs', alpha=0.1, beta=0.1, shape=k) | |
means = Normal('means', 0, 0.001, shape=k) | |
category = Categorical('category', | |
p=dd) | |
points = Normal('obs', | |
means[category], | |
precs[category], | |
shape=ndata, | |
observed=data) | |
tr = sample(3000, step=Metropolis(model.vars)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment