Module: Rust::Descriptive
- Defined in:
- lib/rust-descriptive.rb
Class Method Summary collapse
- .mean(data) ⇒ Object
- .median(data) ⇒ Object
- .quantile(data, percentiles = [0.0, 0.25, 0.5, 0.75, 1.0]) ⇒ Object
- .standard_deviation(data) ⇒ Object (also: sd, stddev)
- .variance(data) ⇒ Object (also: var)
Class Method Details
.mean(data) ⇒ Object
7 8 9 10 11 |
# File 'lib/rust-descriptive.rb', line 7 def mean(data) raise TypeError, "Expecting Array of numerics" if !data.is_a?(Array) || !data.all? { |e| e.is_a?(Numeric) } return data.sum.to_f / data.size end |
.median(data) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rust-descriptive.rb', line 28 def median(data) raise TypeError, "Expecting Array of numerics" if !data.is_a?(Array) || !data.all? { |e| e.is_a?(Numeric) } sorted = data.sort if data.size == 0 return Float::NAN elsif data.size.odd? return sorted[data.size / 2] else i = (data.size / 2) return (sorted[i - 1] + sorted[i]) / 2.0 end end |
.quantile(data, percentiles = [0.0, 0.25, 0.5, 0.75, 1.0]) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rust-descriptive.rb', line 42 def quantile(data, percentiles=[0.0, 0.25, 0.5, 0.75, 1.0]) raise TypeError, "Expecting Array of numerics" if !data.is_a?(Array) || !data.all? { |e| e.is_a?(Numeric) } raise TypeError, "Expecting Array of numerics" if !percentiles.is_a?(Array) || !percentiles.all? { |e| e.is_a?(Numeric) } raise "Percentiles outside the range: #{percentiles}" if percentiles.any? { |e| !e.between?(0, 1) } Rust.exclusive do Rust::R_ENGINE.data = data Rust::R_ENGINE.percs = percentiles call_result = Rust._pull("quantile(data, percs)") assert { call_result.is_a?(Array) } assert { call_result.size == percentiles.size } return percentiles.zip(call_result).to_h end end |
.standard_deviation(data) ⇒ Object Also known as: sd, stddev
13 14 15 16 17 |
# File 'lib/rust-descriptive.rb', line 13 def standard_deviation(data) raise TypeError, "Expecting Array of numerics" if !data.is_a?(Array) || !data.all? { |e| e.is_a?(Numeric) } # TODO implement end |
.variance(data) ⇒ Object Also known as: var
21 22 23 24 25 |
# File 'lib/rust-descriptive.rb', line 21 def variance(data) raise TypeError, "Expecting Array of numerics" if !data.is_a?(Array) || !data.all? { |e| e.is_a?(Numeric) } # TODO implement end |