Class: FastStats::Mean
- Inherits:
-
Object
- Object
- FastStats::Mean
- Defined in:
- lib/fast_stats/mean.rb
Constant Summary collapse
- DEFAULT_ROUND =
15
Instance Attribute Summary collapse
-
#log_sum ⇒ Object
readonly
Returns the value of attribute log_sum.
-
#n ⇒ Object
readonly
Returns the value of attribute n.
-
#sum ⇒ Object
readonly
Returns the value of attribute sum.
Instance Method Summary collapse
- #add(val) ⇒ Object (also: #<<)
- #arithmetic(round: DEFAULT_ROUND) ⇒ Object
- #geometric(round: DEFAULT_ROUND) ⇒ Object
-
#initialize ⇒ Mean
constructor
A new instance of Mean.
- #summary(round: DEFAULT_ROUND) ⇒ Object
Constructor Details
#initialize ⇒ Mean
Returns a new instance of Mean.
11 12 13 14 15 16 |
# File 'lib/fast_stats/mean.rb', line 11 def initialize() @sum = 0.0 @n = 0 @log_sum = 0.0 @log_n = 0 end |
Instance Attribute Details
#log_sum ⇒ Object (readonly)
Returns the value of attribute log_sum.
9 10 11 |
# File 'lib/fast_stats/mean.rb', line 9 def log_sum @log_sum end |
#n ⇒ Object (readonly)
Returns the value of attribute n.
9 10 11 |
# File 'lib/fast_stats/mean.rb', line 9 def n @n end |
#sum ⇒ Object (readonly)
Returns the value of attribute sum.
9 10 11 |
# File 'lib/fast_stats/mean.rb', line 9 def sum @sum end |
Instance Method Details
#add(val) ⇒ Object Also known as: <<
18 19 20 21 22 23 |
# File 'lib/fast_stats/mean.rb', line 18 def add(val) throw ArgumentError.new "#add, val must be >= 0" if val < 0 @sum += val @log_sum += safe_log(val) @n += 1 end |
#arithmetic(round: DEFAULT_ROUND) ⇒ Object
27 28 29 30 |
# File 'lib/fast_stats/mean.rb', line 27 def arithmetic(round: DEFAULT_ROUND) return nil if n == 0 (sum / n).round round end |
#geometric(round: DEFAULT_ROUND) ⇒ Object
32 33 34 35 |
# File 'lib/fast_stats/mean.rb', line 32 def geometric(round: DEFAULT_ROUND) return nil if n == 0 (2 ** (log_sum / n)).round round end |
#summary(round: DEFAULT_ROUND) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/fast_stats/mean.rb', line 37 def summary(round: DEFAULT_ROUND) { "arithmetic" => arithmetic(round: round), "geometric" => geometric(round: round), } end |