Module: Random::Testing
- Defined in:
- lib/random/testing.rb
Overview
Methods related to testing randomness
Class Method Summary collapse
-
.chi_square_statistic(binCounts) ⇒ Object
Calculate the chi square statistic for the counts for each bin given in binCounts (an Array of integers).
-
.chi_square_test(distribution, alpha = 0.01) ⇒ Object
Perform the Chi Square test at a given alpha level.
Class Method Details
.chi_square_statistic(binCounts) ⇒ Object
Calculate the chi square statistic for the counts for each bin given in binCounts (an Array of integers).
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/random/testing.rb', line 25 def self.chi_square_statistic(binCounts) num_bins = binCounts.length sum = binCounts.inject(0) {|s,e| s+e} expected = sum.to_f / num_bins # puts("Expected = #{expected}") binCounts.inject(0) do |chisq, observed| d = observed - expected chisq + ((d * d) / expected) end end |
.chi_square_test(distribution, alpha = 0.01) ⇒ Object
Perform the Chi Square test at a given alpha level. Returns true iff the given distribution can be assumed to come from a uniform distribution at an alpha level of confidence.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/random/testing.rb', line 10 def self.chi_square_test(distribution, alpha = 0.01) labels, counts = distribution.keys, distribution.values degrees_of_freedom = labels.length - 1 chisq = chi_square_statistic(counts) # puts("ChiSq statistic = %.4f" % chisq) # puts("Degrees of freedom = #{degrees_of_freedom}") critical_chi_square_value = Math::Statistics.critical_chi_square_value(degrees_of_freedom, alpha) # If the calculated chi-square statistic is less than the critical value # we accept the distribution as coming from a uniform process. chisq < critical_chi_square_value end |