Search code examples
rmixture-modelexpectation-maximization

How to vectorize likelihood calculation under multiple parameters?


I am trying to implement a bernoulli mixture and was wondering how to vectorize the calculations correctly without looping.

I have tried various versions of apply but can't get the desired output (dim = c(5,4,2). Should my component parameters be in a list instead of a matrix?

set.seed(123)

#Data
X <- matrix(sample(c(0,1), 20, replace = TRUE, prob = c(.6, .4)),
               nrow = 5, ncol = 4)

#Params
parameters <-  matrix(runif(8), nrow = 2, ncol = 4)

#Would like to vectorize this
dbinom(X, 1, parameters[1,], log = TRUE)
dbinom(X, 1, parameters[2,], log = TRUE)

Solution

  • We loop through the rows of parameters with apply and apply the dbinom

    out1 <- do.call(`c`, apply(parameters, 1, function(x) 
                   list(dbinom(X, 1, x, log = TRUE))))
    
    identical(out1[[1]], dbinom(X, 1, parameters[1,], log = TRUE))
    #[1] TRUE
    
    identical(out1[[2]], dbinom(X, 1, parameters[2,], log = TRUE))
    #[1] TRUE
    

    Or using pmap

    library(purrr)
    out2 <- pmap(list(x = list(X), size = 1, prob = split(parameters, 
                  row(parameters)), log = TRUE), dbinom)
    
    identical(out1, out2)
    #[1] TRUE