Skip to content

Design Your Own Mixture

This demo takes a few seconds to load (up to a minute on a slow connection, because the first fit runs in your browser) and runs entirely in your browser (NumPy and matplotlib via Pyodide; no tgmm or PyTorch is involved). It mirrors how tutorial 01 builds its synthetic data with tgmm.generate_gmm_data, and the EM fit it then runs with GaussianMixture (covariance_type='full').

Each blob is a Gaussian with a centre \(\mu_k\), a covariance \(\Sigma_k=R(\theta_k)\,\operatorname{diag}(s_{k1}^2,s_{k2}^2)\,R(\theta_k)^{\top}\) (standard deviations \(s_{k1},s_{k2}\) along two axes rotated by \(\theta_k\)), and a number of points \(n_k\), so the mixing weights are \(\pi_k=n_k/n\). Points are drawn as \(x=\mu_k+L_kz\) with \(z\sim\mathcal N(0,I)\) and \(L_kL_k^{\top}=\Sigma_k\) (a Cholesky factor), which is what generate_gmm_data does, so the covariance you see in the table is the covariance you get. Pick a preset, or move the sliders (they update when released); Reset blobs returns to the chosen preset. Preset values are rounded to the slider grid, so the tutorial blobs are reproduced to within about 1% for the covariance entries.

The preset Tutorial 01 before the covariance fix uses the squares \(\Sigma^{\top}\Sigma\) of the requested matrices: before the fix, generate_gmm_data sampled Z @ cov, so a requested cov really produced \(\operatorname{cov}^{\top}\operatorname{cov}\).

Blob 1
Blob 2
Blob 3
Blob 4

Fit a mixture to your data

What to look for: with the well separated preset the BIC minimum sits at the true \(K=3\), and fitting fewer components merges blobs while fitting more splits one of them. With the overlapping preset BIC typically prefers fewer components than you designed, because heavily overlapping blobs look like a single broader one: the same effect that makes the choice of \(K\) hard in practice (see the model selection tutorial). Make one blob tiny (a small \(n_k\)) and watch it disappear from the BIC preference first. The code budget for this page is about 150 lines of Python.