Class: Saulabs::Gauss::Distribution
- Inherits:
-
Object
- Object
- Saulabs::Gauss::Distribution
- Defined in:
- lib/saulabs/gauss/distribution.rb
Overview
Implementation of a gaussian distribution
Direct Known Subclasses
Constant Summary collapse
- @@sqrt2 =
Math.sqrt(2).freeze
- @@inv_sqrt_2pi =
(1 / Math.sqrt(2 * Math::PI)).freeze
- @@log_sqrt_2pi =
Math.log(Math.sqrt(2 * Math::PI)).freeze
Instance Attribute Summary collapse
-
#deviation ⇒ Object
gaussian normal distribution values.
-
#mean ⇒ Object
gaussian normal distribution values.
-
#precision ⇒ Object
gaussian normal distribution values.
-
#precision_mean ⇒ Object
gaussian normal distribution values.
-
#variance ⇒ Object
gaussian normal distribution values.
Class Method Summary collapse
- .absolute_difference(x, y) ⇒ Object
-
.cumulative_distribution_function(x) ⇒ Object
(also: cdf)
Computes the cummulative Gaussian distribution at a specified point of interest.
- .log_product_normalization(x, y) ⇒ Object
- .log_ratio_normalization(x, y) ⇒ Object
-
.probability_density_function(x) ⇒ Object
(also: pdf)
Computes the Gaussian density at a specified point of interest.
-
.quantile_function(x) ⇒ Object
(also: inv_cdf)
The inverse of the cummulative Gaussian distribution function.
- .standard ⇒ Object
- .with_deviation(mean, deviation) ⇒ Object
- .with_precision(mean, precision) ⇒ Object
- .with_variance(mean, variance) ⇒ Object
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
-
#-(other) ⇒ Object
absolute difference.
- #/(other) ⇒ Object
- #==(other) ⇒ Object
- #equals(other) ⇒ Object
-
#initialize(mean = 0.0, deviation = 0.0) ⇒ Distribution
constructor
A new instance of Distribution.
-
#replace(other) ⇒ Object
copy values from other distribution.
- #to_s ⇒ Object
- #value_at(x) ⇒ Object
Constructor Details
#initialize(mean = 0.0, deviation = 0.0) ⇒ Distribution
Returns a new instance of Distribution.
15 16 17 18 19 20 21 22 23 |
# File 'lib/saulabs/gauss/distribution.rb', line 15 def initialize(mean = 0.0, deviation = 0.0) mean = 0.0 unless mean.to_f.finite? deviation = 0.0 unless deviation.to_f.finite? @mean = mean @deviation = deviation @variance = deviation * deviation @precision = deviation == 0.0 ? 0.0 : 1 / @variance.to_f @precision_mean = @precision * mean end |
Instance Attribute Details
#deviation ⇒ Object
gaussian normal distribution values
13 14 15 |
# File 'lib/saulabs/gauss/distribution.rb', line 13 def deviation @deviation end |
#mean ⇒ Object
gaussian normal distribution values
13 14 15 |
# File 'lib/saulabs/gauss/distribution.rb', line 13 def mean @mean end |
#precision ⇒ Object
gaussian normal distribution values
13 14 15 |
# File 'lib/saulabs/gauss/distribution.rb', line 13 def precision @precision end |
#precision_mean ⇒ Object
gaussian normal distribution values
13 14 15 |
# File 'lib/saulabs/gauss/distribution.rb', line 13 def precision_mean @precision_mean end |
#variance ⇒ Object
gaussian normal distribution values
13 14 15 |
# File 'lib/saulabs/gauss/distribution.rb', line 13 def variance @variance end |
Class Method Details
.absolute_difference(x, y) ⇒ Object
43 44 45 |
# File 'lib/saulabs/gauss/distribution.rb', line 43 def absolute_difference(x, y) [(x.precision_mean - y.precision_mean).abs, Math.sqrt((x.precision - y.precision).abs)].max end |
.cumulative_distribution_function(x) ⇒ Object Also known as: cdf
Computes the cummulative Gaussian distribution at a specified point of interest
63 64 65 |
# File 'lib/saulabs/gauss/distribution.rb', line 63 def cumulative_distribution_function(x) 0.5 * (1 + Math.erf(x / @@sqrt2)) end |
.log_product_normalization(x, y) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/saulabs/gauss/distribution.rb', line 47 def log_product_normalization(x, y) return 0.0 if x.precision == 0.0 || y.precision == 0.0 variance_sum = x.variance + y.variance mean_diff = x.mean - y.mean -@@log_sqrt_2pi - (Math.log(variance_sum) / 2.0) - (mean_diff**2 / (2.0 * variance_sum)) end |
.log_ratio_normalization(x, y) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/saulabs/gauss/distribution.rb', line 54 def log_ratio_normalization(x, y) return 0.0 if x.precision == 0.0 || y.precision == 0.0 variance_diff = y.variance - x.variance return 0.0 if variance_diff == 0.0 mean_diff = x.mean - y.mean Math.log(y.variance) + @@log_sqrt_2pi - (Math.log(variance_diff) / 2.0) + (mean_diff**2 / (2.0 * variance_diff)) end |
.probability_density_function(x) ⇒ Object Also known as: pdf
Computes the Gaussian density at a specified point of interest
69 70 71 |
# File 'lib/saulabs/gauss/distribution.rb', line 69 def probability_density_function(x) @@inv_sqrt_2pi * Math.exp(-0.5 * (x**2)) end |
.quantile_function(x) ⇒ Object Also known as: inv_cdf
The inverse of the cummulative Gaussian distribution function
75 76 77 |
# File 'lib/saulabs/gauss/distribution.rb', line 75 def quantile_function(x) -@@sqrt2 * Math.erfc(2.0 * x) end |
.standard ⇒ Object
27 28 29 |
# File 'lib/saulabs/gauss/distribution.rb', line 27 def standard Distribution.new(0.0, 1.0) end |
.with_deviation(mean, deviation) ⇒ Object
31 32 33 |
# File 'lib/saulabs/gauss/distribution.rb', line 31 def with_deviation(mean, deviation) Distribution.new(mean, deviation) end |
.with_precision(mean, precision) ⇒ Object
39 40 41 |
# File 'lib/saulabs/gauss/distribution.rb', line 39 def with_precision(mean, precision) Distribution.new(mean / precision, Math.sqrt(1 / precision)) end |
.with_variance(mean, variance) ⇒ Object
35 36 37 |
# File 'lib/saulabs/gauss/distribution.rb', line 35 def with_variance(mean, variance) Distribution.new(mean, Math.sqrt(variance)) end |
Instance Method Details
#*(other) ⇒ Object
96 97 98 |
# File 'lib/saulabs/gauss/distribution.rb', line 96 def *(other) Distribution.with_precision(self.precision_mean + other.precision_mean, self.precision + other.precision) end |
#+(other) ⇒ Object
109 110 111 |
# File 'lib/saulabs/gauss/distribution.rb', line 109 def +(other) end |
#-(other) ⇒ Object
absolute difference
105 106 107 |
# File 'lib/saulabs/gauss/distribution.rb', line 105 def -(other) Distribution.absolute_difference(self, other) end |
#/(other) ⇒ Object
100 101 102 |
# File 'lib/saulabs/gauss/distribution.rb', line 100 def /(other) Distribution.with_precision(self.precision_mean - other.precision_mean, self.precision - other.precision) end |
#==(other) ⇒ Object
113 114 115 |
# File 'lib/saulabs/gauss/distribution.rb', line 113 def ==(other) self.mean == other.mean && self.variance == other.variance end |
#equals(other) ⇒ Object
117 118 119 |
# File 'lib/saulabs/gauss/distribution.rb', line 117 def equals(other) self == other end |
#replace(other) ⇒ Object
copy values from other distribution
88 89 90 91 92 93 94 |
# File 'lib/saulabs/gauss/distribution.rb', line 88 def replace(other) @precision = other.precision @precision_mean = other.precision_mean @mean = other.mean @deviation = other.deviation @variance = other.variance end |
#to_s ⇒ Object
121 122 123 |
# File 'lib/saulabs/gauss/distribution.rb', line 121 def to_s "[μ=#{'%.4f' % mean}, σ=#{'%.4f' % deviation}]" end |
#value_at(x) ⇒ Object
82 83 84 85 |
# File 'lib/saulabs/gauss/distribution.rb', line 82 def value_at(x) exp = -(x - @mean)**2.0 / (2.0 * @variance) (1.0/@deviation) * @@inv_sqrt_2pi * Math.exp(exp) end |