Skip to Content

TTEST_IND

Overview

The TTEST_IND function performs an independent two-sample t-test for the means of two groups, using the scipy.stats.ttest_ind method. This test is commonly used to determine whether the means of two independent samples are significantly different. The calculation is based on the following equation for the t-statistic:

t=xˉ1xˉ2sp2(1n1+1n2)t = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_p^2 \left(\frac{1}{n_1} + \frac{1}{n_2}\right)}}

where xˉ1\bar{x}_1 and xˉ2\bar{x}_2 are the sample means, sp2s_p^2 is the pooled variance (for equal variances), and n1n_1, n2n_2 are the sample sizes. For unequal variances (Welch’s t-test), the denominator uses the sample variances separately. The function allows you to choose between the standard t-test (equal variances) and Welch’s t-test (unequal variances) using the equal_var parameter, and supports alternative hypotheses ('two-sided', 'less', 'greater').

This wrapper simplifies the underlying method by excluding advanced options such as permutation tests, trimming, and NaN handling, focusing only on the most commonly used parameters. The function returns both the t-statistic and p-value as a 2D list, unlike Excel’s native T.TEST which returns only the p-value.

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

Usage

To use the function in Excel:

=TTEST_IND(a, b, [equal_var], [alternative])
  • a (2D list, required): First sample data. Must be a rectangular range with at least two rows.
  • b (2D list, required): Second sample data. Must be a rectangular range with at least two rows.
  • equal_var (bool, optional, default=TRUE): If TRUE, assumes equal population variances (standard t-test). If FALSE, performs Welch’s t-test.
  • alternative (string, optional, default="two-sided"): Defines the alternative hypothesis. Must be one of "two-sided", "less", or "greater".

The function returns a 2D list with one row: [t-statistic, p-value], both floats. If the input is invalid, it returns an error message (string).

Examples

Example 1: Basic Equal Variance, Two-Sided Test

Inputs:

abequal_varalternative
1.12.22.02.5TRUEtwo-sided
3.34.43.03.5

Excel formula:

=TTEST_IND({1.1,2.2;3.3,4.4}, {2.0,2.5;3.0,3.5})

Expected output:

t-statisticp-value
0.0001.000

Example 2: Welch’s Test, Greater Alternative

Inputs:

abequal_varalternative
5.06.01.02.0FALSEgreater
7.08.03.04.0

Excel formula:

=TTEST_IND({5.0,6.0;7.0,8.0}, {1.0,2.0;3.0,4.0}, FALSE, "greater")

Expected output:

t-statisticp-value
4.3820.002

Example 3: Equal Variance, Less Alternative

Inputs:

abequal_varalternative
1.02.02.03.0TRUEless
3.04.04.05.0

Excel formula:

=TTEST_IND({1.0,2.0;3.0,4.0}, {2.0,3.0;4.0,5.0}, TRUE, "less")

Expected output:

t-statisticp-value
-1.0950.158

Example 4: Different Shapes, Equal Variance, Two-Sided

Inputs:

abequal_varalternative
10.012.08.09.0TRUEtwo-sided
14.016.010.011.0

Excel formula:

=TTEST_IND({10.0,12.0;14.0,16.0}, {8.0,9.0;10.0,11.0}, TRUE, "two-sided")

Expected output:

t-statisticp-value
2.4250.052

Python Code

from scipy.stats import ttest_ind as scipy_ttest_ind from typing import List, Union def ttest_ind(a: List[List[float]], b: List[List[float]], equal_var: bool = True, alternative: str = 'two-sided') -> Union[List[List[float]], str]: """ Performs the independent two-sample t-test for the means of two groups. Args: a: 2D list of float values. First sample data. b: 2D list of float values. Second sample data. equal_var: If True, perform standard t-test assuming equal population variances. If False, perform Welch's t-test. Default is True. alternative: Defines the alternative hypothesis ('two-sided', 'less', 'greater'). Default is 'two-sided'. Returns: 2D list with one row: [t-statistic, p-value]. Returns an error message (str) if input is invalid. This example function is provided as-is without any representation of accuracy. """ # Validate input types and shapes if not (isinstance(a, list) and all(isinstance(row, list) for row in a) and len(a) >= 2): return "Invalid input: a must be a 2D list with at least two rows." if not (isinstance(b, list) and all(isinstance(row, list) for row in b) and len(b) >= 2): return "Invalid input: b must be a 2D list with at least two rows." # Flatten 2D lists to 1D 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 Exception: return "Invalid input: a and b must contain only numeric values." if len(a_flat) < 2 or len(b_flat) < 2: return "Invalid input: each sample must contain at least two values." if not isinstance(equal_var, bool): return "Invalid input: equal_var must be a boolean." if alternative not in ('two-sided', 'less', 'greater'): return "Invalid input: alternative must be 'two-sided', 'less', or 'greater'." try: res = scipy_ttest_ind(a_flat, b_flat, equal_var=equal_var, alternative=alternative) t_stat = float(res.statistic) p_val = float(res.pvalue) # Disallow nan/inf if any([x is None or isinstance(x, str) or not isinstance(x, (int, float)) or x != x or abs(x) == float('inf') for x in [t_stat, p_val]]): return "Invalid result: t-statistic or p-value is not a finite number." return [[t_stat, p_val]] except Exception as e: return f"scipy.stats.ttest_ind error: {e}"

Example Workbook

Link to Workbook

Last updated on