Class: Statistics::StatisticalTest::ChiSquaredTest

Inherits:
Object
  • Object
show all
Defined in:
lib/statistics/statistical_test/chi_squared_test.rb

Class Method Summary collapse

Class Method Details

.chi_statistic(expected, observed) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/statistics/statistical_test/chi_squared_test.rb', line 4

def self.chi_statistic(expected, observed)
  # If the expected is a number, we asumme that all expected observations
  # has the same probability to occur, hence we expect to see the same number
  # of expected observations per each observed value
  statistic = if expected.is_a? Numeric
                observed.reduce(0) do |memo, observed_value|
                  up = (observed_value - expected) ** 2
                  memo += (up/expected.to_f)
                end
              else
                expected.each_with_index.reduce(0) do |memo, (expected_value, index)|
                  up = (observed[index] - expected_value) ** 2
                  memo += (up/expected_value.to_f)
                end
              end

    [statistic, observed.size - 1]
end

.goodness_of_fit(alpha, expected, observed) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/statistics/statistical_test/chi_squared_test.rb', line 23

def self.goodness_of_fit(alpha, expected, observed)
  chi_score, df = *self.chi_statistic(expected, observed) # Splat array result

  return if chi_score.nil? || df.nil?

  probability = Distribution::ChiSquared.new(df).cumulative_function(chi_score)
  p_value = 1 - probability

  # According to https://stats.stackexchange.com/questions/29158/do-you-reject-the-null-hypothesis-when-p-alpha-or-p-leq-alpha
  # We can assume that if p_value <= alpha, we can safely reject the null hypothesis, ie. accept the alternative hypothesis.
  { probability: probability,
    p_value: p_value,
    alpha: alpha,
    null: alpha < p_value,
    alternative: p_value <= alpha,
    confidence_level: 1 - alpha }
end