Class: Binomial
- Inherits:
-
Object
- Object
- Binomial
- Defined in:
- lib/binomial_distribution.rb
Instance Method Summary collapse
-
#distribute(k) ⇒ Float
Probability.
-
#initialize(n: nil, p: nil, tries: nil, probability: nil, failure: nil) ⇒ Binomial
constructor
@param failure [Float] alias for ‘p = 1 - failure`.
- #to_s ⇒ Object
Constructor Details
#initialize(n: nil, p: nil, tries: nil, probability: nil, failure: nil) ⇒ Binomial
Note:
if no probability is defined, the default value will be 0.5
@param failure [Float] alias for ‘p = 1 - failure`
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/binomial_distribution.rb', line 38 def initialize n: nil, p: nil, tries: nil, probability: nil, failure: nil tries_count = n || tries probability = p || probability || (failure && 1.0 - failure) || 0.5 raise ArgumentError, "The argument `n` `#{tries_count}` is not an Integer" unless tries_count.is_a? Integer raise ArgumentError, "The argument `p` `#{probability}` is not a Numeric" unless probability.is_a? Numeric tries_count = Float(tries_count) raise Math::DomainError, "The argument `n` `#{tries_count}` is not in greater or equal to 0" if tries_count < 0.0 raise Math::DomainError, "The argument `p` `#{probability}` is not in greater or equal to 0" if probability < 0.0 raise Math::DomainError, "The argument `p` `#{probability}` is not in lesser or equal to 1" if probability > 1.0 @n = tries_count.to_i @p = probability.to_f end |
Instance Method Details
#distribute(k) ⇒ Float
Returns probability.
58 59 60 61 62 63 64 65 66 |
# File 'lib/binomial_distribution.rb', line 58 def distribute k if k.is_a? Enumerable distribute_enumerable k elsif k.is_a? Integer distribute_integer k else raise ArgumentError end end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/binomial_distribution.rb', line 51 def to_s "#<#{self.class} @n=#@n, @p=#@p>" end |