Class: Array

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

Overview

Array.

This class extends the standard Ruby Array with a method to remove extreme values using Tukey's method (also known as the Tukey fence for outlier detection). The method calculates the interquartile range and filters out values that fall outside 1.5 times the IQR from the first and third quartiles.

For example:

require 'tukeyized' [1, 6, 3, 8888, 3, 2, 8, -19292].tukeyized

=> [1, 6, 3, 3, 2, 8]

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright (c) 2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Instance Method Details

#tukeyizedArray

Removes extreme values using Tukey's method.

This method identifies and removes outliers from the array by calculating the first quartile (Q1), third quartile (Q3), and interquartile range (IQR). Values outside the range [Q1 - 1.5IQR, Q3 + 1.5IQR] are considered outliers and are excluded from the result.

The original array is not modified; a new array is returned.

For example:

[1, 2, 3, 4, 5, 100].tukeyized

=> [1, 2, 3, 4, 5]

[10, 20, 30, 40, 50].tukeyized

=> [10, 20, 30, 40, 50]

[].tukeyized

=> []

Returns:

  • (Array)

    New array without extreme values



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tukeyized.rb', line 45

def tukeyized
  return self if count < 3
  percentile = lambda do |a, x|
    k = (x / 100.0) * (a.length - 1)
    f = k.floor
    c = k.ceil
    a[f] + ((a[c] - a[f]) * (k - f))
  end
  a = sort
  q1 = percentile.call(a, 25)
  q3 = percentile.call(a, 75)
  iqr = q3 - q1
  lower = q1 - (1.5 * iqr)
  upper = q3 + (1.5 * iqr)
  select { |x| x.between?(lower, upper) }
end