Module: Ratistics::Distribution

Extended by:
Distribution
Included in:
Ratistics, Distribution
Defined in:
lib/ratistics/distribution.rb

Overview

Various distribution computation functions.

Instance Method Summary collapse

Instance Method Details

#range(data, opts = {}) {|item| ... } ⇒ Float, 0

Calculates the statistical range.

Will sort the data set using natural sort order unless the :sorted option is true or a block is given.

When a block is given the block will be applied to every element in the data set. Using a block in this way allows probability to be computed against a specific field in a data set of hashes or objects.

Options Hash (opts):

  • :sorted (true, false)

    indicates of the data is already sorted

Yields:

  • iterates over each element in the data set

Yield Parameters:

  • item

    each element in the data set


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ratistics/distribution.rb', line 87

def range(data, opts={})
  return 0 if data.nil? || data.count <= 1
  data = data.sort unless block_given? || opts[:sorted] == true

  if block_given?
    range = yield(data.last) - yield(data.first)
  else
    range = data.last - data.first
  end

  return range
end

#standard_deviation(data, mu = nil, opts = {}, &block) {|item| ... } ⇒ Float, 0 Also known as: std_dev, stddev

Calculates the statistical standard deviation.

When a block is given the block will be applied to every element in the data set. Using a block in this way allows probability to be computed against a specific field in a data set of hashes or objects.

Yields:

  • iterates over each element in the data set

Yield Parameters:

  • item

    each element in the data set


60
61
62
63
# File 'lib/ratistics/distribution.rb', line 60

def standard_deviation(data, mu=nil, opts={}, &block)
  return 0 if data.nil? || data.empty?
  return ::Math.sqrt(variance(data, mu, &block))
end

#variance(data, mu = nil, opts = {}, &block) {|item| ... } ⇒ Float, 0 Also known as: var

Calculates the statistical variance.

When a block is given the block will be applied to every element in the data set. Using a block in this way allows probability to be computed against a specific field in a data set of hashes or objects.

Yields:

  • iterates over each element in the data set

Yield Parameters:

  • item

    each element in the data set


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ratistics/distribution.rb', line 26

def variance(data, mu=nil, opts={}, &block)
  return 0 if data.nil? || data.empty?

  mu = CentralTendency.mean(data, &block) if mu.nil?

  deviation = data.reduce([]) do |memo, datum|
    datum = yield(datum) if block_given?
    memo << (datum.to_f - mu) ** 2
  end

  variance = CentralTendency.mean(deviation)
  return variance
end