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



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

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


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

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



8
9
10
11
12
# File 'lib/spellr/key_tuner/stats.rb', line 8

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


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

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



38
39
40
# File 'lib/spellr/key_tuner/stats.rb', line 38

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

.variance(values, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



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

def variance(values, &block) # rubocop:disable Metrics/MethodLength
  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