Class: Entropy::ProbabilitySpace
- Inherits:
-
Object
- Object
- Entropy::ProbabilitySpace
- Defined in:
- lib/entropy/probability_space.rb
Overview
Entropy::ProbabilitySpace defines a finite probability space
Direct Known Subclasses
Instance Attribute Summary collapse
-
#prob_space ⇒ Object
Returns the value of attribute prob_space.
Instance Method Summary collapse
-
#add_stream(stream) ⇒ Object
Adds a set of elements (char, bit) to the space.
-
#cardinality(alpha) ⇒ Object
Computes the α-Cardinality of the space.
-
#diversity(alpha) ⇒ Object
Computes the α-Diversity of the space.
-
#initialize ⇒ ProbabilitySpace
constructor
A new instance of ProbabilitySpace.
-
#renyi_entropy(alpha) ⇒ Object
Computes the Renyi Entropy (α-Entropy) of the space.
-
#shannon_entropy ⇒ Object
Computes the Shannon Entropy of the space.
-
#surprise(alpha, p) ⇒ Object
Surprise function.
Constructor Details
Instance Attribute Details
#prob_space ⇒ Object
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_entropy ⇒ Object
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 |