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:
where and are the sample means, is the pooled variance (for equal variances), and , 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
): IfTRUE
, assumes equal population variances (standard t-test). IfFALSE
, 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:
a | b | equal_var | alternative | ||
---|---|---|---|---|---|
1.1 | 2.2 | 2.0 | 2.5 | TRUE | two-sided |
3.3 | 4.4 | 3.0 | 3.5 |
Excel formula:
=TTEST_IND({1.1,2.2;3.3,4.4}, {2.0,2.5;3.0,3.5})
Expected output:
t-statistic | p-value |
---|---|
0.000 | 1.000 |
Example 2: Welch’s Test, Greater Alternative
Inputs:
a | b | equal_var | alternative | ||
---|---|---|---|---|---|
5.0 | 6.0 | 1.0 | 2.0 | FALSE | greater |
7.0 | 8.0 | 3.0 | 4.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-statistic | p-value |
---|---|
4.382 | 0.002 |
Example 3: Equal Variance, Less Alternative
Inputs:
a | b | equal_var | alternative | ||
---|---|---|---|---|---|
1.0 | 2.0 | 2.0 | 3.0 | TRUE | less |
3.0 | 4.0 | 4.0 | 5.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-statistic | p-value |
---|---|
-1.095 | 0.158 |
Example 4: Different Shapes, Equal Variance, Two-Sided
Inputs:
a | b | equal_var | alternative | ||
---|---|---|---|---|---|
10.0 | 12.0 | 8.0 | 9.0 | TRUE | two-sided |
14.0 | 16.0 | 10.0 | 11.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-statistic | p-value |
---|---|
2.425 | 0.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}"