Sha256: b66529a7a72fd945199be5e87dad5231cb2e55d501c3613af004e349802ef796

Contents?: true

Size: 945 Bytes

Versions: 4

Compression:

Stored size: 945 Bytes

Contents

require 'facets/enumerable/probability'

module Enumerable

  # Shannon's entropy for an array - returns the average
  # bits per symbol required to encode the array.
  # Lower values mean less "entropy" - i.e. less unique
  # information in the array.
  #
  #   %w{ a b c d e e e }.entropy  #=>
  #
  #   CREDIT: Derek

  def entropy
    arr = to_a
    probHash = arr.probability
    # h is the Shannon entropy of the array
    h = -1.to_f * probHash.keys.inject(0.to_f) do |sum, i|
      sum + (probHash[i] * (Math.log(probHash[i])/Math.log(2.to_f)))
    end
    h
  end

  # Returns the maximum possible Shannon entropy of the array
  # with given size assuming that it is an "order-0" source
  # (each element is selected independently of the next).
  #
  #   CREDIT: Derek

  def ideal_entropy
    arr = to_a
    unitProb = 1.0.to_f / arr.size.to_f
    (-1.to_f * arr.size.to_f * unitProb * Math.log(unitProb)/Math.log(2.to_f))
  end

end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
facets-2.4.3 lib/core/facets/enumerable/entropy.rb
facets-2.4.4 lib/core/facets/enumerable/entropy.rb
facets-2.4.5 lib/core/facets/enumerable/entropy.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/enumerable/entropy.rb