easystats 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README +15 -25
- data/easystats.gemspec +1 -1
- data/lib/easystats/version.rb +1 -1
- data/lib/easystats.rb +105 -99
- metadata +4 -4
data/README
CHANGED
@@ -1,34 +1,27 @@
|
|
1
1
|
Gem Name: Easystats
|
2
2
|
Gem Author: Matthew Grigajtis (http://www.matthewgrigajtis.com)
|
3
|
-
Description: Provides easy to use statistical functions
|
3
|
+
Description: Provides easy to use statistical functions to use on an array
|
4
4
|
|
5
5
|
Functions Provided:
|
6
|
-
sum
|
7
|
-
Parameters: Takes in an array of numbers
|
6
|
+
sum
|
8
7
|
Returns: The sum of the numbers in the array
|
9
8
|
|
10
|
-
mean
|
11
|
-
Parameters: Takes in an array of numbers
|
9
|
+
mean
|
12
10
|
Returns: The mean (average) of the numbers in the array
|
13
11
|
|
14
|
-
standard_deviation
|
15
|
-
Parameters: Takes in an array of numbers
|
12
|
+
standard_deviation
|
16
13
|
Returns: The standard deviation of the numbers in the array
|
17
14
|
|
18
|
-
median
|
19
|
-
Paramaters: Takes in an array of numbers
|
15
|
+
median
|
20
16
|
Returns: The median of the numbers in the array
|
21
17
|
|
22
|
-
range
|
23
|
-
Parameters: Takes in an array of numbers
|
18
|
+
range
|
24
19
|
Returns: The range of the numbers
|
25
20
|
|
26
|
-
mode
|
27
|
-
Parameters: Takes in an array of numbers
|
21
|
+
mode
|
28
22
|
Returns: The mode, if any. Otherwise a string that says "There is no mode"
|
29
23
|
|
30
|
-
variance
|
31
|
-
Paramaters: Takes in an array of numbers
|
24
|
+
variance
|
32
25
|
Returns: The variance
|
33
26
|
|
34
27
|
Example usage:
|
@@ -40,13 +33,10 @@ require "easystats"
|
|
40
33
|
myNumbers = Array.new
|
41
34
|
myNumbers = [4, 8, 15, 16, 23, 42]
|
42
35
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
puts "
|
47
|
-
puts "
|
48
|
-
puts "
|
49
|
-
puts "
|
50
|
-
puts "Median: " + m.median(myNumbers).to_s
|
51
|
-
puts "Range: " + m.range(myNumbers).to_s
|
52
|
-
puts "Mode: " + m.mode(myNumbers).to_s
|
36
|
+
puts "Range: " + myNumbers.range.to_s
|
37
|
+
puts "Sum: " + myNumbers.sum.to_s
|
38
|
+
puts "Average: " + myNumbers.mean.to_s
|
39
|
+
puts "Median: " + myNumbers.median.to_s
|
40
|
+
puts "Variance: " + myNumbers.variance.to_s
|
41
|
+
puts "Standard Deviation: " + myNumbers.standard_deviation.to_s
|
42
|
+
puts "Mode: " + myNumbers.mode.to_s
|
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 easy to use.
|
15
|
+
s.description = %q{This gem contains statistics functions that are very easy to use. The gem is still in its infancy, but it currently provides easy to use methods for sum, mean, standard deviation, median, range, mode, and variance. More functions will be added as the gem matures, please feel free to fork on Github or send me bug reports!}
|
16
16
|
|
17
17
|
s.rubyforge_project = "easystats"
|
18
18
|
|
data/lib/easystats/version.rb
CHANGED
data/lib/easystats.rb
CHANGED
@@ -1,139 +1,145 @@
|
|
1
|
-
class
|
2
|
-
|
1
|
+
class Object
|
2
|
+
# take in an array of numbers and calculate the sum
|
3
|
+
def sum
|
4
|
+
data = self
|
5
|
+
sum_of_numbers = 0
|
3
6
|
|
4
|
-
|
7
|
+
data.each do |num|
|
8
|
+
sum_of_numbers += num
|
9
|
+
end
|
5
10
|
|
6
|
-
|
7
|
-
|
8
|
-
sum_of_numbers = 0
|
11
|
+
sum_of_numbers
|
12
|
+
end
|
9
13
|
|
10
|
-
|
11
|
-
|
14
|
+
# take in an array of numbers and calculate the mean (average)
|
15
|
+
def mean
|
16
|
+
data = self
|
17
|
+
self.sum / data.count.to_f
|
12
18
|
end
|
13
19
|
|
14
|
-
|
15
|
-
|
20
|
+
# this is an internat function (technically the developer can use it but should have no need)
|
21
|
+
# this function returns the sum of each squared difference of mean
|
22
|
+
def sum_of_deviations_squared
|
23
|
+
data = self
|
16
24
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
25
|
+
deviations = Array.new
|
26
|
+
average = self.mean
|
27
|
+
sum_of_deviations = 0
|
21
28
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
deviations = Array.new
|
26
|
-
average = self.mean(data)
|
27
|
-
sum_of_deviations = 0
|
29
|
+
data.each do |num|
|
30
|
+
deviations.push((num-average)**2)
|
31
|
+
end
|
28
32
|
|
29
|
-
|
30
|
-
|
31
|
-
|
33
|
+
deviations.each do |num|
|
34
|
+
sum_of_deviations += num
|
35
|
+
end
|
32
36
|
|
33
|
-
|
34
|
-
sum_of_deviations += num
|
37
|
+
sum_of_deviations
|
35
38
|
end
|
36
39
|
|
37
|
-
|
38
|
-
|
40
|
+
# take in an array of numbers and calculate the standard deviation
|
41
|
+
def standard_deviation
|
42
|
+
data = self
|
43
|
+
sum_of_deviations = self.sum_of_deviations_squared
|
39
44
|
|
40
|
-
|
41
|
-
|
42
|
-
sum_of_deviations = self.sum_of_deviations_squared(data)
|
45
|
+
Math::sqrt(sum_of_deviations / (data.count-1))
|
46
|
+
end
|
43
47
|
|
44
|
-
|
45
|
-
|
48
|
+
def variance
|
49
|
+
data = self
|
50
|
+
average = self.mean
|
51
|
+
sum_of_deviations = self.sum_of_deviations_squared
|
52
|
+
sum_of_deviations / data.count.to_f
|
53
|
+
end
|
46
54
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
55
|
+
# take in the array of numbers and calculate the median
|
56
|
+
def median
|
57
|
+
data = self
|
58
|
+
|
59
|
+
halfway = data.count / 2
|
52
60
|
|
53
|
-
|
54
|
-
|
55
|
-
halfway = data.count / 2
|
61
|
+
# Sort the array
|
62
|
+
data = data.sort
|
56
63
|
|
57
|
-
|
58
|
-
|
64
|
+
# The median will be different based on the number of numbers in the array
|
65
|
+
# If there is an even number in the array
|
66
|
+
if(data.count % 2) == 0
|
67
|
+
median = (data[halfway] + data[halfway-1]) / 2.0
|
59
68
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
69
|
+
# Else, there is an odd number of elements in the array
|
70
|
+
else
|
71
|
+
median = data[halfway]
|
72
|
+
end
|
64
73
|
|
65
|
-
|
66
|
-
else
|
67
|
-
median = data[halfway]
|
74
|
+
median
|
68
75
|
end
|
69
76
|
|
70
|
-
|
71
|
-
|
77
|
+
# take in an array of numbers and calculate the range
|
78
|
+
def range
|
79
|
+
data = self
|
80
|
+
data = data.sort
|
81
|
+
data[data.count-1] - data[0]
|
82
|
+
end
|
72
83
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
data = data.sort
|
77
|
-
data[data.count-1] - data[0]
|
78
|
-
end
|
84
|
+
# take in an array of numbers and return the mode
|
85
|
+
def mode
|
86
|
+
data = self
|
79
87
|
|
80
|
-
|
81
|
-
|
82
|
-
# Sort the array
|
83
|
-
data = data.sort
|
88
|
+
# Sort the array
|
89
|
+
data = data.sort
|
84
90
|
|
85
|
-
|
86
|
-
|
91
|
+
# create a flag to tell the user if all the numbers only appear once
|
92
|
+
no_mode = true
|
87
93
|
|
88
|
-
|
89
|
-
|
94
|
+
# The variable that will hold the highest number
|
95
|
+
highest_value = 0
|
90
96
|
|
91
|
-
|
92
|
-
|
97
|
+
# The variable that holds the most time the value appears
|
98
|
+
most_times = 0
|
93
99
|
|
94
|
-
|
95
|
-
|
100
|
+
# Create a new hash to hold the numbers
|
101
|
+
tmp = Hash.new
|
96
102
|
|
97
|
-
|
98
|
-
data.each do |num|
|
99
|
-
if tmp["#{num}"].nil? == false
|
100
|
-
tmp["#{num}"] = tmp["#{num}"].to_i + 1
|
101
|
-
else
|
102
|
-
tmp["#{num}"] = 1
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
# Check to make sure that there is a mode
|
107
|
-
data.each do |num|
|
108
|
-
if tmp["#{num}"].to_i > 1
|
109
|
-
no_mode = false
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
if no_mode == true
|
114
|
-
"There is no mode"
|
115
|
-
else
|
103
|
+
# Populate the hash
|
116
104
|
data.each do |num|
|
117
|
-
if tmp["#{num}"].
|
118
|
-
|
119
|
-
|
120
|
-
|
105
|
+
if tmp["#{num}"].nil? == false
|
106
|
+
tmp["#{num}"] = tmp["#{num}"].to_i + 1
|
107
|
+
else
|
108
|
+
tmp["#{num}"] = 1
|
109
|
+
end
|
121
110
|
end
|
122
111
|
|
123
|
-
#
|
112
|
+
# Check to make sure that there is a mode
|
124
113
|
data.each do |num|
|
125
|
-
if num
|
126
|
-
|
127
|
-
no_mode = true
|
128
|
-
end
|
114
|
+
if tmp["#{num}"].to_i > 1
|
115
|
+
no_mode = false
|
129
116
|
end
|
130
117
|
end
|
131
118
|
|
132
119
|
if no_mode == true
|
133
120
|
"There is no mode"
|
134
121
|
else
|
135
|
-
|
122
|
+
data.each do |num|
|
123
|
+
if tmp["#{num}"].to_i > most_times
|
124
|
+
highest_value = num
|
125
|
+
most_times = tmp["#{num}"]
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# now loop through again just to make sure another number doesn't appear an equal number of times
|
130
|
+
data.each do |num|
|
131
|
+
if num != highest_value
|
132
|
+
if tmp["#{num}"].to_i == most_times
|
133
|
+
no_mode = true
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
if no_mode == true
|
139
|
+
"There is no mode"
|
140
|
+
else
|
141
|
+
highest_value
|
142
|
+
end
|
136
143
|
end
|
137
144
|
end
|
138
145
|
end
|
139
|
-
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
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-01-
|
17
|
+
date: 2011-01-14 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
21
|
-
description: This gem contains statistics functions that are easy to use.
|
21
|
+
description: This gem contains statistics functions that are very easy to use. The gem is still in its infancy, but it currently provides easy to use methods for sum, mean, standard deviation, median, range, mode, and variance. More functions will be added as the gem matures, please feel free to fork on Github or send me bug reports!
|
22
22
|
email:
|
23
23
|
- matthew@grigajtis.org
|
24
24
|
executables: []
|