Class: Rubystats::BinomialDistribution

Inherits:
ProbabilityDistribution show all
Includes:
ExtraMath, MakeDiscrete, NumericalConstants, SpecialMath
Defined in:
lib/rubystats/binomial_distribution.rb

Constant Summary

Constants included from NumericalConstants

NumericalConstants::EPS, NumericalConstants::GAMMA, NumericalConstants::GAMMA_X_MAX_VALUE, NumericalConstants::GOLDEN_RATIO, NumericalConstants::LOG_GAMMA_X_MAX_VALUE, NumericalConstants::MAX_FLOAT, NumericalConstants::MAX_ITERATIONS, NumericalConstants::MAX_VALUE, NumericalConstants::PRECISION, NumericalConstants::SQRT2, NumericalConstants::SQRT2PI, NumericalConstants::TWO_PI, NumericalConstants::XMININ

Instance Attribute Summary collapse

Attributes included from SpecialMath

#log_beta_cache_p, #log_beta_cache_q, #log_beta_cache_res, #log_gamma_cache_res, #log_gamma_cache_x

Instance Method Summary collapse

Methods included from MakeDiscrete

#pmf

Methods included from ExtraMath

#binomial

Methods included from SpecialMath

#beta, #beta_fraction, #complementary_error, #error, #gamma, #gamma_fraction, #gamma_series_expansion, #incomplete_beta, #incomplete_gamma, #log_beta, #log_gamma, #orig_gamma

Methods inherited from ProbabilityDistribution

#cdf, #check_range, #find_root, #get_factorial, #icdf, #mean, #pdf, #rng, #variance

Constructor Details

#initialize(trials, prob) ⇒ BinomialDistribution

Constructs a binomial distribution



18
19
20
21
22
23
24
25
26
27
# File 'lib/rubystats/binomial_distribution.rb', line 18

def initialize (trials, prob)
  if trials <= 0
    raise ArgumentError.new("Error: trials must be greater than 0")
  end
  @n = trials.to_i
  if prob < 0.0 || prob > 1.0
    raise ArgumentError.new("prob must be between 0 and 1")
  end
  @p = prob.to_f
end

Instance Attribute Details

#nObject

Returns the value of attribute n.



14
15
16
# File 'lib/rubystats/binomial_distribution.rb', line 14

def n
  @n
end

#pObject

Returns the value of attribute p.



14
15
16
# File 'lib/rubystats/binomial_distribution.rb', line 14

def p
  @p
end

Instance Method Details

#get_meanObject

returns the mean



40
41
42
# File 'lib/rubystats/binomial_distribution.rb', line 40

def get_mean
  @n * @p
end

#get_probability_parameterObject

returns the probability



35
36
37
# File 'lib/rubystats/binomial_distribution.rb', line 35

def get_probability_parameter
  @p
end

#get_trials_parameterObject

returns the number of trials



30
31
32
# File 'lib/rubystats/binomial_distribution.rb', line 30

def get_trials_parameter
  @n
end

#get_varianceObject

returns the variance



45
46
47
# File 'lib/rubystats/binomial_distribution.rb', line 45

def get_variance
  @n * @p * (1.0 - @p)
end