Class: Rubystats::ProbabilityDistribution

Inherits:
Object
  • Object
show all
Includes:
ExtraMath, NumericalConstants, SpecialMath
Defined in:
lib/rubystats/probability_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 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

Constructor Details

#initializeProbabilityDistribution

Returns a new instance of ProbabilityDistribution.



9
10
# File 'lib/rubystats/probability_distribution.rb', line 9

def initialize
end

Instance Method Details

#cdf(x) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubystats/probability_distribution.rb', line 35

def cdf(x)
  if x.class == Array
    cdf_vals = []
    for i in (0...x.size)
      cdf_vals[i] = get_cdf(x[i])
    end
    return cdf_vals
  else
    return get_cdf(x)
  end
end

#check_range(x, lo = 0.0, hi = 1.0) ⇒ Object



83
84
85
86
87
# File 'lib/rubystats/probability_distribution.rb', line 83

def check_range(x, lo=0.0, hi=1.0)
  if (x < lo) || (x > hi)
    return "error"
  end
end

#find_root(prob, guess, x_lo, x_hi) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rubystats/probability_distribution.rb', line 97

def find_root (prob, guess, x_lo, x_hi) 
  accuracy = 1.0e-10
  max_iteration = 150
  x 		= guess
  x_new = guess
  error = 0.0
  pdf 	= 0.0
  dx 		= 1000.0
  i 		= 0
  while ( dx.abs > accuracy && (i += 1) < max_iteration )
    #Apply Newton-Raphson step
    error = cdf(x) - prob
    if error < 0.0
      x_lo = x
    else
      x_hi = x
    end
    pdf = pdf(x)
    if pdf != 0.0
      dx = error / pdf
      x_new = x -dx
    end
    # If the NR fails to converge (which for example may be the 
    # case if the initial guess is too rough) we apply a bisection
    # step to determine a more narrow interval around the root.
    if  x_new < x_lo || x_new > x_hi || pdf == 0.0
      x_new = (x_lo + x_hi) / 2.0
      dx = x_new - x
    end
    x = x_new
  end
  return x
end

#get_cdf(x) ⇒ Object



47
48
# File 'lib/rubystats/probability_distribution.rb', line 47

def get_cdf(x)
end

#get_factorial(n) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/rubystats/probability_distribution.rb', line 89

def get_factorial(n)
  if n <= 1
    return 1
  else 
    return n * get_factorial(n-1)
  end
end

#get_icdf(p) ⇒ Object



62
63
# File 'lib/rubystats/probability_distribution.rb', line 62

def get_icdf(p)
end

#get_meanObject



16
17
# File 'lib/rubystats/probability_distribution.rb', line 16

def get_mean
end

#get_pdf(x) ⇒ Object



32
33
# File 'lib/rubystats/probability_distribution.rb', line 32

def get_pdf(x)
end

#get_rngObject



80
81
# File 'lib/rubystats/probability_distribution.rb', line 80

def get_rng() 
end

#icdf(p) ⇒ Object



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

def icdf(p)
  if p.class == Array
    inv_vals = []
    for i in (0..p.length)
      inv_vals[i] = get_icdf(p[i])
    end
    return inv_vals
  else
    return get_icdf(p)
  end
end

#meanObject



12
13
14
# File 'lib/rubystats/probability_distribution.rb', line 12

def mean
  get_mean
end

#pdf(x) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubystats/probability_distribution.rb', line 20

def pdf(x) 
  if x.class == Array
    pdf_vals = []
    for i in (0..x.length)
      pdf_vals[i] = get_pdf(x[i])
    end
    return pdf_vals
  else
    return get_pdf(x)
  end
end

#rng(n = 1) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rubystats/probability_distribution.rb', line 65

def rng(n=1)
  if n < 1
    return "Number of random numbers to return must be 1 or greater"
  end
  if (n > 1)
    rnd_vals = []
    for i in (0..n)
      rnd_vals[i] = get_rng()
    end
    return rnd_vals
  else
    return get_rng()
  end
end