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

average[R]
avg[R]
max[R]
min[R]
n[R]
sample_mean[R]
sample_size[R]
ssd[R]
sum_squared_deviations[R]

Public Class Methods

new() click to toggle source

Initialize state vars in a new QuickStats object.

# File lib/quickstats.rb, line 25
def initialize
  reset
end

Public Instance Methods

add_all(enumerable_set)
Alias for: add_set
add_set(enumerable_set) click to toggle source

Update the statistics with all elements of an enumerable set.

Arguments
  • enumerable_set -> the set of new observation. All elements must be numeric.

Returns
# File lib/quickstats.rb, line 77
def add_set(enumerable_set)
  enumerable_set.each { |x| new_obs x }
  self
end
Also aliased as: add_all
loss(target:) click to toggle source

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
mle_s()
mle_sample_variance() click to toggle source

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
Also aliased as: mle_var
mle_standard_deviation() click to toggle source

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
Also aliased as: mle_s, mle_std_dev
mle_standard_error() click to toggle source

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
Also aliased as: mle_std_err
mle_std_dev()
mle_std_err()
Alias for: mle_standard_error
mle_var()
Alias for: mle_sample_variance
new_obs(datum) click to toggle source

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
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() click to toggle source

Reset all state vars to initial values.

Returns
# File lib/quickstats.rb, line 36
def reset
  @ssd = @n = 0
  @sample_mean = @max = @min = Float::NAN
  self
end
s()
Alias for: standard_deviation
sample_variance() click to toggle source

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
Also aliased as: var
standard_deviation() click to toggle source

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
Also aliased as: s, std_dev
standard_error() click to toggle source

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
Also aliased as: std_err
std_dev()
Alias for: standard_deviation
std_err()
Alias for: standard_error
var()
Alias for: sample_variance