Class: Rubystats::BetaDistribution

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

Instance Method Summary collapse

Methods included from SpecialMath

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

Methods inherited from ProbabilityDistribution

#check_range, #find_root, #get_cdf, #get_factorial, #get_icdf, #get_mean, #get_pdf, #get_rng, #rng

Methods included from ExtraMath

#binomial

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
    return nil
  end
  @p = dgr_p.to_f
  @q = dgr_q.to_f
end

Instance Attribute Details

#pObject (readonly)

Returns the value of attribute p.



7
8
9
# File 'lib/rubystats/beta_distribution.rb', line 7

def p
  @p
end

#qObject (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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubystats/beta_distribution.rb', line 50

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rubystats/beta_distribution.rb', line 65

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

#meanObject



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
# 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( - log_beta(@p, @q) + (@p - 1.0) * Math.log(x) + (@q - 1.0) * Math.log(1.0 - x)
      )
    end
  end
end

#standard_deviationObject



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