module SimpleStatistics
Extended Modules
Defined in:
simple_statistics.crsimple_statistics/BadCoordinateError.cr
simple_statistics/BadQuantileError.cr
simple_statistics/InvalidNumberError.cr
simple_statistics/Line.cr
simple_statistics/geometric_mean.cr
simple_statistics/linear_regression.cr
simple_statistics/mean.cr
simple_statistics/median.cr
simple_statistics/median_sorted.cr
simple_statistics/mode.cr
simple_statistics/quantile.cr
simple_statistics/quantile_sorted.cr
simple_statistics/range.cr
simple_statistics/standard_deviation.cr
simple_statistics/variance.cr
simple_statistics/version.cr
Constant Summary
-
VERSION =
"0.1.0"
Instance Method Summary
-
#geometric_mean(numbers : Enumerable) : Number
Get the geometric mean of an enumerable.
-
#linear_regression(coordinates : Enumerable(Enumerable(Number))) : Line
Performs a simple linear regression.
-
#mean(numbers : Enumerable) : Number
Get the arithmetic mean (also known as the average) of an enumerable.
-
#median(numbers : Enumerable) : Number
Get the median of an enumerable.
-
#median_sorted(numbers : Enumerable) : Number
Get the median of a sorted enumerable.
-
#mode(values : Enumerable(T)) forall T
Get the mode of an enumerable, which is the value that appears most frequently.
-
#quantile(numbers : Enumerable, p : Number) : Number
Get the quantile from an enumerable.
-
#quantile_sorted(numbers : Enumerable, p : Number) : Number
Get the quantile from a sorted enumerable.
-
#range(numbers : Enumerable(T)) : Range forall T
Get the minimum and maximum of an enumerable as a range.
-
#standard_deviation(numbers : Enumerable) : Number
Get the standard deviation from an enumerable.
-
#variance(numbers : Enumerable) : Number
Get the variance of an enumerable.
Instance Method Detail
Get the geometric mean of an enumerable.
If the enumerable is empty, Enumerable::EmptyError
will be raised. If the enumerable contains negative numbers, SimpleStatistics::InvalidNumberError
will be raised.
SimpleStatistics.geometric_mean([2, 8])
# => 4
Performs a simple linear regression. Returns a Line
.
If only one coordinate is provided, the resulting Line
will be flat (i.e., have a slope
of 0
).
If the enumerable is empty, Enumerable::EmptyError
will be raised. If any of the coordinates don't have a size
of 2
, a BadCoordinateError
will be raised.
line = SimpleStatistics::linear_regression([[0, 0], [1, 1]])
line.slope # => 1
line.intercept # => 0
line.at(2) # => 2
Get the arithmetic mean (also known as the average) of an enumerable. This is the sum of all of the values divided by the number of values.
If the enumerable is empty, Enumerable::EmptyError
will be raised.
SimpleStatistics.mean([3, 1, 2])
# => 2
SimpleStatistics.mean([9, 9])
# => 9
SimpleStatistics.mean([])
# raises Enumerable::EmptyError
Get the median of an enumerable. This is the "middle value".
If the enumerable is empty, Enumerable::EmptyError
will be raised.
SimpleStatistics.median([3, 1, 2])
# => 2
SimpleStatistics.median([0, 2, 3, 5])
# => 2.5
Get the median of a sorted enumerable. If you already know that the enumerable is sorted, this is faster than using #median
.
If the enumerable is empty, Enumerable::EmptyError
will be raised.
SimpleStatistics.median_sorted([1, 2, 3])
# => 2
SimpleStatistics.median_sorted([0, 2, 3, 5])
# => 2.5
Get the mode of an enumerable, which is the value that appears most frequently. If there is a tie, the most recently-seen mode will be returned.
If the enumerable is empty, Enumerable::EmptyError
will be raised.
SimpleStatistics.mode([5, 4, 4, 3])
# => 4
SimpleStatistics.mode([5, 4.0, 4, 3])
# => 4
SimpleStatistics.mode([1, 1, 2, 2, 3])
# => 1
Get the quantile from an enumerable.
If the enumerable is empty, Enumerable::EmptyError
will be raised. If the second argument is not in the range (0..1)
, SimpleStatistics::BadQuantileError
will be raised.
SimpleStatistics.quantile([3, 1, 2], 0.5)
# => 2
SimpleStatistics.quantile([3, 1, 2], 0)
# => 1
SimpleStatistics.quantile([4, 3], 0.5)
# => 3.5
Get the quantile from a sorted enumerable. If you already know that the enumerable is sorted, this is faster than using #quantile
.
If the enumerable is empty, Enumerable::EmptyError
will be raised. If the second argument is not in the range (0..1)
, SimpleStatistics::BadQuantileError
will be raised.
SimpleStatistics.quantile_sorted([1, 2, 3], 0.5)
# => 2
SimpleStatistics.quantile_sorted([1, 2, 3], 0)
# => 1
SimpleStatistics.quantile_sorted([3, 4], 0.5)
# => 3.5
Get the minimum and maximum of an enumerable as a range. If the enumerable is empty, the range will be infinite and contain all values.
SimpleStatistics.range([3, 1, 2])
# => (1..3)
SimpleStatistics.range([5, 5, 5])
# => (5..5)
SimpleStatistics.range([] of Int32)
# => (..)
Get the standard deviation from an enumerable. This is the square root of the #variance
.
If the enumerable is empty, Enumerable::EmptyError
will be raised. If the enumerable only has one entry, 0
will be returned.
SimpleStatistics.standard_deviation([2, 4, 4, 4, 5, 5, 7, 9])
# => 2
SimpleStatistics.standard_deviation([99])
# => 0