Sha256: d4af04a7b6e5d97f54abbf277d5b425b0b38b910dbeeb1e239b62be38300871d

Contents?: true

Size: 968 Bytes

Versions: 10

Compression:

Stored size: 968 Bytes

Contents

require 'facets/array/probability'

class Array

  # 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.
  #
  #   e = %w{ a b c d e e e }.entropy
  #
  #   ("%.3f" % e)  #=> "2.128"
  #
  # CREDIT: Derek

  def entropy
    arr = self
    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 = self
    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

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/array/entropy.rb
facets-3.1.0 lib/core/facets/array/entropy.rb
facets-3.0.0 lib/core/facets/array/entropy.rb
facets-2.9.3 lib/core/facets/array/entropy.rb
facets-2.9.2 lib/core/facets/array/entropy.rb
facets-2.9.2 src/core/facets/array/entropy.rb
facets-2.9.1 lib/core/facets/array/entropy.rb
facets-2.9.0 lib/core/facets/array/entropy.rb
facets-2.9.0.pre.2 lib/core/facets/array/entropy.rb
facets-2.9.0.pre.1 lib/core/facets/array/entropy.rb