Class: Glicko2::NormalDistribution
- Inherits:
-
Object
- Object
- Glicko2::NormalDistribution
- Includes:
- Comparable
- Defined in:
- lib/glicko2/normal_distribution.rb
Overview
Glicko ratings are represented with a rating and rating deviation. For this gem it is assumed that ratings are normally distributed where rating and rating deviation correspond to mean and standard deviation.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#mean ⇒ Object
readonly
Returns the value of attribute mean.
-
#standard_deviation ⇒ Object
(also: #sd)
readonly
Returns the value of attribute standard_deviation.
Instance Method Summary collapse
-
#+(other) ⇒ NormalDistribution
Calculate the sum.
-
#-(other) ⇒ NormalDistribution
Calculate the difference.
- #<=>(other) ⇒ Object
-
#cdf(x) ⇒ Numeric
Calculate the cumulative distribution at ‘x`.
-
#initialize(mean, standard_deviation) ⇒ NormalDistribution
constructor
A new instance of NormalDistribution.
-
#pdf(x) ⇒ Numeric
Calculate the probability density at ‘x`.
- #to_s ⇒ Object
-
#variance ⇒ Numeric
Calculate the distribution variance.
Constructor Details
#initialize(mean, standard_deviation) ⇒ NormalDistribution
Returns a new instance of NormalDistribution.
11 12 13 14 |
# File 'lib/glicko2/normal_distribution.rb', line 11 def initialize(mean, standard_deviation) @mean = mean @standard_deviation = standard_deviation end |
Instance Attribute Details
#mean ⇒ Object (readonly)
Returns the value of attribute mean.
8 9 10 |
# File 'lib/glicko2/normal_distribution.rb', line 8 def mean @mean end |
#standard_deviation ⇒ Object (readonly) Also known as: sd
Returns the value of attribute standard_deviation.
8 9 10 |
# File 'lib/glicko2/normal_distribution.rb', line 8 def standard_deviation @standard_deviation end |
Instance Method Details
#+(other) ⇒ NormalDistribution
Calculate the sum
27 28 29 |
# File 'lib/glicko2/normal_distribution.rb', line 27 def +(other) self.class.new(mean + other.mean, Math.sqrt(variance + other.variance)) end |
#-(other) ⇒ NormalDistribution
Calculate the difference
35 36 37 |
# File 'lib/glicko2/normal_distribution.rb', line 35 def -(other) self.class.new(mean - other.mean, Math.sqrt(variance + other.variance)) end |
#<=>(other) ⇒ Object
56 57 58 |
# File 'lib/glicko2/normal_distribution.rb', line 56 def <=>(other) mean <=> other.mean end |
#cdf(x) ⇒ Numeric
Calculate the cumulative distribution at ‘x`
52 53 54 |
# File 'lib/glicko2/normal_distribution.rb', line 52 def cdf(x) 0.5 * (1.0 + Math.erf((x - mean) / (sd * Math.sqrt(2.0)))) end |
#pdf(x) ⇒ Numeric
Calculate the probability density at ‘x`
43 44 45 46 |
# File 'lib/glicko2/normal_distribution.rb', line 43 def pdf(x) 1.0 / (sd * Math.sqrt(2.0 * Math::PI)) * Math.exp(-(x - mean) ** 2.0 / 2.0 * variance) end |
#to_s ⇒ Object
60 61 62 |
# File 'lib/glicko2/normal_distribution.rb', line 60 def to_s "#<NormalDistribution mean=#{mean}, sd=#{sd}>" end |
#variance ⇒ Numeric
Calculate the distribution variance
19 20 21 |
# File 'lib/glicko2/normal_distribution.rb', line 19 def variance standard_deviation ** 2.0 end |