Skip to content

Commit

Permalink
Add entropy-related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
b4winckler committed Jun 5, 2012
1 parent 99dc88e commit 98779db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ export(
applyWell,
bestPeak,
bins,
entropy,
estimatePeaks,
leftPeak,
peakSummary,
partitionEntropy,
plotBins,
plotPeak,
plotPeaks,
Expand Down
22 changes: 22 additions & 0 deletions R/bapp.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,25 @@ randomProfile <- function(binCount, binSize)
{
data.frame(x=rep(1:binCount, binSize), y=rnorm(binCount*binSize))
}

# Compute entropy of a discrete probability distribution function (pdf). If
# 'relative=TRUE' then the result is normalized by the maximum entropy for a
# pdf on 'n' elements, where 'n' is the number of positive entries in 'p'. The
# effect of this normalization is that the returned value lies in the unit
# interval.
entropy <- function(p, relative=FALSE)
{
q <- p[p > 0 & !is.na(p)]
n <- length(q)
ifelse(n > 1, -sum(q * log2(q)) / ifelse(relative, log2(n), 1), 0)
}

# Compute entropy for a partition of 'x' with the given breaks. The 'breaks'
# argument is documented in the help for 'cut'. If 'relative=TRUE' then the
# entropy is normalized to lie within the unit interval.
partitionEntropy <- function(x, breaks, ...)
{
partition <- cut(x, breaks=breaks)
freq <- tapply(x, partition, length)
entropy(freq / sum(freq, na.rm=TRUE), ...)
}

0 comments on commit 98779db

Please sign in to comment.