-
Notifications
You must be signed in to change notification settings - Fork 3
/
mutualinfo.R
25 lines (25 loc) · 956 Bytes
/
mutualinfo.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
mutualinfo <- function(probs, A, B, base=2){
#### Returns the mutual information between two sets of variates A, B
## load list of variates
variates <- names(dimnames(probs))
## Sanity checks of input arguments
if(!all(A %in% variates)){
message('Discarding A variates not present in probs')
A <- A[A %in% variates]
}
if(!all(B %in% variates)){
message('Discarding B variates not present in probs')
B <- B[B %in% variates]
}
if(any(A %in% B)){
stop('A and B have variates in common.')
}
## Calculate marginal joint probability for (A,B) if necessary
probs <- apply(probs, c(A,B), sum)
probs <- probs/sum(probs)
sum(probs * (log2(probs) - log2(outer(
apply(probs, A, sum),
apply(probs, B, sum),
`*`
))), na.rm=TRUE)/log2(base)
}