R/normalize_weight.R

Defines functions normalize_weight

Documented in normalize_weight

#'@rdname normalize_weight
#'@title Normalize vector of log-weights
#'@description Takes a vector of real values, and return the vector of normalized exponentiated values,
#' as well as average of the exponentiated unnormalized values.
#'@param logweights is a vector of n real values
#'@return a vector of n non-negative values summing to one
#'@examples
#' N <- 1000
#' logweights <- rnorm(N)
#' normalize_weight_results <- normalize_weight(logweights)
#' normalized_weights <- normalize_weight_results$nw
#'@export
normalize_weight <- function(logweights){
  mlw <- max(logweights)
  avew <- mlw + log(mean(exp(logweights - mlw))) # weight average
  w <- exp(logweights - mlw)
  nw <- w / sum(w) # normalized weights
  return(list(nw = nw, avew = avew))
}
pierrejacob/PET documentation built on May 25, 2019, 11:35 p.m.