X-Git-Url: https://git.auder.net/?p=valse.git;a=blobdiff_plain;f=pkg%2FR%2FEMGLLF.R;h=2aeea537b9d6656c04032c2697d407f6c4d1d823;hp=0a279f0bf56f92b84acbbddfab6bc8de1c07dd1a;hb=17b9fa5f6004bb55e915d8916f1c9a1f128a65ce;hpb=1b698c1619dbcf5b3a0608dc894d249945d2bce3 diff --git a/pkg/R/EMGLLF.R b/pkg/R/EMGLLF.R index 0a279f0..2aeea53 100644 --- a/pkg/R/EMGLLF.R +++ b/pkg/R/EMGLLF.R @@ -19,11 +19,12 @@ #' rho : parametre de variance renormalisé, calculé par l'EM #' pi : parametre des proportions renormalisé, calculé par l'EM #' LLF : log vraisemblance associée à cet échantillon, pour les valeurs estimées des paramètres -#' S : ... affec : ... +#' S : ... +#' affec : ... #' #' @export EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, - X, Y, eps, fast = TRUE) + X, Y, eps, fast) { if (!fast) { @@ -41,29 +42,27 @@ EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, X, Y, eps, phi = double(p * m * k), rho = double(m * m * k), pi = double(k), LLF = double(maxi), S = double(p * m * k), affec = integer(n), n, p, m, k, PACKAGE = "valse") + list(phi = phi, rho = rho, pi = pi, llh = llh, S = S, affec=affec) } # R version - slow but easy to read .EMGLLF_R <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, - X2, Y, eps) + X, Y, eps) { # Matrix dimensions - n <- dim(Y)[1] - if (length(dim(phiInit)) == 2) { - p <- 1 - m <- dim(phiInit)[1] - k <- dim(phiInit)[2] - } else { - p <- dim(phiInit)[1] - m <- dim(phiInit)[2] - k <- dim(phiInit)[3] - } - X <- matrix(nrow = n, ncol = p) - X[1:n, 1:p] <- X2 + n <- nrow(X) + p <- ncol(X) + m <- ncol(Y) + k <- length(piInit) + + # Adjustments required when p==1 or m==1 (var.sel. or output dim 1) + if (p==1 || m==1) + phiInit <- array(phiInit, dim=c(p,m,k)) + if (m==1) + rhoInit <- array(rhoInit, dim=c(m,m,k)) # Outputs - phi <- array(NA, dim = c(p, m, k)) - phi[1:p, , ] <- phiInit + phi <- phiInit rho <- rhoInit pi <- piInit llh <- -Inf @@ -119,7 +118,8 @@ EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, # t(m) is the largest value in the grid O.1^k such that it is nonincreasing while (kk < 1000 && -a/n + lambda * sum(pi^gamma * b) < - -sum(gam2 * log(pi2))/n + lambda * sum(pi2^gamma * b)) + # na.rm=TRUE to handle 0*log(0) + -sum(gam2 * log(pi2), na.rm=TRUE)/n + lambda * sum(pi2^gamma * b)) { pi2 <- pi + 0.1^kk * (1/n * gam2 - pi) kk <- kk + 1 @@ -146,8 +146,8 @@ EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, { for (mm in 1:m) { - S[j, mm, r] <- -rho[mm, mm, r] * ps2[j, mm, r] - + sum(phi[-j, mm, r] * Gram2[j, -j, r]) + S[j, mm, r] <- -rho[mm, mm, r] * ps2[j, mm, r] + + sum(phi[-j, mm, r] * Gram2[j, -j, r]) if (abs(S[j, mm, r]) <= n * lambda * (pi[r]^gamma)) { phi[j, mm, r] <- 0 } else if (S[j, mm, r] > n * lambda * (pi[r]^gamma)) { @@ -159,34 +159,39 @@ EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, } } - ######## E step# + ## E step # Precompute det(rho[,,r]) for r in 1...k - detRho <- sapply(1:k, function(r) det(rho[, , r])) - gam1 <- matrix(0, nrow = n, ncol = k) + detRho <- sapply(1:k, function(r) gdet(rho[, , r])) + sumLogLLH <- 0 for (i in 1:n) { - # Update gam[,] - for (r in 1:k) - { - gam1[i, r] <- pi[r] * exp(-0.5 - * sum((Y[i, ] %*% rho[, , r] - X[i, ] %*% phi[, , r])^2)) * detRho[r] - } + # Update gam[,]; use log to avoid numerical problems + logGam <- sapply(1:k, function(r) { + log(pi[r]) + log(detRho[r]) - 0.5 * + sum((Y[i, ] %*% rho[, , r] - X[i, ] %*% phi[, , r])^2) + }) + + logGam <- logGam - max(logGam) #adjust without changing proportions + gam[i, ] <- exp(logGam) + norm_fact <- sum(gam[i, ]) + gam[i, ] <- gam[i, ] / norm_fact + sumLogLLH <- sumLogLLH + log(norm_fact) - log((2 * base::pi)^(m/2)) } - gam <- gam1 / rowSums(gam1) - sumLogLLH <- sum(log(rowSums(gam)) - log((2 * base::pi)^(m/2))) + sumPen <- sum(pi^gamma * b) last_llh <- llh - llh <- -sumLogLLH/n + lambda * sumPen + llh <- -sumLogLLH/n #+ lambda * sumPen dist <- ifelse(ite == 1, llh, (llh - last_llh)/(1 + abs(llh))) Dist1 <- max((abs(phi - Phi))/(1 + abs(phi))) Dist2 <- max((abs(rho - Rho))/(1 + abs(rho))) Dist3 <- max((abs(pi - Pi))/(1 + abs(Pi))) dist2 <- max(Dist1, Dist2, Dist3) - if (ite >= mini && (dist >= eps || dist2 >= sqrt(eps))) + if (ite >= mini && (dist >= eps || dist2 >= sqrt(eps))) break } - list(phi = phi, rho = rho, pi = pi, llh = llh, S = S) + affec = apply(gam, 1, which.max) + list(phi = phi, rho = rho, pi = pi, llh = llh, S = S, affec=affec) }