Class: Entropy::ProbabilityMetricSpace
- Inherits:
-
ProbabilitySpace
- Object
- ProbabilitySpace
- Entropy::ProbabilityMetricSpace
- Defined in:
- lib/entropy/metric_space.rb
Overview
Entropy::ProbabilityMetricSpace defines a finite probability metric space
Instance Attribute Summary collapse
-
#distance_function ⇒ Object
readonly
Returns the value of attribute distance_function.
-
#distance_matrix ⇒ Object
readonly
Returns the value of attribute distance_matrix.
Attributes inherited from ProbabilitySpace
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.
-
#define_distance(*args, &blk) ⇒ Object
Defines the distance among the elements of the set.
-
#diversity(alpha) ⇒ Object
Computes the α-Diversity of the space.
-
#initialize ⇒ ProbabilityMetricSpace
constructor
A new instance of ProbabilityMetricSpace.
-
#rao_entropy ⇒ Object
Computes the Rao Entropy of the space.
-
#renyi_entropy(alpha) ⇒ Object
Computes the Renyi Entropy (α-Entropy) of the space.
-
#shannon_entropy ⇒ Object
Computes the Shannon Entropy of the space.
Methods inherited from ProbabilitySpace
Constructor Details
#initialize ⇒ ProbabilityMetricSpace
Returns a new instance of ProbabilityMetricSpace.
10 11 12 13 14 |
# File 'lib/entropy/metric_space.rb', line 10 def initialize @distance_matrix = nil @density = nil super end |
Instance Attribute Details
#distance_function ⇒ Object (readonly)
Returns the value of attribute distance_function.
8 9 10 |
# File 'lib/entropy/metric_space.rb', line 8 def distance_function @distance_function end |
#distance_matrix ⇒ Object (readonly)
Returns the value of attribute distance_matrix.
8 9 10 |
# File 'lib/entropy/metric_space.rb', line 8 def distance_matrix @distance_matrix end |
Instance Method Details
#add_stream(stream) ⇒ Object
Adds a set of elements (char, bit) to the space
17 18 19 |
# File 'lib/entropy/metric_space.rb', line 17 def add_stream(stream) super(stream) end |
#cardinality(alpha) ⇒ Object
Computes the α-Cardinality of the space
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/entropy/metric_space.rb', line 87 def cardinality(alpha) raise "Warning: distance metric not defined..." if @distance_matrix.nil? card = 0 if alpha == 1 card = 1 @prob_space.each_index do |i| card = card * (@density[i] ** (-@prob_space[i])) end elsif alpha == :infinite card = 1.0/@density.max else @prob_space.each_index do |i| card += @prob_space[i] * (@density[i]**(alpha - 1)) end card = card ** (1.0 / (1 - alpha)) if card != 0 end card end |
#define_distance(*args, &blk) ⇒ Object
Defines the distance among the elements of the set. The method accepts a block, so you can use your function to calculate the distance. The function should accept two parameters, viz. the ordinals i,j of the elements and should return a decimal number, the distance between element i and j. In alternative you could pass a matrix of numbers defining the distances among i’s and j’s.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/entropy/metric_space.rb', line 29 def define_distance(*args, &blk) if !blk.nil? @distance_function = blk update_distance_matrix else mat = args[0] raise "distance matrix is not a matrix object" if !mat.respond_to?("square?") raise "distance matrix is not square" if !mat.square? raise "distance matrix has wrong dimensions" if mat.to_a.size != @prob_space.size @distance_matrix = mat end z = @distance_matrix.map { |d| d == :infinite ? 0 : Math.exp(-d) } @density = z * Matrix.column_vector(@prob_space) @density = @density.column(0).to_a end |
#diversity(alpha) ⇒ Object
Computes the α-Diversity of the space
76 77 78 79 80 81 82 83 84 |
# File 'lib/entropy/metric_space.rb', line 76 def diversity(alpha) raise "Warning: distance metric not defined..." if @distance_matrix.nil? div = [] @prob_space.each_index do |i| p = @prob_space[i] div[i] = p * surprise(alpha, @density[i]) end div.inject(:+) end |
#rao_entropy ⇒ Object
Computes the Rao Entropy of the space
51 52 53 |
# File 'lib/entropy/metric_space.rb', line 51 def rao_entropy diversity(2) end |
#renyi_entropy(alpha) ⇒ Object
Computes the Renyi Entropy (α-Entropy) of the space
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/entropy/metric_space.rb', line 57 def renyi_entropy(alpha) raise "Warning: distance metric not defined..." if @distance_matrix.nil? h = 0 if alpha == 1 @prob_space.each_index do |i| h += -@prob_space[i] * Math.log(@density[i]) end elsif alpha == :infinite h = -Math.log(@density.max) else @prob_space.each_index do |i| h += @prob_space[i] * (@density[i]**(alpha - 1)) end h = 1.0 / (1 - alpha) * Math.log(h) end h end |
#shannon_entropy ⇒ Object
Computes the Shannon Entropy of the space
46 47 48 |
# File 'lib/entropy/metric_space.rb', line 46 def shannon_entropy diversity(1) end |