Class: Rubystats::BinomialDistribution

Inherits:
ProbabilityDistribution show all
Includes:
ExtraMath, 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 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

#check_range, #find_root, #get_factorial, #icdf, #mean, #variance

Constructor Details

#initialize(trials, prob) ⇒ BinomialDistribution

Constructs a binomial distribution



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

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

Instance Attribute Details

#nObject

Returns the value of attribute n.



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

def n
  @n
end

#pObject

Returns the value of attribute p.



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

def p
  @p
end

Instance Method Details

#cdf(_x) ⇒ Object

Cumulative binomial distribution function (equivalent to R pbinom function). _x should be integer-valued and can be single integer or array of integers returns single value or array containing probability that a stochastic variable x is less then X, i.e. P(x < _x).



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rubystats/binomial_distribution.rb', line 71

def cdf(_x)
  if _x.class == Array
    pdf_vals = []
    for i in (0 ..._x.length)
      pdf_vals[i] = get_cdf(_x[i])
    end
    return pdf_vals
  else
    return get_cdf(_x)
  end
end

#get_icdf(prob) ⇒ Object

Inverse of the cumulative binomial distribution function (equivalent to R qbinom function). returns the value X for which P(x < _x).



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubystats/binomial_distribution.rb', line 86

def get_icdf(prob)
  if prob.class == Array
    inv_vals = []
    for i in (0 ...prob.length)
      check_range(prob[i])
      inv_vals[i] = (find_root(prob[i], @n/2, 0.0, @n)).floor
    end
    return inv_vals
  else
    check_range(prob)
    return (find_root(prob, @n/2, 0.0, @n)).floor
  end
end

#get_meanObject

returns the mean



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

def get_mean
  @n * @p
end

#get_probability_parameterObject

returns the probability



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

def get_probability_parameter
  @p
end

#get_trials_parameterObject

returns the number of trials



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

def get_trials_parameter
  @n
end

#get_varianceObject

returns the variance



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

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

#pdf(_x) ⇒ Object

Probability density function of a binomial distribution (equivalent to R dbinom function). _x should be an integer returns the probability that a stochastic variable x has the value _x, i.e. P(x = _x)



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubystats/binomial_distribution.rb', line 53

def pdf(_x)
  if _x.class == Array
    pdf_vals = []
    for i in (0 ... _x.length)
      check_range(_x[i], 0.0, @n)
      pdf_vals[i] = binomial(@n, _x[i]) * (1-@p)**(@n-_x[i])
    end
    return pdf_vals
  else
    check_range(_x, 0.0, @n)
    return binomial(@n, _x) * @p**_x * (1-@p)**(@n-_x)
  end
end

#rng(num_vals = 1) ⇒ Object

Wrapper for binomial RNG function (equivalent to R rbinom function). returns random deviate given trials and p



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rubystats/binomial_distribution.rb', line 102

def rng(num_vals = 1)
  if num_vals < 1
    raise "Error num_vals must be greater than or equal to 1"
  end
  if num_vals == 1
    return get_rng
  else
    rand_vals = []
    for i in (0 ...num_vals)
      rand_vals[i] = get_rng
    end
    return rand_vals
  end
end