R/FoldedNormal.R

Defines functions FoldedNormal

Documented in FoldedNormal

#' (Weighted) MLE of Folded Normal Distribution
#' 
#' Folded-Normal distribution is characterized by the following probability density function,
#' \deqn{f(x;\mu,\sigma) = \frac{1}{\sigma \sqrt{2\pi}} \exp\left( -\frac{(x-\mu)^2}{2\sigma^2} \right) + \frac{1}{\sigma \sqrt{2\pi}} \exp\left( -\frac{(x+\mu)^2}{2\sigma^2} \right)}
#' where the domain is \eqn{x \in [0,\infty)} with two parameters \eqn{\mu} for location and \eqn{\sigma > 0} for scale.
#' 
#' @param x a length-\eqn{n} vector of values in \eqn{[0,\infty)}.
#' @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{mu}{location parameter \eqn{\mu}.}
#' \item{sigma}{scale parameter \eqn{\sigma}.}
#' }
#' 
#' @examples
#' #  generate data from half-normal distribution
#' x = abs(stats::rnorm(100))
#' 
#' #  fit unweighted
#' FoldedNormal(x)
#' 
#' \dontrun{
#' # put random weights to see effect of weights
#' niter = 500
#' ndata = 200
#' 
#' # generate data as above and fit unweighted MLE
#' x    = abs(stats::rnorm(ndata))
#' xmle = FoldedNormal(x)
#' 
#' # iterate
#' vec.mu  = rep(0,niter)
#' vec.sig = rep(0,niter)
#' for (i in 1:niter){
#'   # random weight
#'   ww = abs(stats::rnorm(ndata))
#' 
#'   MLE = FoldedNormal(x, weight=ww)
#'   vec.mu[i]  = MLE$mu
#'   vec.sig[i] = MLE$sigma
#'   if ((i%%10) == 0){
#'     print(paste0(" iteration ",i,"/",niter," complete.."))
#'   }
#' }
#' 
#' # distribution of weighted estimates + standard MLE
#' opar <- par(no.readonly=TRUE)
#' par(mfrow=c(1,2))
#' hist(vec.mu, main="location 'mu'")
#' abline(v=xmle$mu,    lwd=3, col="red")
#' hist(vec.sig,  main="scale 'sigma'")
#' abline(v=xmle$sigma, lwd=3, col="blue")
#' par(opar)
#' } 
#' 
#' @author Kisung You
#' @export
FoldedNormal <- function(x, weight=NULL){
  #############################################
  # Preprocessing
  x      = handle_cts_nonneg("FoldedNormal", x) # nonnegative real numbers
  nx     = length(x)
  weight = handle_weight("FoldedNormal", weight, nx)
  maceps = 10*.Machine$double.eps
  
  #############################################
  # Optimize : DEoptim
  fopt.FoldedNormal <- function(pars){
    # parameters
    mu    = pars[1]
    sigma = pars[2]
    # log-likelihood
    term1 = 2*(-0.5*log(2*pi) - log(sigma))
    term2 = -((x-mu)^2)/(2*(sigma^2))
    term3 = -((x+mu)^2)/(2*(sigma^2))
    loglkd = term1+term2+term3
    # return
    return(-sum(loglkd*weight))
  }
  sol = DEoptim::DEoptim(fopt.FoldedNormal, lower=c(-1e+5, maceps), upper=c(1e+5,1e+5), 
                         control=DEoptim::DEoptim.control(trace=FALSE))$optim$bestmem
  #############################################
  # Return
  output = list()
  output$mu    = as.double(sol[1])
  output$sigma = as.double(sol[2])
  return(output)
}
kyoustat/T4mle documentation built on March 26, 2020, 12:09 a.m.