用R中的分箱(直方图)计算互信息

时间:2015-02-09 18:02:42

标签: r

我有以下代码,其中包含两个函数,用于使用分箱方法计算变量xy的互信息。

entropy <- function(p) {
  if (sum(p) == 0) {
    return(0) 
  }
  p <- p/sum(p) 
  p <- p[p > 0] 
  H = -sum(p*log(p,base=2))
  return(H)
}

mutual.info <- function(counts) {

  marginal.entropy = entropy(rowSums(counts))
  probs <- colSums(counts)/sum(counts)
  column.entropies = apply(counts,2,entropy)
  conditional.entropy = sum(probs*column.entropies)
  mutual.information = marginal.entropy - conditional.entropy
  return(mutual.information)
}

x=c(0.0,0.2,0.4,0.6,0.8,1.0)
y=c(0.8,1.0,0.6,0.4,0.0,0.2)

xc<-quantile(x,(0:3)/3)
yc<-quantile(y,(0:3)/3)

xbin<-summary(cut(x,xc,include.lowest=TRUE))
ybin<-summary(cut(y,yc,include.lowest=TRUE))

mi <- as.matrix(cbind(xbin,ybin))

mutual.info(mi)

我想要一些人帮我找到上面代码的互信息计算问题,它总是给我零,但答案是〜1.58使用bins = 3.

谢谢,

0 个答案:

没有答案