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.

The t-distribution (Student’s t-distribution) is a probability distribution that is used when estimating the mean of a normally distributed population in situations where the sample size is small and the population standard deviation is unknown.

Examples:

Creating a t-distribution

t_dist = MoreMath::TDistribution.new(10)  # 10 degrees of freedom

Calculating probabilities

t_dist = MoreMath::TDistribution.new(10)
p = t_dist.probability(2.228)  # Probability that X <= 2.228

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

Returns a TDistribution instance for the degrees of freedom df.

Parameters:

  • df (Integer)

    The degrees of freedom



169
170
171
# File 'lib/more_math/distributions.rb', line 169

def initialize(df)
  @df = df
end

Instance Attribute Details

#dfInteger (readonly)

Degrees of freedom.

Returns:

  • (Integer)

    The degrees of freedom



176
177
178
# File 'lib/more_math/distributions.rb', line 176

def df
  @df
end

Instance Method Details

#inverse_probability(p) ⇒ Float

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

This finds the value x such that P(X <= x) = p where X ~ t(df).

Parameters:

  • p (Numeric)

    The probability (0 < p < 1)

Returns:

  • (Float)

    The inverse cumulative probability (quantile)



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/more_math/distributions.rb', line 205

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

#probability(x) ⇒ Float

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

This calculates P(X <= x) where X ~ t(df).

Parameters:

  • x (Numeric)

    The t-value at which to calculate the cumulative probability

Returns:

  • (Float)

    The cumulative probability P(X <= x)



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/more_math/distributions.rb', line 185

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