Class: FlatKit::StatType::NominalStats
Overview
Internal: Status object to keep track of the count and frequency of values.
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
for, nominal_types, numerical_types, ordinal_types, #to_hash, #to_json
Constructor Details
#initialize(collecting_frequencies: false) ⇒ NominalStats
Returns a new instance of NominalStats.
18
19
20
21
22
23
24
|
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 18
def initialize(collecting_frequencies: false)
super()
@mutex = Mutex.new
@count = 0
@collecting_frequencies = collecting_frequencies
@frequencies = Hash.new(0)
end
|
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
8
9
10
|
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 8
def count
@count
end
|
Class Method Details
.all_stats ⇒ Object
14
15
16
|
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 14
def self.all_stats
@all_stats ||= %w[count unique_count unique_values mode]
end
|
.default_stats ⇒ Object
10
11
12
|
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 10
def self.default_stats
@default_stats ||= %w[count]
end
|
Instance Method Details
#collected_stats ⇒ Object
26
27
28
29
30
|
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 26
def collected_stats
return self.class.default_stats unless @collecting_frequencies
self.class.all_stats
end
|
#frequencies ⇒ Object
50
51
52
53
54
|
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 50
def frequencies
return nil unless @collecting_frequencies
@frequencies
end
|
#mode ⇒ Object
32
33
34
35
36
|
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 32
def mode
return nil unless @collecting_frequencies
@frequencies.max_by { |_item, item_count| item_count }.first
end
|
#unique_count ⇒ Object
38
39
40
41
42
|
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 38
def unique_count
return nil unless @collecting_frequencies
@frequencies.size
end
|
#unique_values ⇒ Object
44
45
46
47
48
|
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 44
def unique_values
return nil unless @collecting_frequencies
@frequencies.keys
end
|
#update(value) ⇒ Object
56
57
58
59
60
61
|
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 56
def update(value)
@mutex.synchronize do
@count += 1
@frequencies[value] += 1 if @collecting_frequencies
end
end
|