class QuickStats
Computationally stable and efficient basic descriptive statistics. This class uses Kalman Filter updating to tally sample mean and sum of squares, along with min, max, and sample size. Sample variance, standard deviation and standard error are calculated on demand.
- Author
-
Paul J Sanchez (pjs@alum.mit.edu)
- Copyright
-
Copyright © Paul J Sanchez
- License
-
MIT
Attributes
Public Class Methods
Initialize state vars in a new QuickStats
object.
-
n = 0
-
sample_mean
=sample_variance
= min = max = NaN
# File lib/quickstats.rb, line 25 def initialize reset end
Public Instance Methods
Update the statistics with all elements of an enumerable set.
- Arguments
-
enumerable_set
-> the set of new observation. All elements must be numeric.
-
- Returns
-
a reference to the
QuickStats
object.
-
# File lib/quickstats.rb, line 77 def add_set(enumerable_set) enumerable_set.each { |x| new_obs x } self end
Estimates of quadratic loss (a la Taguchi) relative to a specified target value.
- Arguments
-
target:
-> the designated target value for the loss function.
-
- Returns
-
the quadratic loss calculated for the data, or NaN if this is a new or just-reset
QuickStats
object.
-
# File lib/quickstats.rb, line 160 def loss(target:) fail 'Must supply target to loss function' unless target @n > 1 ? (@sample_mean - target)**2 + @ssd / @n : Float::NAN end
Calculates the MLE sample variance on demand (divisor is n).
- Returns
-
the MLE sample variance of the data, or NaN if this is a new or just-reset
QuickStats
object.
-
# File lib/quickstats.rb, line 100 def mle_sample_variance @n > 1 ? @ssd / @n : Float::NAN end
Calculates the square root of the MLE sample variance on demand.
- Returns
-
the MLE standard deviation of the data, or NaN if this is a new or just-reset
QuickStats
object.
-
# File lib/quickstats.rb, line 123 def mle_standard_deviation Math.sqrt mle_sample_variance end
Calculates sqrt(mle_sample_variance
/ n) on demand.
- Returns
-
the sample standard error of the data, or NaN if this is a new or just-reset
QuickStats
object.
-
# File lib/quickstats.rb, line 146 def mle_standard_error Math.sqrt(mle_sample_variance / @n) end
Update the sample size, sample mean, sum of squares, min, and max given a new observation. All but the sample size are maintained as floating point.
- Arguments
-
datum
-> the new observation. Must be numeric
-
- Returns
-
a reference to the
QuickStats
object.
-
- Raises
-
RuntimeError if datum is non-numeric
-
# File lib/quickstats.rb, line 52 def new_obs(datum) fail 'Observations must be numeric' unless datum.is_a? Numeric x = datum.to_f @max = x unless x <= @max @min = x unless x >= @min if @n > 0 delta = x - @sample_mean @n += 1 @sample_mean += delta / n @ssd += delta * (x - @sample_mean) else @sample_mean = x @n += 1 end self end
Reset all state vars to initial values.
-
ssd = n = 0
-
sample_mean
=sample_variance
= min = max = NaN
- Returns
-
a reference to the
QuickStats
object.
-
# File lib/quickstats.rb, line 36 def reset @ssd = @n = 0 @sample_mean = @max = @min = Float::NAN self end
Calculates the unbiased sample variance on demand (divisor is n-1).
- Returns
-
the sample variance of the data, or NaN if this is a new or just-reset
QuickStats
object.
-
# File lib/quickstats.rb, line 89 def sample_variance @n > 1 ? @ssd / (@n - 1) : Float::NAN end
Calculates the square root of the unbiased sample variance on demand.
- Returns
-
the sample standard deviation of the data, or NaN if this is a new or just-reset
QuickStats
object.
-
# File lib/quickstats.rb, line 111 def standard_deviation Math.sqrt sample_variance end
Calculates sqrt(sample_variance
/ n) on demand.
- Returns
-
the sample standard error of the data, or NaN if this is a new or just-reset
QuickStats
object.
-
# File lib/quickstats.rb, line 135 def standard_error Math.sqrt(sample_variance / @n) end