Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from charlieegan3/master
Various stylistic improvements
- Loading branch information
Showing
13 changed files
with
42 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
module DescriptiveStatistics | ||
def mean(collection = self) | ||
values = Support::convert(collection) | ||
return DescriptiveStatistics.mean_empty_collection_default_value unless values.size > 0 | ||
return DescriptiveStatistics.mean_empty_collection_default_value if values.empty? | ||
|
||
values.sum / values.number | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
module DescriptiveStatistics | ||
def median(collection = self) | ||
values = Support::convert(collection) | ||
return DescriptiveStatistics.median_empty_collection_default_value unless values.size > 0 | ||
return DescriptiveStatistics.median_empty_collection_default_value if values.empty? | ||
|
||
values.percentile(50) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
module DescriptiveStatistics | ||
def mode(collection = self) | ||
values = Support::extract(collection) | ||
return unless values.size > 0 | ||
return if values.to_a.empty? | ||
|
||
values | ||
.group_by { |e| e } | ||
.values | ||
.max_by(&:size) | ||
.first | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
module DescriptiveStatistics | ||
def percentile(p, collection = self) | ||
values = Support::convert(collection) | ||
return DescriptiveStatistics.percentile_empty_collection_default_value unless values.size > 0 | ||
|
||
return values.first unless values.size > 1 | ||
|
||
sorted = values.sort | ||
return sorted[-1] if p == 100 | ||
rank = p / 100.0 * (values.number - 1) | ||
lrank = rank.floor | ||
d = rank - lrank | ||
lower = sorted[lrank] | ||
upper = sorted[lrank+1] | ||
lower + (upper - lower) * d | ||
if values.empty?; DescriptiveStatistics.percentile_empty_collection_default_value | ||
elsif values.size == 1; values.first | ||
else | ||
values.sort! | ||
return values.last if p == 100 | ||
rank = p / 100.0 * (values.size - 1) | ||
lower, upper = values[rank.floor,2] | ||
lower + (upper - lower) * (rank - rank.floor) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
module DescriptiveStatistics | ||
def range(collection = self) | ||
values = Support::convert(collection) | ||
return DescriptiveStatistics.range_empty_collection_default_value unless values.size > 0 | ||
return DescriptiveStatistics.range_empty_collection_default_value if values.empty? | ||
|
||
values.max - values.min | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
module DescriptiveStatistics | ||
def sum(collection = self) | ||
values = Support::convert(collection) | ||
return DescriptiveStatistics.sum_empty_collection_default_value unless values.size > 0 | ||
return DescriptiveStatistics.sum_empty_collection_default_value if values.empty? | ||
|
||
return values.inject(:+) | ||
end | ||
return values.reduce(:+) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
module DescriptiveStatistics | ||
def variance(collection = self) | ||
values = Support::convert(collection) | ||
return DescriptiveStatistics.variance_empty_collection_default_value unless values.size > 0 | ||
return DescriptiveStatistics.variance_empty_collection_default_value if values.empty? | ||
|
||
mean = values.mean | ||
values.map{ |sample| (mean - sample) ** 2 }.inject(:+) / values.number | ||
values.map { |sample| (mean - sample) ** 2 }.reduce(:+) / values.number | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters