Open In App

# 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,
x?1 is the mean of first data set
x?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++

 `// CPP Program to implement t-test.``#include ``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;``}`

## Java

 `// 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`

## 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#

 `// 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.`

## PHP

 ``

## Javascript

 ``

Output

`-1.09789`