GMM Model Selection by BIC

Quick Overview

Implement a grid search over Gaussian mixture covariance types and component counts that scores every fit with AIC and BIC and returns the lowest-BIC configuration. It tests free-parameter counting, log-likelihood scaling and deterministic tie-breaking with scikit-learn.

GMM Model Selection by BIC

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `select_gmm_by_bic(X, n_components_range, covariance_types, random_state=0, n_init=1, reg_covar=1e-6, max_iter=100)`. Fit a scikit-learn `GaussianMixture` for every (covariance type, component count) pair, score each fit with AIC and BIC, and return the configuration with the lowest BIC. The function must follow these rules: - Pass `random_state`, `n_init`, `reg_covar` and `max_iter` to each model. Loop over `covariance_types` in the outer loop and `n_components_range` in the inner loop. This is the row order of the results. - Compute the total log-likelihood as `log_likelihood = gm.score(X) * n`. The number of free parameters is `p = cov + k*d + (k - 1)`, where `cov` is `k*d*(d+1)/2` for `full`, `k*d` for `diag`, `d*(d+1)/2` for `tied` and `k` for `spherical`. - Use `aic = -2*LL + 2*p` and `bic = -2*LL + p*ln(n)`. - Return a dict with `results` (a DataFrame with columns `covariance_type, n_components, n_parameters, log_likelihood, aic, bic`, where the counts are `int64` and the scores are `float64`), `best_covariance_type`, `best_n_components`, `best_bic` (a float) and `best_model` (the fitted model). - When two configurations tie on BIC, keep the first one in grid order. An empty grid raises `ValueError`. For example, with `X` holding 300 two-dimensional points from three well-separated blobs: ```python out = select_gmm_by_bic(X, [3], ["full", "diag", "tied", "spherical"]) out["results"]["n_parameters"].tolist() # [17, 14, 11, 11] ``` ```hint Ties Think about which comparison keeps the earlier configuration when two BIC values are exactly equal. ``` ```hint Column types A DataFrame built from Python lists of mixed values does not always get the dtypes the contract asks for. Check them before you return. ``` ### What a Strong Answer Covers - The free-parameter count for each of the four covariance types, including why the means and mixing weights add $kd + k - 1$. - Why `score(X)` has to be multiplied by $n$ before it goes into AIC and BIC. - Grid order, the tie rule and the empty-grid error. - The returned structure: column dtypes, the best configuration and the fitted best model. - The cost of the grid search in terms of the number of fits, EM iterations, $n$, $k$ and $d$. ### Follow-up Questions - Why might AIC and BIC select different numbers of components? - How would you make the selection less sensitive to poor EM local optima? - What does `reg_covar` protect against, and how does it interact with model selection?

Overview: Implement a grid search over Gaussian mixture covariance types and component counts that scores every fit with AIC and BIC and returns the lowest-BIC configuration. It tests free-parameter counting, log-likelihood scaling and deterministic tie-breaking with scikit-learn.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement select_gmm_by_bic(X, n_components_range, covariance_types, random_state=0, n_init=1, reg_covar=1e-6, max_iter=100). Fit a scikit-learn GaussianMixture for every (covariance type, component count) pair, score each fit with AIC and BIC, and return the configuration with the lowest BIC.

The function must follow these rules:

  • Pass random_state , n_init , reg_covar and max_iter to each model. Loop over covariance_types in the outer loop and n_components_range in the inner loop. This is the row order of the results.
  • Compute the total log-likelihood as log_likelihood = gm.score(X) * n . The number of free parameters is p = cov + k*d + (k - 1) , where cov is k*d*(d+1)/2 for full , k*d for diag , d*(d+1)/2 for tied and k for spherical .
  • Use aic = -2*LL + 2*p and bic = -2*LL + p*ln(n) .
  • Return a dict with results (a DataFrame with columns covariance_type, n_components, n_parameters, log_likelihood, aic, bic , where the counts are int64 and the scores are float64 ), best_covariance_type , best_n_components , best_bic (a float) and best_model (the fitted model).
  • When two configurations tie on BIC, keep the first one in grid order. An empty grid raises ValueError .

For example, with X holding 300 two-dimensional points from three well-separated blobs:

out = select_gmm_by_bic(X, [3], ["full", "diag", "tied", "spherical"])
out["results"]["n_parameters"].tolist()
# [17, 14, 11, 11]

What a Strong Answer Covers Guidance

  • The free-parameter count for each of the four covariance types, including why the means and mixing weights add kd+k−1kd + k - 1 .
  • Why score(X) has to be multiplied by nn before it goes into AIC and BIC.
  • Grid order, the tie rule and the empty-grid error.
  • The returned structure: column dtypes, the best configuration and the fitted best model.
  • The cost of the grid search in terms of the number of fits, EM iterations, nn , kk and dd .

Follow-up Questions Guidance

  • Why might AIC and BIC select different numbers of components?
  • How would you make the selection less sensitive to poor EM local optima?
  • What does reg_covar protect against, and how does it interact with model selection?