Module: MoreMath::Entropy
- Defined in:
- lib/more_math/entropy.rb
Instance Method Summary collapse
Instance Method Details
#entropy(text) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/more_math/entropy.rb', line 3 def entropy(text) chars = text.chars size = chars.size chars.each_with_object(Hash.new(0.0)) { |c, h| h[c] += 1 }. each_value.reduce(0.0) do |entropy, count| frequency = count / size entropy + frequency * Math.log2(frequency) end.abs end |
#entropy_ideal(size) ⇒ Object
14 15 16 17 18 |
# File 'lib/more_math/entropy.rb', line 14 def entropy_ideal(size) size <= 1 and return 0.0 frequency = 1.0 / size -1.0 * size * frequency * Math.log2(frequency) end |
#entropy_ratio(text) ⇒ Object
20 21 22 23 24 |
# File 'lib/more_math/entropy.rb', line 20 def entropy_ratio(text) size = text.each_char.size size <= 1 and return 0.0 entropy(text) / entropy_ideal(size) end |