Skip to content

Student's T test

Esteban Zapata Rojas edited this page Apr 17, 2018 · 4 revisions

Student's T test

Basic One/Two sample test.

It Performs a T-Test for one or two samples. It can be one-tailored or two-tailored test.

  • For one-tailored test, specify :one_tail as second parameter.
  • For two-tailored test, specify :two_tail as second parameter.

In the following example, the test suggest to accept the null hypothesis and reject the alternative one.

# One sample test.
[138] pry(main)> StatisticalTest::TTest.perform(alpha = 0.05, tail = :two_tail, comparison_mean = 3, group_to_compare = [1, 2, 3])
=> {:t_score=>-1.732050807568877, :probability=>0.09084505690827795, :p_value=>1.8183098861834441, :alpha=>0.05, :null=>true, :alternative=>false, :confidence_level=>0.95}

If the specified group has a standard deviation of 0.0, it will raise a ZeroStdError asking you to reconsider the samples.

StatisticalTest::TTest.perform(alpha = 0.0, tail = :two_tail, comparison_mean = 1.0, group_to_compare = [1.0, 1.0, 1.0])
Traceback (most recent call last):
        1: from ruby-statistics/lib/statistics/statistical_test/t_test.rb:21:in `perform'
Statistics::StatisticalTest::TTest::ZeroStdError (Standard deviation for the difference or group is zero. Please, reconsider sample contents)

In the following example, the test suggest to accept the null hypothesis and reject the alternative one.

# Two samples test
[139] pry(main)> StatisticalTest::TTest.perform(alpha = 0.05, tail = :one_tail, group_one = [1, 2, 3], group_two = [4, 5, 6])
=> {:t_score=>3.6742346141747673, :probability=>0.9893441794356217, :p_value=>0.010655820564378304, :alpha=>0.05, :null=>false, :alternative=>true, :confidence_level=>0.95}

Paired T-test

It performs a one or two tailored paired T-test. It returns a hash with the following keys:

  • t_score: it calculates the T score using the Student's T distribution cumulative function.
  • probability: it calculates the probability of the t statistic, using the Student's T distribution CDF.
  • p_value: It returns the p value, calculated as 1 - probability.
  • alpha: the specified alpha value.
  • null: Either true or false. If true, it means that the null hypothesis should not be rejected.
  • alternative: Either true or false. If true, it means that the null hypothesis can be rejected.
  • confidence_level: Defined as 1 - alpha.

Keep in mind that the null and alternative keys cannot be true at the same time.

pry(main)> left_group
=> [0.12819915256260872, 0.24345459073897613, 0.27517650565714014, 0.8522185144081152, 0.05471111219486524]
pry(main)> right_group
=> [0.3272414061985621, 0.2989306116723194, 0.642664937717922, 0.9476073892620895, 0.7050008194345182]
# Alpha of 0.05 and one tail.
pry(main)> StatisticalTest::TTest.paired_test(alpha = 0.05, :one_tail, left_group, right_group)
=> {:t_score=>-2.520215812331144, :probability=>0.032670873044446366, :p_value=>0.9673291269555536, :alpha=>0.05, :null=>true, :alternative=>false, :confidence_level=>0.95}
# alpha of 0.01 and one tail.
pry(main)> StatisticalTest::TTest.paired_test(alpha = 0.01, :one_tail, left_group, right_group)
=> {:t_score=>-2.520215812331144, :probability=>0.032670873044446366, :p_value=>0.9673291269555536, :alpha=>0.01, :null=>true, :alternative=>false, :confidence_level=>0.99}
# Alpha of 0.01 and two tail.
pry(main)> StatisticalTest::TTest.paired_test(alpha = 0.01, :two_tail, left_group, right_group)
=> {:t_score=>-2.520215812331144, :probability=>0.032670873044446366, :p_value=>1.9346582539111072, :alpha=>0.01, :null=>true, :alternative=>false, :confidence_level=>0.99}

It will raise an StandardError if both groups are the same or ZeroStdError if the standard deviation of the difference is zero:

StatisticalTest::TTest.paired_test(alpha = 0.01, :two_tail, [1.0, 2.0, 3.0], [1.0, 2.0, 3.0])
Traceback (most recent call last):
        1: from ruby-statistics/lib/statistics/statistical_test/t_test.rb:55:in `paired_test'
StandardError (both samples are the same)

StatisticalTest::TTest.paired_test(alpha = 0.01, :two_tail, [1.0, 2.0, 3.0], [2.0, 3.0, 4.0])
Traceback (most recent call last):
        1: from ruby-statistics/lib/statistics/statistical_test/t_test.rb:63:in `paired_test'
Statistics::StatisticalTest::TTest::ZeroStdError (Standard deviation for the difference or group is zero. Please, reconsider sample contents)