#' (Weighted) MLE of Lévy Distribution
#'
#' Lévy distribution is characterized by the following probability density function,
#' \deqn{f(x;\mu,c) = \sqrt{\frac{c}{2\pi}} \frac{e^{-\frac{c}{2(x-\mu)}}}{(x-\mu)^{3/2}}}
#' where the domain is \eqn{x \in [\mu,\infty)} with two parameters \eqn{\mu} for location and \eqn{c} for scale.
#'
#' @param x a length-\eqn{n} vector of values in \eqn{[\mu,\infty)}. Due to its dependence on parameter \eqn{\mu}, actual input can be any real-valued vector.
#' @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{c}{scale parameter \eqn{c}.}
#' }
#'
#' @examples
#' # generate data from exponential distribution and shift
#' x = abs(stats::rexp(100)) + 2
#'
#' # fit unweighted
#' Levy(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::rexp(ndata)) + 2
#' xmle = Levy(x)
#'
#' # iterate
#' vec.mu = rep(0,niter)
#' vec.c = rep(0,niter)
#' for (i in 1:niter){
#' # random weight
#' ww = abs(stats::rnorm(ndata))
#'
#' MLE = Levy(x, weight=ww)
#' vec.mu[i] = MLE$mu
#' vec.c[i] = MLE$c
#' 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.c, main="scale 'c'")
#' abline(v=xmle$c, lwd=3, col="blue")
#' par(opar)
#' }
#'
#' @author Kisung You
#' @export
Levy <- function(x, weight=NULL){
#############################################
# Preprocessing
x = handle_cts("Levy", x) # nonnegative real numbers
nx = length(x)
weight = handle_weight("Levy", weight, nx)
maceps = 10*.Machine$double.eps
#############################################
# Optimize : DEoptim
fopt.Levy <- function(pars){
# parameters
mu = pars[1]
c = pars[2]
# log-likelihood
xx = x[(x>=mu)]
term1 = 0.5*(log(c)-log(2*pi))
term2 = -c/(2*(xx-mu))
term3 = -(3/2)*log(xx-mu)
loglkd = term1+term2+term3
# return
return(-sum(loglkd*weight))
}
xmin = min(x)
sol = DEoptim::DEoptim(fopt.Levy, lower=c(min(-1e+5,xmin-1), maceps), upper=c(xmin,1e+5),
control=DEoptim::DEoptim.control(trace=FALSE))$optim$bestmem
#############################################
# Return
output = list()
output$mu = as.double(sol[1])
output$c = as.double(sol[2])
return(output)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.