Skip to content

Commit

Permalink
t-test: two-tail: Calculate p value properly. (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanz01 authored May 18, 2018
1 parent 3eecc14 commit 1c726ab
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/statistics/statistical_test/t_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,16 @@ def self.perform(alpha, tails, *args)
(sample_left_mean - sample_right_mean).abs/standard_error.to_f
end

probability = Distribution::TStudent.new(degrees_of_freedom).cumulative_function(t_score)
p_value = 1 - probability
p_value *= 2 if tails == :two_tail
t_distribution = Distribution::TStudent.new(degrees_of_freedom)
probability = t_distribution.cumulative_function(t_score)

# Steps grabbed from https://support.minitab.com/en-us/minitab/18/help-and-how-to/statistics/basic-statistics/supporting-topics/basics/manually-calculate-a-p-value/
# See https://github.com/estebanz01/ruby-statistics/issues/23
p_value = if tails == :two_tail
2 * (1 - t_distribution.cumulative_function(t_score.abs))
else
1 - probability
end

{ t_score: t_score,
probability: probability,
Expand Down

0 comments on commit 1c726ab

Please sign in to comment.