GPCM Covariance Shapes
This demo takes a few seconds to load and runs entirely in your browser (NumPy and matplotlib via Pyodide; no tgmm or PyTorch is involved).
It mirrors the covariance parameterisation of
GaussianMixture(covariance_type=...) (API: tgmm.covariance.gpcm).
Each component's covariance is split into three factors,
\[\Sigma_k=\lambda_k\,D_k\,A_k\,D_k^{\top},\qquad \det A_k=1,\quad D_k^{\top}D_k=I,\]
a volume \(\lambda_k\) (size), a shape \(A_k=\operatorname{diag}(a_{k1},a_{k2})\) (elongation, \(a_{k1}/a_{k2}\) is the axis ratio) and an
orientation \(D_k\) (rotation by \(\theta_k\)). For two components, choose for each factor whether the components share it
(E, equal: component 2 copies component 1), let it V**ary (each has its own sliders) or, for shape and orientation, fix it to the
**I**dentity (**I: a circle, or no rotation). The three letters give the model's mclust code.
import%20marimo%20as%20mo%0Aimport%20numpy%20as%20np%0Aimport%20matplotlib.pyplot%20as%20plt%0A%0Avol_mode%20%3D%20mo.ui.dropdown(%5B%22E%22%2C%20%22V%22%5D%2C%20value%3D%22V%22%2C%20label%3D%22volume%22)%0Ashape_mode%20%3D%20mo.ui.dropdown(%5B%22E%22%2C%20%22V%22%2C%20%22I%22%5D%2C%20value%3D%22E%22%2C%20label%3D%22shape%22)%0Aorient_mode%20%3D%20mo.ui.dropdown(%5B%22E%22%2C%20%22V%22%2C%20%22I%22%5D%2C%20value%3D%22V%22%2C%20label%3D%22orientation%22)%0Alam1%20%3D%20mo.ui.slider(0.2%2C%203.0%2C%20step%3D0.1%2C%20value%3D1.0%2C%20label%3D%22%CE%BB%E2%82%81%22)%0Alam2%20%3D%20mo.ui.slider(0.2%2C%203.0%2C%20step%3D0.1%2C%20value%3D2.0%2C%20label%3D%22%CE%BB%E2%82%82%22)%0Aratio1%20%3D%20mo.ui.slider(1.0%2C%2012.0%2C%20step%3D0.5%2C%20value%3D4.0%2C%20label%3D%22a%E2%82%81%2Fa%E2%82%82%20(comp.%201)%22)%0Aratio2%20%3D%20mo.ui.slider(1.0%2C%2012.0%2C%20step%3D0.5%2C%20value%3D4.0%2C%20label%3D%22a%E2%82%81%2Fa%E2%82%82%20(comp.%202)%22)%0Aang1%20%3D%20mo.ui.slider(0%2C%20180%2C%20step%3D5%2C%20value%3D30%2C%20label%3D%22%CE%B8%E2%82%81%20(%C2%B0)%22)%0Aang2%20%3D%20mo.ui.slider(0%2C%20180%2C%20step%3D5%2C%20value%3D120%2C%20label%3D%22%CE%B8%E2%82%82%20(%C2%B0)%22)%0Amo.vstack(%5B%0A%20%20%20%20mo.hstack(%5Bvol_mode%2C%20shape_mode%2C%20orient_mode%5D%2C%20justify%3D%22start%22)%2C%0A%20%20%20%20mo.hstack(%5Blam1%2C%20ratio1%2C%20ang1%5D%2C%20justify%3D%22start%22)%2C%0A%20%20%20%20mo.hstack(%5Blam2%2C%20ratio2%2C%20ang2%5D%2C%20justify%3D%22start%22)%2C%0A%5D)
Component 2's slider is ignored whenever its factor is E (it copies component 1), and both shape sliders are ignored for shape I
(both orientation sliders for orientation I).
VALID%20%3D%20%7B%22EII%22%2C%20%22VII%22%2C%20%22EEI%22%2C%20%22VEI%22%2C%20%22EVI%22%2C%20%22VVI%22%2C%20%22EEE%22%2C%20%22VEE%22%2C%20%22EVE%22%2C%20%22EEV%22%2C%20%22VVE%22%2C%20%22VEV%22%2C%20%22EVV%22%2C%20%22VVV%22%7D%0AK%2C%20d%20%3D%202%2C%202%0A_v%2C%20_s%2C%20_o%20%3D%20vol_mode.value%2C%20shape_mode.value%2C%20orient_mode.value%0Acode%20%3D%20_v%20%2B%20_s%20%2B%20_o%0Al1%2C%20l2%20%3D%20lam1.value%2C%20(lam1.value%20if%20_v%20%3D%3D%20%22E%22%20else%20lam2.value)%0Ar1%20%3D%201.0%20if%20_s%20%3D%3D%20%22I%22%20else%20ratio1.value%0Ar2%20%3D%201.0%20if%20_s%20%3D%3D%20%22I%22%20else%20(r1%20if%20_s%20%3D%3D%20%22E%22%20else%20ratio2.value)%0At1%20%3D%200.0%20if%20_o%20%3D%3D%20%22I%22%20else%20ang1.value%0At2%20%3D%200.0%20if%20_o%20%3D%3D%20%22I%22%20else%20(t1%20if%20_o%20%3D%3D%20%22E%22%20else%20ang2.value)%0An_params%20%3D%20(%7B%22E%22%3A%201%2C%20%22V%22%3A%20K%7D%5B_v%5D%20%2B%20%7B%22I%22%3A%200%2C%20%22E%22%3A%20d%20-%201%2C%20%22V%22%3A%20K%20*%20(d%20-%201)%7D%5B_s%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%7B%22I%22%3A%200%2C%20%22E%22%3A%20d%20*%20(d%20-%201)%20%2F%2F%202%2C%20%22V%22%3A%20K%20*%20d%20*%20(d%20-%201)%20%2F%2F%202%7D%5B_o%5D)%0A%0Adef%20make_cov(lam%2C%20ratio%2C%20deg)%3A%0A%20%20%20%20a%20%3D%20np.array(%5Bnp.sqrt(ratio)%2C%201%20%2F%20np.sqrt(ratio)%5D)%20%20%20%20%20%20%23%20det%20A%20%3D%201%0A%20%20%20%20c%2C%20s%20%3D%20np.cos(np.radians(deg))%2C%20np.sin(np.radians(deg))%0A%20%20%20%20D%20%3D%20np.array(%5B%5Bc%2C%20-s%5D%2C%20%5Bs%2C%20c%5D%5D)%0A%20%20%20%20return%20lam%20*%20D%20%40%20np.diag(a)%20%40%20D.T%0A%0Acovs%20%3D%20%5Bmake_cov(l1%2C%20r1%2C%20t1)%2C%20make_cov(l2%2C%20r2%2C%20t2)%5D%0Averdict%20%3D%20(%22one%20of%20the%2014%20GPCM%20models%22%20if%20code%20in%20VALID%20else%0A%20%20%20%20%20%20%20%20%20%20%20%22**not%20a%20valid%20GPCM%20model**%3A%20with%20a%20spherical%20(identity)%20shape%20the%20orientation%20is%20meaningless%2C%20so%20use%20orientation%20I%22)%0Amo.md(f%22%23%23%23%20mclust%20code%20%60%7Bcode%7D%60%20%E2%80%94%20%7Bverdict%7D%5Cn%5CnFree%20covariance%20parameters%20for%20%24d%3D2%24%2C%20%24K%3D2%24%3A%20**%7Bn_params%7D**%20%22%0A%20%20%20%20%20%20f%22(volume%20%2B%20shape%20%2B%20orientation%20contributions%3B%20%24%5C%5Cdet%5C%5CSigma_1%3D%7Bnp.linalg.det(covs%5B0%5D)%3A.2f%7D%24%2C%20%24%5C%5Cdet%5C%5CSigma_2%3D%7Bnp.linalg.det(covs%5B1%5D)%3A.2f%7D%24).%22)
_t%20%3D%20np.linspace(0%2C%202%20*%20np.pi%2C%20200)%0A_circle%20%3D%20np.stack(%5Bnp.cos(_t)%2C%20np.sin(_t)%5D)%0Afig%2C%20ax%20%3D%20plt.subplots(figsize%3D(7%2C%204))%0Afor%20_k%2C%20(_S%2C%20_c%2C%20_x0)%20in%20enumerate(zip(covs%2C%20%5B%22%232a78d6%22%2C%20%22%23eb6834%22%5D%2C%20%5B-3.0%2C%203.0%5D))%3A%0A%20%20%20%20_L%20%3D%20np.linalg.cholesky(_S)%0A%20%20%20%20for%20_n%20in%20(1%2C%202)%3A%0A%20%20%20%20%20%20%20%20ax.plot(_x0%20%2B%20_n%20*%20(_L%20%40%20_circle)%5B0%5D%2C%20_n%20*%20(_L%20%40%20_circle)%5B1%5D%2C%20color%3D_c%2C%20lw%3D2%20if%20_n%20%3D%3D%201%20else%201)%0A%20%20%20%20ax.plot(_x0%2C%200%2C%20%22o%22%2C%20color%3D_c)%0Aax.set_aspect(%22equal%22)%3B%20ax.set_xlim(-7%2C%207)%3B%20ax.set_ylim(-3.5%2C%203.5)%0Aax.set_title(f%221%CF%83%20and%202%CF%83%20contours%20of%20%CE%A3%E2%82%81%20(blue)%20and%20%CE%A3%E2%82%82%20(orange)%2C%20model%20%7Bcode%7D%22)%0Afig
Things to try: with EII / VII both shapes are circles (identity), EEE shares everything (the same ellipse twice), and VVV frees all three factors.
The parameter count is what bic/aic charge for the covariances, which is why constrained models win on small data
(see the Covariance Structures tutorial).