I'm writing a function for Gaussian mixture models with spherical covariance structures--ie $\Sigma_k = \sigma_k^2 I$
. This particular function is similar to the mclust
package with identifier VII.
http://en.wikipedia.org/wiki/Mixture_model
Anyways, the problem I'm having is running into infinite values for the weight matrix. Definition: Let W be an n x m matrix where n = 1, ..., n (number of obs) and m = 1, ..., m (number of mixtues). Each element of W (ie w_ij) can essentially be defined as a specific form of:
w_im = \frac{a / b * exp(c)}{\sum_i=1^m [a_i / b_i * exp(c_i)]}
Computing this numerically is giving me infinite values. So I'm trying to use the log-identity log(x+y) = log(x) + log(1 + y/x)
. But the issue is that it's not as simple as log(x+y)
but rather log(\sum_i=1^m [a_i / b_i * exp(c_i)])
.
Here's some code define:
n_im = a / b * exp(c)
;d_.m = \sum_i=1^m [a_i / b_i * exp(c_i)]
; andc_mat[i,j]
as the value of the exponent for the [i,j]th term.n_mat[, i] <- log(a[i]) - log(b[i]) - c[,i] # numerator of w_im internal_vec1[i] <- (a[i] * b[1])/ (a[1] * b[i]) # an internal for the step below c_mat2 <- cbind(rep(1, n), c_mat[,1] - c_mat[,-1]) # since e^a / e^b = e^(a-b) for (i in 1:n) { d_vec[i] <- n_mat[i,1] + log(sum(internal_vec1 * exp(c_mat2[i,))) } ## still getting infinite values
I'm trying to define the problem as briefly as possible. the entire function is obviously much larger than this. But, since the problem I'm running into is specifically dealing with infinite (and 1/infinity) values, I'm hoping this snippet is sufficient. Anyone with a coding trick here?