Class: MoreMath::NormalDistribution
- Includes:
- Constants::FunctionsConstants, Functions
- Defined in:
- lib/more_math/distributions.rb
Overview
This class is used to compute the Normal Distribution.
The normal distribution is a continuous probability distribution that describes real-valued random variables whose distributions are symmetric around their mean.
Constant Summary
Constants included from Constants::FunctionsConstants
Constants::FunctionsConstants::ERF_A, Constants::FunctionsConstants::HALF_LOG_2_PI, Constants::FunctionsConstants::LANCZOS_COEFFICIENTS, Constants::FunctionsConstants::ROOT2
Instance Attribute Summary collapse
-
#mu ⇒ Float
readonly
Returns the mean of this normal distribution.
-
#sigma ⇒ Float
readonly
Returns the standard deviation of this normal distribution.
Instance Method Summary collapse
-
#initialize(mu = 0.0, sigma = 1.0) ⇒ NormalDistribution
constructor
Creates a NormalDistribution instance for the values
muandsigma. -
#inverse_probability(p) ⇒ Float
Returns the inverse cumulative probability value of the NormalDistribution for the probability
p. -
#probability(x) ⇒ Float
Returns the cumulative probability (p-value) of the NormalDistribution for the value
x.
Methods included from Functions
#beta, beta, #beta_regularized, beta_regularized, #cantor_pairing, cantor_pairing, #cantor_pairing_inv, cantor_pairing_inv, #erf, #erfc, #gamma, gamma, #gammaP_regularized, gammaP_regularized, #gammaQ_regularized, gammaQ_regularized, #log_beta, log_beta, #log_ceil, log_ceil, #log_floor, log_floor, #log_gamma, #logb, logb, #numberify_string, numberify_string, #stringify_number, stringify_number
Constructor Details
#initialize(mu = 0.0, sigma = 1.0) ⇒ NormalDistribution
Creates a NormalDistribution instance for the values mu and sigma.
30 31 32 |
# File 'lib/more_math/distributions.rb', line 30 def initialize(mu = 0.0, sigma = 1.0) @mu, @sigma = mu.to_f, sigma.to_f end |
Instance Attribute Details
#mu ⇒ Float (readonly)
Returns the mean of this normal distribution.
37 38 39 |
# File 'lib/more_math/distributions.rb', line 37 def mu @mu end |
#sigma ⇒ Float (readonly)
Returns the standard deviation of this normal distribution.
42 43 44 |
# File 'lib/more_math/distributions.rb', line 42 def sigma @sigma end |
Instance Method Details
#inverse_probability(p) ⇒ Float
Returns the inverse cumulative probability value of the NormalDistribution for the probability p.
This finds the value x such that P(X <= x) = p where X ~ N(μ, σ²).
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/more_math/distributions.rb', line 62 def inverse_probability(p) case when p <= 0 -1 / 0.0 # Negative infinity when p >= 1 1 / 0.0 # Positive infinity when (p - 0.5).abs <= Float::EPSILON @mu # Median equals mean for normal distribution else begin NewtonBisection.new { |x| probability(x) - p }.solve(nil, 1_000_000) rescue 0 / 0.0 # NaN on error end end end |
#probability(x) ⇒ Float
Returns the cumulative probability (p-value) of the NormalDistribution for the value x.
This calculates P(X <= x) where X ~ N(μ, σ²).
51 52 53 |
# File 'lib/more_math/distributions.rb', line 51 def probability(x) 0.5 * (1 + erf((x - @mu) / (@sigma * ROOT2))) end |