Class: MoreMath::TDistribution

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

Overview

This class is used to compute the T-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) ⇒ TDistribution

Returns a TDistribution instance for the degrees of freedom df.



92
93
94
# File 'lib/more_math/distributions.rb', line 92

def initialize(df)
  @df = df
end

Instance Attribute Details

#dfObject (readonly)

Degrees of freedom.



97
98
99
# File 'lib/more_math/distributions.rb', line 97

def df
  @df
end

Instance Method Details

#inverse_probability(p) ⇒ Object

Returns the inverse cumulative probability (t-value) of the TDistribution for the probability p.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/more_math/distributions.rb', line 116

def inverse_probability(p)
  case
  when p <= 0
    -1 / 0.0
  when p >= 1
    1 / 0.0
  else
    begin
      bisect = NewtonBisection.new { |x| probability(x) - p }
      range = bisect.bracket(-10..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 TDistribution for the t-value x.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/more_math/distributions.rb', line 101

def probability(x)
  if x == 0
    0.5
  else
    t = beta_regularized(@df / (@df + x ** 2.0), 0.5 * @df, 0.5)
    if x < 0.0
      0.5 * t
    else
      1 - 0.5 * t
    end
  end
end