Class: Pest::Estimator::Frequency::Distribution

Inherits:
Object
  • Object
show all
Includes:
Distribution
Defined in:
lib/pest/estimator/frequency.rb

Constant Summary collapse

OFFSET =
0

Instance Attribute Summary collapse

Attributes included from Distribution

#variables

Instance Method Summary collapse

Methods included from Distribution

#batch_probability, #initialize, #variable_array

Instance Attribute Details

#checksumObject (readonly)

Returns the value of attribute checksum.



16
17
18
# File 'lib/pest/estimator/frequency.rb', line 16

def checksum
  @checksum
end

#frequenciesObject (readonly)

Returns the value of attribute frequencies.



16
17
18
# File 'lib/pest/estimator/frequency.rb', line 16

def frequencies
  @frequencies
end

Instance Method Details

#cache_modelObject



18
19
20
21
22
23
24
25
26
# File 'lib/pest/estimator/frequency.rb', line 18

def cache_model
  if @frequencies.nil?
    @frequencies = Hash.new(0)
    @estimator.data.pick(*variable_array).each do |vector|
      # Make sure this vector is consistently ordered
      @frequencies[Array(vector)] += 1
    end
  end
end

#entropyObject



37
38
39
40
41
42
43
# File 'lib/pest/estimator/frequency.rb', line 37

def entropy
  cache_model

  probabilities = probability(unique_event_dataset)

  (-probabilities * NMath.log2(probabilities)).sum
end

#probability(data) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/pest/estimator/frequency.rb', line 28

def probability(data)
  cache_model

  array = NArray[ data.pick(*variable_array).map do |vector|
    @frequencies[Array(vector)].to_f
  end ]
  (OFFSET + array.reshape!(data.length)) / (OFFSET + @estimator.data.length)
end