Class: RubyStatistics::Distribution::Binomial
- Inherits:
-
Object
- Object
- RubyStatistics::Distribution::Binomial
- Defined in:
- lib/ruby-statistics/distribution/binomial.rb
Instance Attribute Summary collapse
-
#number_of_trials ⇒ Object
Returns the value of attribute number_of_trials.
-
#probability_per_trial ⇒ Object
Returns the value of attribute probability_per_trial.
Instance Method Summary collapse
- #cumulative_function(k) ⇒ Object
-
#initialize(n, p) ⇒ Binomial
constructor
A new instance of Binomial.
- #mean ⇒ Object
- #mode ⇒ Object
- #probability_mass_function(k) ⇒ Object
- #variance ⇒ Object
Constructor Details
#initialize(n, p) ⇒ Binomial
Returns a new instance of Binomial.
5 6 7 8 |
# File 'lib/ruby-statistics/distribution/binomial.rb', line 5 def initialize(n, p) self.number_of_trials = n.to_i self.probability_per_trial = p end |
Instance Attribute Details
#number_of_trials ⇒ Object
Returns the value of attribute number_of_trials.
4 5 6 |
# File 'lib/ruby-statistics/distribution/binomial.rb', line 4 def number_of_trials @number_of_trials end |
#probability_per_trial ⇒ Object
Returns the value of attribute probability_per_trial.
4 5 6 |
# File 'lib/ruby-statistics/distribution/binomial.rb', line 4 def probability_per_trial @probability_per_trial end |
Instance Method Details
#cumulative_function(k) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/ruby-statistics/distribution/binomial.rb', line 18 def cumulative_function(k) return if k < 0 || k > number_of_trials k = k.to_i p = 1 - probability_per_trial Math.incomplete_beta_function(p, number_of_trials - k, 1 + k) end |
#mean ⇒ Object
26 27 28 |
# File 'lib/ruby-statistics/distribution/binomial.rb', line 26 def mean number_of_trials * probability_per_trial end |
#mode ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ruby-statistics/distribution/binomial.rb', line 34 def mode test = (number_of_trials + 1) * probability_per_trial returned = if test == 0 || (test % 1 != 0) test.floor elsif (test % 1 == 0) && (test >= 1 && test <= number_of_trials) [test, test - 1] elsif test == number_of_trials + 1 number_of_trials end returned end |
#probability_mass_function(k) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/ruby-statistics/distribution/binomial.rb', line 10 def probability_mass_function(k) return if k < 0 || k > number_of_trials k = k.to_i Math.combination(number_of_trials, k) * (probability_per_trial ** k) * ((1 - probability_per_trial) ** (number_of_trials - k)) end |
#variance ⇒ Object
30 31 32 |
# File 'lib/ruby-statistics/distribution/binomial.rb', line 30 def variance mean * (1 - probability_per_trial) end |