R/Bernoulli.R

Defines functions Bernoulli

Documented in Bernoulli

#' (Weighted) MLE of Bernoulli Distribution
#' 
#' Bernoulli distribution is characterized by the following probability mass function,
#' \deqn{f(x;p) = p^x (1-p)^{1-x}}
#' where the domain is \eqn{x \in \lbrace 0,1 \rbrace} with proportion parameter \eqn{p \in [0,1].}
#' 
#' @param x a length-\eqn{n} vector of values \eqn{\lbrace 0, 1 \rbrace}.
#' @param weight a length-\eqn{n} weight vector. If set as \code{NULL}, it gives an equal weight, leading to standard MLE.
#' 
#' @return a named list containing (weighted) MLE of \describe{
#' \item{p}{proportion parameter \eqn{p}.}
#' }
#' 
#' @examples
#' #  generate data with p=0.5
#' x = sample(0:1, 50, replace=TRUE, prob=c(0.5,0.5))
#' 
#' #  fit unweighted
#' Bernoulli(x)
#' 
#' \dontrun{
#' # put random weights to see effect of weights
#' niter = 1000
#' ndata = 200
#' 
#' # generate data as above and fit unweighted MLE
#' x    = sample(0:1, ndata, replace=TRUE, prob=c(0.5,0.5))
#' xmle = Bernoulli(x)
#' 
#' # iterate
#' vec.p = rep(0,niter)
#' for (i in 1:niter){
#'   # random weight
#'   ww = abs(stats::rnorm(ndata))
#'   
#'   # fit
#'   MLE = Bernoulli(x, weight=ww)
#'   vec.p[i] = MLE$p
#'   if ((i%%10) == 0){
#'     print(paste0(" iteration ",i,"/",niter," complete.."))
#'   }
#' }
#' 
#' # distribution of weighted estimates + standard MLE
#' opar <- par(no.readonly=TRUE)
#' hist(vec.p, main="proportion 'p'")
#' abline(v=xmle$p, lwd=3, col="red")
#' par(opar)
#' } 
#' 
#' @author Kisung You
#' @export
Bernoulli <- function(x, weight=NULL){
  #############################################
  # Preprocessing
  x  = handle_binary("Bernoulli", x)
  nx = length(x)
  weight = handle_weight("Bernoulli", weight, nx)
  maceps = 10*.Machine$double.eps
  
  #############################################
  # Optimize : R's optimize
  fopt.Bernoulli <- function(p){
    loglkd = x*log(p) + (1-x)*log(1-p)
    return(sum(loglkd*weight))
  }
  sol = stats::optimize(fopt.Bernoulli, lower=maceps, upper=(1-maceps), maximum = TRUE)
  
  #############################################
  # Return
  output = list()
  output$p = as.double(sol$maximum)
  return(output)
}
kyoustat/T4mle documentation built on March 26, 2020, 12:09 a.m.