statsample 0.17.0 → 0.18.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.
@@ -0,0 +1,200 @@
1
+ 214.80000 131.00000 131.10000 9.00000 9.70000 141.00000
2
+ 214.60000 129.70000 129.70000 8.10000 9.50000 141.70000
3
+ 214.80000 129.70000 129.70000 8.70000 9.60000 142.20000
4
+ 214.80000 129.70000 129.60000 7.50000 10.40000 142.00000
5
+ 215.00000 129.60000 129.70000 10.40000 7.70000 141.80000
6
+ 215.70000 130.80000 130.50000 9.00000 10.10000 141.40000
7
+ 215.50000 129.50000 129.70000 7.90000 9.60000 141.60000
8
+ 214.50000 129.60000 129.20000 7.20000 10.70000 141.70000
9
+ 214.90000 129.40000 129.70000 8.20000 11.00000 141.90000
10
+ 215.20000 130.40000 130.30000 9.20000 10.00000 140.70000
11
+ 215.30000 130.40000 130.30000 7.90000 11.70000 141.80000
12
+ 215.10000 129.50000 129.60000 7.70000 10.50000 142.20000
13
+ 215.20000 130.80000 129.60000 7.90000 10.80000 141.40000
14
+ 214.70000 129.70000 129.70000 7.70000 10.90000 141.70000
15
+ 215.10000 129.90000 129.70000 7.70000 10.80000 141.80000
16
+ 214.50000 129.80000 129.80000 9.30000 8.50000 141.60000
17
+ 214.60000 129.90000 130.10000 8.20000 9.80000 141.70000
18
+ 215.00000 129.90000 129.70000 9.00000 9.00000 141.90000
19
+ 215.20000 129.60000 129.60000 7.40000 11.50000 141.50000
20
+ 214.70000 130.20000 129.90000 8.60000 10.00000 141.90000
21
+ 215.00000 129.90000 129.30000 8.40000 10.00000 141.40000
22
+ 215.60000 130.50000 130.00000 8.10000 10.30000 141.60000
23
+ 215.30000 130.60000 130.00000 8.40000 10.80000 141.50000
24
+ 215.70000 130.20000 130.00000 8.70000 10.00000 141.60000
25
+ 215.10000 129.70000 129.90000 7.40000 10.80000 141.10000
26
+ 215.30000 130.40000 130.40000 8.00000 11.00000 142.30000
27
+ 215.50000 130.20000 130.10000 8.90000 9.80000 142.40000
28
+ 215.10000 130.30000 130.30000 9.80000 9.50000 141.90000
29
+ 215.10000 130.00000 130.00000 7.40000 10.50000 141.80000
30
+ 214.80000 129.70000 129.30000 8.30000 9.00000 142.00000
31
+ 215.20000 130.10000 129.80000 7.90000 10.70000 141.80000
32
+ 214.80000 129.70000 129.70000 8.60000 9.10000 142.30000
33
+ 215.00000 130.00000 129.60000 7.70000 10.50000 140.70000
34
+ 215.60000 130.40000 130.10000 8.40000 10.30000 141.00000
35
+ 215.90000 130.40000 130.00000 8.90000 10.60000 141.40000
36
+ 214.60000 130.20000 130.20000 9.40000 9.70000 141.80000
37
+ 215.50000 130.30000 130.00000 8.40000 9.70000 141.80000
38
+ 215.30000 129.90000 129.40000 7.90000 10.00000 142.00000
39
+ 215.30000 130.30000 130.10000 8.50000 9.30000 142.10000
40
+ 213.90000 130.30000 129.00000 8.10000 9.70000 141.30000
41
+ 214.40000 129.80000 129.20000 8.90000 9.40000 142.30000
42
+ 214.80000 130.10000 129.60000 8.80000 9.90000 140.90000
43
+ 214.90000 129.60000 129.40000 9.30000 9.00000 141.70000
44
+ 214.90000 130.40000 129.70000 9.00000 9.80000 140.90000
45
+ 214.80000 129.40000 129.10000 8.20000 10.20000 141.00000
46
+ 214.30000 129.50000 129.40000 8.30000 10.20000 141.80000
47
+ 214.80000 129.90000 129.70000 8.30000 10.20000 141.50000
48
+ 214.80000 129.90000 129.70000 7.30000 10.90000 142.00000
49
+ 214.60000 129.70000 129.80000 7.90000 10.30000 141.10000
50
+ 214.50000 129.00000 129.60000 7.80000 9.80000 142.00000
51
+ 214.60000 129.80000 129.40000 7.20000 10.00000 141.30000
52
+ 215.30000 130.60000 130.00000 9.50000 9.70000 141.10000
53
+ 214.50000 130.10000 130.00000 7.80000 10.90000 140.90000
54
+ 215.40000 130.20000 130.20000 7.60000 10.90000 141.60000
55
+ 214.50000 129.40000 129.50000 7.90000 10.00000 141.40000
56
+ 215.20000 129.70000 129.40000 9.20000 9.40000 142.00000
57
+ 215.70000 130.00000 129.40000 9.20000 10.40000 141.20000
58
+ 215.00000 129.60000 129.40000 8.80000 9.00000 141.10000
59
+ 215.10000 130.10000 129.90000 7.90000 11.00000 141.30000
60
+ 215.10000 130.00000 129.80000 8.20000 10.30000 141.40000
61
+ 215.10000 129.60000 129.30000 8.30000 9.90000 141.60000
62
+ 215.30000 129.70000 129.40000 7.50000 10.50000 141.50000
63
+ 215.40000 129.80000 129.40000 8.00000 10.60000 141.50000
64
+ 214.50000 130.00000 129.50000 8.00000 10.80000 141.40000
65
+ 215.00000 130.00000 129.80000 8.60000 10.60000 141.50000
66
+ 215.20000 130.60000 130.00000 8.80000 10.60000 140.80000
67
+ 214.60000 129.50000 129.20000 7.70000 10.30000 141.30000
68
+ 214.80000 129.70000 129.30000 9.10000 9.50000 141.50000
69
+ 215.10000 129.60000 129.80000 8.60000 9.80000 141.80000
70
+ 214.90000 130.20000 130.20000 8.00000 11.20000 139.60000
71
+ 213.80000 129.80000 129.50000 8.40000 11.10000 140.90000
72
+ 215.20000 129.90000 129.50000 8.20000 10.30000 141.40000
73
+ 215.00000 129.60000 130.20000 8.70000 10.00000 141.20000
74
+ 214.40000 129.90000 129.60000 7.50000 10.50000 141.80000
75
+ 215.20000 129.90000 129.70000 7.20000 10.60000 142.10000
76
+ 214.10000 129.60000 129.30000 7.60000 10.70000 141.70000
77
+ 214.90000 129.90000 130.10000 8.80000 10.00000 141.20000
78
+ 214.60000 129.80000 129.40000 7.40000 10.60000 141.00000
79
+ 215.20000 130.50000 129.80000 7.90000 10.90000 140.90000
80
+ 214.60000 129.90000 129.40000 7.90000 10.00000 141.80000
81
+ 215.10000 129.70000 129.70000 8.60000 10.30000 140.60000
82
+ 214.90000 129.80000 129.60000 7.50000 10.30000 141.00000
83
+ 215.20000 129.70000 129.10000 9.00000 9.70000 141.90000
84
+ 215.20000 130.10000 129.90000 7.90000 10.80000 141.30000
85
+ 215.40000 130.70000 130.20000 9.00000 11.10000 141.20000
86
+ 215.10000 129.90000 129.60000 8.90000 10.20000 141.50000
87
+ 215.20000 129.90000 129.70000 8.70000 9.50000 141.60000
88
+ 215.00000 129.60000 129.20000 8.40000 10.20000 142.10000
89
+ 214.90000 130.30000 129.90000 7.40000 11.20000 141.50000
90
+ 215.00000 129.90000 129.70000 8.00000 10.50000 142.00000
91
+ 214.70000 129.70000 129.30000 8.60000 9.60000 141.60000
92
+ 215.40000 130.00000 129.90000 8.50000 9.70000 141.40000
93
+ 214.90000 129.40000 129.50000 8.20000 9.90000 141.50000
94
+ 214.50000 129.50000 129.30000 7.40000 10.70000 141.50000
95
+ 214.70000 129.60000 129.50000 8.30000 10.00000 142.00000
96
+ 215.60000 129.90000 129.90000 9.00000 9.50000 141.70000
97
+ 215.00000 130.40000 130.30000 9.10000 10.20000 141.10000
98
+ 214.40000 129.70000 129.50000 8.00000 10.30000 141.20000
99
+ 215.10000 130.00000 129.80000 9.10000 10.20000 141.50000
100
+ 214.70000 130.00000 129.40000 7.80000 10.00000 141.20000
101
+ 214.40000 130.10000 130.30000 9.70000 11.70000 139.80000
102
+ 214.90000 130.50000 130.20000 11.00000 11.50000 139.50000
103
+ 214.90000 130.30000 130.10000 8.70000 11.70000 140.20000
104
+ 215.00000 130.40000 130.60000 9.90000 10.90000 140.30000
105
+ 214.70000 130.20000 130.30000 11.80000 10.90000 139.70000
106
+ 215.00000 130.20000 130.20000 10.60000 10.70000 139.90000
107
+ 215.30000 130.30000 130.10000 9.30000 12.10000 140.20000
108
+ 214.80000 130.10000 130.40000 9.80000 11.50000 139.90000
109
+ 215.00000 130.20000 129.90000 10.00000 11.90000 139.40000
110
+ 215.20000 130.60000 130.80000 10.40000 11.20000 140.30000
111
+ 215.20000 130.40000 130.30000 8.00000 11.50000 139.20000
112
+ 215.10000 130.50000 130.30000 10.60000 11.50000 140.10000
113
+ 215.40000 130.70000 131.10000 9.70000 11.80000 140.60000
114
+ 214.90000 130.40000 129.90000 11.40000 11.00000 139.90000
115
+ 215.10000 130.30000 130.00000 10.60000 10.80000 139.70000
116
+ 215.50000 130.40000 130.00000 8.20000 11.20000 139.20000
117
+ 214.70000 130.60000 130.10000 11.80000 10.50000 139.80000
118
+ 214.70000 130.40000 130.10000 12.10000 10.40000 139.90000
119
+ 214.80000 130.50000 130.20000 11.00000 11.00000 140.00000
120
+ 214.40000 130.20000 129.90000 10.10000 12.00000 139.20000
121
+ 214.80000 130.30000 130.40000 10.10000 12.10000 139.60000
122
+ 215.10000 130.60000 130.30000 12.30000 10.20000 139.60000
123
+ 215.30000 130.80000 131.10000 11.60000 10.60000 140.20000
124
+ 215.10000 130.70000 130.40000 10.50000 11.20000 139.70000
125
+ 214.70000 130.50000 130.50000 9.90000 10.30000 140.10000
126
+ 214.90000 130.00000 130.30000 10.20000 11.40000 139.60000
127
+ 215.00000 130.40000 130.40000 9.40000 11.60000 140.20000
128
+ 215.50000 130.70000 130.30000 10.20000 11.80000 140.00000
129
+ 215.10000 130.20000 130.20000 10.10000 11.30000 140.30000
130
+ 214.50000 130.20000 130.60000 9.80000 12.10000 139.90000
131
+ 214.30000 130.20000 130.00000 10.70000 10.50000 139.80000
132
+ 214.50000 130.20000 129.80000 12.30000 11.20000 139.20000
133
+ 214.90000 130.50000 130.20000 10.60000 11.50000 139.90000
134
+ 214.60000 130.20000 130.40000 10.50000 11.80000 139.70000
135
+ 214.20000 130.00000 130.20000 11.00000 11.20000 139.50000
136
+ 214.80000 130.10000 130.10000 11.90000 11.10000 139.50000
137
+ 214.60000 129.80000 130.20000 10.70000 11.10000 139.40000
138
+ 214.90000 130.70000 130.30000 9.30000 11.20000 138.30000
139
+ 214.60000 130.40000 130.40000 11.30000 10.80000 139.80000
140
+ 214.50000 130.50000 130.20000 11.80000 10.20000 139.60000
141
+ 214.80000 130.20000 130.30000 10.00000 11.90000 139.30000
142
+ 214.70000 130.00000 129.40000 10.20000 11.00000 139.20000
143
+ 214.60000 130.20000 130.40000 11.20000 10.70000 139.90000
144
+ 215.00000 130.50000 130.40000 10.60000 11.10000 139.90000
145
+ 214.50000 129.80000 129.80000 11.40000 10.00000 139.30000
146
+ 214.90000 130.60000 130.40000 11.90000 10.50000 139.80000
147
+ 215.00000 130.50000 130.40000 11.40000 10.70000 139.90000
148
+ 215.30000 130.60000 130.30000 9.30000 11.30000 138.10000
149
+ 214.70000 130.20000 130.10000 10.70000 11.00000 139.40000
150
+ 214.90000 129.90000 130.00000 9.90000 12.30000 139.40000
151
+ 214.90000 130.30000 129.90000 11.90000 10.60000 139.80000
152
+ 214.60000 129.90000 129.70000 11.90000 10.10000 139.00000
153
+ 214.60000 129.70000 129.30000 10.40000 11.00000 139.30000
154
+ 214.50000 130.10000 130.10000 12.10000 10.30000 139.40000
155
+ 214.50000 130.30000 130.00000 11.00000 11.50000 139.50000
156
+ 215.10000 130.00000 130.30000 11.60000 10.50000 139.70000
157
+ 214.20000 129.70000 129.60000 10.30000 11.40000 139.50000
158
+ 214.40000 130.10000 130.00000 11.30000 10.70000 139.20000
159
+ 214.80000 130.40000 130.60000 12.50000 10.00000 139.30000
160
+ 214.60000 130.60000 130.10000 8.10000 12.10000 137.90000
161
+ 215.60000 130.10000 129.70000 7.40000 12.20000 138.40000
162
+ 214.90000 130.50000 130.10000 9.90000 10.20000 138.10000
163
+ 214.60000 130.10000 130.00000 11.50000 10.60000 139.50000
164
+ 214.70000 130.10000 130.20000 11.60000 10.90000 139.10000
165
+ 214.30000 130.30000 130.00000 11.40000 10.50000 139.80000
166
+ 215.10000 130.30000 130.60000 10.30000 12.00000 139.70000
167
+ 216.30000 130.70000 130.40000 10.00000 10.10000 138.80000
168
+ 215.60000 130.40000 130.10000 9.60000 11.20000 138.60000
169
+ 214.80000 129.90000 129.80000 9.60000 12.00000 139.60000
170
+ 214.90000 130.00000 129.90000 11.40000 10.90000 139.70000
171
+ 213.90000 130.70000 130.50000 8.70000 11.50000 137.80000
172
+ 214.20000 130.60000 130.40000 12.00000 10.20000 139.60000
173
+ 214.80000 130.50000 130.30000 11.80000 10.50000 139.40000
174
+ 214.80000 129.60000 130.00000 10.40000 11.60000 139.20000
175
+ 214.80000 130.10000 130.00000 11.40000 10.50000 139.60000
176
+ 214.90000 130.40000 130.20000 11.90000 10.70000 139.00000
177
+ 214.30000 130.10000 130.10000 11.60000 10.50000 139.70000
178
+ 214.50000 130.40000 130.00000 9.90000 12.00000 139.60000
179
+ 214.80000 130.50000 130.30000 10.20000 12.10000 139.10000
180
+ 214.50000 130.20000 130.40000 8.20000 11.80000 137.80000
181
+ 215.00000 130.40000 130.10000 11.40000 10.70000 139.10000
182
+ 214.80000 130.60000 130.60000 8.00000 11.40000 138.70000
183
+ 215.00000 130.50000 130.10000 11.00000 11.40000 139.30000
184
+ 214.60000 130.50000 130.40000 10.10000 11.40000 139.30000
185
+ 214.70000 130.20000 130.10000 10.70000 11.10000 139.50000
186
+ 214.70000 130.40000 130.00000 11.50000 10.70000 139.40000
187
+ 214.50000 130.40000 130.00000 8.00000 12.20000 138.50000
188
+ 214.80000 130.00000 129.70000 11.40000 10.60000 139.20000
189
+ 214.80000 129.90000 130.20000 9.60000 11.90000 139.40000
190
+ 214.60000 130.30000 130.20000 12.70000 9.10000 139.20000
191
+ 215.10000 130.20000 129.80000 10.20000 12.00000 139.40000
192
+ 215.40000 130.50000 130.60000 8.80000 11.00000 138.60000
193
+ 214.70000 130.30000 130.20000 10.80000 11.10000 139.20000
194
+ 215.00000 130.50000 130.30000 9.60000 11.00000 138.50000
195
+ 214.90000 130.30000 130.50000 11.60000 10.60000 139.80000
196
+ 215.00000 130.40000 130.30000 9.90000 12.10000 139.60000
197
+ 215.10000 130.30000 129.90000 10.30000 11.50000 139.70000
198
+ 214.80000 130.30000 130.40000 10.60000 11.10000 140.00000
199
+ 214.70000 130.70000 130.80000 11.20000 11.20000 139.40000
200
+ 214.30000 129.90000 129.90000 10.20000 11.50000 139.60000
@@ -1,13 +1,16 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- $:.unshift(File.dirname(__FILE__)+'/')
1
+ $:.unshift(File.expand_path(File.dirname(__FILE__)+'/../lib/'))
2
+ $:.unshift(File.expand_path(File.dirname(__FILE__)+'/'))
3
+
3
4
 
4
- require 'statsample'
5
5
  require 'minitest/unit'
6
6
  require 'tempfile'
7
7
  require 'tmpdir'
8
8
  require 'shoulda'
9
9
  require 'fixtures/correlation_matrix'
10
10
 
11
+ require 'statsample'
12
+
13
+
11
14
  module MiniTest
12
15
  class Unit
13
16
  class TestCase
@@ -19,6 +22,22 @@ module MiniTest
19
22
  end
20
23
 
21
24
  module Assertions
25
+ def assert_similar_vector(exp, obs, delta=1e-10,msg=nil)
26
+ msg||="Different vectors #{exp} - #{obs}"
27
+ assert_equal(exp.size, obs.size)
28
+ exp.data_with_nils.each_with_index {|v,i|
29
+ assert_in_delta(v,obs[i],delta)
30
+ }
31
+ end
32
+ def assert_equal_matrix(exp,obs,delta=1e-10,msg=nil)
33
+ assert_equal(exp.row_size, obs.row_size, "Different row size.#{msg}")
34
+ assert_equal(exp.column_size, obs.column_size, "Different column size.#{msg}")
35
+ exp.row_size.times {|i|
36
+ exp.column_size.times {|j|
37
+ assert_in_delta(exp[i,j],obs[i,j], delta, "Different element #{i},#{j}\nExpected:\n#{exp}\nObserved:\n#{obs}.#{msg}")
38
+ }
39
+ }
40
+ end
22
41
  alias :assert_raise :assert_raises unless method_defined? :assert_raise
23
42
  alias :assert_not_equal :refute_equal unless method_defined? :assert_not_equal
24
43
  alias :assert_not_same :refute_same unless method_defined? :assert_not_same
data/test/test_factor.rb CHANGED
@@ -1,7 +1,114 @@
1
1
  require(File.expand_path(File.dirname(__FILE__)+'/helpers_tests.rb'))
2
+ #require 'rserve'
3
+ #require 'statsample/rserve_extension'
2
4
 
3
5
  class StatsampleFactorTestCase < MiniTest::Unit::TestCase
4
6
  include Statsample::Fixtures
7
+ # Based on Hardle and Simar
8
+ def setup
9
+ @fixtures_dir=File.expand_path(File.dirname(__FILE__)+"/fixtures")
10
+ end
11
+ # Based on Hurdle example
12
+ def test_covariance_matrix
13
+ ds=Statsample::PlainText.read(@fixtures_dir+"/bank2.dat", %w{v1 v2 v3 v4 v5 v6})
14
+ ds.fields.each {|f|
15
+ ds[f]=ds[f].centered
16
+ }
17
+ cm=ds.covariance_matrix
18
+ pca =Statsample::Factor::PCA.new( cm, :m=>6)
19
+ #puts pca.summary
20
+ #puts pca.feature_matrix
21
+ exp_eig=[2.985, 0.931,0.242, 0.194, 0.085, 0.035].to_scale
22
+ assert_similar_vector(exp_eig, pca.eigenvalues.to_scale, 0.1)
23
+ pcs=pca.principal_components(ds)
24
+ k=6
25
+ comp_matrix=pca.component_matrix()
26
+ k.times {|i|
27
+ pc_id="PC_#{i+1}"
28
+ k.times {|j| # variable
29
+ ds_id="v#{j+1}"
30
+ r= Statsample::Bivariate.correlation(ds[ds_id], pcs[pc_id])
31
+ assert_in_delta( r, comp_matrix[j,i])
32
+ }
33
+ }
34
+
35
+ end
36
+ def test_principalcomponents_ruby_gsl
37
+
38
+ ran=Distribution::Normal.rng_ugaussian
39
+
40
+ # @r=::Rserve::Connection.new
41
+
42
+ samples=20
43
+ (3..7).each {|k|
44
+ v={}
45
+ v["x0"]=samples.times.map { ran.call()}.to_scale.centered
46
+ (1...k).each {|i|
47
+ v["x#{i}"]=samples.times.map {|ii| ran.call()*0.5+v["x#{i-1}"][ii]*0.5}.to_scale.centered
48
+ }
49
+ ds=v.to_dataset
50
+ cm=ds.covariance_matrix
51
+ # @r.assign('ds',ds)
52
+ # @r.eval('cm<-cor(ds);sm<-eigen(cm, sym=TRUE);v<-sm$vectors')
53
+ # puts "eigenvalues"
54
+ # puts @r.eval('v').to_ruby.to_s
55
+ pca_ruby=Statsample::Factor::PCA.new( cm, :m=>k, :use_gsl=>false )
56
+ pca_gsl =Statsample::Factor::PCA.new( cm, :m=>k, :use_gsl=>true )
57
+ pc_ruby = pca_ruby.principal_components(ds)
58
+ pc_gsl = pca_gsl.principal_components(ds)
59
+ # Test component matrix correlation!
60
+ cm_ruby=pca_ruby.component_matrix
61
+ #puts cm_ruby.summary
62
+ k.times {|i|
63
+ pc_id="PC_#{i+1}"
64
+ assert_in_delta(pca_ruby.eigenvalues[i], pca_gsl.eigenvalues[i],1e-10)
65
+ # Revert gsl component values
66
+ pc_gsl_data= (pc_gsl[pc_id][0]-pc_ruby[pc_id][0]).abs>1e-6 ? pc_gsl[pc_id].recode {|v| -v} : pc_gsl[pc_id]
67
+ assert_similar_vector(pc_gsl_data, pc_ruby[pc_id], 1e-6,"PC for #{k} variables")
68
+ if false
69
+ k.times {|j| # variable
70
+ ds_id="x#{j}"
71
+ r= Statsample::Bivariate.correlation(ds[ds_id],pc_ruby[pc_id])
72
+ puts "#{pc_id}-#{ds_id}:#{r}"
73
+ }
74
+ end
75
+ }
76
+ }
77
+ #@r.close
78
+ end
79
+ def test_principalcomponents()
80
+ principalcomponents(true)
81
+ principalcomponents(false)
82
+
83
+ end
84
+ def principalcomponents(gsl)
85
+ ran=Distribution::Normal.rng_ugaussian
86
+ samples=50
87
+ x1=samples.times.map { ran.call()}.to_scale
88
+ x2=samples.times.map {|i| ran.call()*0.5+x1[i]*0.5}.to_scale
89
+ ds={'x1'=>x1,'x2'=>x2}.to_dataset
90
+
91
+ cm=ds.correlation_matrix
92
+ r=cm[0,1]
93
+ pca=Statsample::Factor::PCA.new(cm,:m=>2,:use_gsl=>gsl)
94
+ assert_in_delta(1+r,pca.eigenvalues[0],1e-10)
95
+ assert_in_delta(1-r,pca.eigenvalues[1],1e-10)
96
+ hs=1.0 / Math.sqrt(2)
97
+ assert_equal_matrix(hs*Matrix[[1],[1]],pca.eigenvectors[0])
98
+ m_1=gsl ? Matrix[[-1],[1]] : Matrix[[1],[-1]]
99
+ assert_equal_matrix(hs*m_1, pca.eigenvectors[1])
100
+
101
+ pcs=pca.principal_components(ds)
102
+ exp_pc_1=ds.collect_with_index {|row,i|
103
+ hs*(row['x1']+row['x2'])
104
+ }
105
+ exp_pc_2=ds.collect_with_index {|row,i|
106
+ gsl ? hs*(row['x2']-row['x1']) : hs*(row['x1']-row['x2'])
107
+
108
+ }
109
+ assert_similar_vector(exp_pc_1, pcs["PC_1"])
110
+ assert_similar_vector(exp_pc_2, pcs["PC_2"])
111
+ end
5
112
  def test_antiimage
6
113
  cor=Matrix[[1,0.964, 0.312],[0.964,1,0.411],[0.312,0.411,1]]
7
114
  expected=Matrix[[0.062,-0.057, 0.074],[-0.057, 0.057, -0.089], [0.074, -0.089, 0.729]]
@@ -100,14 +207,14 @@ class StatsampleFactorTestCase < MiniTest::Unit::TestCase
100
207
  cov_matrix=Statsample::Bivariate.covariance_matrix(ds)
101
208
  if Statsample.has_gsl?
102
209
  pca=Statsample::Factor::PCA.new(cov_matrix,:use_gsl=>true)
103
- pca_set(pca)
210
+ pca_set(pca,"gsl")
104
211
  else
105
212
  skip("Eigenvalues could be calculated with GSL (requires gsl)")
106
213
  end
107
214
  pca=Statsample::Factor::PCA.new(cov_matrix,:use_gsl=>false)
108
- pca_set(pca)
215
+ pca_set(pca,"ruby")
109
216
  end
110
- def pca_set(pca)
217
+ def pca_set(pca,type)
111
218
  expected_eigenvalues=[1.284, 0.0490]
112
219
  expected_eigenvalues.each_with_index{|ev,i|
113
220
  assert_in_delta(ev,pca.eigenvalues[i],0.001)
@@ -117,15 +224,11 @@ class StatsampleFactorTestCase < MiniTest::Unit::TestCase
117
224
  assert_in_delta(ev,pca.communalities[i],0.001)
118
225
  }
119
226
  expected_cm=[0.768, 0.833]
120
- obs=pca.component_matrix(1).column(0).to_a
227
+ obs=pca.component_matrix_correlation(1).column(0).to_a
121
228
  expected_cm.each_with_index{|ev,i|
122
229
  assert_in_delta(ev,obs[i],0.001)
123
230
  }
124
231
 
125
- expected_fm_1=::Matrix[[0.677], [0.735]]
126
- expected_fm_2=::Matrix[[0.677,0.735], [0.735, -0.677]]
127
- _test_matrix(expected_fm_1,pca.feature_vector(1))
128
- _test_matrix(expected_fm_2,pca.feature_vector(2))
129
232
  assert(pca.summary)
130
233
  end
131
234
 
@@ -139,7 +242,7 @@ class StatsampleFactorTestCase < MiniTest::Unit::TestCase
139
242
 
140
243
  cm=::Matrix[[0.923],[0.912],[0.507],[0.483]]
141
244
 
142
- _test_matrix(cm,fa.component_matrix)
245
+ assert_equal_matrix(cm,fa.component_matrix,0.001)
143
246
 
144
247
  h2=[0.852,0.832,0.257,0.233]
145
248
  h2.each_with_index{|ev,i|
@@ -172,14 +275,9 @@ class StatsampleFactorTestCase < MiniTest::Unit::TestCase
172
275
  assert(!varimax.component_transformation_matrix.nil?, "Component matrix shouldn't be empty")
173
276
  assert(!varimax.h2.nil?, "H2 shouldn't be empty")
174
277
 
175
- _test_matrix(expected,varimax.rotated)
278
+ assert_equal_matrix(expected,varimax.rotated,1e-6)
176
279
  assert(varimax.summary.size>0)
177
280
  end
178
- def _test_matrix(a,b)
179
- a.row_size.times {|i|
180
- a.column_size.times {|j|
181
- assert_in_delta(a[i,j], b[i,j],0.001)
182
- }
183
- }
184
- end
281
+
282
+
185
283
  end
@@ -76,7 +76,31 @@ class StatsampleHistogramTestCase < MiniTest::Unit::TestCase
76
76
  }
77
77
  assert_equal(min,h.min_val)
78
78
  end
79
-
79
+ should "return correct estimated mean" do
80
+ a=[1.5,1.5,1.5,3.5,3.5,3.5].to_scale
81
+ h=Statsample::Histogram.alloc(5,[0,5])
82
+ h.increment(a)
83
+ assert_equal(2.5, h.estimated_mean)
84
+ end
85
+ should "return correct estimated standard deviation" do
86
+ a=[0.5,1.5,1.5,1.5,2.5, 3.5,3.5,3.5,4.5].to_scale
87
+ h=Statsample::Histogram.alloc(5,[0,5])
88
+ h.increment(a)
89
+ assert_equal(a.sd, h.estimated_standard_deviation)
90
+ end
91
+ should "return correct sum for all values" do
92
+ h=Statsample::Histogram.alloc(5,[0,5])
93
+ n=rand(100)
94
+ n.times { h.increment(1)}
95
+ assert_equal(n, h.sum)
96
+ end
97
+ should "return correct sum for a subset of values" do
98
+ h=Statsample::Histogram.alloc(5,[0,5])
99
+ h.increment([0.5,2.5,4.5])
100
+ assert_equal(1,h.sum(0,1))
101
+ assert_equal(2,h.sum(1,4))
102
+
103
+ end
80
104
 
81
105
  end
82
106
  end