-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard_deviation.rb
62 lines (54 loc) · 1.35 KB
/
standard_deviation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'pry'
class StandardDeviation
attr_accessor :dataset, :mean, :variance_dataset, :variance, :variance_pop, :standard_deviation
def initialize(dataset)
@dataset = dataset #The inputed set of values
@mean = 0
@variance_dataset = []
@variance = 0
@variance_pop = 0
@standard_deviation = 0
calculate_mean
calculate_variance_dataset #The calculated set of variance values based on dataset
calculate_variance #For regular standard deviation
calculate_variance_pop #For sample size standard deviation
end
#calculate the mean
def calculate_mean
total = 0
@dataset.each do |i|
total = total + i
end
@mean = total/@dataset.size.to_f
return @mean
end
#calculate the variance
def calculate_variance_dataset
@dataset.each do |i|
@variance_dataset << (i-@mean)**2
end
return @variance_dataset
end
#calculate the standard deviation
def calculate_variance
total = 0
@variance_dataset.each do |i|
total = total + i
end
@variance = total/variance_dataset.size.to_f
return @variance
end
#calcualte the standard deviation of sample population
def calculate_variance_pop
total = 0
@variance_dataset.each do |i|
total = total + i
end
@variance_pop = total/((variance_dataset.size.to_f)-1)
return @variance_pop
end
def calculate_standard_deviation(input)
return Math.sqrt(input)
end
end
binding.pry