R/histogram.distances.R

Defines functions histogram.chisq histogram.mse histogram.ks histogram.jaccard histogram.intersection

#' Histogram intersection
#' @param x histogram 1
#' @param y histogram 2
histogram.intersection <- function(x, y) {
  overlap = pmin(a = x, b = y, na.rm = T)
  # sums = c(sum(x), sum(y))
  # If one of the histograms is all zero, then return the other count
  1 - sum(overlap) / sum(x)
}

#' Jaccard index
#' @param x histogram 1
#' @param y histogram 2
histogram.jaccard <- function(x, y) {
  overlap = pmin(a = x, b = y, na.rm = T)
  union = pmax(a = x, b = y, na.rm = T)
  1 - sum(overlap)/sum(union)
}

#' Kolmogorov-Smirnov Divergance
#' @param x histogram 1
#' @param y histogram 2
histogram.ks <- function(x, y) {
  max(abs(x - y), na.rm = TRUE)
}

#' Mean-squared error
histogram.mse <- function(x, y) {
  mean((x - y)^2)
}

#' Chi-Squared
histogram.chisq <- function(x, y) {
  sum((x - y)^2 / (x + y))
}
helen-zhu/ConsensusPeaks documentation built on Dec. 25, 2021, 9:39 a.m.