Class: Statsample::Test::ChiSquare::WithMatrix
- Defined in:
- lib/statsample/test/chisquare.rb
Instance Attribute Summary collapse
-
#df ⇒ Object
readonly
Returns the value of attribute df.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #calculate_expected ⇒ Object
- #chi_square ⇒ Object
- #compute_chi ⇒ Object
-
#initialize(observed, expected = nil) ⇒ WithMatrix
constructor
A new instance of WithMatrix.
- #probability ⇒ Object
- #to_f ⇒ Object
Constructor Details
#initialize(observed, expected = nil) ⇒ WithMatrix
Returns a new instance of WithMatrix.
7 8 9 10 11 12 13 |
# 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 |
Instance Attribute Details
#df ⇒ Object (readonly)
Returns the value of attribute df.
5 6 7 |
# File 'lib/statsample/test/chisquare.rb', line 5 def df @df end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/statsample/test/chisquare.rb', line 6 def value @value end |
Instance Method Details
#calculate_expected ⇒ Object
14 15 16 17 18 19 20 21 |
# 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 ⇒ Object
25 26 27 |
# File 'lib/statsample/test/chisquare.rb', line 25 def chi_square @value end |
#compute_chi ⇒ Object
31 32 33 34 35 36 37 38 39 |
# 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 ⇒ Object
28 29 30 |
# File 'lib/statsample/test/chisquare.rb', line 28 def probability 1-Distribution::ChiSquare.cdf(@value.to_f,@df) end |
#to_f ⇒ Object
22 23 24 |
# File 'lib/statsample/test/chisquare.rb', line 22 def to_f @value end |