TTEST_IND

Overview

The TTEST_IND function performs an independent two-sample t-test to determine whether two independent samples have statistically different population means. This test is fundamental in experimental design, A/B testing, and scientific research where comparing two groups is required.

The independent samples t-test evaluates the null hypothesis that two populations have identical mean values. This implementation uses the SciPy library’s scipy.stats.ttest_ind function. For complete documentation, see the official SciPy reference.

When the equal_var parameter is set to TRUE (the default), a Student’s t-test is performed, which assumes both populations have identical variances. When set to FALSE, Welch’s t-test is performed, which does not assume equal population variances and is generally more robust when sample sizes or variances differ between groups. For background on these methods, see Independent two-sample t-test and Welch’s t-test on Wikipedia.

The t-statistic is computed as:

t = \frac{\bar{x}_1 - \bar{x}_2}{SE}

where \bar{x}_1 and \bar{x}_2 are the sample means and SE is the standard error of the difference. The degrees of freedom differ between the standard t-test (pooled variance) and Welch’s t-test (adjusted for unequal variances using the Welch-Satterthwaite approximation).

The function supports three alternative hypotheses via the ttest_alternative parameter: "two-sided" tests whether the means are unequal, "less" tests whether the first sample’s mean is less than the second, and "greater" tests whether the first sample’s mean is greater than the second.

The function returns both the t-statistic and the p-value. A p-value below a chosen significance level (commonly 0.05) indicates evidence against the null hypothesis of equal population means.

This example function is provided as-is without any representation of accuracy.

Excel Usage

=TTEST_IND(a, b, equal_var, ttest_alternative)
  • a (list[list], required): First sample data as a 2D array of numeric values.
  • b (list[list], required): Second sample data as a 2D array of numeric values.
  • equal_var (bool, optional, default: true): If TRUE, assumes equal population variances (standard t-test). If FALSE, performs Welch’s t-test.
  • ttest_alternative (str, optional, default: “two-sided”): Defines the alternative hypothesis for the test.

Returns (list[list]): 2D list [[statistic, p_value]], or error message string.

Examples

Example 1: Basic equal variance, two-sided test

Inputs:

a b equal_var ttest_alternative
1.1 2.2 2 2.5 true two-sided
3.3 4.4 3 3.5

Excel formula:

=TTEST_IND({1.1,2.2;3.3,4.4}, {2,2.5;3,3.5}, TRUE, "two-sided")

Expected output:

Result
0 1

Example 2: Welch’s t-test with greater alternative

Inputs:

a b equal_var ttest_alternative
5 6 1 2 false greater
7 8 3 4

Excel formula:

=TTEST_IND({5,6;7,8}, {1,2;3,4}, FALSE, "greater")

Expected output:

Result
4.3818 0.0023

Example 3: Equal variance with less alternative

Inputs:

a b equal_var ttest_alternative
1 2 2 3 true less
3 4 4 5

Excel formula:

=TTEST_IND({1,2;3,4}, {2,3;4,5}, TRUE, "less")

Expected output:

Result
-1.0954 0.1577

Example 4: Two-sided test with different sample values

Inputs:

a b equal_var ttest_alternative
10 12 8 9 true two-sided
14 16 10 11

Excel formula:

=TTEST_IND({10,12;14,16}, {8,9;10,11}, TRUE, "two-sided")

Expected output:

Result
2.4249 0.0515

Python Code

from scipy.stats import ttest_ind as scipy_ttest_ind
import math

def ttest_ind(a, b, equal_var=True, ttest_alternative='two-sided'):
    """
    Performs the independent two-sample t-test for the means of two groups.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        a (list[list]): First sample data as a 2D array of numeric values.
        b (list[list]): Second sample data as a 2D array of numeric values.
        equal_var (bool, optional): If TRUE, assumes equal population variances (standard t-test). If FALSE, performs Welch's t-test. Default is True.
        ttest_alternative (str, optional): Defines the alternative hypothesis for the test. Valid options: Two-sided, Less, Greater. Default is 'two-sided'.

    Returns:
        list[list]: 2D list [[statistic, p_value]], or error message string.
    """
    def to2d(x):
        return [[x]] if not isinstance(x, list) else x

    a = to2d(a)
    b = to2d(b)

    if not isinstance(a, list) or not all(isinstance(row, list) for row in a):
        return "Error: Invalid input: a must be a 2D list."
    if not isinstance(b, list) or not all(isinstance(row, list) for row in b):
        return "Error: Invalid input: b must be a 2D list."

    try:
        a_flat = [float(x) for row in a for x in row]
        b_flat = [float(x) for row in b for x in row]
    except (TypeError, ValueError):
        return "Error: Invalid input: a and b must contain only numeric values."

    if len(a_flat) < 2 or len(b_flat) < 2:
        return "Error: Invalid input: each sample must contain at least two values."

    if not isinstance(equal_var, bool):
        return "Error: Invalid input: equal_var must be a boolean."

    if ttest_alternative not in ('two-sided', 'less', 'greater'):
        return "Error: Invalid input: ttest_alternative must be 'two-sided', 'less', or 'greater'."

    try:
        res = scipy_ttest_ind(a_flat, b_flat, equal_var=equal_var, alternative=ttest_alternative)
        t_stat = float(res.statistic)
        p_val = float(res.pvalue)

        if not math.isfinite(t_stat) or not math.isfinite(p_val):
            return "Error: Invalid result: t-statistic or p-value is not a finite number."

        return [[t_stat, p_val]]
    except Exception as e:
        return f"Error: scipy.stats.ttest_ind error: {e}"

Online Calculator