We are hiring ! See our job offers.
https://github.com/cran/FSelector
Raw File
Tip revision: c3894396287d55ab31393f3317dbaa4bab83caed authored by Lars Kotthoff on 28 February 2013, 00:00:00 UTC
version 0.19
Tip revision: c389439
entropy.R
# H(C)
entropy <- function(vec) {
	probs = table(vec)
	data_length = sum(probs)
	probs = probs / data_length
	return(sum(sapply(probs, function(t) {
			p = as.numeric(t)
			return(- p * log2(p))
		})))
}

# H(A,B)
joint.entropy <- function(a, b) {
	data_length = length(a)
	probs = as.vector(table(a, b))
	probs = probs / sum(probs)
	return(sum(sapply(probs, function(t) {
			p = as.numeric(t)
			return(
				if(p == 0) {
					0
				} else {
					- p * log2(p)
				}
			)
		})))

}
back to top