R/GenGamma.R

Defines functions GenGamma

Documented in GenGamma

#' (Weighted) MLE of Generalized Gamma Distribution
#' 
#' Generalized Gamma distribution is characterized by the following probability density function,
#' \deqn{f(x;a,d,p) = \frac{p/ a^d }{\Gamma(d/p)} x^{d-1} \exp\left( -(x/a)^p \right)}
#' where the domain is \eqn{x \in (0,\infty)} with three parameters 
#' \eqn{a > 0} for scale and \eqn{d, p > 0}.
#' 
#' @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{a}{scale parameter \eqn{a}.}
#' \item{b}{parameter \eqn{b}.}
#' \item{p}{parameter \eqn{p}.}
#' }
#' 
#' @examples
#' #  generate data from half normal distribution
#' x = abs(stats::rnorm(100))
#' 
#' #  fit unweighted
#' GenGamma(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 = GenGamma(x)
#' 
#' # iterate
#' vec.a = rep(0,niter)
#' vec.d = rep(0,niter)
#' vec.p = rep(0,niter)
#' for (i in 1:niter){
#'   # random weight
#'   ww = abs(stats::rnorm(ndata))
#' 
#'   MLE = GenGamma(x, weight=ww)
#'   vec.a[i] = MLE$a
#'   vec.d[i] = MLE$d
#'   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)
#' par(mfrow=c(1,3))
#' hist(vec.a, main="scale 'a'")
#' abline(v=xmle$a, lwd=3, col="red")
#' hist(vec.d, main="'d'")
#' abline(v=xmle$d, lwd=3, col="blue")
#' hist(vec.p, main="'p'")
#' abline(v=xmle$p, lwd=3, col="green")
#' par(opar)
#' } 
#' 
#' @author Kisung You
#' @export
GenGamma <- function(x, weight=NULL){
  #############################################
  # Preprocessing
  x      = handle_cts_pos("InvGamma", x) # nonnegative real numbers
  nx     = length(x)
  weight = handle_weight("InvGamma", weight, nx)
  maceps = 10*.Machine$double.eps
  
  #############################################
  # Optimize : DEoptim
  fopt.GenGamma <- function(pars){
    # parameters
    a = pars[1]
    d = pars[2]
    p = pars[3]
    # log-likelihood
    term1 = log(p) - (d*log(a)) - base::lgamma(d/p)
    term2 = (d-1)*log(x)
    term3 = -((x/a)^p)
    loglkd = term1+term2+term3
    # return
    return(-sum(loglkd*weight))
  }
  mylower = c(maceps, maceps, maceps)
  myupper = c(1e+2, 1e+2, 1e+2)
  sol = DEoptim::DEoptim(fopt.GenGamma, lower=mylower, upper=myupper, 
                         control=DEoptim::DEoptim.control(trace=FALSE))$optim$bestmem
  
  #############################################
  # Return
  output = list()
  output$a = as.double(sol[1])
  output$d = as.double(sol[2])
  output$p = as.double(sol[3])
  return(output)
}
kyoustat/T4mle documentation built on March 26, 2020, 12:09 a.m.