R/RcppExports.R

Defines functions log_sum_exp dmvnrm2 logdmvn loglike Estep Mstep

Documented in dmvnrm2 Estep logdmvn log_sum_exp Mstep

# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#' The log sum exponential trick
#'
#' @param y vector to compute the log sum exponential sum
#'
#' @return return the log exponential sum
#' @export
#' 
#' @examples 
#' log_sum_exp(c(1,2,3,4,5))
log_sum_exp <- function(y) {
    .Call('_RcppParallelGMM_log_sum_exp', PACKAGE = 'RcppParallelGMM', y)
}

#' Density of multivariate normal distribution
#'
#' @param x vector to compute the density
#' @param mean location parameter
#' @param sigma scale/variance parameter
#' @param logd bool variable. If true, output log density, otherwise output true density
#'
#' @return return the density 
#' @export
#'
#' @examples
#' Y = Readjpeg(train)
#' K = 5
#' p = ncol(Y)
#' mean = matrix(rnorm(K*p), K, p)
#' sigma = array(rep(diag(p), K), dim = c(p, p, K)) 
#' z = matrix(0, K, nrow(Y))
#' for (i in 1:nrow(Y)){
#'   for(j in 1:nrow(mean)){
#' z[j,i] = dmvnrm2(Y[i, ], mean[j, ],sigma[, , j], true);
#' }
#' }
dmvnrm2 <- function(x, mean, sigma, logd = FALSE) {
    .Call('_RcppParallelGMM_dmvnrm2', PACKAGE = 'RcppParallelGMM', x, mean, sigma, logd)
}

#' Density matrix of input matrix Y over classes 
#' This function use dmvnrm2 function in Rcpp which compute a density matrix for each input
#' over each class
#' @param Y data matrix 
#' @param mean mean parameter matrix with each row the mean parameter of specific class
#' @param sigma scale/variance parameter cube with each slice the variance matrix of specific class
#'
#' @return return the density matrix
#' @export
#' @examples
#' Y = Readjpeg(shuffle_train)
#' N = nrow(Y)
#' p = ncol(Y)
#' K = 2
#' prob = rep(1/K, K)
#' mean = matrix(rnorm(K*p), K, p)
#' sigma = array(rep(diag(p), K), dim = c(p, p, K))
#' logdmvn(Y, mean, sigma)
logdmvn <- function(Y, mean, sigma) {
    .Call('_RcppParallelGMM_logdmvn', PACKAGE = 'RcppParallelGMM', Y, mean, sigma)
}

loglike <- function(p, Z) {
    .Call('_RcppParallelGMM_loglike', PACKAGE = 'RcppParallelGMM', p, Z)
}

#'  Estep for EM algorithm
#'
#' @param Z Density matrix, each (i,j) element corresponds to log conditional probability of jth point w.r.t class i     
#' @param p Current probability for each class
#'
#' @return return probability of posterior
#' @export
#'
#' @examples
#' Y = Readjpeg(shuffle_train)
#' z = logdmvn(Y, mean, sigma)
#' E = Estep(z, K)
#' 
Estep <- function(Z, p) {
    .Call('_RcppParallelGMM_Estep', PACKAGE = 'RcppParallelGMM', Z, p)
}

#'  Mstep for EM algorithm
#'
#' @param W Density matrix, each (i,j) element corresponds to log conditional probability of jth point w.r.t class i     
#' @param Y The data matrix 
#'
#' @return P Updated probability of each class 
#' @return mean Updated mean of each class
#' @return sigma Updated variance of each class
#' @export
#'
#' @examples
#' Y = Readjpeg(shuffle_train)
#' z = logdmvn(Y, mean, sigma)
#' E = Estep(z, K)
#' M = Mstep(E, Y)
Mstep <- function(W, Y) {
    .Call('_RcppParallelGMM_Mstep', PACKAGE = 'RcppParallelGMM', W, Y)
}
yehanxuan/tamu-689-final documentation built on Dec. 8, 2019, 5:25 p.m.