savanna-outliers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/lib/savanna-outliers.rb +1 -0
- data/lib/savanna-outliers/chauvenets.rb +27 -0
- data/lib/savanna-outliers/core.rb +50 -0
- data/lib/savanna-outliers/general_parameters.rb +25 -0
- data/lib/savanna-outliers/grubbs.rb +43 -0
- data/lib/savanna-outliers/operations.rb +21 -0
- data/lib/savanna-outliers/savanna_outliers.rb +132 -0
- data/lib/savanna-outliers/version.rb +5 -0
- data/spec/savanna-outliers/core_spec.rb +100 -0
- data/spec/savanna-outliers/outliers_spec.rb +141 -0
- data/spec/spec_helper.rb +3 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YjhlN2E2NjM5Zjg4NTM4ODUzM2NkM2E1ZmQyNmQ0ZTgyM2YyZDhhOQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NWY1NjY2MjFiNDM2M2U4OTk4YTZmNTAyZjI3ZjYxNDc0MzU2YWZhMA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YjMzMjJiNTUzYzBmMGUyODJlODI5ZmJmMzQwNWM1OGViYmE3MmI2YjZjMjQx
|
10
|
+
MjE2YTNmOWFjMWUzZDAzZjM2YjI0NTFkNzcwNDczZWY1ODMwMDY4OGY5N2Q4
|
11
|
+
ODZmMWIyZTU2MDY5YjQ5OGYyOWVkNGVmY2RjNjNiNTZkYzY1ZGE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
Mjg5OWQzMDY2NTU2ZDI3OTYzNmIxMDAxNDUyYmUwNGE3ZTA1MDQwNzBiYjY0
|
14
|
+
ZTg2YTRmNDc3MjNkZTU5OTJlYTgxYjFhZjg0OGM4MTliZWQyNDQxYTlkOWI4
|
15
|
+
ZDlhNGE3NzE1ZDllM2M0YjMzMTM2MDI3YWUyNjRjZGQzZTVlYWE=
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'savanna-outliers/savanna_outliers'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Savanna
|
2
|
+
module Outliers
|
3
|
+
module Chauvenets
|
4
|
+
def chauvenets_criterion_min
|
5
|
+
delta = (mean - array.min).abs
|
6
|
+
(1/standard_deviation)*Statistics2.normaldist((delta - mean)/standard_deviation)*size
|
7
|
+
end
|
8
|
+
|
9
|
+
def chauvenets_criterion_max
|
10
|
+
delta = (mean - array.max).abs
|
11
|
+
(1/standard_deviation)*Statistics2.normaldist((delta - mean)/standard_deviation)*size
|
12
|
+
end
|
13
|
+
|
14
|
+
def outliers_chauvenets?
|
15
|
+
@outliers_chauvenets ||= max_outlier_chauvenets? or min_outlier_chauvenets?
|
16
|
+
end
|
17
|
+
|
18
|
+
def max_outlier_chauvenets?
|
19
|
+
@max_outliers_chauvenets ||= chauvenets_criterion_max < 0.5
|
20
|
+
end
|
21
|
+
|
22
|
+
def min_outlier_chauvenets?
|
23
|
+
@min_outliers_chauvenets ||= chauvenets_criterion_min < 0.5
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'descriptive_statistics/safe'
|
2
|
+
require 'statistics2'
|
3
|
+
require 'savanna-outliers/general_parameters'
|
4
|
+
require 'savanna-outliers/grubbs'
|
5
|
+
require 'savanna-outliers/chauvenets'
|
6
|
+
require 'savanna-outliers/operations'
|
7
|
+
|
8
|
+
module Savanna
|
9
|
+
module Outliers
|
10
|
+
class Core
|
11
|
+
include Savanna::Outliers::GeneralParameters
|
12
|
+
include Savanna::Outliers::Grubbs
|
13
|
+
include Savanna::Outliers::Chauvenets
|
14
|
+
include Savanna::Outliers::Operations
|
15
|
+
attr_reader :array
|
16
|
+
attr_accessor :method
|
17
|
+
def initialize(input_array, method = :grubbs)
|
18
|
+
@array = input_array.extend(DescriptiveStatistics)
|
19
|
+
@method = method
|
20
|
+
end
|
21
|
+
|
22
|
+
def outliers?
|
23
|
+
case
|
24
|
+
when method == :grubbs then outliers_grubbs?
|
25
|
+
when method == :chauvenets then outliers_chauvenets?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def max_outlier?
|
30
|
+
case
|
31
|
+
when method == :grubbs then max_outlier_grubbs?
|
32
|
+
when method == :chauvenets then max_outlier_chauvenets?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def min_outlier?
|
37
|
+
case
|
38
|
+
when method == :grubbs then min_outlier_grubbs?
|
39
|
+
when method == :chauvenets then min_outlier_chauvenets?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def indexed_array
|
46
|
+
@indexed_array ||= array.each_with_index
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Savanna
|
2
|
+
module Outliers
|
3
|
+
module GeneralParameters
|
4
|
+
def size
|
5
|
+
@size ||= array.size
|
6
|
+
end
|
7
|
+
|
8
|
+
def mean
|
9
|
+
@mean ||= array.mean
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_mean(m)
|
13
|
+
@mean = m
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_standard_deviation(sd)
|
17
|
+
@standard_deviation = sd
|
18
|
+
end
|
19
|
+
|
20
|
+
def standard_deviation
|
21
|
+
@standard_deviation ||= array.standard_deviation
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Savanna
|
2
|
+
module Outliers
|
3
|
+
module Grubbs
|
4
|
+
def grubbs_test_statistic
|
5
|
+
array.map{|el| (el - mean).abs}.max/standard_deviation
|
6
|
+
end
|
7
|
+
|
8
|
+
def grubbs_test_statistic_maximum
|
9
|
+
(array.max - mean)/standard_deviation
|
10
|
+
end
|
11
|
+
|
12
|
+
def grubbs_test_statistic_minimum
|
13
|
+
(mean - array.min)/standard_deviation
|
14
|
+
end
|
15
|
+
|
16
|
+
def grubbs_two_sided_test(n = size, alpha = 0.05)
|
17
|
+
a = (n - 1)/Math.sqrt(n)
|
18
|
+
t = Statistics2.ptdist(n-2,alpha/(2*n))**2
|
19
|
+
b = Math.sqrt( t/(n-2+t) )
|
20
|
+
a*b
|
21
|
+
end
|
22
|
+
|
23
|
+
def grubbs_one_sided_test(n = size, alpha = 0.05)
|
24
|
+
a = (n - 1)/Math.sqrt(n)
|
25
|
+
t = Statistics2.ptdist(n-2,alpha/n)**2
|
26
|
+
b = Math.sqrt( t/(n-2+t) )
|
27
|
+
a*b
|
28
|
+
end
|
29
|
+
|
30
|
+
def outliers_grubbs?
|
31
|
+
@outliers_grubbs ||= grubbs_test_statistic > grubbs_two_sided_test
|
32
|
+
end
|
33
|
+
|
34
|
+
def max_outlier_grubbs?
|
35
|
+
@max_outliers_grubbs ||= grubbs_test_statistic_maximum > grubbs_one_sided_test
|
36
|
+
end
|
37
|
+
|
38
|
+
def min_outlier_grubbs?
|
39
|
+
@min_outliers_grubbs ||= grubbs_test_statistic_minimum > grubbs_one_sided_test
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Savanna
|
2
|
+
module Outliers
|
3
|
+
module Operations
|
4
|
+
def get_max_outlier
|
5
|
+
indexed_array.max[0] if max_outlier?
|
6
|
+
end
|
7
|
+
|
8
|
+
def get_min_outlier
|
9
|
+
indexed_array.min[0] if min_outlier?
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_max_outlier_index
|
13
|
+
indexed_array.max[1] if max_outlier?
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_min_outlier_index
|
17
|
+
indexed_array.min[1] if min_outlier?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'savanna-outliers/core'
|
2
|
+
|
3
|
+
module Savanna
|
4
|
+
module Outliers
|
5
|
+
self.extend Savanna::Outliers
|
6
|
+
def outliers?(dataset, type = :all, method = :grubbs)
|
7
|
+
case
|
8
|
+
when (dataset.class == Array && type == :all) then Core.new(dataset, method).outliers?
|
9
|
+
when (dataset.class == Array && type == :max) then Core.new(dataset, method).max_outlier?
|
10
|
+
when (dataset.class == Array && type == :min) then Core.new(dataset, method).min_outlier?
|
11
|
+
when (dataset.class == Hash && type == :all) then Core.new(dataset.values, method).outliers?
|
12
|
+
when (dataset.class == Hash && type == :max) then Core.new(dataset.values, method).max_outlier?
|
13
|
+
when (dataset.class == Hash && type == :min) then Core.new(dataset.values, method).min_outlier?
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_outliers(dataset, type = :all, method = :grubbs)
|
18
|
+
case
|
19
|
+
when (dataset.class == Array && type == :all) then get_all_outliers_from_array(dataset, method)
|
20
|
+
when (dataset.class == Array && type == :max) then get_max_outliers_from_array(dataset, method)
|
21
|
+
when (dataset.class == Array && type == :min) then get_min_outliers_from_array(dataset, method)
|
22
|
+
when (dataset.class == Hash && type == :all) then get_all_outliers_from_hash(dataset, method)
|
23
|
+
when (dataset.class == Hash && type == :max) then get_max_outliers_from_hash(dataset, method)
|
24
|
+
when (dataset.class == Hash && type == :min) then get_min_outliers_from_hash(dataset, method)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def remove_outliers(dataset, type = :all, method = :grubbs)
|
29
|
+
case
|
30
|
+
when (dataset.class == Array && type == :all) then remove_all_outliers_from_array(dataset, method)
|
31
|
+
when (dataset.class == Array && type == :max) then remove_max_outliers_from_array(dataset, method)
|
32
|
+
when (dataset.class == Array && type == :min) then remove_min_outliers_from_array(dataset, method)
|
33
|
+
when (dataset.class == Hash && type == :all) then remove_all_outliers_from_hash(dataset, method)
|
34
|
+
when (dataset.class == Hash && type == :max) then remove_max_outliers_from_hash(dataset, method)
|
35
|
+
when (dataset.class == Hash && type == :min) then remove_min_outliers_from_hash(dataset, method)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def get_max_outliers_from_array(array, method)
|
42
|
+
input_array = array.clone; outliers = []
|
43
|
+
outliers << input_array.delete_at(Core.new(input_array, method).get_max_outlier_index) while Core.new(input_array, method).max_outlier?
|
44
|
+
outliers
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_min_outliers_from_array(array, method)
|
48
|
+
input_array = array.clone; outliers = []
|
49
|
+
outliers << input_array.delete_at(Core.new(input_array, method).get_min_outlier_index) while Core.new(input_array, method).min_outlier?
|
50
|
+
outliers
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_all_outliers_from_array(array, method)
|
54
|
+
get_max_outliers_from_array(array, method) + get_min_outliers_from_array(array, method)
|
55
|
+
end
|
56
|
+
|
57
|
+
def get_max_outliers_from_hash(hash, method)
|
58
|
+
keys_array = hash.keys; input_array = hash.values
|
59
|
+
output_keys = []; output_values = []
|
60
|
+
while Core.new(input_array, method).max_outlier?
|
61
|
+
index = Core.new(input_array, method).get_max_outlier_index
|
62
|
+
output_values << input_array.delete_at(index)
|
63
|
+
output_keys << keys_array.delete_at(index)
|
64
|
+
end
|
65
|
+
build_hash(output_keys, output_values)
|
66
|
+
end
|
67
|
+
|
68
|
+
def get_min_outliers_from_hash(hash, method)
|
69
|
+
keys_array = hash.keys; input_array = hash.values
|
70
|
+
output_keys = []; output_values = []
|
71
|
+
while Core.new(input_array, method).min_outlier?
|
72
|
+
index = Core.new(input_array, method).get_min_outlier_index
|
73
|
+
output_values << input_array.delete_at(index)
|
74
|
+
output_keys << keys_array.delete_at(index)
|
75
|
+
end
|
76
|
+
build_hash(output_keys, output_values)
|
77
|
+
end
|
78
|
+
|
79
|
+
def get_all_outliers_from_hash(hash, method)
|
80
|
+
get_max_outliers_from_hash(hash, method).merge(get_min_outliers_from_hash(hash, method))
|
81
|
+
end
|
82
|
+
|
83
|
+
def remove_max_outliers_from_array(array, method)
|
84
|
+
input_array = array.clone
|
85
|
+
input_array.delete_at(Core.new(input_array, method).get_max_outlier_index) while Core.new(input_array, method).max_outlier?
|
86
|
+
input_array
|
87
|
+
end
|
88
|
+
|
89
|
+
def remove_min_outliers_from_array(array, method)
|
90
|
+
input_array = array.clone
|
91
|
+
input_array.delete_at(Core.new(input_array, method).get_min_outlier_index) while Core.new(input_array, method).min_outlier?
|
92
|
+
input_array
|
93
|
+
end
|
94
|
+
|
95
|
+
def remove_all_outliers_from_array(array, method)
|
96
|
+
array - get_all_outliers_from_array(array, method)
|
97
|
+
end
|
98
|
+
|
99
|
+
def remove_max_outliers_from_hash(hash, method)
|
100
|
+
keys_array = hash.keys; input_array = hash.values
|
101
|
+
while Core.new(input_array, method).max_outlier?
|
102
|
+
index = Core.new(input_array, method).get_max_outlier_index
|
103
|
+
input_array.delete_at(index)
|
104
|
+
keys_array.delete_at(index)
|
105
|
+
end
|
106
|
+
build_hash(keys_array, input_array)
|
107
|
+
end
|
108
|
+
|
109
|
+
def remove_min_outliers_from_hash(hash, method)
|
110
|
+
keys_array = hash.keys; input_array = hash.values
|
111
|
+
while Core.new(input_array, method).min_outlier?
|
112
|
+
index = Core.new(input_array, method).get_min_outlier_index
|
113
|
+
input_array.delete_at(index)
|
114
|
+
keys_array.delete_at(index)
|
115
|
+
end
|
116
|
+
build_hash(keys_array, input_array)
|
117
|
+
end
|
118
|
+
|
119
|
+
def remove_all_outliers_from_hash(hash, method)
|
120
|
+
get_all_outliers_from_hash(hash, method).keys.each do |key|
|
121
|
+
hash.tap { |hs| hs.delete(key) }
|
122
|
+
end
|
123
|
+
hash
|
124
|
+
end
|
125
|
+
|
126
|
+
def build_hash(keys, values)
|
127
|
+
output_hash = {}
|
128
|
+
keys.each_index{|i| output_hash[keys[i]] = values[i]}
|
129
|
+
output_hash
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Savanna Outliers Core' do
|
4
|
+
before(:each) do
|
5
|
+
@simple_example = Savanna::Outliers::Core.new([1,2,3,4,5,100,200,300])
|
6
|
+
@min_outlier_example = Savanna::Outliers::Core.new([-30,20,25,19,21,22,23,18])
|
7
|
+
@max_outlier_example = Savanna::Outliers::Core.new([100,20,25,19,21,22,23,18])
|
8
|
+
@min_max_outlier_example = Savanna::Outliers::Core.new([100,20,25,19,21,22,23,18,1])
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should return array' do
|
12
|
+
@simple_example.array.should == [1,2,3,4,5,100,200,300]
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should return size' do
|
16
|
+
@simple_example.size.should == 8
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should return mean' do
|
20
|
+
@simple_example.mean.should == 76.875
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should return standard deviation' do
|
24
|
+
@simple_example.standard_deviation.should == 107.68987591691246
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should calculate grubbs test statistic' do
|
28
|
+
@simple_example.grubbs_test_statistic.should == 2.0719217855924628
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should calculate grubbs test statistic for maximum' do
|
32
|
+
@simple_example.grubbs_test_statistic_maximum.should == 2.0719217855924628
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should calculate grubbs test statistic for minimum' do
|
36
|
+
@simple_example.grubbs_test_statistic_minimum.should == 0.7045694811510503
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should calculate grubbs two sided test' do
|
40
|
+
@simple_example.grubbs_two_sided_test(3,0.05).should == 1.1543048503229865
|
41
|
+
@simple_example.grubbs_two_sided_test(10).should == 2.289952254608721
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should calculate grubbs one sided test' do
|
45
|
+
@simple_example.grubbs_one_sided_test(3,0.05).should == 1.1531180570049688
|
46
|
+
@simple_example.grubbs_one_sided_test(10).should == 2.176061415982935
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should identify outliers existance with grubbs' do
|
50
|
+
@min_max_outlier_example.outliers_grubbs?.should == true
|
51
|
+
@min_max_outlier_example.method = :grubbs
|
52
|
+
@min_max_outlier_example.outliers?.should == true
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should identify min outlier existance with grubbs' do
|
56
|
+
@min_outlier_example.min_outlier_grubbs?.should == true
|
57
|
+
@min_outlier_example.method = :grubbs
|
58
|
+
@min_outlier_example.min_outlier?.should == true
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should identify max outlier existance with grubbs' do
|
62
|
+
@max_outlier_example.max_outlier_grubbs?.should == true
|
63
|
+
@max_outlier_example.method = :grubbs
|
64
|
+
@max_outlier_example.max_outlier?.should == true
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should identify outliers existance with chauvenets' do
|
68
|
+
@min_max_outlier_example.outliers_chauvenets?.should == true
|
69
|
+
@min_max_outlier_example.method = :chauvenets
|
70
|
+
@min_max_outlier_example.outliers?.should == true
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should identify min outlier existance with chauvenets' do
|
74
|
+
@min_outlier_example.min_outlier_chauvenets?.should == true
|
75
|
+
@min_outlier_example.method = :chauvenets
|
76
|
+
@min_outlier_example.min_outlier?.should == true
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should identify max outlier existance with chauvenets' do
|
80
|
+
@max_outlier_example.max_outlier_chauvenets?.should == true
|
81
|
+
@max_outlier_example.method = :chauvenets
|
82
|
+
@max_outlier_example.max_outlier?.should == true
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should return max outlier' do
|
86
|
+
@max_outlier_example.get_max_outlier.should == 100
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should return min outlier' do
|
90
|
+
@min_outlier_example.get_min_outlier.should == -30
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'should return max outlier index' do
|
94
|
+
@max_outlier_example.get_max_outlier_index.should == 0
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should return min outlier index' do
|
98
|
+
@min_outlier_example.get_min_outlier_index.should == 0
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Savanna Outliers' do
|
4
|
+
|
5
|
+
it 'should identify any outliers existance' do
|
6
|
+
array_one = [10, 12, 8, 11, 9, 13, 12, 10, 1000, 12, 10, 1100, 9, 5000]
|
7
|
+
hash_one = {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, i: 1000, j: 12, k: 10, l: 1100, m: 9, n: 5000}
|
8
|
+
Savanna::Outliers.outliers?(array_one, :all, :grubbs).should == true
|
9
|
+
Savanna::Outliers.outliers?(hash_one, :all, :grubbs).should == true
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should identify max outliers existance' do
|
13
|
+
array_one = [10, 12, 8, 11, 9, 13, 12, 10, 1000, 12, 10, 1100, 9, 5000]
|
14
|
+
hash_one = {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, i: 1000, j: 12, k: 10, l: 1100, m: 9, n: 5000}
|
15
|
+
Savanna::Outliers.outliers?(array_one, :max, :grubbs).should == true
|
16
|
+
Savanna::Outliers.outliers?(hash_one, :max, :grubbs).should == true
|
17
|
+
Savanna::Outliers.outliers?(array_one, :max).should == true
|
18
|
+
Savanna::Outliers.outliers?(hash_one, :max).should == true
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should identify min outliers existance' do
|
22
|
+
array_one = [10, 12, 8, 11, 9, 13, 12, 10, -1000, 12, 10, -1100, 9, -5000]
|
23
|
+
hash_one = {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, i: -1000, j: 12, k: 10, l: -1100, m: 9, n: -5000}
|
24
|
+
Savanna::Outliers.outliers?(array_one, :min, :grubbs).should == true
|
25
|
+
Savanna::Outliers.outliers?(hash_one, :min, :grubbs).should == true
|
26
|
+
Savanna::Outliers.outliers?(array_one, :min).should == true
|
27
|
+
Savanna::Outliers.outliers?(hash_one, :min).should == true
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should return max outliers from array' do
|
31
|
+
array_one = [10, 12, 8, 11, 9, 13, 12, 10, 1000, 12, 10, 1100, 9, 5000]
|
32
|
+
array_two = [20, 30, 100, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15, 90]
|
33
|
+
Savanna::Outliers.get_outliers(array_one, :max, :grubbs).should == [5000, 1100, 1000]
|
34
|
+
Savanna::Outliers.get_outliers(array_two, :max, :grubbs).should == [100, 90]
|
35
|
+
Savanna::Outliers.get_outliers(array_one, :max).should == [5000, 1100, 1000]
|
36
|
+
Savanna::Outliers.get_outliers(array_two, :max).should == [100, 90]
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should return min outliers from array' do
|
40
|
+
array_one = [10, 12, 8, 11, 9, 13, 12, 10, -1000, 12, 10, -1100, 9, -5000]
|
41
|
+
array_two = [20, 30, -100, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15, -90]
|
42
|
+
Savanna::Outliers.get_outliers(array_one, :min, :grubbs).should == [-5000, -1100, -1000]
|
43
|
+
Savanna::Outliers.get_outliers(array_two, :min, :grubbs).should == [-100, -90]
|
44
|
+
Savanna::Outliers.get_outliers(array_one, :min).should == [-5000, -1100, -1000]
|
45
|
+
Savanna::Outliers.get_outliers(array_two, :min).should == [-100, -90]
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should return all outliers from array' do
|
49
|
+
array_one = [10, 12, 8, 11, 9, 13, 12, 10, -1000, 1000, 12, 10, 9]
|
50
|
+
array_two = [20, 30, -100, 140, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15]
|
51
|
+
Savanna::Outliers.get_outliers(array_one, :all, :grubbs).should == [1000, -1000]
|
52
|
+
Savanna::Outliers.get_outliers(array_two, :all, :grubbs).should == [140, -100]
|
53
|
+
Savanna::Outliers.get_outliers(array_one, :all).should == [1000, -1000]
|
54
|
+
Savanna::Outliers.get_outliers(array_two, :all).should == [140, -100]
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
it 'should return max outliers from hash' do
|
59
|
+
hash_one = {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, i: 1000, j: 12, k: 10, l: 1100, m: 9, n: 5000}
|
60
|
+
hash_two = {a: 20, b: 30, c: 100, d: 10, e: 15, f: 10, g: 12, h: 13, i: 5, j: 25, k: 40, l: 35, m: 22, n: 15, o: 90}
|
61
|
+
Savanna::Outliers.get_outliers(hash_one, :max, :grubbs).should == {n: 5000, l: 1100, i: 1000}
|
62
|
+
Savanna::Outliers.get_outliers(hash_two, :max, :grubbs).should == {c: 100, o: 90}
|
63
|
+
Savanna::Outliers.get_outliers(hash_one, :max).should == {n: 5000, l: 1100, i: 1000}
|
64
|
+
Savanna::Outliers.get_outliers(hash_two, :max).should == {c: 100, o: 90}
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should return min outliers from hash' do
|
68
|
+
hash_one = {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, i: -1000, j: 12, k: 10, l: -1100, m: 9, n: -5000}
|
69
|
+
hash_two = {a: 20, b: 30, c: -100, d: 10, e: 15, f: 10, g: 12, h: 13, i: 5, j: 25, k: 40, l: 35, m: 22, n: 15, o: -90}
|
70
|
+
Savanna::Outliers.get_outliers(hash_one, :min, :grubbs).should == {n: -5000, l: -1100, i: -1000}
|
71
|
+
Savanna::Outliers.get_outliers(hash_two, :min, :grubbs).should == {c: -100, o: -90}
|
72
|
+
Savanna::Outliers.get_outliers(hash_one, :min).should == {n: -5000, l: -1100, i: -1000}
|
73
|
+
Savanna::Outliers.get_outliers(hash_two, :min).should == {c: -100, o: -90}
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should return all outliers from hash' do
|
77
|
+
hash_one = {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, i: -1000, j: 1000, k: 12, l: 10, m: 9}
|
78
|
+
hash_two = {a: 20, b: 30, c: -100, d: 140, e: 10, f: 15, g: 10, h: 12, i: 13, j: 5, k: 25, l: 40, m: 35, n: 22, o: 15}
|
79
|
+
Savanna::Outliers.get_outliers(hash_one, :all, :grubbs).should == {j: 1000, i: -1000}
|
80
|
+
Savanna::Outliers.get_outliers(hash_two, :all, :grubbs).should == {d: 140, c: -100}
|
81
|
+
Savanna::Outliers.get_outliers(hash_one, :all).should == {j: 1000, i: -1000}
|
82
|
+
Savanna::Outliers.get_outliers(hash_two, :all).should == {d: 140, c: -100}
|
83
|
+
end
|
84
|
+
|
85
|
+
###############################
|
86
|
+
|
87
|
+
it 'should remove max outliers from array' do
|
88
|
+
array_one = [10, 12, 8, 11, 9, 13, 12, 10, 1000, 12, 10, 1100, 9, 5000]
|
89
|
+
array_two = [20, 30, 100, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15, 90]
|
90
|
+
Savanna::Outliers.remove_outliers(array_one, :max, :grubbs).should == [10, 12, 8, 11, 9, 13, 12, 10, 12, 10, 9]
|
91
|
+
Savanna::Outliers.remove_outliers(array_two, :max, :grubbs).should == [20, 30, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15]
|
92
|
+
Savanna::Outliers.remove_outliers(array_one, :max).should == [10, 12, 8, 11, 9, 13, 12, 10, 12, 10, 9]
|
93
|
+
Savanna::Outliers.remove_outliers(array_two, :max).should == [20, 30, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15]
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should remove min outliers from array' do
|
97
|
+
array_one = [10, 12, 8, 11, 9, 13, 12, 10, -1000, 12, 10, -1100, 9, -5000]
|
98
|
+
array_two = [20, 30, -100, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15, -90]
|
99
|
+
Savanna::Outliers.remove_outliers(array_one, :min, :grubbs).should == [10, 12, 8, 11, 9, 13, 12, 10, 12, 10, 9]
|
100
|
+
Savanna::Outliers.remove_outliers(array_two, :min, :grubbs).should == [20, 30, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15]
|
101
|
+
Savanna::Outliers.remove_outliers(array_one, :min).should == [10, 12, 8, 11, 9, 13, 12, 10, 12, 10, 9]
|
102
|
+
Savanna::Outliers.remove_outliers(array_two, :min).should == [20, 30, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15]
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'should remove all outliers from array' do
|
106
|
+
array_one = [10, 12, 8, 11, 9, 13, 12, 10, -1000, 1000, 12, 10, 9]
|
107
|
+
array_two = [20, 30, -100, 140, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15]
|
108
|
+
Savanna::Outliers.remove_outliers(array_one, :all, :grubbs).should == [10, 12, 8, 11, 9, 13, 12, 10, 12, 10, 9]
|
109
|
+
Savanna::Outliers.remove_outliers(array_two, :all, :grubbs).should == [20, 30, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15]
|
110
|
+
Savanna::Outliers.remove_outliers(array_one, :all).should == [10, 12, 8, 11, 9, 13, 12, 10, 12, 10, 9]
|
111
|
+
Savanna::Outliers.remove_outliers(array_two, :all).should == [20, 30, 10, 15, 10, 12, 13, 5, 25, 40, 35, 22, 15]
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
it 'should remove max outliers from hash' do
|
116
|
+
hash_one = {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, i: 1000, j: 12, k: 10, l: 1100, m: 9, n: 5000}
|
117
|
+
hash_two = {a: 20, b: 30, c: 100, d: 10, e: 15, f: 10, g: 12, h: 13, i: 5, j: 25, k: 40, l: 35, m: 22, n: 15, o: 90}
|
118
|
+
Savanna::Outliers.remove_outliers(hash_one, :max, :grubbs).should == {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, j: 12, k: 10, m: 9}
|
119
|
+
Savanna::Outliers.remove_outliers(hash_two, :max, :grubbs).should == {a: 20, b: 30, d: 10, e: 15, f: 10, g: 12, h: 13, i: 5, j: 25, k: 40, l: 35, m: 22, n: 15}
|
120
|
+
Savanna::Outliers.remove_outliers(hash_one, :max).should == {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, j: 12, k: 10, m: 9}
|
121
|
+
Savanna::Outliers.remove_outliers(hash_two, :max).should == {a: 20, b: 30, d: 10, e: 15, f: 10, g: 12, h: 13, i: 5, j: 25, k: 40, l: 35, m: 22, n: 15}
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should remove min outliers from hash' do
|
125
|
+
hash_one = {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, i: -1000, j: 12, k: 10, l: -1100, m: 9, n: -5000}
|
126
|
+
hash_two = {a: 20, b: 30, c: -100, d: 10, e: 15, f: 10, g: 12, h: 13, i: 5, j: 25, k: 40, l: 35, m: 22, n: 15, o: -90}
|
127
|
+
Savanna::Outliers.remove_outliers(hash_one, :min, :grubbs).should == {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, j: 12, k: 10, m: 9}
|
128
|
+
Savanna::Outliers.remove_outliers(hash_two, :min, :grubbs).should == {a: 20, b: 30, d: 10, e: 15, f: 10, g: 12, h: 13, i: 5, j: 25, k: 40, l: 35, m: 22, n: 15}
|
129
|
+
Savanna::Outliers.remove_outliers(hash_one, :min).should == {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, j: 12, k: 10, m: 9}
|
130
|
+
Savanna::Outliers.remove_outliers(hash_two, :min).should == {a: 20, b: 30, d: 10, e: 15, f: 10, g: 12, h: 13, i: 5, j: 25, k: 40, l: 35, m: 22, n: 15}
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'should remove all outliers from hash' do
|
134
|
+
hash_one = {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, i: -1000, j: 1000, k: 12, l: 10, m: 9}
|
135
|
+
hash_two = {a: 20, b: 30, c: -100, d: 140, e: 10, f: 15, g: 10, h: 12, i: 13, j: 5, k: 25, l: 40, m: 35, n: 22, o: 15}
|
136
|
+
Savanna::Outliers.remove_outliers(hash_one, :all, :grubbs).should == {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, k: 12, l: 10, m: 9}
|
137
|
+
Savanna::Outliers.remove_outliers(hash_two, :all, :grubbs).should == {a: 20, b: 30, e: 10, f: 15, g: 10, h: 12, i: 13, j: 5, k: 25, l: 40, m: 35, n: 22, o: 15}
|
138
|
+
Savanna::Outliers.remove_outliers(hash_one, :all).should == {a: 10, b: 12, c: 8, d: 11, e: 9, f: 13, g: 12, h: 10, k: 12, l: 10, m: 9}
|
139
|
+
Savanna::Outliers.remove_outliers(hash_two, :all).should == {a: 20, b: 30, e: 10, f: 15, g: 10, h: 12, i: 13, j: 5, k: 25, l: 40, m: 35, n: 22, o: 15}
|
140
|
+
end
|
141
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: savanna-outliers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Max Makarochkin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: descriptive_statistics
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: statistics2
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Read more documentation at repository homepage.
|
84
|
+
email: maxim.makarochkin@gmail.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- lib/savanna-outliers/chauvenets.rb
|
90
|
+
- lib/savanna-outliers/core.rb
|
91
|
+
- lib/savanna-outliers/general_parameters.rb
|
92
|
+
- lib/savanna-outliers/grubbs.rb
|
93
|
+
- lib/savanna-outliers/operations.rb
|
94
|
+
- lib/savanna-outliers/savanna_outliers.rb
|
95
|
+
- lib/savanna-outliers/version.rb
|
96
|
+
- lib/savanna-outliers.rb
|
97
|
+
- spec/savanna-outliers/core_spec.rb
|
98
|
+
- spec/savanna-outliers/outliers_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
homepage: https://github.com/savanna-initiative/savanna-outliers
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: 1.9.3
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.0.7
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: ! 'Savanna outliers: Plug & Play Anomaly Detection'
|
124
|
+
test_files: []
|
125
|
+
has_rdoc:
|