1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
module Statistics
module Distribution
class ChiSquared
attr_accessor :degrees_of_freedom
alias_method :mean, :degrees_of_freedom
def initialize(k)
self.degrees_of_freedom = k
end
def cumulative_function(value)
k = degrees_of_freedom/2.0
Math.lower_incomplete_gamma_function(k, value/2.0)/Math.gamma(k)
end
def density_function(value)
return 0 if value < 0
common = degrees_of_freedom/2.0
left_down = (2 ** common) * Math.gamma(common)
right = (value ** (common - 1)) * Math.exp(-(value/2.0))
(1.0/left_down) * right
end
def mode
[degrees_of_freedom - 2, 0].max
end
def variance
degrees_of_freedom * 2
end
end
end
end
|