Class: RubyStatistics::Distribution::NegativeBinomial
- Inherits:
-
Object
- Object
- RubyStatistics::Distribution::NegativeBinomial
- Defined in:
- lib/ruby-statistics/distribution/negative_binomial.rb
Instance Attribute Summary collapse
-
#number_of_failures ⇒ Object
Returns the value of attribute number_of_failures.
-
#probability_per_trial ⇒ Object
Returns the value of attribute probability_per_trial.
Instance Method Summary collapse
- #cumulative_function(k) ⇒ Object
-
#initialize(r, p) ⇒ NegativeBinomial
constructor
A new instance of NegativeBinomial.
- #mean ⇒ Object
- #mode ⇒ Object
- #probability_mass_function(k) ⇒ Object
- #skewness ⇒ Object
- #variance ⇒ Object
Constructor Details
#initialize(r, p) ⇒ NegativeBinomial
Returns a new instance of NegativeBinomial.
6 7 8 9 |
# File 'lib/ruby-statistics/distribution/negative_binomial.rb', line 6 def initialize(r, p) self.number_of_failures = r.to_i self.probability_per_trial = p end |
Instance Attribute Details
#number_of_failures ⇒ Object
Returns the value of attribute number_of_failures.
4 5 6 |
# File 'lib/ruby-statistics/distribution/negative_binomial.rb', line 4 def number_of_failures @number_of_failures end |
#probability_per_trial ⇒ Object
Returns the value of attribute probability_per_trial.
4 5 6 |
# File 'lib/ruby-statistics/distribution/negative_binomial.rb', line 4 def probability_per_trial @probability_per_trial end |
Instance Method Details
#cumulative_function(k) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/ruby-statistics/distribution/negative_binomial.rb', line 20 def cumulative_function(k) return if k < 0 || k > number_of_failures k = k.to_i 1.0 - Math.incomplete_beta_function(probability_per_trial, k + 1, number_of_failures) end |
#mean ⇒ Object
27 28 29 |
# File 'lib/ruby-statistics/distribution/negative_binomial.rb', line 27 def mean (probability_per_trial * number_of_failures)/(1 - probability_per_trial).to_r end |
#mode ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ruby-statistics/distribution/negative_binomial.rb', line 39 def mode if number_of_failures > 1 up = probability_per_trial * (number_of_failures - 1) down = (1 - probability_per_trial).to_r (up/down).floor elsif number_of_failures <= 1 0.0 end end |
#probability_mass_function(k) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/ruby-statistics/distribution/negative_binomial.rb', line 11 def probability_mass_function(k) return if number_of_failures < 0 || k < 0 || k > number_of_failures left = Math.combination(k + number_of_failures - 1, k) right = ((1 - probability_per_trial) ** number_of_failures) * (probability_per_trial ** k) left * right end |
#skewness ⇒ Object
35 36 37 |
# File 'lib/ruby-statistics/distribution/negative_binomial.rb', line 35 def skewness (1 + probability_per_trial).to_r / Math.sqrt(probability_per_trial * number_of_failures) end |
#variance ⇒ Object
31 32 33 |
# File 'lib/ruby-statistics/distribution/negative_binomial.rb', line 31 def variance (probability_per_trial * number_of_failures)/((1 - probability_per_trial) ** 2).to_r end |