R/jsd.R

Defines functions jsd

Documented in jsd

#' Estimate Jensen-Shannon divergence
#'
#' Unified front-end for JSD estimation for continuous and discrete variables.
#'
#' @param x First vector.
#' @param y Second vector.
#' @param type One of `"auto"`, `"continuous"`, or `"discrete"`.
#' @param base Logarithm base. Defaults to 2. Use `exp(1)` for nats.
#' @param ... Additional arguments passed to the type-specific estimator.
#'
#' @return An object of class `"jsd_estimate"`.
#' @export
jsd <- function(x, y,
                type = c("auto", "continuous", "discrete"),
                base = 2,
                ...) {
  type <- match.arg(type)

  if (type == "auto") {
    type <- detect_type(x, y)
  }

  out <- switch(
    type,
    continuous = jsd_continuous(x, y, base = base, ...),
    discrete = jsd_discrete(x, y, base = base, ...),
    stop("Unsupported type.")
  )

  out$call <- match.call()
  out
}

Try the jsdtools package in your browser

Any scripts or data that you put into this service are public.

jsdtools documentation built on March 31, 2026, 1:06 a.m.