Class: Statsample::Test::ChiSquare::WithMatrix
- Includes:
- Shared
- Defined in:
- lib/statsample/test/chisquare.rb
Instance Attribute Summary
Attributes included from Shared
Instance Method Summary collapse
- #calculate_expected ⇒ Object
- #compute_chi ⇒ Object
-
#initialize(observed, expected = nil) ⇒ WithMatrix
constructor
A new instance of WithMatrix.
Methods included from Shared
#chi_square, #probability, #to_f
Constructor Details
#initialize(observed, expected = nil) ⇒ WithMatrix
Returns a new instance of 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_expected ⇒ Object
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_chi ⇒ Object
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 |