Program to implement t-test

The t test (also called Student’s T Test) compares two averages (means) and tells if they are different from each other. The t-test also tells how significant the differences are. In other words it lets you know if those differences could have happened by chance. t-test can be calculated by using formula :

where,
1 is the mean of first data set
2 is the mean of second data set
S12 is the standard deviation of first data set
S22 is the standard deviation of second data set
N1 is the number of elements in the first data set
N2 is the number of elements in the second data set

Examples :

Input : arr1[] = {10, 20, 30, 40, 50}
        arr2[] = {1, 29, 46, 78, 99}
Output : -1.09789

Input : arr1[] = {5, 20, 40, 80, 100, 120}
        arr2[] = {1, 29, 46, 78, 99}
Output : 0.399518

Explanation :
In example 1, x̄1 = 30, x̄2 = 50.6, S12 = 15.8114, S12 = 38.8626
using the formula, t-test = -1.09789



Below is the implementation of t-test.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to implement t-test.
#include <bits/stdc++.h>
using namespace std;
  
// Function to find mean.
float Mean(float arr[], int n)
{
    float sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + arr[i];
    return sum / n;
}
  
// Function to find standard
// deviation of given array.
float standardDeviation(float arr[], int n)
{
    float sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + (arr[i] - Mean(arr, n)) *
                    (arr[i] - Mean(arr, n));
  
    return sqrt(sum / (n - 1));
}
  
// Function to find t-test of
// two set of statistical data.
float tTest(float arr1[], int n,
            float arr2[], int m)
{
    float mean1 = Mean(arr1, n);
    float mean2 = Mean(arr2, m);
    float sd1 = standardDeviation(arr1, n);
    float sd2 = standardDeviation(arr2, m);
  
    // Formula to find t-test
    // of two set of data.
    float t_test = (mean1 - mean2) / sqrt((sd1 * sd1)
                              / n + (sd2 * sd2) / m);
    return t_test;
}
  
// Driver function.
int main()
{
    float arr1[] = { 10, 20, 30, 40, 50 };
  
    // Calculate size of first array.
    int n = sizeof(arr1) / sizeof(arr1[0]);
    float arr2[] = { 1, 29, 46, 78, 99 };
  
    // Calculate size of second array.
    int m = sizeof(arr2) / sizeof(arr2[0]);
  
    // Function call.
    cout << tTest(arr1, n, arr2, m);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to implement t-test.
import java.util.*;
import java.io.*;
  
class GFG 
{
    // Function to find mean.
    static float Mean(float arr[], int n)
    {
        float sum = 0;
        for (int i = 0; i < n; i++)
            sum = sum + arr[i];
        return sum / n;
    }
  
    // Function to find standard
    // deviation of given array.
    static float standardDeviation(float arr[], int n)
    {
        float sum = 0;
        for (int i = 0; i < n; i++)
            sum = sum + (arr[i] - Mean(arr, n)) 
                  * (arr[i] - Mean(arr, n));
  
        return (float)Math.sqrt(sum / (n - 1));
    }
  
    // Function to find t-test of
    // two set of statistical data.
    static float tTest(float arr1[], int n,
                        float arr2[], int m)
    {
        float mean1 = Mean(arr1, n);
        float mean2 = Mean(arr2, m);
        float sd1 = standardDeviation(arr1, n);
        float sd2 = standardDeviation(arr2, m);
  
        // Formula to find t-test
        // of two set of data.
        float t_test = (mean1 - mean2) / 
                       (float)Math.sqrt((sd1 * sd1) /
                       n + (sd2 * sd2) / m);
        return t_test;
    }
  
    // Driver code
    public static void main(String args[])
    {
         float arr1[] = { 10, 20, 30, 40, 50 };
  
        // Calculate size of first array.
        int n = arr1.length;
        float arr2[] = { 1, 29, 46, 78, 99 };
  
        // Calculate size of second array.
        int m = arr2.length;
  
        // Function call.
        System.out.print(tTest(arr1, n, arr2, m));
    }
}
  
// This code is contributed by Sahil_Bansall

chevron_right


Python3

# Python 3 Program to implement t-test.
from math import sqrt

# Function to find mean.
def Mean(arr, n):
sum = 0
for i in range(0, n, 1):
sum = sum + arr[i]
return sum / n

# Function to find standard
# deviation of given array.
def standardDeviation(arr, n):
sum = 0
for i in range(0, n, 1):
sum = (sum + (arr[i] – Mean(arr, n)) *
(arr[i] – Mean(arr, n)))

return sqrt(sum / (n – 1))

# Function to find t-test of
# two set of statistical data.
def tTest(arr1, n, arr2, m):
mean1 = Mean(arr1, n)
mean2 = Mean(arr2, m)
sd1 = standardDeviation(arr1, n)
sd2 = standardDeviation(arr2, m)

# Formula to find t-test
# of two set of data.
t_test = (mean1 – mean2) / sqrt((sd1 * sd1) / n +
(sd2 * sd2) / m)
return t_test

# Driver Code
if __name__ == ‘__main__’:
arr1 = [10, 20, 30, 40, 50]

# Calculate size of first array.
n = len(arr1)
arr2 = [1, 29, 46, 78, 99]

# Calculate size of second array.
m = len(arr2)

# Function call.
print(‘{0:.6}’.format(tTest(arr1, n, arr2, m)))

# This code is contributed by
# Surendra_Gangwar

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to implement t-test.
using System;
  
class GFG {
      
    // Function to find mean.
    static float Mean(float[] arr, int n)
    {
        float sum = 0;
        for (int i = 0; i < n; i++)
            sum = sum + arr[i];
        return sum / n;
    }
  
    // Function to find standard
    // deviation of given array.
    static float standardDeviation(float[] arr, int n)
    {
        float sum = 0;
        for (int i = 0; i < n; i++)
            sum = sum + (arr[i] - Mean(arr, n)) *
                        (arr[i] - Mean(arr, n));
  
        return (float)Math.Sqrt(sum / (n - 1));
    }
  
    // Function to find t-test of
    // two set of statistical data.
    static float tTest(float[] arr1, int n,
                       float[] arr2, int m)
    {
        float mean1 = Mean(arr1, n);
        float mean2 = Mean(arr2, m);
        float sd1 = standardDeviation(arr1, n);
        float sd2 = standardDeviation(arr2, m);
  
        // Formula to find t-test
        // of two set of data.
        float t_test = (mean1 - mean2) / 
                       (float)Math.Sqrt((sd1 * sd1) /
                       n + (sd2 * sd2) / m);
        return t_test;
    }
      
    // Driver code
    static public void Main ()
    {
        float[] arr1 = {10, 20, 30, 40, 50};
  
        // Calculate size of first array
        int n = arr1.Length;
        float[] arr2 = { 1, 29, 46, 78, 99 };
  
        // Calculate size of second array
        int m = arr2.Length;
  
        // Function calling
        Console.Write(tTest(arr1, n, arr2, m));
    }
}
  
// This code is contributed by Ajit.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to implement t-test.
  
// Function to find mean.
function Mean($arr, $n)
{
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
        $sum = $sum + $arr[$i];
    return $sum / $n;
}
  
// Function to find standard
// deviation of given array.
function standardDeviation($arr,$n)
{
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
        $sum = $sum + ($arr[$i] - Mean($arr, $n)) *
                      ($arr[$i] - Mean($arr, $n));
  
    return sqrt($sum / ($n - 1));
}
  
// Function to find t-test of
// two set of statistical data.
function tTest($arr1, $n,
               $arr2, $m)
{
    $mean1 = Mean($arr1, $n);
    $mean2 = Mean($arr2, $m);
    $sd1 = standardDeviation($arr1, $n);
    $sd2 = standardDeviation($arr2, $m);
  
    // Formula to find t-test
    // of two set of data.
    $t_test = ($mean1 - $mean2) / 
               sqrt(($sd1 * $sd1) / 
               $n + ($sd2 * $sd2) / $m);
    return $t_test;
}
  
// Driver Code
{
    $arr1 = array(10, 20, 30, 40, 50);
  
    // Calculate size of first array.
    $n = sizeof($arr1) / sizeof($arr1[0]);
      
    $arr2 = array( 1, 29, 46, 78, 99 );
  
    // Calculate size of second array.
    $m = sizeof($arr2) / sizeof($arr2[0]);
  
    // Function call.
    echo tTest($arr1, $n, $arr2, $m);
  
    return 0;
}
  
// This code is contributed by nitin mittal.
?>

chevron_right



Output:

-1.09789


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.