# Module: Ratistics::Distribution

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

## Overview

Various distribution computation functions.

## 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.

Parameters:

• data (Enumerable)

the data set to compute the range of

• opts (Hash) (defaults to: {})

a customizable set of options

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

Returns:

• (Float, 0)

the statistical range of the given data set or zero if the data set is empty

 ``` 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, 0Also 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.

Parameters:

• data (Enumerable)

the data set to compute the standard deviation of

• mu (Float) (defaults to: nil)

datum around which to compute the standard deviation, defaults to the statistical mean of the sample

• block (Block)

optional block for per-item processing

Yields:

• iterates over each element in the data set

Yield Parameters:

• item

each element in the data set

Returns:

• (Float, 0)

the standard deviation of the given data set or zero if the data set is empty

 ``` 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, 0Also 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.

Parameters:

• data (Enumerable)

the data set to compute the variance of

• mu (Float) (defaults to: nil)

datum around which to compute the variance, defaults to the statistical mean of the sample

• block (Block)

optional block for per-item processing

Yields:

• iterates over each element in the data set

Yield Parameters:

• item

each element in the data set

Returns:

• (Float, 0)

the statistical variance of the given data set or zero if the data set is empty

 ``` 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```