Method: MiniHistogram#edges

Defined in:
lib/mini_histogram.rb

#edgesObject Also known as: edge

Finds the “edges” of a given histogram that will mark the boundries for the histogram’s “bins”

Example:

a = [1,1,1, 5, 5, 5, 5, 10, 10, 10]
MiniHistogram.new(a).edges
# => [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0]

There are multiple ways to find edges, this was taken from
https://github.com/mrkn/enumerable-statistics/issues/24

Another good set of implementations is in numpy
https://github.com/numpy/numpy/blob/d9b1e32cb8ef90d6b4a47853241db2a28146a57d/numpy/lib/histograms.py#L222


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/mini_histogram.rb', line 122

def edges
  return @edges if @edges

  return @edges = [0.0] if array.empty?

  lo = @min
  hi = @max

  nbins = sturges.to_f

  if hi == lo
    start = lo
    step = 1.0
    divisor = 1.0
    len = 1
  else
    bw = (hi - lo) / nbins
    lbw = Math.log10(bw)
    if lbw >= 0
      step = 10 ** lbw.floor * 1.0
      r = bw/step

      if r <= 1.1
        # do nothing
      elsif r <= 2.2
        step *= 2.0
      elsif r <= 5.5
        step *= 5.0
      else
        step *= 10
      end
      divisor = 1.0
      start = step * (lo/step).floor
      len = ((hi - start)/step).ceil
    else
      divisor = 10 ** - lbw.floor
      r = bw * divisor
      if r <= 1.1
        # do nothing
      elsif r <= 2.2
        divisor /= 2.0
      elsif r <= 5.5
        divisor /= 5.0
      else
        divisor /= 10.0
      end
      step = 1.0
      start = (lo * divisor).floor
      len = (hi * divisor - start).ceil
    end
  end

  if left_p
    while (lo < start/divisor)
      start -= step
    end

    while (start + (len - 1)*step)/divisor <= hi
      len += 1
    end
  else
    while lo <= start/divisor
      start -= step
    end
    while (start + (len - 1)*step)/divisor < hi
      len += 1
    end
  end

  @edges = []
  len.times.each do
    @edges << start/divisor
    start += step
  end

  return @edges
end