Class: Skab::Models::Binomial
- Inherits:
-
Object
- Object
- Skab::Models::Binomial
- Defined in:
- lib/skab/models/binomial.rb
Class Method Summary collapse
Instance Method Summary collapse
- #differential ⇒ Object
- #distribution ⇒ Object
-
#initialize(args) ⇒ Binomial
constructor
A new instance of Binomial.
- #percentile(p) ⇒ Object
Constructor Details
#initialize(args) ⇒ Binomial
Returns a new instance of Binomial.
5 6 7 8 9 10 11 |
# 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 @fact = { } end |
Class Method Details
.help ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/skab/models/binomial.rb', line 64 def self.help "skab [output] binomial [trials_a] [successes_a] [trials_b] [successes_b]\n\\tWhere: all parameters are integers\n\\tPlots the binomial distribution for A and B, given their respective\n\\tnumber of successes and trials\n HELP\nend\n" |
Instance Method Details
#differential ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/skab/models/binomial.rb', line 38 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] / 1000 j += 1 end i += 1 end @differential end |
#distribution ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/skab/models/binomial.rb', line 13 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][1] *= 1000 @distribution[i][2] /= sums[2] @distribution[i][2] *= 1000 i += 1 end @distribution end |
#percentile(p) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/skab/models/binomial.rb', line 53 def percentile(p) sum = 0.0 Hash[differential.sort].each do |k, v| sum += v if sum >= p * 1000 return k end end percentile end |