Module: ABAnalyzer

Defined in:
lib/abanalyzer/abtest.rb,
lib/abanalyzer/matrix.rb,
lib/abanalyzer/sample.rb,
lib/abanalyzer/version.rb,
lib/abanalyzer/exceptions.rb

Defined Under Namespace

Classes: ABTest, InsufficientDataError, Matrix, MatrixFormatError

Constant Summary collapse

VERSION =
'1.0.0'.freeze

Class Method Summary collapse

Class Method Details

.calculate_size(p1, p2, significance, power) ⇒ Object

Calculate the minimum sample size (per group) based on the desire to detect a increase from proportion p1 to proportion p2. Significance is generally safe at 0.05 (why? just because) and a power of 0.8 (why? just because)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/abanalyzer/sample.rb', line 7

def self.calculate_size(p1, p2, significance, power)
  [p1, p2, significance, power].each do |a|
    raise 'All arguments to calculate_size must be Floats' unless a.is_a?(Float)
  end

  pbar = (p1 + p2) / 2.0
  sides = 2.0

  zcrit = Statistics2.pnormaldist(1 - (significance / sides))
  zpow = Statistics2.pnormaldist(power)

  numerator = (zcrit * Math.sqrt(2 * pbar * (1 - pbar)) + zpow * Math.sqrt(p2 * (1 - p2) + p1 * (1 - p1)))**2
  denominator = (p2 - p1)**2
  (numerator / denominator).ceil
end

.confidence_interval(successes, trials, confidence) ⇒ Object

Calculate the confidence interval given the number of successes and trials at the desired confidence level. Returns an Array of [lower, upper]



25
26
27
28
29
30
31
32
33
# File 'lib/abanalyzer/sample.rb', line 25

def self.confidence_interval(successes, trials, confidence)
  sides = 2.0
  alpha = 1 - confidence
  zcrit = Statistics2.pnormaldist(1 - (alpha / sides))
  p = successes.to_f / trials.to_f

  interval = zcrit * Math.sqrt((p * (1 - p)) / trials.to_f)
  [p - interval, p + interval]
end

.relative_confidence_interval(successes, trials, compared_proportion, confidence) ⇒ Object

Like confidence_interval, but returns the relative interval compared to the baseline given in compared_proportion



37
38
39
40
# File 'lib/abanalyzer/sample.rb', line 37

def self.relative_confidence_interval(successes, trials, compared_proportion, confidence)
  ci = confidence_interval(successes, trials, confidence)
  [(ci.first - compared_proportion) / compared_proportion, (ci.last - compared_proportion) / compared_proportion]
end