Class: Binomial::CumulativeCalculator

Inherits:
Calculator
  • Object
show all
Defined in:
lib/binomial/cumulative_calculator.rb

Instance Attribute Summary collapse

Attributes inherited from Calculator

#probability, #target, #trials

Instance Method Summary collapse

Methods inherited from Calculator

#check_positive, #equation, #model

Constructor Details

#initialize(params = {}) ⇒ CumulativeCalculator

sign: P(X {sign} {target})

The probability X is [equal to, less than, greater than, etc] the 
target when X = number of successes


10
11
12
13
# File 'lib/binomial/cumulative_calculator.rb', line 10

def initialize(params = {})
  super
  @sign = params[:sign]
end

Instance Attribute Details

#signObject

Returns the value of attribute sign.



5
6
7
# File 'lib/binomial/cumulative_calculator.rb', line 5

def sign
  @sign
end

Instance Method Details

#calc_cumulative(up_to) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/binomial/cumulative_calculator.rb', line 33

def calc_cumulative(up_to)
  total = 0.0
  while up_to > 0 do
    calc = Calculator.new trials: @trials, probability: @probability, target: up_to
    total += calc.calculate
    up_to -= 1
  end
  total
end

#calculateObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/binomial/cumulative_calculator.rb', line 15

def calculate
  total = 0
  case @sign
  when :equals
    super
  when :less_than
    calc_cumulative @target - 1
  when :less_than_or_equal_to
    calc_cumulative @target
  when :greater_than
    1 - calc_cumulative(@target)
  when :greater_than_or_equal_to
    1 - calc_cumulative(@target - 1)
  else
    raise "Error: invalid sign '#{@sign.to_s}' supplied"
  end
end