counter entropy and TrackedStatistics

10 views
Skip to first unread message

Steven Bethard

unread,
Jul 8, 2010, 8:23:26 AM7/8/10
to scal...@googlegroups.com
Is there a method somewhere for the entropy of a counter? I didn't see one, so I ended up writing my own:

def entropy[K, V](map: MutablePartialMap[K, V])(implicit numeric: Numeric[V]) = {
var entropy = 0.0
val log2 = math.log(2)
val total = numeric.toDouble(map.valuesIterator.sum)
for ((k, v) <- map; if v != 0.0) {
val count = numeric.toDouble(v) / total
entropy -= count * math.log(count) / log2
}
entropy
}

This wouldn't have to calculate the total every time if TrackedStatistics and TrackedIntStatistics played better with Numeric - that is, if there was just a single TrackedStatistics.Total that returned a total of type V.

Steve


Daniel Ramage

unread,
Jul 8, 2010, 2:33:49 PM7/8/10
to scal...@googlegroups.com
Thanks, Steven. Counters are getting folded into scalala proper in
the next release, so your comment about playing nice with Numeric is a
good observation. Not sure where this method should live - either as
a method on the appropriate data type or as a library call - but one
optimization that'll save you some cycles in the meantime is to divide
by math.log(2) at the end.

dan

> --
> You received this message because you are subscribed to the Google Groups "ScalaNLP" group.
> To post to this group, send email to scal...@googlegroups.com.
> To unsubscribe from this group, send email to scalanlp+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/scalanlp?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages