R/stick_breaking.R

Defines functions stick_breaking

Documented in stick_breaking

#' Stick Breaking Representation for Dirichlet Process
#'
#' @param V
#' @param K
#'
#' @return
#' @export
#'
#' @examples
stick_breaking <- function(V, K) {

    pi <- rep(NA, K)

    for(k in 1:K) {
        if(k == 1){
            pi[k] <- V[k]
        } else {
            stick_prob <- V[k]
            for(j in 1:(k-1)){
                stick_prob <- stick_prob*(1 - V[j])
            }
            pi[k] <- stick_prob
        }
    }
    pi[K] = 1 - sum(pi[1:(K-1)])

    return(pi)
}
michaelellis003/VBMM documentation built on March 20, 2022, 4:09 a.m.