easystats 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rspec +2 -0
- data/.travis.yml +1 -1
- data/Gemfile +1 -1
- data/Guardfile +5 -4
- data/README.md +51 -0
- data/Rakefile +8 -9
- data/easystats.gemspec +4 -3
- data/lib/easystats.rb +110 -136
- data/lib/easystats/version.rb +1 -1
- data/spec/lib/easystats_spec.rb +74 -0
- data/spec/spec_helper.rb +5 -0
- metadata +43 -9
- data/README +0 -45
- data/test/easystats_test.rb +0 -74
- data/test/test_helper.rb +0 -5
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Guardfile
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
guard :
|
2
|
-
watch(%r{^
|
3
|
-
watch(%r{^
|
4
|
-
watch('
|
1
|
+
guard 'rspec', :version => 2 do
|
2
|
+
watch(%r{^spec/.+_spec\.rb$})
|
3
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
4
|
+
watch('spec/spec_helper.rb') { "spec" }
|
5
5
|
end
|
6
|
+
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Easystats [![Build Status](https://secure.travis-ci.org/mgrigajtis/easystats.png)](https://secure.travis-ci.org/mgrigajtis/easystats)
|
2
|
+
|
3
|
+
> Created by [Matthew Grigajtis](http://www.matthewgrigajtis.com)
|
4
|
+
|
5
|
+
Provides easy to use statistical functions to use on an array
|
6
|
+
|
7
|
+
## Install
|
8
|
+
|
9
|
+
In your shell:
|
10
|
+
|
11
|
+
```sh
|
12
|
+
gem install easystats
|
13
|
+
```
|
14
|
+
|
15
|
+
or in your Gemfile:
|
16
|
+
|
17
|
+
```rb
|
18
|
+
gem 'easystats'
|
19
|
+
```
|
20
|
+
|
21
|
+
## Example
|
22
|
+
|
23
|
+
```rb
|
24
|
+
require 'easystats'
|
25
|
+
|
26
|
+
array = [4, 8, 15, 16, 23, 42, 42]
|
27
|
+
|
28
|
+
%w[
|
29
|
+
average
|
30
|
+
median
|
31
|
+
mode
|
32
|
+
range
|
33
|
+
standard_deviation
|
34
|
+
sum
|
35
|
+
variance
|
36
|
+
].each do |method|
|
37
|
+
puts "#{method}: #{array.send(method.to_sym)}"
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
This will result in:
|
42
|
+
|
43
|
+
```sh
|
44
|
+
average: 21.428571428571427
|
45
|
+
median: 16
|
46
|
+
mode: 42
|
47
|
+
range: 38
|
48
|
+
standard_deviation: 15.295501984321435
|
49
|
+
sum: 150
|
50
|
+
variance: 200.53061224489798
|
51
|
+
```
|
data/Rakefile
CHANGED
@@ -4,12 +4,11 @@ require 'rake/testtask'
|
|
4
4
|
|
5
5
|
Bundler::GemHelper.install_tasks
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
|
9
|
+
desc 'Default: run specs.'
|
10
|
+
task :default => :spec
|
11
|
+
|
12
|
+
desc "Run specs"
|
13
|
+
RSpec::Core::RakeTask.new
|
14
|
+
|
data/easystats.gemspec
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
3
|
require "easystats/version"
|
4
|
-
require "complex"
|
5
|
-
include Math
|
6
4
|
|
7
5
|
Gem::Specification.new do |s|
|
8
6
|
s.name = "easystats"
|
@@ -21,5 +19,8 @@ Gem::Specification.new do |s|
|
|
21
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
20
|
s.require_paths = ["lib"]
|
23
21
|
|
24
|
-
s.add_development_dependency "guard-
|
22
|
+
s.add_development_dependency "guard-rspec"
|
23
|
+
s.add_development_dependency "rake"
|
24
|
+
s.add_development_dependency "rspec"
|
25
|
+
s.add_development_dependency "simplecov"
|
25
26
|
end
|
data/lib/easystats.rb
CHANGED
@@ -1,169 +1,143 @@
|
|
1
1
|
class Array
|
2
|
+
# take in an array of numbers and calculate the mean (average)
|
3
|
+
def mean
|
4
|
+
return unless self.any?
|
2
5
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
sum_of_numbers = 0
|
6
|
+
self.sum / self.count.to_f
|
7
|
+
end unless Array.instance_methods.include? "mean"
|
8
|
+
alias_method :average, :mean unless Array.instance_methods.include? "average"
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
sum_of_numbers += num
|
12
|
-
end
|
13
|
-
else
|
14
|
-
sum_of_numbers = 0
|
15
|
-
end
|
10
|
+
# take in the array of numbers and calculate the median
|
11
|
+
def median
|
12
|
+
return unless self.any?
|
16
13
|
|
17
|
-
|
18
|
-
end unless Array.instance_methods.include? "sum"
|
14
|
+
data = self
|
19
15
|
|
20
|
-
|
21
|
-
def mean
|
22
|
-
data = self
|
23
|
-
|
24
|
-
# Check to make sure there are numbers to avoid division by 0
|
25
|
-
if data.count > 0
|
26
|
-
self.sum / data.count.to_f
|
27
|
-
else
|
28
|
-
0
|
29
|
-
end
|
30
|
-
end unless Array.instance_methods.include? "mean"
|
31
|
-
alias_method :average, :mean unless Array.instance_methods.include? "average"
|
32
|
-
|
33
|
-
# take in an array of numbers and calculate the standard deviation
|
34
|
-
def standard_deviation
|
35
|
-
data = self
|
36
|
-
sum_of_deviations_squared = self.sum_of_deviations_squared
|
37
|
-
|
38
|
-
if data.count > 1
|
39
|
-
Math::sqrt(sum_of_deviations_squared / (data.count-1))
|
40
|
-
else
|
41
|
-
0
|
42
|
-
end
|
43
|
-
end unless Array.instance_methods.include? "standard_deviation"
|
44
|
-
|
45
|
-
def variance
|
46
|
-
data = self
|
47
|
-
average = self.mean
|
48
|
-
sum_of_deviations = self.sum_of_deviations_squared
|
49
|
-
|
50
|
-
if data.count > 0
|
51
|
-
sum_of_deviations / data.count.to_f
|
52
|
-
else
|
53
|
-
0
|
54
|
-
end
|
55
|
-
end unless Array.instance_methods.include? "variance"
|
16
|
+
halfway = data.count / 2
|
56
17
|
|
57
|
-
#
|
58
|
-
|
59
|
-
data = self
|
18
|
+
# Sort the array
|
19
|
+
data = data.sort
|
60
20
|
|
61
|
-
|
21
|
+
# The median will be different based on the number of numbers in the array
|
22
|
+
# If there is an even number in the array
|
23
|
+
if(data.count % 2) == 0
|
24
|
+
median = (data[halfway] + data[halfway-1]) / 2.0
|
62
25
|
|
63
|
-
|
64
|
-
|
26
|
+
# Else, there is an odd number of elements in the array
|
27
|
+
else
|
28
|
+
median = data[halfway]
|
29
|
+
end
|
65
30
|
|
66
|
-
|
67
|
-
|
68
|
-
if(data.count % 2) == 0
|
69
|
-
median = (data[halfway] + data[halfway-1]) / 2.0
|
31
|
+
median
|
32
|
+
end unless Array.instance_methods.include? "median"
|
70
33
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
end
|
34
|
+
# take in an array of numbers and return the mode
|
35
|
+
def mode
|
36
|
+
return unless self.any?
|
75
37
|
|
76
|
-
|
77
|
-
|
38
|
+
# Sort the array
|
39
|
+
data = self.sort
|
78
40
|
|
79
|
-
#
|
80
|
-
|
81
|
-
data = self
|
82
|
-
data = data.sort
|
83
|
-
data[data.count-1] - data[0]
|
84
|
-
end unless Array.instance_methods.include? "range"
|
41
|
+
# create a flag to tell the user if all the numbers only appear once
|
42
|
+
no_mode = true
|
85
43
|
|
86
|
-
#
|
87
|
-
|
88
|
-
data = self
|
44
|
+
# The variable that will hold the highest number
|
45
|
+
highest_value = 0
|
89
46
|
|
90
|
-
|
91
|
-
|
47
|
+
# The variable that holds the most time the value appears
|
48
|
+
most_times = 0
|
92
49
|
|
93
|
-
|
94
|
-
|
50
|
+
# Create a new hash to hold the numbers
|
51
|
+
tmp = Hash.new
|
95
52
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
53
|
+
# Populate the hash
|
54
|
+
data.each do |num|
|
55
|
+
if tmp["#{num}"].nil? == false
|
56
|
+
tmp["#{num}"] = tmp["#{num}"].to_i + 1
|
57
|
+
else
|
58
|
+
tmp["#{num}"] = 1
|
59
|
+
end
|
60
|
+
end
|
101
61
|
|
102
|
-
|
103
|
-
|
62
|
+
# Check to make sure that there is a mode
|
63
|
+
data.each do |num|
|
64
|
+
if tmp["#{num}"].to_i > 0
|
65
|
+
no_mode = false
|
66
|
+
end
|
67
|
+
end
|
104
68
|
|
105
|
-
|
69
|
+
if no_mode == true
|
70
|
+
nil
|
71
|
+
else
|
106
72
|
data.each do |num|
|
107
|
-
if tmp["#{num}"].
|
108
|
-
|
109
|
-
|
110
|
-
tmp["#{num}"] = 1
|
73
|
+
if tmp["#{num}"].to_i > most_times
|
74
|
+
highest_value = num
|
75
|
+
most_times = tmp["#{num}"]
|
111
76
|
end
|
112
77
|
end
|
113
78
|
|
114
|
-
#
|
79
|
+
# now loop through again just to make sure another number doesn't appear an equal number of times
|
115
80
|
data.each do |num|
|
116
|
-
if
|
117
|
-
|
81
|
+
if num != highest_value
|
82
|
+
if tmp["#{num}"].to_i == most_times
|
83
|
+
no_mode = true
|
84
|
+
end
|
118
85
|
end
|
119
86
|
end
|
120
|
-
|
87
|
+
|
121
88
|
if no_mode == true
|
122
|
-
|
89
|
+
nil
|
123
90
|
else
|
124
|
-
|
125
|
-
if tmp["#{num}"].to_i > most_times
|
126
|
-
highest_value = num
|
127
|
-
most_times = tmp["#{num}"]
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
# now loop through again just to make sure another number doesn't appear an equal number of times
|
132
|
-
data.each do |num|
|
133
|
-
if num != highest_value
|
134
|
-
if tmp["#{num}"].to_i == most_times
|
135
|
-
no_mode = true
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
if no_mode == true
|
141
|
-
nil
|
142
|
-
else
|
143
|
-
highest_value
|
144
|
-
end
|
91
|
+
highest_value
|
145
92
|
end
|
146
|
-
end
|
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
|
93
|
+
end
|
94
|
+
end unless Array.instance_methods.include? "mode"
|
157
95
|
|
158
|
-
|
159
|
-
|
160
|
-
|
96
|
+
# take in an array of numbers and calculate the range
|
97
|
+
def range
|
98
|
+
return unless self.any?
|
161
99
|
|
162
|
-
|
163
|
-
|
164
|
-
|
100
|
+
data = self.sort
|
101
|
+
data.last - data.first
|
102
|
+
end unless Array.instance_methods.include? "range"
|
103
|
+
|
104
|
+
# take in an array of numbers and calculate the standard deviation
|
105
|
+
def standard_deviation
|
106
|
+
return unless self.any?
|
107
|
+
return 0 if self.one?
|
165
108
|
|
166
|
-
|
109
|
+
Math::sqrt(self.sum_of_deviations_squared / (self.count-1))
|
110
|
+
end unless Array.instance_methods.include? "standard_deviation"
|
111
|
+
|
112
|
+
# take in an array of numbers and calculate the sum
|
113
|
+
def sum
|
114
|
+
self.reduce { |total, number| total + number }
|
115
|
+
end unless Array.instance_methods.include? "sum"
|
116
|
+
|
117
|
+
def variance
|
118
|
+
return unless self.any?
|
119
|
+
|
120
|
+
self.sum_of_deviations_squared / self.count.to_f
|
121
|
+
end unless Array.instance_methods.include? "variance"
|
122
|
+
|
123
|
+
protected
|
124
|
+
|
125
|
+
# this function returns the sum of each squared difference of mean
|
126
|
+
def sum_of_deviations_squared
|
127
|
+
data = self
|
128
|
+
|
129
|
+
deviations = Array.new
|
130
|
+
average = self.mean
|
131
|
+
sum_of_deviations_squared = 0
|
132
|
+
|
133
|
+
data.each do |num|
|
134
|
+
deviations.push((num-average)**2)
|
167
135
|
end
|
168
|
-
|
136
|
+
|
137
|
+
deviations.each do |num|
|
138
|
+
sum_of_deviations_squared += num
|
139
|
+
end
|
140
|
+
|
141
|
+
sum_of_deviations_squared
|
142
|
+
end
|
169
143
|
end
|
data/lib/easystats/version.rb
CHANGED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
describe "#mean" do
|
5
|
+
it { [].mean.should be_nil }
|
6
|
+
it { [1].mean.should == 1 }
|
7
|
+
it { [1,2].mean.should == 1.5 }
|
8
|
+
it { [1,2,3].mean.should == 2 }
|
9
|
+
it { [1,2,3,4].mean.should == 2.5 }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#average" do
|
13
|
+
it { [1,2,3,4].average.should == 2.5 }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#median" do
|
17
|
+
it { [].median.should be_nil }
|
18
|
+
it { [1].median.should == 1 }
|
19
|
+
it { [1,2].median.should == 1.5 }
|
20
|
+
it { [1,2,3].median.should == 2 }
|
21
|
+
it { [1,2,3,4].median.should == 2.5 }
|
22
|
+
it { [1,2,2,4].median.should == 2 }
|
23
|
+
it { [1,3,3,4].median.should == 3 }
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#mode" do
|
27
|
+
it { [].mode.should be_nil }
|
28
|
+
it { [1].mode.should == 1 }
|
29
|
+
it { [1,2].mode.should be_nil }
|
30
|
+
it { [1,2,3].mode.should be_nil }
|
31
|
+
it { [1,2,3,4].mode.should be_nil }
|
32
|
+
it { [1,2,2,4].mode.should == 2 }
|
33
|
+
it { [1,3,3,4].mode.should == 3 }
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#range" do
|
37
|
+
it { [].range.should be_nil }
|
38
|
+
it { [1].range.should == 0 }
|
39
|
+
it { [1,2].range.should == 1 }
|
40
|
+
it { [1,2,3].range.should == 2 }
|
41
|
+
it { [1,2,3,4].range.should == 3 }
|
42
|
+
it { [1,2,2,4].range.should == 3 }
|
43
|
+
it { [1,3,3,4].range.should == 3 }
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#standard_deviation" do
|
47
|
+
let(:delta) { 0.00000000000001 }
|
48
|
+
|
49
|
+
it { [].standard_deviation.should be_nil }
|
50
|
+
it { [1].standard_deviation.should == 0 }
|
51
|
+
it { [1,2].standard_deviation.should be_within(delta).of(0.707106781186548) }
|
52
|
+
it { [1,2,3].standard_deviation.should be_within(delta).of(1) }
|
53
|
+
it { [1,2,3,4].standard_deviation.should be_within(delta).of(1.29099444873581) }
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#sum" do
|
57
|
+
it { [].sum.should be_nil }
|
58
|
+
it { [1].sum.should == 1 }
|
59
|
+
it { [1,2].sum.should == 3 }
|
60
|
+
it { [1,2,3].sum.should == 6 }
|
61
|
+
it { [1,2,3,4].sum.should == 10 }
|
62
|
+
it { [1,2,2,4].sum.should == 9 }
|
63
|
+
it { [1,3,3,4].sum.should == 11 }
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#variance" do
|
67
|
+
it { [].variance.should be_nil }
|
68
|
+
it { [1].variance.should == 0.0 }
|
69
|
+
it { [1,2].variance.should == 0.25 }
|
70
|
+
it { [1,2,3].variance.should == 2.0/3.0 }
|
71
|
+
it { [1,2,3,4].variance.should == 1.25 }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easystats
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,8 +13,8 @@ cert_chain: []
|
|
13
13
|
date: 2012-04-11 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name: guard-
|
17
|
-
requirement: &
|
16
|
+
name: guard-rspec
|
17
|
+
requirement: &70146983589700 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,7 +22,40 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70146983589700
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: &70146983589240 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *70146983589240
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
requirement: &70146983588620 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *70146983588620
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: simplecov
|
50
|
+
requirement: &70146983588020 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *70146983588020
|
26
59
|
description: This gem contains statistics functions that are very easy to use. Much
|
27
60
|
easier and much more complete than many of the other statistical gems available
|
28
61
|
out there. If you need a feature added, send me a message on Github!
|
@@ -34,17 +67,18 @@ extensions: []
|
|
34
67
|
extra_rdoc_files: []
|
35
68
|
files:
|
36
69
|
- .gitignore
|
70
|
+
- .rspec
|
37
71
|
- .rvmrc
|
38
72
|
- .travis.yml
|
39
73
|
- Gemfile
|
40
74
|
- Guardfile
|
41
|
-
- README
|
75
|
+
- README.md
|
42
76
|
- Rakefile
|
43
77
|
- easystats.gemspec
|
44
78
|
- lib/easystats.rb
|
45
79
|
- lib/easystats/version.rb
|
46
|
-
-
|
47
|
-
-
|
80
|
+
- spec/lib/easystats_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
48
82
|
homepage: https://github.com/mgrigajtis/easystats
|
49
83
|
licenses: []
|
50
84
|
post_install_message:
|
@@ -70,5 +104,5 @@ signing_key:
|
|
70
104
|
specification_version: 3
|
71
105
|
summary: Easy to use statistics functions
|
72
106
|
test_files:
|
73
|
-
-
|
74
|
-
-
|
107
|
+
- spec/lib/easystats_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
data/README
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
Gem Name: Easystats
|
2
|
-
Gem Author: Matthew Grigajtis (http://www.matthewgrigajtis.com)
|
3
|
-
Description: Provides easy to use statistical functions to use on an array
|
4
|
-
|
5
|
-
Functions Provided:
|
6
|
-
sum
|
7
|
-
Returns: The sum of the numbers in the array
|
8
|
-
|
9
|
-
mean
|
10
|
-
Returns: The mean (average) of the numbers in the array
|
11
|
-
|
12
|
-
average
|
13
|
-
An alias of mean
|
14
|
-
|
15
|
-
standard_deviation
|
16
|
-
Returns: The standard deviation of the numbers in the array
|
17
|
-
|
18
|
-
median
|
19
|
-
Returns: The median of the numbers in the array
|
20
|
-
|
21
|
-
range
|
22
|
-
Returns: The range of the numbers
|
23
|
-
|
24
|
-
mode
|
25
|
-
Returns: The mode. If there is no mode returns 0.
|
26
|
-
|
27
|
-
variance
|
28
|
-
Returns: The variance
|
29
|
-
|
30
|
-
Example usage:
|
31
|
-
|
32
|
-
require "rubygems"
|
33
|
-
require "easystats"
|
34
|
-
|
35
|
-
# Create a test array of numbers
|
36
|
-
myNumbers = Array.new
|
37
|
-
myNumbers = [4, 8, 15, 16, 23, 42]
|
38
|
-
|
39
|
-
puts "Range: " + myNumbers.range.to_s
|
40
|
-
puts "Sum: " + myNumbers.sum.to_s
|
41
|
-
puts "Average: " + myNumbers.mean.to_s
|
42
|
-
puts "Median: " + myNumbers.median.to_s
|
43
|
-
puts "Variance: " + myNumbers.variance.to_s
|
44
|
-
puts "Standard Deviation: " + myNumbers.standard_deviation.to_s
|
45
|
-
puts "Mode: " + myNumbers.mode.to_s
|
data/test/easystats_test.rb
DELETED
@@ -1,74 +0,0 @@
|
|
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
|