Skip to content

Commit 8b5c8f7

Browse files
committed
Rewrite sample spec to use chi squared test.
1 parent 0f63df6 commit 8b5c8f7

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

spec/ruby/core/array/sample_spec.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@
66
ary = [0, 1, 2, 3]
77
3.times do |i|
88
counts = [0, 0, 0, 0]
9-
4000.times do
10-
counts[ary.sample(3)[i]] += 1
9+
iters = 4000
10+
expected = iters / counts.size
11+
iters.times do
12+
x = ary.sample(3)[i]
13+
counts[x] += 1
1114
end
15+
chi_squared = 0.0
1216
counts.each do |count|
13-
(800..1200).should include(count)
17+
chi_squared += (((count - expected) ** 2) * 1.0 / expected)
1418
end
19+
20+
# Chi squared critical values for tests with 4 degrees of freedom
21+
# Values obtained from NIST Engineering Statistic Handbook at
22+
# https://www.itl.nist.gov/div898/handbook/eda/section3/eda3674.htm
23+
24+
chi_squared.should <= 9.488
25+
chi_squared.should >= 0.711
1526
end
1627
end
1728

0 commit comments

Comments
 (0)