Compute soft cluster memberships for a Gaussian mixture
A Gaussian mixture is a soft k-means: each point belongs to every cluster with some probability, called its responsibility. The E-step computes them.
Write gmm_responsibilities(x, weights, means, variances) for 1D data x and k components. For point i and component j:
r[i, j] = w_j N(x_i; mu_j, var_j) / sum over j of the same thing.
Return the (n, k) array. Compute it in log space with the log-sum-exp trick so far-away points do not give 0 / 0.
def gmm_responsibilities(x, weights, means, variances)Understand the idea
A Gaussian mixture is a soft version of k-means. Instead of each point belonging to one cluster, it gets a share of membership in every cluster, called a responsibility, and each point's shares add up to 1. The share is the cluster's weight times its bell-curve height at that point, divided by the total over all clusters. Worked example: x = 0, two components with weights 0.5 and 0.5, means 0 and 2, variance 1. The bell heights are proportional to exp(0) = 1 and exp(-(0 - 2)^2 / 2) = exp(-2) = 0.135. Responsibilities are 1 / 1.135 = 0.881 and 0.135 / 1.135 = 0.119. For a point very far away, both heights underflow to 0 and you get 0 / 0. Work in logs: log w + log N, then subtract the log-sum-exp of each row. Rule: log r_ij = log w_j + log N(x_i; mu_j, var_j) - logsumexp over j. Normalise across components, not across points. EM training of mixture models runs this step repeatedly.