class Statistics::Distribution::TStudent

Attributes

degrees_of_freedom[RW]
mode[R]

Public Class Methods

new(v) click to toggle source
# File lib/statistics/distribution/t_student.rb, line 7
def initialize(v)
  self.degrees_of_freedom = v
  @mode = 0
end

Public Instance Methods

cumulative_function(value) click to toggle source

Extracted from codeplea.com/incomplete-beta-function-c This function is shared under zlib license and the author is Lewis Van Winkle

# File lib/statistics/distribution/t_student.rb, line 14
def cumulative_function(value)
  upper = (value + Math.sqrt(value * value + degrees_of_freedom))
  lower = (2.0 * Math.sqrt(value * value + degrees_of_freedom))

  x = upper/lower

  alpha = degrees_of_freedom/2.0
  beta = degrees_of_freedom/2.0

  Math.incomplete_beta_function(x, alpha, beta)
end
density_function(value) click to toggle source
# File lib/statistics/distribution/t_student.rb, line 26
def density_function(value)
  return if degrees_of_freedom <= 0

  upper = Math.gamma((degrees_of_freedom + 1)/2.0)
  lower = Math.sqrt(degrees_of_freedom * Math::PI) * Math.gamma(degrees_of_freedom/2.0)
  left = upper/lower
  right = (1 + ((value ** 2)/degrees_of_freedom.to_r)) ** -((degrees_of_freedom + 1)/2.0)

  left * right
end
mean() click to toggle source
# File lib/statistics/distribution/t_student.rb, line 37
def mean
  0 if degrees_of_freedom > 1
end
random(elements: 1, seed: Random.new_seed) click to toggle source

Quantile function extracted from www.jennessent.com/arcview/idf.htm TODO: Make it truly Student's T sample.

# File lib/statistics/distribution/t_student.rb, line 51
def random(elements: 1, seed: Random.new_seed)
  warn 'This is an alpha version code. The generated sample is similar to an uniform distribution'
  srand(seed)

  v = degrees_of_freedom
  results = []

  # Because the Quantile function of a student-t distribution is between (-Infinity, y)
  # we setup an small threshold in order to properly compute the integral
  threshold = 10_000.0e-12

  elements.times do
    y = rand
    results << Math.simpson_rule(threshold, y, 10_000) do |t|
      up = Math.gamma((v+1)/2.0)
      down = Math.sqrt(Math::PI * v) * Math.gamma(v/2.0)
      right = (1 + ((y ** 2)/v.to_r)) ** ((v+1)/2.0)
      left = up/down.to_r

      left * right
    end
  end

  if elements == 1
    results.first
  else
    results
  end
end
variance() click to toggle source
# File lib/statistics/distribution/t_student.rb, line 41
def variance
  if degrees_of_freedom > 1 && degrees_of_freedom <= 2
    Float::INFINITY
  elsif degrees_of_freedom > 2
    degrees_of_freedom/(degrees_of_freedom - 2.0)
  end
end