easystats 0.0.7 → 0.1.0
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.
- data/README +3 -0
- data/Rakefile +13 -0
- data/autotest/discover.rb +3 -0
- data/autotest/testunit.rb +19 -0
- data/easystats.gemspec +1 -1
- data/lib/easystats.rb +38 -32
- data/lib/easystats/version.rb +1 -1
- data/test/easystats_test.rb +74 -0
- data/test/test_helper.rb +5 -0
- metadata +8 -4
data/README
CHANGED
data/Rakefile
CHANGED
@@ -1,2 +1,15 @@
|
|
1
1
|
require 'bundler'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
2
5
|
Bundler::GemHelper.install_tasks
|
6
|
+
|
7
|
+
desc 'Default: run unit tests.'
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
desc 'Run unit tests.'
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << 'lib'
|
13
|
+
t.pattern = 'test/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'autotest'
|
2
|
+
|
3
|
+
class Autotest::Testunit < Autotest
|
4
|
+
def initialize
|
5
|
+
super
|
6
|
+
|
7
|
+
@test_mappings = {
|
8
|
+
%r%^test/.*\.rb$% => proc { |filename, _|
|
9
|
+
filename
|
10
|
+
},
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
# Given the string filename as the path, determine
|
15
|
+
# the corresponding tests for it, in an array.
|
16
|
+
def tests_for_file(filename)
|
17
|
+
super.select { |f| @files.has_key? f }
|
18
|
+
end
|
19
|
+
end
|
data/easystats.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.email = ["matthew@grigajtis.org"]
|
13
13
|
s.homepage = "https://github.com/mgrigajtis/easystats"
|
14
14
|
s.summary = %q{Easy to use statistics functions}
|
15
|
-
s.description = %q{This gem contains statistics functions that are very easy to use.
|
15
|
+
s.description = %q{This gem contains statistics functions that are very easy to use. Much easier and much more complete than many of the other statistical gems available out there. If you need a feature added, send me a message on Github!}
|
16
16
|
|
17
17
|
s.rubyforge_project = "easystats"
|
18
18
|
|
data/lib/easystats.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
class Array
|
2
2
|
|
3
3
|
# take in an array of numbers and calculate the sum
|
4
4
|
def sum
|
@@ -15,7 +15,7 @@
|
|
15
15
|
end
|
16
16
|
|
17
17
|
sum_of_numbers
|
18
|
-
end
|
18
|
+
end unless Array.instance_methods.include? "sum"
|
19
19
|
|
20
20
|
# take in an array of numbers and calculate the mean (average)
|
21
21
|
def mean
|
@@ -27,40 +27,20 @@
|
|
27
27
|
else
|
28
28
|
0
|
29
29
|
end
|
30
|
-
end
|
31
|
-
alias_method :average, :mean
|
32
|
-
|
33
|
-
# this is an internat function (technically the developer can use it but should have no need)
|
34
|
-
# this function returns the sum of each squared difference of mean
|
35
|
-
def sum_of_deviations_squared
|
36
|
-
data = self
|
37
|
-
|
38
|
-
deviations = Array.new
|
39
|
-
average = self.mean
|
40
|
-
sum_of_deviations_squared = 0
|
41
|
-
|
42
|
-
data.each do |num|
|
43
|
-
deviations.push((num-average)**2)
|
44
|
-
end
|
45
|
-
|
46
|
-
deviations.each do |num|
|
47
|
-
sum_of_deviations_squared += num
|
48
|
-
end
|
49
|
-
|
50
|
-
sum_of_deviations_squared
|
51
|
-
end
|
30
|
+
end unless Array.instance_methods.include? "mean"
|
31
|
+
alias_method :average, :mean unless Array.instance_methods.include? "average"
|
52
32
|
|
53
33
|
# take in an array of numbers and calculate the standard deviation
|
54
34
|
def standard_deviation
|
55
35
|
data = self
|
56
36
|
sum_of_deviations_squared = self.sum_of_deviations_squared
|
57
37
|
|
58
|
-
if data.count > 1
|
38
|
+
if data.count > 1
|
59
39
|
Math::sqrt(sum_of_deviations_squared / (data.count-1))
|
60
40
|
else
|
61
41
|
0
|
62
42
|
end
|
63
|
-
end
|
43
|
+
end unless Array.instance_methods.include? "standard_deviation"
|
64
44
|
|
65
45
|
def variance
|
66
46
|
data = self
|
@@ -72,7 +52,7 @@
|
|
72
52
|
else
|
73
53
|
0
|
74
54
|
end
|
75
|
-
end
|
55
|
+
end unless Array.instance_methods.include? "variance"
|
76
56
|
|
77
57
|
# take in the array of numbers and calculate the median
|
78
58
|
def median
|
@@ -94,14 +74,14 @@
|
|
94
74
|
end
|
95
75
|
|
96
76
|
median
|
97
|
-
end
|
77
|
+
end unless Array.instance_methods.include? "median"
|
98
78
|
|
99
79
|
# take in an array of numbers and calculate the range
|
100
80
|
def range
|
101
81
|
data = self
|
102
82
|
data = data.sort
|
103
83
|
data[data.count-1] - data[0]
|
104
|
-
end
|
84
|
+
end unless Array.instance_methods.include? "range"
|
105
85
|
|
106
86
|
# take in an array of numbers and return the mode
|
107
87
|
def mode
|
@@ -133,7 +113,7 @@
|
|
133
113
|
|
134
114
|
# Check to make sure that there is a mode
|
135
115
|
data.each do |num|
|
136
|
-
if tmp["#{num}"].to_i >
|
116
|
+
if tmp["#{num}"].to_i > 0
|
137
117
|
no_mode = false
|
138
118
|
end
|
139
119
|
end
|
@@ -145,7 +125,7 @@
|
|
145
125
|
if tmp["#{num}"].to_i > most_times
|
146
126
|
highest_value = num
|
147
127
|
most_times = tmp["#{num}"]
|
148
|
-
end
|
128
|
+
end
|
149
129
|
end
|
150
130
|
|
151
131
|
# now loop through again just to make sure another number doesn't appear an equal number of times
|
@@ -158,10 +138,36 @@
|
|
158
138
|
end
|
159
139
|
|
160
140
|
if no_mode == true
|
161
|
-
|
141
|
+
nil
|
162
142
|
else
|
163
143
|
highest_value
|
164
144
|
end
|
165
145
|
end
|
146
|
+
end unless Array.instance_methods.include? "mode"
|
147
|
+
|
148
|
+
protected
|
149
|
+
|
150
|
+
# this function returns the sum of each squared difference of mean
|
151
|
+
def sum_of_deviations_squared
|
152
|
+
data = self
|
153
|
+
|
154
|
+
deviations = Array.new
|
155
|
+
average = self.mean
|
156
|
+
sum_of_deviations_squared = 0
|
157
|
+
|
158
|
+
data.each do |num|
|
159
|
+
deviations.push((num-average)**2)
|
160
|
+
end
|
161
|
+
|
162
|
+
deviations.each do |num|
|
163
|
+
sum_of_deviations_squared += num
|
164
|
+
end
|
165
|
+
|
166
|
+
sum_of_deviations_squared
|
166
167
|
end
|
168
|
+
|
169
|
+
<<<<<<< HEAD
|
170
|
+
end
|
171
|
+
=======
|
167
172
|
end
|
173
|
+
>>>>>>> origin/master
|
data/lib/easystats/version.rb
CHANGED
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
# TODO: autotest "Unable to map class EasystatsTest to a file"
|
4
|
+
class EasystatsTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_mean
|
7
|
+
assert_equal 1, [1].mean, "1"
|
8
|
+
assert_equal 1.5, [1,2].mean, "1,2"
|
9
|
+
assert_equal 2, [1,2,3].mean, "1,2,3"
|
10
|
+
assert_equal 2.5, [1,2,3,4].mean, "1,2,3,4"
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_average_synonym_for_mean
|
14
|
+
assert_equal 2.5, [1,2,3,4].average, "1,2,3,4"
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_median
|
18
|
+
assert_equal 1, [1].median, "1"
|
19
|
+
assert_equal 1.5, [1,2].median, "1,2"
|
20
|
+
assert_equal 2, [1,2,3].median, "1,2,3"
|
21
|
+
assert_equal 2.5, [1,2,3,4].median, "1,2,3,4"
|
22
|
+
assert_equal 2, [1,2,2,4].median, "1,2,2,4"
|
23
|
+
assert_equal 3, [1,3,3,4].median, "1,3,3,4"
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_mode
|
27
|
+
assert_equal 1, [1].mode, "1"
|
28
|
+
assert_equal nil, [1,2].mode, "1,2"
|
29
|
+
assert_equal nil, [1,2,3].mode, "1,2,3"
|
30
|
+
assert_equal nil, [1,2,3,4].mode, "1,2,3,4"
|
31
|
+
assert_equal 2, [1,2,2,4].mode, "1,2,2,4"
|
32
|
+
assert_equal 3, [1,3,3,4].mode, "1,3,3,4"
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_range
|
36
|
+
assert_equal 0, [1].range, "1"
|
37
|
+
assert_equal 1, [1,2].range, "1,2"
|
38
|
+
assert_equal 2, [1,2,3].range, "1,2,3"
|
39
|
+
assert_equal 3, [1,2,3,4].range, "1,2,3,4"
|
40
|
+
assert_equal 3, [1,2,2,4].range, "1,2,2,4"
|
41
|
+
assert_equal 3, [1,3,3,4].range, "1,3,3,4"
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_sum
|
45
|
+
assert_equal 1, [1].sum, "1"
|
46
|
+
assert_equal 3, [1,2].sum, "1,2"
|
47
|
+
assert_equal 6, [1,2,3].sum, "1,2,3"
|
48
|
+
assert_equal 10, [1,2,3,4].sum, "1,2,3,4"
|
49
|
+
assert_equal 9, [1,2,2,4].sum, "1,2,2,4"
|
50
|
+
assert_equal 11, [1,3,3,4].sum, "1,3,3,4"
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_variance
|
54
|
+
assert_equal 0.0, [1].variance, "1"
|
55
|
+
assert_equal 0.25, [1,2].variance, "1,2"
|
56
|
+
assert_equal 2.0/3.0, [1,2,3].variance, "1,2,3"
|
57
|
+
assert_equal 1.25, [1,2,3,4].variance, "1,2,3,4"
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_standard_deviation
|
61
|
+
delta = 0.00000000000001
|
62
|
+
assert_in_delta 0, [1].standard_deviation, delta, "1"
|
63
|
+
assert_in_delta 0.707106781186548, [1,2].standard_deviation, delta, "1,2"
|
64
|
+
assert_in_delta 1, [1,2,3].standard_deviation, delta, "1,2,3"
|
65
|
+
assert_in_delta 1.29099444873581, [1,2,3,4].standard_deviation, delta, "1,2,3,4"
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_should_not_be_able_to_access_protected_method_sum_of_deviations_squared
|
69
|
+
assert_raise(NoMethodError) do
|
70
|
+
[1].sum_of_deviations_squared
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.7
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Matthew Grigajtis
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-05-22 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
21
|
-
description: This gem contains statistics functions that are very easy to use.
|
21
|
+
description: This gem contains statistics functions that are very easy to use. Much easier and much more complete than many of the other statistical gems available out there. If you need a feature added, send me a message on Github!
|
22
22
|
email:
|
23
23
|
- matthew@grigajtis.org
|
24
24
|
executables: []
|
@@ -32,9 +32,13 @@ files:
|
|
32
32
|
- Gemfile
|
33
33
|
- README
|
34
34
|
- Rakefile
|
35
|
+
- autotest/discover.rb
|
36
|
+
- autotest/testunit.rb
|
35
37
|
- easystats.gemspec
|
36
38
|
- lib/easystats.rb
|
37
39
|
- lib/easystats/version.rb
|
40
|
+
- test/easystats_test.rb
|
41
|
+
- test/test_helper.rb
|
38
42
|
has_rdoc: true
|
39
43
|
homepage: https://github.com/mgrigajtis/easystats
|
40
44
|
licenses: []
|