Class: Rubystats::ProbabilityDistribution
- Inherits:
-
Object
- Object
- Rubystats::ProbabilityDistribution
- Includes:
- ExtraMath, NumericalConstants, SpecialMath
- Defined in:
- lib/rubystats/probability_distribution.rb
Overview
The ProbabilityDistribution superclass provides an object for encapsulating probability distributions.
Author: Jaco van Kooten Author: Mark Hale Author: Paul Meagher Author: Jesus Castagnetto Author: Bryan Donovan (port from PHPmath to Ruby)
Direct Known Subclasses
BetaDistribution, BinomialDistribution, CauchyDistribution, ExponentialDistribution, GammaDistribution, LognormalDistribution, MultivariateNormalDistribution, NormalDistribution, PoissonDistribution, StudentTDistribution, UniformDistribution, WeibullDistribution
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
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
Cummulative distribution function.
-
#check_range(x, lo = 0.0, hi = 1.0) ⇒ Object
check that variable is between lo and hi limits.
- #find_root(prob, guess, x_lo, x_hi) ⇒ Object
- #get_factorial(n) ⇒ Object
-
#icdf(p) ⇒ Object
Inverse CDF.
-
#initialize ⇒ ProbabilityDistribution
constructor
A new instance of ProbabilityDistribution.
-
#mean ⇒ Object
returns the distribution mean.
-
#pdf(x) ⇒ Object
Probability density function.
-
#rng(n = 1) ⇒ Object
Returns random number(s) using subclass’s get_rng method.
-
#variance ⇒ Object
returns distribution variance.
Methods included from ExtraMath
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
Constructor Details
#initialize ⇒ ProbabilityDistribution
Returns a new instance of ProbabilityDistribution.
18 19 |
# File 'lib/rubystats/probability_distribution.rb', line 18 def initialize end |
Instance Method Details
#cdf(x) ⇒ Object
Cummulative distribution function
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rubystats/probability_distribution.rb', line 45 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
check that variable is between lo and hi limits. lo default is 0.0 and hi default is 1.0
122 123 124 125 126 127 |
# File 'lib/rubystats/probability_distribution.rb', line 122 def check_range(x, lo=0.0, hi=1.0) raise ArgumentError.new("x cannot be nil") if x.nil? if x < lo or x > hi raise ArgumentError.new("x must be greater than lo (#{lo}) and less than hi (#{hi})") end end |
#find_root(prob, guess, x_lo, x_hi) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rubystats/probability_distribution.rb', line 137 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_factorial(n) ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/rubystats/probability_distribution.rb', line 129 def get_factorial(n) if n <= 1 return 1 else return n.downto(1).reduce(:*) end end |
#icdf(p) ⇒ Object
Inverse CDF
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rubystats/probability_distribution.rb', line 58 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 |
#mean ⇒ Object
returns the distribution mean
22 23 24 |
# File 'lib/rubystats/probability_distribution.rb', line 22 def mean get_mean end |
#pdf(x) ⇒ Object
Probability density function
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rubystats/probability_distribution.rb', line 32 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
Returns random number(s) using subclass’s get_rng method
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rubystats/probability_distribution.rb', line 71 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 |
#variance ⇒ Object
returns distribution variance
27 28 29 |
# File 'lib/rubystats/probability_distribution.rb', line 27 def variance get_variance end |