examples/bayesian-methods-for-hackers / ExamplesFromChapters / Chapter3 / ClusteringWithGaussians.py
5380 viewsimport numpy as np1import pymc as pm234data = np.loadtxt("../../Chapter3_MCMC/data/mixture_data.csv", delimiter=",")567p = pm.Uniform("p", 0, 1)89assignment = pm.Categorical("assignment", [p, 1 - p], size=data.shape[0])1011taus = 1.0 / pm.Uniform("stds", 0, 100, size=2) ** 2 # notice the size!12centers = pm.Normal("centers", [150, 150], [0.001, 0.001], size=2)1314"""15The below deterministic functions map an assignment, in this case 0 or 1,16to a set of parameters, located in the (1, 2) arrays `taus` and `centers.`17"""181920@pm.deterministic21def center_i(assignment=assignment, centers=centers):22return centers[assignment]232425@pm.deterministic26def tau_i(assignment=assignment, taus=taus):27return taus[assignment]2829# and to combine it with the observations:30observations = pm.Normal("obs", center_i, tau_i,31value=data, observed=True)3233# below we create a model class34model = pm.Model([p, assignment, taus, centers])353637map_ = pm.MAP(model)38map_.fit()39mcmc = pm.MCMC(model)40mcmc.sample(100000, 50000)414243