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.

The normal distribution is a continuous probability distribution that describes real-valued random variables whose distributions are symmetric around their mean.

Examples:

Creating a normal distribution

# Standard normal (mean=0, std_dev=1)
norm = MoreMath::NormalDistribution.new

# Custom parameters
custom_norm = MoreMath::NormalDistribution.new(5.0, 2.0)  # mean=5, std_dev=2

Calculating probabilities

norm = MoreMath::NormalDistribution.new
p = norm.probability(1.96)     # Cumulative probability at z=1.96
x = norm.inverse_probability(0.975)  # Inverse: find z such that P(Z <= z) = 0.975

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, #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.

Parameters:

  • mu (Numeric) (defaults to: 0.0)

    The mean of the distribution (default: 0.0)

  • sigma (Numeric) (defaults to: 1.0)

    The standard deviation of the distribution (default: 1.0)



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

#muFloat (readonly)

Returns the mean of this normal distribution.

Returns:

  • (Float)

    The mean value



37
38
39
# File 'lib/more_math/distributions.rb', line 37

def mu
  @mu
end

#sigmaFloat (readonly)

Returns the standard deviation of this normal distribution.

Returns:

  • (Float)

    The standard deviation value



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(μ, σ²).

Parameters:

  • p (Numeric)

    The probability (0 < p < 1)

Returns:

  • (Float)

    The inverse cumulative probability (quantile)



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(μ, σ²).

Parameters:

  • x (Numeric)

    The value at which to calculate the cumulative probability

Returns:

  • (Float)

    The cumulative probability P(X <= x)



51
52
53
# File 'lib/more_math/distributions.rb', line 51

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