Class: Glicko2::NormalDistribution

Inherits:
Object
  • Object
show all
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

Rating

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#meanObject (readonly)

Returns the value of attribute mean.



8
9
10
# File 'lib/glicko2/normal_distribution.rb', line 8

def mean
  @mean
end

#standard_deviationObject (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

Parameters:

Returns:



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

Parameters:

Returns:



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`

Parameters:

  • x (Numeric)

Returns:

  • (Numeric)


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`

Parameters:

  • x (Numeric)

Returns:

  • (Numeric)


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_sObject



60
61
62
# File 'lib/glicko2/normal_distribution.rb', line 60

def to_s
  "#<NormalDistribution mean=#{mean}, sd=#{sd}>"
end

#varianceNumeric

Calculate the distribution variance

Returns:

  • (Numeric)


19
20
21
# File 'lib/glicko2/normal_distribution.rb', line 19

def variance
  standard_deviation ** 2.0
end