class Source

A Source contains attributes of a sequence of bytes, called symbols.

Attributes

entropy[R]

The entropy of the probability distribution.

frequencies[R]

The frequency distribution of symbols.

length[R]

The length of the given sequence of symbols.

probabilities[R]

The empirical probability distribution of symbols.

Public Class Methods

new(symbols) click to toggle source

Analyze sequence generated by `symbols.each_byte'.

# File source.rb, line 9
def initialize(symbols)
    freq = @frequencies = Hash.new(0)
    prob = @probabilities = Hash.new
    symbols.each_byte { |symbol| freq[symbol.chr] += 1 }
    @length = freq.values.reduce(:+)
    freq.each { |symbol, n| prob[symbol] = n / @length.to_f }
    @entropy = prob.values.inject(0){ |sum, p| sum - p * Math.log2(p) }
end