Module: Stats

Extended by:
Math
Included in:
PossibleKey
Defined in:
lib/spellr/key_tuner/stats.rb

Class Method Summary collapse

Class Method Details

.gaussian_probability(value, standard_deviation:, mean:, variance:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/spellr/key_tuner/stats.rb', line 44

def gaussian_probability(value, standard_deviation:, mean:, variance:)
  # deal with the edge case of a 0 standard deviation
  if standard_deviation == 0
    return mean == value ? 1.0 : 0.0
  end

  # calculate the gaussian probability
  exp = -((value - mean)**2) / (2 * variance)
  (1.0 / sqrt(2 * Math::PI * variance)) * (Math::E**exp)
end

.max(values) {|values.max_by(&block)| ... } ⇒ Object

Yields:

  • (values.max_by(&block))


23
24
25
26
27
28
# File 'lib/spellr/key_tuner/stats.rb', line 23

def max(values, &block)
  return 0 if values.empty?
  return values.max unless block_given?

  yield values.max_by(&block)
end

.mean(values, &block) ⇒ Object



10
11
12
13
14
# File 'lib/spellr/key_tuner/stats.rb', line 10

def mean(values, &block)
  return 0 if values.empty?

  values.sum(&block).to_f / values.length
end

.min(values) {|values.min_by(&block)| ... } ⇒ Object

Yields:

  • (values.min_by(&block))


16
17
18
19
20
21
# File 'lib/spellr/key_tuner/stats.rb', line 16

def min(values, &block)
  return 0 if values.empty?
  return values.min unless block_given?

  yield values.min_by(&block)
end

.standard_deviation(values, &block) ⇒ Object



40
41
42
# File 'lib/spellr/key_tuner/stats.rb', line 40

def standard_deviation(values, &block)
  sqrt(variance(values, &block))
end

.variance(values, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/spellr/key_tuner/stats.rb', line 30

def variance(values, &block)
  return 0 if values.empty?

  mean = mean(values, &block)
  values.sum do |value|
    value = yield value if block_given?
    (mean - value)**2
  end.to_f / values.length
end