Example #1
0
plt.figure()
plt.plot(data[:, 0], data[:, 1], 'kx')
plt.title('data')

nb_models = 25

gating_hypparams = dict(K=nb_models, alphas=np.ones((nb_models, )))
gating_prior = Dirichlet(**gating_hypparams)

components_hypparams = dict(mu=np.zeros((2, )), kappa=0.01,
                            psi=np.eye(2), nu=3)
components_prior = NormalWishart(**components_hypparams)

gmm = BayesianMixtureOfGaussians(gating=CategoricalWithDirichlet(gating_prior),
                                 components=[GaussianWithNormalWishart(components_prior)
                                             for _ in range(nb_models)])

gmm.add_data(data, labels_from_prior=True)

allscores = []
allmodels = []
for superitr in range(5):
    # Gibbs sampling to wander around the posterior
    gmm.resample(maxiter=25)
    # mean field to lock onto a mode
    scores = gmm.meanfield_coordinate_descent(maxiter=100)

    allscores.append(scores)
    allmodels.append(copy.deepcopy(gmm))
Example #2
0
gating = Categorical(K=2)

components = [GaussianWithDiagonalCovariance(mu=np.array([1., 1.]), sigmas=np.array([0.25, 0.5])),
              GaussianWithDiagonalCovariance(mu=np.array([-1., -1.]), sigmas=np.array([0.5, 0.25]))]

gmm = MixtureOfGaussians(gating=gating, components=components)

obs, z = gmm.rvs(500)
gmm.plot(obs)

gating_hypparams = dict(K=2, alphas=np.ones((2, )))
gating_prior = Dirichlet(**gating_hypparams)

components_hypparams = dict(mu=np.zeros((2, )),
                            kappas=1e-2 * np.ones((2, )),
                            alphas=1. * np.ones((2, )),
                            betas=1. / (2. * 1e4) * np.ones((2, )))
components_prior = NormalGamma(**components_hypparams)

model = BayesianMixtureOfGaussians(gating=CategoricalWithDirichlet(gating_prior),
                                   components=[GaussianWithNormalGamma(components_prior)
                                               for _ in range(2)])

model.add_data(obs)

model.max_aposteriori(maxiter=1000)

plt.figure()
model.plot(obs)
Example #3
0
gating = Categorical(K=2)

components = [GaussianWithCovariance(mu=np.array([1., 1.]), sigma=0.25 * np.eye(2)),
              GaussianWithCovariance(mu=np.array([-1., -1.]), sigma=0.5 * np.eye(2))]

gmm = MixtureOfGaussians(gating=gating, components=components)

obs, z = gmm.rvs(500)
gmm.plot(obs)

gating_hypparams = dict(K=2, alphas=np.ones((2, )))
gating_prior = Dirichlet(**gating_hypparams)

components_hypparams = dict(mu=np.zeros((2, )),
                            kappas=1e-2 * np.ones((2, )),
                            alphas=1. * np.ones((2, )),
                            betas=1. / (2. * 1e4) * np.ones((2, )))
components_prior = NormalGamma(**components_hypparams)

model = BayesianMixtureOfGaussians(gating=CategoricalWithDirichlet(gating_prior),
                                   components=[GaussianWithNormalGamma(components_prior)
                                               for _ in range(2)])

model.add_data(obs)

model.resample()
model.meanfield_coordinate_descent(maxiter=2500)

plt.figure()
model.plot(obs)
Example #4
0
plt.figure()
plt.plot(data[:, 0], data[:, 1], 'kx')
plt.title('data')

nb_models = 25

gating_hypparams = dict(K=nb_models, alphas=np.ones((nb_models, )))
gating_prior = Dirichlet(**gating_hypparams)

components_hypparams = dict(mu=np.zeros((2, )),
                            kappa=0.01,
                            psi=np.eye(2),
                            nu=3)
components_prior = NormalWishart(**components_hypparams)

gmm = BayesianMixtureOfGaussians(
    gating=CategoricalWithDirichlet(gating_prior),
    components=[
        GaussianWithNormalWishart(components_prior) for _ in range(nb_models)
    ])

gmm.add_data(data, labels_from_prior=True)

gmm.resample(maxiter=2500)

plt.figure()
plt.title('posterior')
gmm.plot()
plt.show()
Example #5
0
plt.figure()
plt.plot(data[:, 0], data[:, 1], 'kx')
plt.title('data')

nb_models = 25

gating_hypparams = dict(K=nb_models, alphas=np.ones((nb_models, )))
gating_prior = Dirichlet(**gating_hypparams)

components_hypparams = dict(mu=np.zeros((2, )), kappa=0.01,
                            psi=np.eye(2), nu=3)
components_prior = NormalWishart(**components_hypparams)

gmm = BayesianMixtureOfGaussians(gating=CategoricalWithDirichlet(gating_prior),
                                 components=[GaussianWithNormalWishart(components_prior)
                                             for _ in range(nb_models)])

gmm.add_data(data, labels_from_prior=True)

allscores = []
allmodels = []
for superitr in range(5):
    # Gibbs sampling to wander around the posterior
    gmm.resample(maxiter=25)
    # Stochastic Meanfield VI
    gmm.meanfield_stochastic_descent(stepsize=1e-2, maxiter=100, batchsize=128)

    allscores.append(gmm.meanfield_update())
    allmodels.append(copy.deepcopy(gmm))