statsample 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. data/History.txt +7 -0
  2. data/Manifest.txt +15 -9
  3. data/README.txt +6 -0
  4. data/Rakefile +8 -0
  5. data/{demo → examples}/correlation_matrix.rb +0 -0
  6. data/{demo/dominanceanalysis.rb → examples/dominance_analysis.rb} +0 -0
  7. data/{demo → examples}/dominance_analysis_bootstrap.rb +0 -0
  8. data/{demo → examples}/levene.rb +0 -0
  9. data/{demo → examples}/multiple_regression.rb +5 -3
  10. data/{demo → examples}/multivariate_correlation.rb +0 -0
  11. data/{demo → examples}/polychoric.rb +0 -0
  12. data/{demo → examples}/principal_axis.rb +0 -0
  13. data/examples/t_test.rb +11 -0
  14. data/{demo → examples}/tetrachoric.rb +0 -0
  15. data/lib/statistics2.rb +1 -1
  16. data/lib/statsample.rb +57 -6
  17. data/lib/statsample/bivariate/polychoric.rb +12 -25
  18. data/lib/statsample/bivariate/tetrachoric.rb +1 -3
  19. data/lib/statsample/converter/csv.rb +11 -12
  20. data/lib/statsample/dominanceanalysis/bootstrap.rb +2 -3
  21. data/lib/statsample/factor/principalaxis.rb +0 -2
  22. data/lib/statsample/factor/rotation.rb +6 -8
  23. data/lib/statsample/graph.rb +8 -0
  24. data/lib/statsample/graph/svggraph.rb +0 -4
  25. data/lib/statsample/regression/multiple/baseengine.rb +25 -28
  26. data/lib/statsample/regression/multiple/matrixengine.rb +30 -34
  27. data/lib/statsample/test.rb +36 -1
  28. data/lib/statsample/test/levene.rb +11 -7
  29. data/lib/statsample/test/t.rb +189 -0
  30. data/test/test_anova.rb +8 -10
  31. data/test/test_bivariate.rb +40 -37
  32. data/test/test_codification.rb +9 -13
  33. data/test/test_combination.rb +37 -39
  34. data/test/test_crosstab.rb +46 -48
  35. data/test/test_csv.rb +40 -45
  36. data/test/test_dataset.rb +150 -152
  37. data/test/test_distribution.rb +24 -21
  38. data/test/test_dominance_analysis.rb +10 -12
  39. data/test/test_factor.rb +95 -91
  40. data/test/test_ggobi.rb +30 -33
  41. data/test/test_gsl.rb +4 -4
  42. data/test/test_helpers.rb +26 -0
  43. data/test/test_histogram.rb +5 -6
  44. data/test/test_logit.rb +20 -21
  45. data/test/test_matrix.rb +47 -48
  46. data/test/test_mle.rb +130 -131
  47. data/test/test_multiset.rb +95 -96
  48. data/test/test_permutation.rb +35 -36
  49. data/test/test_promise_after.rb +39 -0
  50. data/test/test_regression.rb +49 -51
  51. data/test/test_reliability.rb +29 -30
  52. data/test/test_resample.rb +22 -23
  53. data/test/test_srs.rb +8 -9
  54. data/test/test_statistics.rb +12 -6
  55. data/test/test_stest.rb +18 -10
  56. data/test/test_stratified.rb +15 -16
  57. data/test/test_svg_graph.rb +11 -22
  58. data/test/test_test_t.rb +40 -0
  59. data/test/test_umannwhitney.rb +14 -15
  60. data/test/test_vector.rb +33 -37
  61. data/test/test_xls.rb +34 -41
  62. metadata +22 -11
@@ -1,15 +1,13 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'statsample'
3
- require 'test/unit'
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
4
2
 
5
- class StatsampleCSVTestCase < Test::Unit::TestCase
3
+ class StatsampleDominanceAnalysisTestCase < MiniTest::Unit::TestCase
6
4
  def aatest_dominance_univariate
7
5
  # Example from Budescu (1993)
8
6
  m=Matrix[[1, 0.683, 0.154, 0.460, 0.618],[0.683, 1, -0.050, 0.297, 0.461], [0.154, -0.050, 1, 0.006, 0.262],[0.460, 0.297, 0.006, 1, 0.507],[0.618, 0.461, 0.262, 0.507, 1]]
9
7
  m.extend Statsample::CovariateMatrix
10
8
  m.fields=%w{x1 x2 x3 x4 y}
11
9
  da=Statsample::DominanceAnalysis.new(m,'y')
12
-
10
+
13
11
  contr_x1={'x2'=>0.003, 'x3'=>0.028, 'x4'=>0.063}
14
12
  contr_x1.each do |k,v|
15
13
  assert_in_delta(v, da.models_data[['x1']].contributions[k], 0.001)
@@ -23,21 +21,21 @@ class StatsampleCSVTestCase < Test::Unit::TestCase
23
21
  assert_equal(expected_dominances[i], da.conditional_dominance_pairwise(a[0],a[1]))
24
22
  assert_equal(expected_g_dominances[i], da.general_dominance_pairwise(a[0],a[1]))
25
23
  end
26
- end
24
+ end
27
25
  def test_dominance_multivariate
28
26
  m=Matrix[[1.0, -0.19, -0.358, -0.343, 0.359, 0.257], [-0.19, 1.0, 0.26, 0.29, -0.11, -0.11], [-0.358, 0.26, 1.0, 0.54, -0.49, -0.23], [-0.343, 0.29, 0.54, 1.0, -0.22, -0.41], [0.359, -0.11, -0.49, -0.22, 1.0, 0.62], [0.257, -0.11, -0.23, -0.41, 0.62, 1]]
29
27
  m.extend Statsample::CovariateMatrix
30
28
  m.fields=%w{y1 y2 x1 x2 x3 x4}
31
29
  m2=m.submatrix(%w{y1 x1 x2 x3 x4})
32
-
33
-
30
+
31
+
34
32
  da=Statsample::DominanceAnalysis.new(m, ['y1','y2'], :cases=>683, :method_association=>:p2yx)
35
-
33
+
36
34
  contr_x1={'x2'=>0.027, 'x3'=>0.024, 'x4'=>0.017}
37
35
  contr_x1.each do |k,v|
38
36
  assert_in_delta(v, da.models_data[['x1']].contributions[k], 0.003)
39
37
  end
40
-
41
-
38
+
39
+
42
40
  end
43
- end
41
+ end
data/test/test_factor.rb CHANGED
@@ -1,98 +1,102 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'statsample'
3
- require 'test/unit'
4
- class StatsampleFactorTestCase < Test::Unit::TestCase
5
- # Tested with SPSS and R
6
- def test_pca
7
- if Statsample.has_gsl?
8
- require 'gsl'
9
- a=[2.5, 0.5, 2.2, 1.9, 3.1, 2.3, 2.0, 1.0, 1.5, 1.1].to_scale
10
- b=[2.4, 0.7, 2.9, 2.2, 3.0, 2.7, 1.6, 1.1, 1.6, 0.9].to_scale
11
- a.recode! {|c| c-a.mean}
12
- b.recode! {|c| c-b.mean}
13
- ds={'a'=>a,'b'=>b}.to_dataset
14
- cov_matrix=Statsample::Bivariate.covariance_matrix(ds)
15
- pca=Statsample::Factor::PCA.new(cov_matrix)
16
- expected_eigenvalues=[1.284, 0.0490]
17
- expected_eigenvalues.each_with_index{|ev,i|
18
- assert_in_delta(ev,pca.eigenvalues[i],0.001)
19
- }
20
- expected_communality=[0.590, 0.694]
21
- expected_communality.each_with_index{|ev,i|
22
- assert_in_delta(ev,pca.communalities[i],0.001)
23
- }
24
- expected_cm=[0.768, 0.833]
25
-
26
- obs=pca.component_matrix(1).column(0).to_a
27
- expected_cm.each_with_index{|ev,i|
28
- assert_in_delta(ev,obs[i],0.001)
29
- }
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
30
2
 
31
- expected_fm_1=GSL::Matrix[[0.677], [0.735]]
32
- expected_fm_2=GSL::Matrix[[0.677,0.735], [0.735, -0.677]]
33
- _test_matrix(expected_fm_1,pca.feature_vector(1))
34
- _test_matrix(expected_fm_2,pca.feature_vector(2))
35
- assert(pca.summary)
36
- else
37
- puts "PCA not tested. Requires GSL"
38
- end
3
+ class StatsampleFactorTestCase < MiniTest::Unit::TestCase
4
+ # Tested with SPSS and R
5
+ def test_pca
6
+ if Statsample.has_gsl?
7
+ require 'gsl'
8
+ a=[2.5, 0.5, 2.2, 1.9, 3.1, 2.3, 2.0, 1.0, 1.5, 1.1].to_scale
9
+ b=[2.4, 0.7, 2.9, 2.2, 3.0, 2.7, 1.6, 1.1, 1.6, 0.9].to_scale
10
+ a.recode! {|c| c-a.mean}
11
+ b.recode! {|c| c-b.mean}
12
+ ds={'a'=>a,'b'=>b}.to_dataset
13
+ cov_matrix=Statsample::Bivariate.covariance_matrix(ds)
14
+ pca=Statsample::Factor::PCA.new(cov_matrix)
15
+ expected_eigenvalues=[1.284, 0.0490]
16
+ expected_eigenvalues.each_with_index{|ev,i|
17
+ assert_in_delta(ev,pca.eigenvalues[i],0.001)
18
+ }
19
+ expected_communality=[0.590, 0.694]
20
+ expected_communality.each_with_index{|ev,i|
21
+ assert_in_delta(ev,pca.communalities[i],0.001)
22
+ }
23
+ expected_cm=[0.768, 0.833]
24
+
25
+ obs=pca.component_matrix(1).column(0).to_a
26
+ expected_cm.each_with_index{|ev,i|
27
+ assert_in_delta(ev,obs[i],0.001)
28
+ }
29
+
30
+ expected_fm_1=GSL::Matrix[[0.677], [0.735]]
31
+ expected_fm_2=GSL::Matrix[[0.677,0.735], [0.735, -0.677]]
32
+ _test_matrix(expected_fm_1,pca.feature_vector(1))
33
+ _test_matrix(expected_fm_2,pca.feature_vector(2))
34
+ assert(pca.summary)
35
+ else
36
+ puts "PCA not tested. Requires GSL"
39
37
  end
40
-
41
- # Tested with R
42
- def test_principalaxis
43
- if Statsample.has_gsl?
44
- require 'gsl'
45
- matrix=Matrix[
46
- [1.0, 0.709501601093587, 0.877596585880047, 0.272219316266807], [0.709501601093587, 1.0, 0.291633797330304, 0.871141831433844], [0.877596585880047, 0.291633797330304, 1.0, -0.213373722977167], [0.272219316266807, 0.871141831433844, -0.213373722977167, 1.0]]
47
- fa=Statsample::Factor::PrincipalAxis.new(matrix,:m=>1)
48
- cm=Matrix[[0.923],[0.912],[0.507],[0.483]].to_gsl
49
- _test_matrix(cm,fa.component_matrix)
50
- h2=[0.852,0.832,0.257,0.233]
51
- h2.each_with_index{|ev,i|
52
- assert_in_delta(ev,fa.communalities[i],0.001)
53
- }
54
- eigen1=2.175
55
- assert_in_delta(eigen1, fa.eigenvalues[0],0.001)
56
-
57
- fa=Statsample::Factor::PrincipalAxis.new(matrix,:smc=>false)
58
- assert_raise RuntimeError do
59
- fa.iterate
60
-
61
- end
62
-
63
-
64
-
65
- assert(fa.summary)
66
-
67
- else
68
- puts "Principal Axis not tested. Requires GSL"
38
+ end
39
+
40
+ # Tested with R
41
+ def test_principalaxis
42
+ if Statsample.has_gsl?
43
+ require 'gsl'
44
+ matrix=Matrix[
45
+ [1.0, 0.709501601093587, 0.877596585880047, 0.272219316266807], [0.709501601093587, 1.0, 0.291633797330304, 0.871141831433844], [0.877596585880047, 0.291633797330304, 1.0, -0.213373722977167], [0.272219316266807, 0.871141831433844, -0.213373722977167, 1.0]]
46
+ fa=Statsample::Factor::PrincipalAxis.new(matrix,:m=>1)
47
+
48
+ cm=Matrix[[0.923],[0.912],[0.507],[0.483]].to_gsl
49
+
50
+ _test_matrix(cm,fa.component_matrix)
51
+
52
+ h2=[0.852,0.832,0.257,0.233]
53
+ h2.each_with_index{|ev,i|
54
+ assert_in_delta(ev,fa.communalities[i],0.001)
55
+ }
56
+ eigen1=2.175
57
+ assert_in_delta(eigen1, fa.eigenvalues[0],0.001)
58
+
59
+ fa=Statsample::Factor::PrincipalAxis.new(matrix,:smc=>false)
60
+ assert_raise RuntimeError do
61
+ fa.iterate
62
+
69
63
  end
64
+
65
+
66
+
67
+ assert(fa.summary)
68
+
69
+ else
70
+ puts "Principal Axis not tested. Requires GSL"
70
71
  end
71
-
72
-
73
- def test_rotation_varimax
74
- if Statsample.has_gsl?
75
- a = Matrix[ [ 0.4320, 0.8129, 0.3872] ,
76
- [0.7950, -0.5416, 0.2565] ,
77
- [0.5944, 0.7234, -0.3441],
78
- [0.8945, -0.3921, -0.1863] ]
79
-
80
- expected= Matrix[[-0.0204423, 0.938674, -0.340334],
81
- [0.983662, 0.0730206, 0.134997],
82
- [0.0826106, 0.435975, -0.893379],
83
- [0.939901, -0.0965213, -0.309596]].to_gsl
84
- varimax=Statsample::Factor::Varimax.new(a)
85
- varimax.iterate
86
- _test_matrix(expected,varimax.rotated)
87
- else
88
- puts "Rotation not tested. Requires GSL"
89
- end
72
+ end
73
+
74
+
75
+ def test_rotation_varimax
76
+ if Statsample.has_gsl?
77
+ a = Matrix[ [ 0.4320, 0.8129, 0.3872] ,
78
+ [0.7950, -0.5416, 0.2565] ,
79
+ [0.5944, 0.7234, -0.3441],
80
+ [0.8945, -0.3921, -0.1863] ]
81
+
82
+ expected= Matrix[[-0.0204423, 0.938674, -0.340334],
83
+ [0.983662, 0.0730206, 0.134997],
84
+ [0.0826106, 0.435975, -0.893379],
85
+ [0.939901, -0.0965213, -0.309596]].to_gsl
86
+ varimax=Statsample::Factor::Varimax.new(a)
87
+ refute(varimax.rotated.nil?,"Rotated shouldn't be empty")
88
+ refute(varimax.component_transformation_matrix.nil?, "Component matrix shouldn't be empty")
89
+ refute(varimax.h2.nil?,"H2 shouldn't be empty")
90
+ _test_matrix(expected,varimax.rotated)
91
+ else
92
+ puts "Rotation not tested. Requires GSL"
90
93
  end
91
- def _test_matrix(a,b)
92
- a.size1.times {|i|
93
- a.size2.times {|j|
94
- assert_in_delta(a[i,j], b[i,j],0.001)
95
- }
94
+ end
95
+ def _test_matrix(a,b)
96
+ a.size1.times {|i|
97
+ a.size2.times {|j|
98
+ assert_in_delta(a[i,j], b[i,j],0.001)
96
99
  }
97
- end
100
+ }
101
+ end
98
102
  end
data/test/test_ggobi.rb CHANGED
@@ -1,27 +1,24 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'statsample'
3
- require 'test/unit'
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
2
+ require 'ostruct'
3
+ class StatsampleGGobiTestCase < MiniTest::Unit::TestCase
4
4
 
5
- class StatsampleGGobiTestCase < Test::Unit::TestCase
6
-
7
- def initialize(*args)
8
- super
9
- v1=([10.2,20.3,10,20,30,40,30,20,30,40]*10).to_vector(:scale)
10
- @v2=(%w{a b c a a a b b c d}*10).to_vector(:nominal)
11
- @v2.labels={"a"=>"letter a","d"=>"letter d"}
12
- v3=([1,2,3,4,5,4,3,2,1,2]*10).to_vector(:ordinal)
13
- @ds={'v1'=>v1,'v2'=>@v2,'v3'=>v3}.to_dataset
14
- end
15
- def test_values_definition
16
- a=[1.0,2,"a",nil]
17
- assert_equal("1.0 2 a NA", Statsample::GGobi.values_definition(a,"NA"))
18
- end
19
- def test_variable_definition
20
- carrier=OpenStruct.new
21
- carrier.categorials=[]
22
- carrier.conversions={}
23
- real_var_definition=Statsample::GGobi.variable_definition(carrier,@v2,'variable 2',"v2")
24
- expected=<<EOS
5
+ def setup
6
+ v1=([10.2,20.3,10,20,30,40,30,20,30,40]*10).to_vector(:scale)
7
+ @v2=(%w{a b c a a a b b c d}*10).to_vector(:nominal)
8
+ @v2.labels={"a"=>"letter a","d"=>"letter d"}
9
+ v3=([1,2,3,4,5,4,3,2,1,2]*10).to_vector(:ordinal)
10
+ @ds={'v1'=>v1,'v2'=>@v2,'v3'=>v3}.to_dataset
11
+ end
12
+ def test_values_definition
13
+ a=[1.0,2,"a",nil]
14
+ assert_equal("1.0 2 a NA", Statsample::GGobi.values_definition(a,"NA"))
15
+ end
16
+ def test_variable_definition
17
+ carrier=OpenStruct.new
18
+ carrier.categorials=[]
19
+ carrier.conversions={}
20
+ real_var_definition=Statsample::GGobi.variable_definition(carrier,@v2,'variable 2',"v2")
21
+ expected=<<-EOS
25
22
  <categoricalvariable name="variable 2" nickname="v2">
26
23
  <levels count="4">
27
24
  <level value="1">letter a</level>
@@ -29,14 +26,14 @@ class StatsampleGGobiTestCase < Test::Unit::TestCase
29
26
  <level value="3">c</level>
30
27
  <level value="4">letter d</level></levels>
31
28
  </categoricalvariable>
32
- EOS
33
- assert_equal(expected.gsub(/\s/," "),real_var_definition.gsub(/\s/," "))
34
- assert_equal({'variable 2'=>{'a'=>1,'b'=>2,'c'=>3,'d'=>4}},carrier.conversions)
35
- assert_equal(['variable 2'],carrier.categorials)
36
- end
37
- def test_out
38
- filename="/tmp/test_statsample_ggobi.xml"
39
- go=Statsample::GGobi.out(@ds)
40
-
41
- end
29
+ EOS
30
+ assert_equal(expected.gsub(/\s/," "),real_var_definition.gsub(/\s/," "))
31
+ assert_equal({'variable 2'=>{'a'=>1,'b'=>2,'c'=>3,'d'=>4}},carrier.conversions)
32
+ assert_equal(['variable 2'],carrier.categorials)
33
+ end
34
+ def test_out
35
+ filename="/tmp/test_statsample_ggobi.xml"
36
+ go=Statsample::GGobi.out(@ds)
37
+
38
+ end
42
39
  end
data/test/test_gsl.rb CHANGED
@@ -1,7 +1,7 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'statsample'
3
- require 'test/unit'
4
- class StatsampleGSLTestCase < Test::Unit::TestCase
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
2
+
3
+
4
+ class StatsampleGSLTestCase < MiniTest::Unit::TestCase
5
5
  def test_matrix_to_gsl
6
6
  if Statsample.has_gsl?
7
7
  a=[1,2,3,4,20].to_vector(:scale)
@@ -0,0 +1,26 @@
1
+ $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
+ require 'statsample'
3
+ require 'minitest/unit'
4
+ require 'tempfile'
5
+ require 'tmpdir'
6
+
7
+ module MiniTest::Assertions
8
+ alias :assert_raise :assert_raises unless method_defined? :assert_raise
9
+ alias :assert_not_equal :refute_equal unless method_defined? :assert_not_equal
10
+ alias :assert_not_same :refute_same unless method_defined? :assert_not_same
11
+ unless method_defined? :assert_nothing_raised
12
+ def assert_nothing_raised(msg=nil)
13
+ msg||="Nothing should be raised, but raised %s"
14
+ begin
15
+ yield
16
+ not_raised=true
17
+ rescue Exception => e
18
+ not_raised=false
19
+ msg=sprintf(msg,e)
20
+ end
21
+ assert(not_raised,msg)
22
+ end
23
+ end
24
+ end
25
+
26
+ MiniTest::Unit.autorun
@@ -1,9 +1,8 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'statsample'
3
- require 'tmpdir'
4
- require 'test/unit'
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
5
2
 
6
- class StatsampleHistogramTestCase < Test::Unit::TestCase
3
+
4
+
5
+ class StatsampleHistogramTestCase < MiniTest::Unit::TestCase
7
6
  def test_control
8
7
  h = Statsample::Histogram.alloc(4)
9
8
  assert_equal([0.0]*4, h.bin)
@@ -23,4 +22,4 @@ class StatsampleHistogramTestCase < Test::Unit::TestCase
23
22
  h.increment(5)
24
23
  assert_equal([3.0,0.0,1.0,2.0,0.0], h.bin)
25
24
  end
26
- end
25
+ end
data/test/test_logit.rb CHANGED
@@ -1,22 +1,21 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'statsample'
3
- require 'test/unit'
4
- class StatsampleLogitTestCase < Test::Unit::TestCase
5
- def test_logit_1
6
- crime=File.dirname(__FILE__)+'/../data/test_binomial.csv'
7
- ds=Statsample::CSV.read(crime)
8
- lr=Statsample::Regression::Binomial::Logit.new(ds,'y')
9
- assert_in_delta(-38.8669,lr.log_likehood,0.001)
10
- assert_in_delta(-5.3658,lr.constant,0.001)
11
-
12
- exp_coeffs={"a"=>0.3270,"b"=>0.8147, "c"=>-0.4031}
13
- exp_coeffs.each{|k,v|
14
- assert_in_delta(v,lr.coeffs[k],0.001)
15
- }
16
- exp_errors={'a'=>0.4390,'b'=>0.4270,'c'=>0.3819}
17
- exp_errors.each{|k,v|
18
- assert_in_delta(v,lr.coeffs_se[k],0.001)
19
- }
20
- assert_equal(7,lr.iterations)
21
- end
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
2
+
3
+ class StatsampleLogitTestCase < MiniTest::Unit::TestCase
4
+ def test_logit_1
5
+ crime=File.dirname(__FILE__)+'/../data/test_binomial.csv'
6
+ ds=Statsample::CSV.read(crime)
7
+ lr=Statsample::Regression::Binomial::Logit.new(ds,'y')
8
+ assert_in_delta(-38.8669,lr.log_likehood,0.001)
9
+ assert_in_delta(-5.3658,lr.constant,0.001)
10
+
11
+ exp_coeffs={"a"=>0.3270,"b"=>0.8147, "c"=>-0.4031}
12
+ exp_coeffs.each{|k,v|
13
+ assert_in_delta(v,lr.coeffs[k],0.001)
14
+ }
15
+ exp_errors={'a'=>0.4390,'b'=>0.4270,'c'=>0.3819}
16
+ exp_errors.each{|k,v|
17
+ assert_in_delta(v,lr.coeffs_se[k],0.001)
18
+ }
19
+ assert_equal(7,lr.iterations)
20
+ end
22
21
  end
data/test/test_matrix.rb CHANGED
@@ -1,51 +1,50 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'test/unit'
3
- require 'statsample'
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
4
2
 
5
- class StatsampleMatrixTestCase < Test::Unit::TestCase
6
-
7
- def setup
8
- end
9
- def test_subindex
10
- matrix=Matrix[[1,2,3],[4,5,6],[7,8,9]]
11
- assert_equal(5, matrix[1,1])
12
- assert_equal(Matrix[[1,2,3]], matrix[0,:*])
13
- assert_equal(Matrix[[1],[4],[7]], matrix[:*,0])
14
- assert_equal(Matrix[[1,2],[4,5],[7,8]], matrix[:*,0..1])
15
- assert_equal(Matrix[[1,2],[4,5]], matrix[0..1,0..1])
16
- end
17
- def test_sums
18
- matrix=Matrix[[1,2,3],[4,5,6],[7,8,9]]
19
- assert_equal(6,matrix.row_sum[0])
20
- assert_equal(12,matrix.column_sum[0])
21
- assert_equal(45,matrix.total_sum)
22
- end
23
- def test_covariate
24
- a=Matrix[[1.0, 0.3, 0.2], [0.3, 1.0, 0.5], [0.2, 0.5, 1.0]]
25
- a.extend Statsample::CovariateMatrix
26
- a.fields=%w{a b c}
27
- assert_equal(:correlation, a.type)
28
-
29
- assert_equal(Matrix[[0.5],[0.3]], a.submatrix(%w{c a}, %w{b}))
30
- assert_equal(Matrix[[1.0, 0.2] , [0.2, 1.0]], a.submatrix(%w{c a}))
31
- assert_equal(:correlation, a.submatrix(%w{c a}).type)
32
-
33
- a=Matrix[[20,30,10], [30,60,50], [10,50,50]]
34
-
35
- a.extend Statsample::CovariateMatrix
36
-
37
- assert_equal(:covariance, a.type)
38
-
39
- a=100.times.collect {rand()}.to_scale
40
- b=100.times.collect {rand()}.to_scale
41
- c=100.times.collect {rand()}.to_scale
42
- ds={'a'=>a,'b'=>b,'c'=>c}.to_dataset
43
- corr=Statsample::Bivariate.correlation_matrix(ds)
44
- real=Statsample::Bivariate.covariance_matrix(ds).correlation
45
- corr.row_size.times do |i|
46
- corr.column_size.times do |j|
47
- assert_in_delta(corr[i,j], real[i,j],1e-15)
48
- end
3
+
4
+ class StatsampleMatrixTestCase < MiniTest::Unit::TestCase
5
+
6
+ def setup
7
+ end
8
+ def test_subindex
9
+ matrix=Matrix[[1,2,3],[4,5,6],[7,8,9]]
10
+ assert_equal(5, matrix[1,1])
11
+ assert_equal(Matrix[[1,2,3]], matrix[0,:*])
12
+ assert_equal(Matrix[[1],[4],[7]], matrix[:*,0])
13
+ assert_equal(Matrix[[1,2],[4,5],[7,8]], matrix[:*,0..1])
14
+ assert_equal(Matrix[[1,2],[4,5]], matrix[0..1,0..1])
15
+ end
16
+ def test_sums
17
+ matrix=Matrix[[1,2,3],[4,5,6],[7,8,9]]
18
+ assert_equal(6,matrix.row_sum[0])
19
+ assert_equal(12,matrix.column_sum[0])
20
+ assert_equal(45,matrix.total_sum)
21
+ end
22
+ def test_covariate
23
+ a=Matrix[[1.0, 0.3, 0.2], [0.3, 1.0, 0.5], [0.2, 0.5, 1.0]]
24
+ a.extend Statsample::CovariateMatrix
25
+ a.fields=%w{a b c}
26
+ assert_equal(:correlation, a.type)
27
+
28
+ assert_equal(Matrix[[0.5],[0.3]], a.submatrix(%w{c a}, %w{b}))
29
+ assert_equal(Matrix[[1.0, 0.2] , [0.2, 1.0]], a.submatrix(%w{c a}))
30
+ assert_equal(:correlation, a.submatrix(%w{c a}).type)
31
+
32
+ a=Matrix[[20,30,10], [30,60,50], [10,50,50]]
33
+
34
+ a.extend Statsample::CovariateMatrix
35
+
36
+ assert_equal(:covariance, a.type)
37
+
38
+ a=100.times.collect {rand()}.to_scale
39
+ b=100.times.collect {rand()}.to_scale
40
+ c=100.times.collect {rand()}.to_scale
41
+ ds={'a'=>a,'b'=>b,'c'=>c}.to_dataset
42
+ corr=Statsample::Bivariate.correlation_matrix(ds)
43
+ real=Statsample::Bivariate.covariance_matrix(ds).correlation
44
+ corr.row_size.times do |i|
45
+ corr.column_size.times do |j|
46
+ assert_in_delta(corr[i,j], real[i,j],1e-15)
49
47
  end
50
48
  end
51
- end
49
+ end
50
+ end