Class: MoreMath::ChiSquareDistribution

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

Overview

This class is used to compute the Chi-Square Distribution.

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(df) ⇒ ChiSquareDistribution

Creates a ChiSquareDistribution for df degrees of freedom.



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

def initialize(df)
  @df = df
  @df_half = @df / 2.0
end

Instance Attribute Details

#dfObject (readonly)

Returns the value of attribute df.



57
58
59
# File 'lib/more_math/distributions.rb', line 57

def df
  @df
end

Instance Method Details

#inverse_probability(p) ⇒ Object

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/more_math/distributions.rb', line 71

def inverse_probability(p)
  case
  when p <= 0, p >= 1
    0.0
  else
    begin
      bisect = NewtonBisection.new { |x| probability(x) - p }
      range = bisect.bracket 0.5..10
      bisect.solve(range, 1_000_000)
    rescue
      0 / 0.0
    end
  end
end

#probability(x) ⇒ Object

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



61
62
63
64
65
66
67
# File 'lib/more_math/distributions.rb', line 61

def probability(x)
  if x < 0
    0.0
  else
    gammaP_regularized(x / 2, @df_half)
  end
end