#' Compute Rényi's index for a wealth vector
#'
#' @param w Numeric vector of wealth values (non-negative, at least one positive).
#' @param alpha Positive parameter controlling the sensitivity of the index.
#' @return The Rényi index, a number between 0 and 1.
#' @references
#' Eliazar, Iddo. "Randomness, evenness, and Rényi’s index." Physica A: Statistical Mechanics and its Applications 390.11 (2011): 1982-1990.
#' @examples
#' renyiindex(runif(100,0.1,1), 2)
#' @export
renyiindex <- function(w, alpha) {
  # Input validation
  if (any(w < 0)) stop("Wealth values must be non-negative.")
  if (sum(w) == 0) stop("Total wealth cannot be zero.")
  if (alpha <= 0) stop("Alpha must be positive.")

  n <- length(w)
  mean_w <- mean(w)                     # \bar{w}
  x <- w / mean_w                       # w_i / \bar{w}

  if (alpha == 1) {
    # x * log(x) is defined as 0 when x == 0
    term <- sum(ifelse(x == 0, 0, x * log(x))) / n
    I <- 1 - exp(-term)
  } else {
    term <- sum(x^alpha) / n
    # term^(1/(1-alpha)) = exp(log(term)/(1-alpha))
    I <- 1 - term^(1/(1 - alpha))
  }

  return(I)
}
