Compute the score, the gradient of the log density with respect to the point, of a mixture of isotropic Gaussians at every row of x.
∇xlogp(x)=k=1∑Krk(x)σk2μk−x,rk(x)=∑j=1KπjN(x;μj,σj2I)πkN(x;μk,σk2I)The mixture density is p(x)=∑kπkN(x;μk,σk2I). Here x holds N points of dimension D, weights holds the mixture weights πk, means holds the component means μk as rows, and variances holds one variance σk2 per component that applies to every coordinate. The responsibility rk(x) is the posterior probability that component k produced the point, and the log density of one component is
logN(x;μk,σk2I)=−2Dlog(2πσk2)−2σk2∥x−μk∥2.Points can lie far from every mean, where each density underflows to zero in floating point.
Input: x = [[0.0],[1.0],[3.0]], weights = [0.5,0.5], means = [[-1.0],[1.0]], variances = [1.0,1.0]
Output: [[0.0],[-0.238406],[-2.004945]]
Explanation: At x = 0 the two components pull equally hard in opposite directions, so the score is 0. At x = 3 nearly all responsibility belongs to the component at 1, so the score is close to -(3 - 1) / 1 = -2.
Input: x = [[0.5,0.5],[-1.0,2.0]], weights = [0.8,0.2], means = [[0.0,0.0],[2.0,2.0]], variances = [0.5,2.0]
Output: [[-0.943351,-0.943351],[1.752823,-2.022588]]
Input: x = [[60.0,-40.0]], weights = [0.3,0.7], means = [[1.0,0.0],[0.0,1.0]], variances = [0.04,0.09]
Output: [[-666.666667,455.555556]]
x[:, None, :] - means[None, :, :] gives every pairwise difference at once, with shape (N, K, D).
Subtracting the row maximum of the log terms before np.exp keeps the softmax stable and leaves the normalized responsibilities unchanged.
A matrix product with @ between an (N, K) array of per-component coefficients and the (K, D) means sums over the components in one step.
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Compute the score, the gradient of the log density with respect to the point, of a mixture of isotropic Gaussians at every row of x.
∇xlogp(x)=k=1∑Krk(x)σk2μk−x,rk(x)=∑j=1KπjN(x;μj,σj2I)πkN(x;μk,σk2I)The mixture density is p(x)=∑kπkN(x;μk,σk2I). Here x holds N points of dimension D, weights holds the mixture weights πk, means holds the component means μk as rows, and variances holds one variance σk2 per component that applies to every coordinate. The responsibility rk(x) is the posterior probability that component k produced the point, and the log density of one component is
logN(x;μk,σk2I)=−2Dlog(2πσk2)−2σk2∥x−μk∥2.Points can lie far from every mean, where each density underflows to zero in floating point.
Input: x = [[0.0],[1.0],[3.0]], weights = [0.5,0.5], means = [[-1.0],[1.0]], variances = [1.0,1.0]
Output: [[0.0],[-0.238406],[-2.004945]]
Explanation: At x = 0 the two components pull equally hard in opposite directions, so the score is 0. At x = 3 nearly all responsibility belongs to the component at 1, so the score is close to -(3 - 1) / 1 = -2.
Input: x = [[0.5,0.5],[-1.0,2.0]], weights = [0.8,0.2], means = [[0.0,0.0],[2.0,2.0]], variances = [0.5,2.0]
Output: [[-0.943351,-0.943351],[1.752823,-2.022588]]
Input: x = [[60.0,-40.0]], weights = [0.3,0.7], means = [[1.0,0.0],[0.0,1.0]], variances = [0.04,0.09]
Output: [[-666.666667,455.555556]]
x[:, None, :] - means[None, :, :] gives every pairwise difference at once, with shape (N, K, D).
Subtracting the row maximum of the log terms before np.exp keeps the softmax stable and leaves the normalized responsibilities unchanged.
A matrix product with @ between an (N, K) array of per-component coefficients and the (K, D) means sums over the components in one step.
Accepts: array
Accepts: array
Accepts: array
Accepts: array