robertness
11/10/2014 - 10:39 PM

Interaction Information calculation on Markov blankets of nodes in Bayesian network

Interaction Information calculation on Markov blankets of nodes in Bayesian network

#Calculated by using inclusion-exclusion sum of joint entropy on subsets of variables
#alarmBN is of the bnfit class of class bnlearn
#Converts to independence network grain class of gRain, computes probabilities through logic sampling algorithm

require(utils)
require(gRain)
getEntropy <- function(probDist, base=exp(1)){
  -sum(log(probDist, base=base) * probDist)
}
jtree <- compile(as.grain(alarmBN))
error.table$interactionInformation <- sapply(names(alarm), function(node){
  nodeMB <- c(node, mb(alarmBN, node))
  mbSize <- length(nodeMB)
  levelList <- lapply(nodeMB, function(node) levels(test[, node]))
  names(levelList) <- nodeMB
  nWiseEntropies <- NULL  
  for(i in 1:length(nodeMB)){
    iWiseEntropies <- combn(nodeMB, i, function(MBSubset){
      MBsubsetList <- levelList[MBSubset]
      mbCombinations <- expand.grid(MBsubsetList, stringsAsFactors = FALSE)
      mbCombinations
      jointDist <- apply(mbCombinations, 1, function(row){
        evidence.grain <- setFinding(jtree, nodes = MBSubset, states = row)
        pEvidence(evidence.grain)
      })
      subsetEntropy <- getEntropy(jointDist)
      subsetEntropy
      })
    names(iWiseEntropies) <- combn(nodeMB, i, function(MBSubset) paste(MBSubset, collapse ="-"))
    addSubtract <- (-1)^(mbSize - i)
    nWiseEntropies <- c(nWiseEntropies, addSubtract * iWiseEntropies)
  }
  interactionInformation <- sum(nWiseEntropies)
  interactionInformation
})