Sha256: 3a62c7a7664895712e187e0b0a949841947b040b3e8b082885249885a94e23fd

Contents?: true

Size: 910 Bytes

Versions: 26

Compression:

Stored size: 910 Bytes

Contents

module Enumerable

  # 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 goes to Derek.
  #++
  def ideal_entropy
    arr = self.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

# Not needed b/c ideal_entropy is only dependent on size.
# class String
#   def ideal_entropy
#     self.split(//).ideal_entropy
#   end
# end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCEnumerable < Test::Unit::TestCase

    def test_ideal_entropy
      assert_equal( 1.0, %w{ a b }.ideal_entropy )
      assert_equal( 2.0, %w{ a a b b }.ideal_entropy )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-1.0.0 lib/facet/enumerable/ideal_entropy.rb
facets-0.9.0 lib/nano/enumerable/ideal_entropy.rb
facets-1.0.3 packages/core/lib/facet/enumerable/ideal_entropy.rb
facets-1.2.1 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.2.0 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.3.0 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.1.0 lib/facet/enumerable/ideal_entropy.rb
facets-1.3.1 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.3.3 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.3.2 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.4.0 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.4.2 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.4.1 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.4.3 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.4.5 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.4.4 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.7.30 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.7.38 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.7.0 lib/facets/core/enumerable/ideal_entropy.rb
facets-1.7.46 lib/facets/core/enumerable/ideal_entropy.rb