Class: Statsample::Test::ChiSquare::WithMatrix

Inherits:
Object
  • Object
show all
Includes:
Shared
Defined in:
lib/statsample/test/chisquare.rb

Instance Attribute Summary

Attributes included from Shared

#df, #value

Instance Method Summary collapse

Methods included from Shared

#chi_square, #probability, #to_f

Constructor Details

#initialize(observed, expected = nil) ⇒ WithMatrix


24
25
26
27
28
29
30
# File 'lib/statsample/test/chisquare.rb', line 24

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

Instance Method Details

#calculate_expectedObject


32
33
34
35
36
37
38
39
# File 'lib/statsample/test/chisquare.rb', line 32

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

#compute_chiObject


41
42
43
44
45
46
47
48
49
# File 'lib/statsample/test/chisquare.rb', line 41

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