class Statsample::Test::ChiSquare::WithMatrix

Attributes

df[R]
value[R]

Public Class Methods

new(observed, expected=nil) click to toggle source
# File lib/statsample/test/chisquare.rb, line 7
def initialize(observed, expected=nil)
  @observed=observed
  @expected=expected or calculate_expected
  raise "Observed size!=expected size" if @observed.row_size!=@expected.row_size or @observed.column_size!=@expected.column_size
  @df=(@observed.row_size-1)*(@observed.column_size-1)
  @value=compute_chi
end

Public Instance Methods

calculate_expected() click to toggle source
# File lib/statsample/test/chisquare.rb, line 14
def calculate_expected
  sum=@observed.total_sum
  @expected=Matrix.rows( @observed.row_size.times.map {|i|
    @observed.column_size.times.map {|j|
      (@observed.row_sum[i].quo(sum) * @observed.column_sum[j].quo(sum))*sum
    }
  })          
end
chi_square() click to toggle source
# File lib/statsample/test/chisquare.rb, line 25
def chi_square
  @value
end
compute_chi() click to toggle source
# File lib/statsample/test/chisquare.rb, line 31
def compute_chi
    sum=0
    (0...@observed.row_size).each {|i|
      (0...@observed.column_size).each {|j|
      sum+=((@observed[i, j] - @expected[i,j])**2).quo(@expected[i,j])
      }
    }
    sum
end
probability() click to toggle source
# File lib/statsample/test/chisquare.rb, line 28
def probability
  1-Distribution::ChiSquare.cdf(@value.to_f,@df)
end
to_f() click to toggle source
# File lib/statsample/test/chisquare.rb, line 22
def to_f
  @value
end