easystats 0.0.1 → 0.0.2
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 +20 -2
- data/lib/easystats/version.rb +1 -1
- data/lib/easystats.rb +35 -3
- metadata +2 -2
data/README
CHANGED
@@ -1,6 +1,23 @@
|
|
1
1
|
Gem Name: Easystats
|
2
2
|
Gem Author: Matthew Grigajtis (http://www.matthewgrigajtis.com)
|
3
|
-
Description: Provides easy to use statistical
|
3
|
+
Description: Provides easy to use statistical functions
|
4
|
+
|
5
|
+
Functions Provided:
|
6
|
+
sum(x)
|
7
|
+
Parameters: Takes in an array of numbers
|
8
|
+
Returns: The sum of the numbers in the array
|
9
|
+
|
10
|
+
mean(x)
|
11
|
+
Parameters: Takes in an array of numbers
|
12
|
+
Returns: The mean (average) of the numbers in the array
|
13
|
+
|
14
|
+
standard_deviation(x)
|
15
|
+
Parameters: Takes in an array of numbers
|
16
|
+
Returns: The standard deviation of the numbers in the array
|
17
|
+
|
18
|
+
median(x)
|
19
|
+
Paramaters: Takes in an array of numbers
|
20
|
+
Returns: The median of the numbers in the array
|
4
21
|
|
5
22
|
Example usage:
|
6
23
|
|
@@ -14,6 +31,7 @@ myNumbers = [4, 8, 15, 16, 23, 42]
|
|
14
31
|
# Create a new Easystats Object
|
15
32
|
m = Easystats.new
|
16
33
|
|
34
|
+
puts "Sum: " + m.sum(myNumbers).to_s
|
17
35
|
puts "Average: " + m.mean(myNumbers).to_s
|
18
36
|
puts "Standard Deviation: " + m.standard_deviation(myNumbers).to_s
|
19
|
-
|
37
|
+
puts "Median: " + m.median(myNumbers).to_s
|
data/lib/easystats/version.rb
CHANGED
data/lib/easystats.rb
CHANGED
@@ -3,15 +3,20 @@ class Easystats
|
|
3
3
|
|
4
4
|
end
|
5
5
|
|
6
|
-
# take in an array of numbers and calculate the
|
7
|
-
def
|
6
|
+
# take in an array of numbers and calculate the sum
|
7
|
+
def sum(data)
|
8
8
|
sum_of_numbers = 0
|
9
9
|
|
10
10
|
data.each do |num|
|
11
11
|
sum_of_numbers += num
|
12
12
|
end
|
13
13
|
|
14
|
-
sum_of_numbers
|
14
|
+
sum_of_numbers
|
15
|
+
end
|
16
|
+
|
17
|
+
# take in an array of numbers and calculate the mean (average)
|
18
|
+
def mean(data)
|
19
|
+
self.sum(data) / data.count
|
15
20
|
end
|
16
21
|
|
17
22
|
# take in an array of numbers and calculate the standard deviation
|
@@ -30,5 +35,32 @@ class Easystats
|
|
30
35
|
|
31
36
|
Math::sqrt(sum_of_deviations / (data.count-1))
|
32
37
|
end
|
38
|
+
|
39
|
+
# take in the array of numbers and calculate the median
|
40
|
+
def median(data)
|
41
|
+
halfway = data.count / 2
|
42
|
+
|
43
|
+
# Sort the array
|
44
|
+
data = data.sort
|
45
|
+
|
46
|
+
# The median will be different based on the number of numbers in the array
|
47
|
+
# If there is an even number in the array
|
48
|
+
if(data.count % 2) == 0
|
49
|
+
median = (data[halfway] + data[halfway-1]) / 2.0
|
50
|
+
|
51
|
+
# Else, there is an odd number in the array
|
52
|
+
else
|
53
|
+
median = data[halfway]
|
54
|
+
end
|
55
|
+
|
56
|
+
median
|
57
|
+
end
|
58
|
+
|
59
|
+
# take in an array of numbers and calculate the range
|
60
|
+
def range(data)
|
61
|
+
# Sort the array
|
62
|
+
data = data.sort
|
63
|
+
data[data.count-1] - data[0]
|
64
|
+
end
|
33
65
|
end
|
34
66
|
|