Class: Entropy::ProbabilitySpace

Inherits:
Object
  • Object
show all
Defined in:
lib/entropy/probability_space.rb

Overview

Entropy::ProbabilitySpace defines a finite probability space

Direct Known Subclasses

ProbabilityMetricSpace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProbabilitySpace

Returns a new instance of ProbabilitySpace.



8
9
10
11
12
# File 'lib/entropy/probability_space.rb', line 8

def initialize
  @space = Entropy::Stream.new
  @dict = Entropy::Dict.new()
  @prob_space = []
end

Instance Attribute Details

#prob_spaceObject

Returns the value of attribute prob_space.



6
7
8
# File 'lib/entropy/probability_space.rb', line 6

def prob_space
  @prob_space
end

Instance Method Details

#add_stream(stream) ⇒ Object

Adds a set of elements (char, bit) to the space



15
16
17
18
19
# File 'lib/entropy/probability_space.rb', line 15

def add_stream(stream)
  puts "Adding a set in the probability space..."
  @space.add_stream(stream)
  update_probability_space(stream)
end

#cardinality(alpha) ⇒ Object

Computes the α-Cardinality of the space



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/entropy/probability_space.rb', line 60

def cardinality(alpha)
  card = 0
  if alpha == 1
    card = @prob_space.map {|p| p**(-p)}.inject(1, :*)
  elsif alpha == :infinite
    card = 1.0/@prob_space.max
  else
    card = @prob_space.map {|p| p**alpha}.inject(:+)
    card = card ** (1.0 / (1 - alpha))
  end
  card
end

#diversity(alpha) ⇒ Object

Computes the α-Diversity of the space



41
42
43
# File 'lib/entropy/probability_space.rb', line 41

def diversity(alpha)
  @prob_space.map {|p| p * surprise(alpha, p) }.inject(:+)
end

#renyi_entropy(alpha) ⇒ Object

Computes the Renyi Entropy (α-Entropy) of the space



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/entropy/probability_space.rb', line 27

def renyi_entropy(alpha)
  h = 0
    if alpha == 1
      h = @prob_space.map {|p| -p * Math.log(p)}.inject(:+)
    elsif alpha == :infinite
      h = -Math.log(@prob_space.max)
    else
      h =  @prob_space.map {|p| p**alpha}.inject(:+)
      h = 1.0 / (1 - alpha) * Math.log(h)
    end
  h
end

#shannon_entropyObject

Computes the Shannon Entropy of the space



22
23
24
# File 'lib/entropy/probability_space.rb', line 22

def shannon_entropy
  diversity(1)
end

#surprise(alpha, p) ⇒ Object

Surprise function



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/entropy/probability_space.rb', line 46

def surprise(alpha, p)
  s = 0
  if alpha == 1
    s = -Math.log(p)
  elsif alpha == :infinite
    s = 0
  else
    e = alpha - 1
    s = 1.0 / e * (1 - p**e)
  end
  s
end