easystats 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -19,6 +19,18 @@ median(x)
19
19
  Paramaters: Takes in an array of numbers
20
20
  Returns: The median of the numbers in the array
21
21
 
22
+ range(x)
23
+ Parameters: Takes in an array of numbers
24
+ Returns: The range of the numbers
25
+
26
+ mode(x)
27
+ Parameters: Takes in an array of numbers
28
+ Returns: The mode, if any. Otherwise a string that says "There is no mode"
29
+
30
+ variance(x)
31
+ Paramaters: Takes in an array of numbers
32
+ Returns: The variance
33
+
22
34
  Example usage:
23
35
 
24
36
  require "rubygems"
@@ -33,5 +45,8 @@ m = Easystats.new
33
45
 
34
46
  puts "Sum: " + m.sum(myNumbers).to_s
35
47
  puts "Average: " + m.mean(myNumbers).to_s
48
+ puts "Variance: " + m.variance(myNumbers).to_s
36
49
  puts "Standard Deviation: " + m.standard_deviation(myNumbers).to_s
37
50
  puts "Median: " + m.median(myNumbers).to_s
51
+ puts "Range: " + m.range(myNumbers).to_s
52
+ puts "Mode: " + m.mode(myNumbers).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 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!}
16
16
 
17
17
  s.rubyforge_project = "easystats"
18
18
 
data/lib/easystats.rb CHANGED
@@ -16,13 +16,14 @@ class Easystats
16
16
 
17
17
  # take in an array of numbers and calculate the mean (average)
18
18
  def mean(data)
19
- self.sum(data) / data.count
19
+ self.sum(data) / data.count.to_f
20
20
  end
21
21
 
22
- # take in an array of numbers and calculate the standard deviation
23
- def standard_deviation(data)
24
- average = self.mean(data)
22
+ # this is an internat function (technically the developer can use it but should have no need)
23
+ # this function returns the sum of each squared difference of mean
24
+ def sum_of_deviations_squared(data)
25
25
  deviations = Array.new
26
+ average = self.mean(data)
26
27
  sum_of_deviations = 0
27
28
 
28
29
  data.each do |num|
@@ -33,9 +34,22 @@ class Easystats
33
34
  sum_of_deviations += num
34
35
  end
35
36
 
37
+ sum_of_deviations
38
+ end
39
+
40
+ # take in an array of numbers and calculate the standard deviation
41
+ def standard_deviation(data)
42
+ sum_of_deviations = self.sum_of_deviations_squared(data)
43
+
36
44
  Math::sqrt(sum_of_deviations / (data.count-1))
37
45
  end
38
46
 
47
+ def variance(data)
48
+ average = self.mean(data)
49
+ sum_of_deviations = self.sum_of_deviations_squared(data)
50
+ sum_of_deviations / data.count.to_f
51
+ end
52
+
39
53
  # take in the array of numbers and calculate the median
40
54
  def median(data)
41
55
  halfway = data.count / 2
@@ -48,7 +62,7 @@ class Easystats
48
62
  if(data.count % 2) == 0
49
63
  median = (data[halfway] + data[halfway-1]) / 2.0
50
64
 
51
- # Else, there is an odd number in the array
65
+ # Else, there is an odd number of elements in the array
52
66
  else
53
67
  median = data[halfway]
54
68
  end
@@ -62,5 +76,64 @@ class Easystats
62
76
  data = data.sort
63
77
  data[data.count-1] - data[0]
64
78
  end
65
- end
66
79
 
80
+ # take in an array of numbers and return the mode
81
+ def mode(data)
82
+ # Sort the array
83
+ data = data.sort
84
+
85
+ # create a flag to tell the user if all the numbers only appear once
86
+ no_mode = true
87
+
88
+ # The variable that will hold the highest number
89
+ highest_value = 0
90
+
91
+ # The variable that holds the most time the value appears
92
+ most_times = 0
93
+
94
+ # Create a new hash to hold the numbers
95
+ tmp = Hash.new
96
+
97
+ # Populate the hash
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
116
+ data.each do |num|
117
+ if tmp["#{num}"].to_i > most_times
118
+ highest_value = num
119
+ most_times = tmp["#{num}"]
120
+ end
121
+ end
122
+
123
+ # now loop through again just to make sure another number doesn't appear an equal number of times
124
+ data.each do |num|
125
+ if num != highest_value
126
+ if tmp["#{num}"].to_i == most_times
127
+ no_mode = true
128
+ end
129
+ end
130
+ end
131
+
132
+ if no_mode == true
133
+ "There is no mode"
134
+ else
135
+ highest_value
136
+ end
137
+ end
138
+ end
139
+ end
@@ -1,3 +1,3 @@
1
1
  module Easystats
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
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-12 00:00:00 -05:00
17
+ date: 2011-01-13 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 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!
22
22
  email:
23
23
  - matthew@grigajtis.org
24
24
  executables: []