active_object 4.0.12 → 4.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +31 -0
- data/lib/active_object/enumerable.rb +88 -3
- data/lib/active_object/numeric.rb +1 -1
- data/lib/active_object/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0af37321a91317b8d25371fb0b08f35b64eb65ec
|
4
|
+
data.tar.gz: 4768af3d81ae21baff77cdb6582d12545d9528b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7338a890b66d3b688cc15077db61fa5b2f7a7cab86fb8860adb7f6a0d648abff36172b5b13813f2494d945dbf67c2651727d0c966db45d127c15eb5fcd02aa5
|
7
|
+
data.tar.gz: ad3c1d0a26a12c65152512e3e1f0771baa98c29535b1325049bec89922f36862f9c1f903ccdb28c86457b807e5b78ead379c7c0bd098a6785741ba2645c4f66e
|
data/README.md
CHANGED
@@ -231,6 +231,15 @@ end
|
|
231
231
|
|
232
232
|
## Enumerable
|
233
233
|
|
234
|
+
**Critical zscore:**
|
235
|
+
`critical_zscore` returns the critical_zscore of elements of a collection.
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
[].critical_zscore #=> nil
|
239
|
+
[].critical_zscore(0) #=> 0
|
240
|
+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].critical_zscore #=> 2.29
|
241
|
+
```
|
242
|
+
|
234
243
|
**Cluster:**
|
235
244
|
`cluster` clusters together adjacent elements into a list of sub-arrays.
|
236
245
|
|
@@ -391,6 +400,20 @@ end
|
|
391
400
|
[1,2,6].range #=> 5
|
392
401
|
```
|
393
402
|
|
403
|
+
**Reject outliers:**
|
404
|
+
`reject_outliers` and `reject_outliers!` removes the outliers of collection of numbers.
|
405
|
+
|
406
|
+
```ruby
|
407
|
+
[1, 2, 3, 30].reject_outliers #=> [1, 2, 3]
|
408
|
+
```
|
409
|
+
|
410
|
+
**Select outliers:**
|
411
|
+
`select_outliers` select the outliers of collection of numbers.
|
412
|
+
|
413
|
+
```ruby
|
414
|
+
[1, 2, 3, 30].select_outliers #=> [30]
|
415
|
+
```
|
416
|
+
|
394
417
|
**Several:**
|
395
418
|
`several?` returns if collection has more than one element while not respecting nil and false as an element.
|
396
419
|
|
@@ -446,6 +469,14 @@ end
|
|
446
469
|
[1,2,6].variance #=> 7
|
447
470
|
```
|
448
471
|
|
472
|
+
**Zscore:**
|
473
|
+
`zscore` returns the zscore of elements of a collection.
|
474
|
+
|
475
|
+
```ruby
|
476
|
+
[].zscore(3) #=> 0
|
477
|
+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].zscore(3) #=> 0.8257228238447705
|
478
|
+
```
|
479
|
+
|
449
480
|
## Hash
|
450
481
|
|
451
482
|
**Assert Valid Keys:**
|
@@ -1,5 +1,56 @@
|
|
1
1
|
module Enumerable
|
2
2
|
|
3
|
+
CRITICAL_ZSCORE ||= {
|
4
|
+
3 => 1.15,
|
5
|
+
4 => 1.48,
|
6
|
+
5 => 1.71,
|
7
|
+
6 => 1.89,
|
8
|
+
7 => 2.02,
|
9
|
+
8 => 2.13,
|
10
|
+
9 => 2.21,
|
11
|
+
10 => 2.29,
|
12
|
+
11 => 2.34,
|
13
|
+
12 => 2.41,
|
14
|
+
13 => 2.46,
|
15
|
+
14 => 2.51,
|
16
|
+
15 => 2.55,
|
17
|
+
16 => 2.59,
|
18
|
+
17 => 2.62,
|
19
|
+
18 => 2.65,
|
20
|
+
19 => 2.68,
|
21
|
+
20 => 2.71,
|
22
|
+
21 => 2.73,
|
23
|
+
22 => 2.76,
|
24
|
+
23 => 2.78,
|
25
|
+
24 => 2.80,
|
26
|
+
25 => 2.82,
|
27
|
+
26 => 2.84,
|
28
|
+
27 => 2.86,
|
29
|
+
28 => 2.88,
|
30
|
+
29 => 2.89,
|
31
|
+
30 => 2.91,
|
32
|
+
31 => 2.92,
|
33
|
+
32 => 2.94,
|
34
|
+
33 => 2.95,
|
35
|
+
34 => 2.97,
|
36
|
+
35 => 2.98,
|
37
|
+
36 => 2.99,
|
38
|
+
37 => 3.00,
|
39
|
+
38 => 3.01,
|
40
|
+
39 => 3.03,
|
41
|
+
40 => 3.04,
|
42
|
+
50 => 3.13,
|
43
|
+
60 => 3.20,
|
44
|
+
70 => 3.26,
|
45
|
+
80 => 3.31,
|
46
|
+
90 => 3.35,
|
47
|
+
100 => 3.38,
|
48
|
+
110 => 3.42,
|
49
|
+
120 => 3.44,
|
50
|
+
130 => 3.47,
|
51
|
+
140 => 3.49
|
52
|
+
}.freeze
|
53
|
+
|
3
54
|
# rubocop:disable Lint/UnusedMethodArgument
|
4
55
|
def cluster(&block)
|
5
56
|
result = []
|
@@ -11,6 +62,18 @@ module Enumerable
|
|
11
62
|
end
|
12
63
|
# rubocop:enable Lint/UnusedMethodArgument
|
13
64
|
|
65
|
+
def critical_zscore(identity = nil)
|
66
|
+
collection_length = length
|
67
|
+
result = nil
|
68
|
+
|
69
|
+
CRITICAL_ZSCORE.keys.sort.each do |key|
|
70
|
+
break if key > collection_length
|
71
|
+
result = CRITICAL_ZSCORE[key]
|
72
|
+
end
|
73
|
+
|
74
|
+
result || identity
|
75
|
+
end
|
76
|
+
|
14
77
|
def difference(identity = 0, &block)
|
15
78
|
if block_given?
|
16
79
|
map(&block).difference(identity)
|
@@ -145,10 +208,11 @@ module Enumerable
|
|
145
208
|
rank = (num.to_f / 100) * (length + 1)
|
146
209
|
|
147
210
|
if rank.fraction?
|
148
|
-
|
149
|
-
|
211
|
+
truncated_rank = rank.truncate
|
212
|
+
sample_one = collection_sorted[truncated_rank - 1]
|
213
|
+
sample_two = collection_sorted[truncated_rank]
|
150
214
|
|
151
|
-
(rank.fraction * (
|
215
|
+
(rank.fraction * (sample_two - sample_one)) + sample_one
|
152
216
|
else
|
153
217
|
collection_sorted[rank - 1]
|
154
218
|
end
|
@@ -161,6 +225,20 @@ module Enumerable
|
|
161
225
|
collection_sorted.last - collection_sorted.first
|
162
226
|
end
|
163
227
|
|
228
|
+
def reject_outliers
|
229
|
+
cz = critical_zscore
|
230
|
+
reject { |value| zscore(value) > cz }
|
231
|
+
end
|
232
|
+
|
233
|
+
def reject_outliers!
|
234
|
+
replace(reject_outliers)
|
235
|
+
end
|
236
|
+
|
237
|
+
def select_outliers
|
238
|
+
cz = critical_zscore
|
239
|
+
select { |value| zscore(value) > cz }
|
240
|
+
end
|
241
|
+
|
164
242
|
def several?
|
165
243
|
found_count = 0
|
166
244
|
if block_given?
|
@@ -210,4 +288,11 @@ module Enumerable
|
|
210
288
|
total.to_f / (collection_length.to_f - 1.0)
|
211
289
|
end
|
212
290
|
|
291
|
+
def zscore(value)
|
292
|
+
sd = standard_deviation
|
293
|
+
return 0 if sd.zero?
|
294
|
+
|
295
|
+
(mean - value).abs / sd
|
296
|
+
end
|
297
|
+
|
213
298
|
end
|