Class: Skab::Models::Binomial

Inherits:
Object
  • Object
show all
Defined in:
lib/skab/models/binomial.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Binomial

Returns a new instance of Binomial.



5
6
7
8
9
10
# File 'lib/skab/models/binomial.rb', line 5

def initialize(args)
  @a_trials = args.shift.to_i
  @a_success = args.shift.to_i
  @b_trials = args.shift.to_i
  @b_success = args.shift.to_i
end

Class Method Details

.helpObject



50
51
52
53
54
55
56
57
# File 'lib/skab/models/binomial.rb', line 50

def self.help
  <<-HELP
skab [output] binomial [trials_a] [successes_a] [trials_b] [successes_b]
\tWhere: all parameters are integers
\tPlots the binomial distribution for A and B, given their respective
\tnumber of successes and trials
  HELP
end

Instance Method Details

#differentialObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/skab/models/binomial.rb', line 35

def differential
  return @differential if @differential
  @differential = Hash.new(0)
  i = 0.0
  while i <= 1000
    j = 0.0
    while j <= 1000
      @differential[(j - i) / 1000] += distribution[j][2] * distribution[i][1]
      j += 1
    end
    i += 1
  end
  @differential
end

#distributionObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/skab/models/binomial.rb', line 12

def distribution
  return @distribution if @distribution
  @distribution = []
  sums = [0, 0, 0]
  i = 0.0
  while i <= 1000
    @distribution[i] = []
    @distribution[i][0] = i / 1000
    @distribution[i][1] = binomial(@a_trials, @a_success, i / 1000)
    @distribution[i][2] = binomial(@b_trials, @b_success, i / 1000)
    sums[1] += binomial(@a_trials, @a_success, i / 1000)
    sums[2] += binomial(@b_trials, @b_success, i / 1000)
    i += 1
  end
  i = 0.0
  while i <= 1000
    @distribution[i][1] /= sums[1]
    @distribution[i][2] /= sums[2]
    i += 1
  end
  @distribution
end