<jupyter_start><jupyter_text>bap_mixture_models> API details.<jupyter_code>#hide
from nbdev.showdoc import *
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pymc3 as pm
import arviz as az
import scipy.stats as stats
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Data<jupyter_code>clusters = 3
n_cluster = [90, 50, 75]
n_total = sum(n_cluster)
means = [9, 21, 35]
std_devs = [2, 2, 2]
mix = np.random.normal(np.repeat(means, n_cluster), np.repeat(std_devs, n_cluster))

sns.kdeplot(np.array(mix))
plt.xlabel('$x$', fontsize=14)<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>with pm.Model() as model_ug:
    p = pm.Dirichlet("p", a=np.ones(clusters))
    category = pm.Categorical('category', p=p, shape=n_total)
    
    means = pm.Normal('means', mu=[10, 20, 30], sd=2, shape=clusters)
    sd = pm.HalfCauchy('sd', 5)
    
    y = pm.Normal('y', mu=means[category], sd=sd, observed=mix)
    
    
    step1 = pm.ElemwiseCategorical(vars=[category], values=range(clusters))
    step2 = pm.Metropolis(vars=[means, sd, p])
    trace_ug = pm.sample(10000, step=[step1, step2])
    chain_ug = trace_ug[1000:]
    varnames = ['means','sd','p']
    pm.traceplot(chain_ug, varnames)
az.summary(chain_ug, varnames)<jupyter_output><empty_output><jupyter_text>Plot model samples<jupyter_code>ppc = pm.sample_posterior_predictive(chain_ug, 50, model_ug)
for i in ppc['y']:
    sns.kdeplot(i, alpha=0.1, color='b')
    
sns.kdeplot(np.array(mix), lw=2, color='k')
plt.xlabel('$x$', fontsize=4)<jupyter_output>/home/jovyan/pymc3/sampling.py:1618: UserWarning: samples parameter is smaller than nchains times ndraws, some draws and/or chains may not be represented in the returned posterior predictive sample
  "samples parameter is smaller than nchains times ndraws, some draws "<jupyter_text>2. Marginalized Gaussian mixture model<jupyter_code>with pm.Model() as model_mg:
    p = pm.Dirichlet("p", a=np.ones(clusters))
    means = pm.Normal('means', mu=[10, 20, 30], sd=2, shape=clusters)
    sd = pm.HalfCauchy('sd', 5, shape=clusters)
    
    y = pm.NormalMixture('y', w=p, mu=means, sd=sd, observed=mix)
    
    step = pm.Metropolis()
    trace_mg = pm.sample(10000, step)
    chain_mg = trace_mg[1000:]
    varnames = ['means','sd','p']
    pm.traceplot(chain_mg, varnames)
az.summary(chain_mg, varnames)
ppc = pm.sample_posterior_predictive(chain_ug, 50, model_ug)
for i in ppc['y']:
    sns.kdeplot(i, alpha=0.1, color='b')
    
sns.kdeplot(np.array(mix), lw=2, color='k')
plt.xlabel('$x$', fontsize=4)<jupyter_output>/home/jovyan/pymc3/sampling.py:1618: UserWarning: samples parameter is smaller than nchains times ndraws, some draws and/or chains may not be represented in the returned posterior predictive sample
  "samples parameter is smaller than nchains times ndraws, some draws "<jupyter_text>3. Mixture models and count data  Poisson distribution<jupyter_code>lam_params = [0.5, 1, 1.5, 3, 8, 15]
k = np.arange(0, 30)

for lam in lam_params:
    y = stats.poisson(lam).pmf(k)
    plt.plot(k, y, 'o-', label=f"lambda = {lam}")
plt.legend()
plt.xlabel('k')
plt.ylabel('pmf(k)')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1471329552614997, 'fraction_numerical': 0.028985507246376812, 'mean_word_length': 5.189083820662768, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '391521', 'license': 'Apache-2.0', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1219, 'n_words': 270, 'path': '25_bap_mixture_models.ipynb', 'repo_name': 'danhphan/bayesian_inferences', 'size': '3174'}