Sha256: 842f111964d4f76504335bc84a0f9920d0cd79eeb04517de9550ceab087e4491

Contents?: true

Size: 973 Bytes

Versions: 1

Compression:

Stored size: 973 Bytes

Contents

#--
# Credit goes to Derek.
#++
require 'nano/enumerable/ideal_entropy'
require 'nano/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  #=>
  #
  def entropy
    arr = self.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
end

class String
  def entropy
    self.split(//).entropy
  end
end



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

  require 'test/unit'

  class TCEnumerable < Test::Unit::TestCase

    def test_entropy
      assert_equal( 1.0, %w{ a b }.entropy )
    end

  end

=end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/enumerable/entropy.rb