Class: Rubystats::BetaDistribution
- Inherits:
-
ProbabilityDistribution
- Object
- ProbabilityDistribution
- Rubystats::BetaDistribution
- Includes:
- SpecialMath
- Defined in:
- lib/rubystats/beta_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
-
#p ⇒ Object
readonly
Returns the value of attribute p.
-
#q ⇒ Object
readonly
Returns the value of attribute q.
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
- #cdf(x) ⇒ Object
- #icdf(prob) ⇒ Object
-
#initialize(dgr_p, dgr_q) ⇒ BetaDistribution
constructor
dgr_p = degrees of freedom p dgr_q = degrees of freedom q.
- #mean ⇒ Object
- #pdf(x) ⇒ Object
- #standard_deviation ⇒ Object
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, #rng, #variance
Methods included from ExtraMath
Constructor Details
#initialize(dgr_p, dgr_q) ⇒ BetaDistribution
dgr_p = degrees of freedom p dgr_q = degrees of freedom q
11 12 13 14 15 16 17 |
# File 'lib/rubystats/beta_distribution.rb', line 11 def initialize(dgr_p, dgr_q) if dgr_p <= 0 || dgr_q <= 0 raise ArgumentError.new("Paramters must be greater than zero.") end @p = dgr_p.to_f @q = dgr_q.to_f end |
Instance Attribute Details
#p ⇒ Object (readonly)
Returns the value of attribute p.
7 8 9 |
# File 'lib/rubystats/beta_distribution.rb', line 7 def p @p end |
#q ⇒ Object (readonly)
Returns the value of attribute q.
7 8 9 |
# File 'lib/rubystats/beta_distribution.rb', line 7 def q @q end |
Instance Method Details
#cdf(x) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rubystats/beta_distribution.rb', line 51 def cdf(x) if x.class == Array cdf_vals = Array.new for i in 0 ... x.size check_range(x[i]) cdf_vals[i] = incomplete_beta(x[i], @p, @q) end return cdf_vals else check_range(x) cdf_val = incomplete_beta(x, @p, @q) return cdf_val end end |
#icdf(prob) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rubystats/beta_distribution.rb', line 66 def icdf(prob) if prob.class == Array inv_vals = Array.new for i in 0 ... prob.size check_range(prob[i]) if prob[i] == 0.0 inv_vals[i] = 0.0 end if prob[i] == 1.0 inv_vals[i] = 1.0 end inv_vals[i] = find_root(prob[i], 0.5, 0.0, 1.0) end return inv_vals else check_range(prob) return 0.0 if prob == 0.0 return 1.0 if prob == 1.0 return find_root(prob, 0.5, 0.0, 1.0) end end |
#mean ⇒ Object
19 20 21 |
# File 'lib/rubystats/beta_distribution.rb', line 19 def mean @p.to_f / (@p.to_f + @q.to_f) end |
#pdf(x) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rubystats/beta_distribution.rb', line 27 def pdf(x) if x.class == Array pdf_vals = [] for i in (0 ... x.size) check_range(x[i]) if x[i] == 0.0 || x[i] == 1.0 pdf_vals[i] = 0.0 else pdf_vals[i] = Math.exp( - log_beta(@p, @q) + (@p - 1.0) * Math.log(x[i]) + (@q - 1.0) * Math.log(1.0 - x[i])) end end return pdf_vals else check_range(x) if (x == 0.0) || (x == 1.0) return 0.0 else return Math.exp( -1.0 * log_beta(@p, @q) + (@p - 1.0) * Math.log(x) + (@q - 1.0) * Math.log(1.0 - x)) end end end |
#standard_deviation ⇒ Object
23 24 25 |
# File 'lib/rubystats/beta_distribution.rb', line 23 def standard_deviation Math.sqrt(@p * @q / ((@p + @q)**2 * (@p + @q + 1))) end |