Class: Statistics::Bin

Inherits:
Object
  • Object
show all
Defined in:
lib/Statistics/Bin.rb

Constant Summary collapse

METHODS =
[:square_root, :cube_root, :freedman_diaconis, :scott, :sturges, :tuneable_root].freeze
DEFAULT_METHOD =
:square_root
DEFAULT_FACTOR =
2.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countObject (readonly)

class << self



115
116
117
# File 'lib/Statistics/Bin.rb', line 115

def count
  @count
end

#intervalObject (readonly)

class << self



115
116
117
# File 'lib/Statistics/Bin.rb', line 115

def interval
  @interval
end

Class Method Details

.bin_for_value(value, bins, bottom_boundary, bin_width) ⇒ Object



44
45
46
47
# File 'lib/Statistics/Bin.rb', line 44

def bin_for_value(value, bins, bottom_boundary, bin_width)
  index = index_for_value(value, bins.count, bottom_boundary, bin_width)
  bins[index]
end

.boundaries(values, bin_width: nil, bin_count: nil, method: :square_root, factor: nil) ⇒ Object



38
39
40
41
42
# File 'lib/Statistics/Bin.rb', line 38

def boundaries(values, bin_width: nil, bin_count: nil, method: :square_root, factor: nil)
  validate!(values, method: method, factor: factor)
  w = bin_width || width(values, bin_count: bin_count, method: method, factor: factor)
  values.first.step(to: values.last + w, by: w).to_a
end

.count(values, method: :square_root, factor: nil) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/Statistics/Bin.rb', line 29

def count(values, method: :square_root, factor: nil)
  validate!(values, method: method, factor: factor)
  if method == :tuneable_root
    tuneable_root_count(values, factor || DEFAULT_FACTOR)
  else
    send("#{method}_count", values)
  end
end

.width(values, bin_width: nil, bin_count: nil, method: :square_root, factor: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/Statistics/Bin.rb', line 14

def width(values, bin_width: nil, bin_count: nil, method: :square_root, factor: nil)
  validate!(values, method: method, factor: factor)
  if bin_width
    bin_width
  elsif bin_count
    data_range(values) / bin_count.to_f
  elsif data_range(values) == 0
    1.0
  elsif method == :tuneable_root
    tuneable_root_width(values, factor || DEFAULT_FACTOR)
  else
    send("#{method}_width", values)
  end
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/Statistics/Bin.rb', line 125

def empty?
  @count == 0
end

#incrementObject



117
118
119
# File 'lib/Statistics/Bin.rb', line 117

def increment
  @count += 1
end

#widthObject



121
122
123
# File 'lib/Statistics/Bin.rb', line 121

def width
  @interval.end - @interval.begin
end