#' (Weighted) MLE of Kumaraswamy Distribution
#'
#' Kumaraswamy distribution is characterized by the following probability density function,
#' \deqn{f(x;a,b) = a b x^{a-1} (1-x^a)^{b-1}}
#' where the domain is \eqn{x \in (0,1)} with two shape parameters \eqn{a,b > 0}.
#'
#' @param x a length-\eqn{n} vector of values in \eqn{(0,1)}.
#' @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}{nonnegative shape parameter \eqn{a}.}
#' \item{b}{nonnegative shape parameter \eqn{b}.}
#' }
#'
#' @examples
#' # generate data from Unif(0,1)
#' x = stats::runif(100)
#'
#' # fit unweighted
#' Kumaraswamy(x)
#'
#' \dontrun{
#' # put random weights to see effect of weights
#' niter = 500
#' ndata = 200
#'
#' # generate data as above and fit unweighted MLE
#' x = stats::runif(ndata)
#' xmle = Gumbel(x)
#'
#' # iterate
#' vec.a = rep(0,niter)
#' vec.b = rep(0,niter)
#' for (i in 1:niter){
#' # random weight
#' ww = abs(stats::rnorm(ndata))
#'
#' MLE = Kumaraswamy(x, weight=ww)
#' vec.a[i] = MLE$a
#' vec.b[i] = MLE$b
#' 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.a, main="shape 'a'")
#' abline(v=xmle$a, lwd=3, col="red")
#' hist(vec.b, main="shape 'b'")
#' abline(v=xmle$b, lwd=3, col="blue")
#' par(opar)
#' }
#'
#' @author Kisung You
#' @export
Kumaraswamy <- function(x, weight=NULL){
#############################################
# Preprocessing
# inputs
x = handle_cts_bdd("Kumaraswamy", x, lower=0, upper=1, include.boundary = FALSE)
nx = length(x)
weight = handle_weight("Kumaraswamy", weight, nx)
# others
maceps = 10*.Machine$double.eps
#############################################
# Optimize : DEoptim
fopt.Kumaraswamy <- function(pars){
# parameters
a = pars[1]
b = pars[2]
# MLE
loglkd = (a-1)*log(x) + (b-1)*log(1-(x^a)) + log(a) + log(b)
return(-sum(loglkd*weight))
}
sol = DEoptim::DEoptim(fopt.Kumaraswamy, lower=c(maceps, maceps), upper=c(1e+5,1e+5),
control=DEoptim::DEoptim.control(trace=FALSE))$optim$bestmem
#############################################
# Return
output = list()
output$a = as.double(sol[1])
output$b = as.double(sol[2])
return(output)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.