Module: Laboratory::Calculations::ConfidenceLevel

Defined in:
lib/laboratory/calculations/confidence_level.rb

Class Method Summary collapse

Class Method Details

.calculate(n1:, p1:, n2:, p2:) ⇒ Object

rubocop:disable Naming/MethodParameterName



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/laboratory/calculations/confidence_level.rb', line 4

def self.calculate(n1:, p1:, n2:, p2:) # rubocop:disable Naming/MethodParameterName
  cvr1 = p1.fdiv(n1)
  cvr2 = p2.fdiv(n2)

  z = ZScore.calculate(
    n1: n1,
    p1: cvr1,
    n2: n2,
    p2: cvr2
  )

  percentage_from_z_score(-z).round(4)
end

.percentage_from_z_score(z) ⇒ Object

rubocop:disable Naming/MethodParameterName, Metrics/AbcSize, Metrics/MethodLength



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/laboratory/calculations/confidence_level.rb', line 18

def self.percentage_from_z_score(z) # rubocop:disable Naming/MethodParameterName, Metrics/AbcSize, Metrics/MethodLength
  return 0 if z < -6.5
  return 1 if z > 6.5

  factk = 1
  sum = 0
  term = 1
  k = 0
  const = 0.3989422804

  loop_stop = Math.exp(-23)
  while term.abs > loop_stop do # rubocop:disable Style/WhileUntilDo
    term =
      const * ((-1)**k) * (z**k) / (2 * k + 1) / (2**k) * (z**(k + 1)) / factk # rubocop:disable Layout/LineLength

    sum += term
    k += 1
    factk *= k
  end

  sum += 0.5
  1 - sum
end