Class: MoreMath::NormalDistribution

Inherits:
Object
  • Object
show all
Includes:
Constants::FunctionsConstants, Functions
Defined in:
lib/more_math/distributions.rb

Overview

This class is used to compute the Normal Distribution.

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

Instance Method Summary collapse

Methods included from Functions

beta, beta_regularized, cantor_pairing, cantor_pairing_inv, #erf, #erfc, gamma, gammaP_regularized, gammaQ_regularized, log_beta, log_ceil, log_floor, #log_gamma, logb, numberify_string, stringify_number

Constructor Details

#initialize(mu = 0.0, sigma = 1.0) ⇒ NormalDistribution

Creates a NormalDistribution instance for the values mu and sigma.



11
12
13
# File 'lib/more_math/distributions.rb', line 11

def initialize(mu = 0.0, sigma = 1.0)
  @mu, @sigma = mu.to_f, sigma.to_f
end

Instance Attribute Details

#muObject (readonly)

Returns the value of attribute mu.



15
16
17
# File 'lib/more_math/distributions.rb', line 15

def mu
  @mu
end

#sigmaObject (readonly)

Returns the value of attribute sigma.



17
18
19
# File 'lib/more_math/distributions.rb', line 17

def sigma
  @sigma
end

Instance Method Details

#inverse_probability(p) ⇒ Object

Returns the inverse cumulative probability value of the NormalDistribution for the probability p.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/more_math/distributions.rb', line 27

def inverse_probability(p)
  case
  when p <= 0
    -1 / 0.0
  when p >= 1
    1 / 0.0
  when (p - 0.5).abs <= Float::EPSILON
    @mu
  else
    begin
      NewtonBisection.new { |x| probability(x) - p }.solve(nil, 1_000_000)
    rescue
      0 / 0.0
    end
  end
end

#probability(x) ⇒ Object

Returns the cumulative probability (p-value) of the NormalDistribution for the value x.



21
22
23
# File 'lib/more_math/distributions.rb', line 21

def probability(x)
  0.5 * (1 + erf((x - @mu) / (@sigma * ROOT2)))
end