TTEST_1SAMP
Overview
The TTEST_1SAMP function performs a one-sample t-test, a statistical hypothesis test used to determine whether the mean of a single sample differs significantly from a known or hypothesized population mean. This test is commonly applied in quality control, experimental research, and data analysis scenarios where you need to compare observed measurements against a target or expected value.
The one-sample t-test evaluates the null hypothesis that the sample comes from a population with the specified mean (popmean). The t-statistic measures how many standard errors the sample mean is from the hypothesized population mean and is calculated as:
t = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}
where \bar{x} is the sample mean, \mu_0 is the hypothesized population mean, s is the sample standard deviation, and n is the sample size. The resulting t-statistic follows a Student’s t-distribution with n - 1 degrees of freedom.
This implementation uses the scipy.stats.ttest_1samp function from the SciPy library. The function returns three values: the t-statistic, the two-tailed p-value, and the degrees of freedom. A positive t-statistic indicates the sample mean exceeds the hypothesized mean, while a negative value indicates it falls below. The p-value represents the probability of observing a test statistic at least as extreme as the calculated value, assuming the null hypothesis is true.
Key assumptions for valid interpretation include: (1) the sample is randomly drawn from the population, (2) the observations are independent, and (3) the population is approximately normally distributed (though the test is robust to moderate departures from normality for larger samples due to the Central Limit Theorem). For more on the theoretical foundations, see the Student’s t-test article on Wikipedia or the SciPy GitHub repository.
This example function is provided as-is without any representation of accuracy.
Excel Usage
=TTEST_1SAMP(data, popmean)
data(list[list], required): 2D array of sample observations (must contain at least two numeric values).popmean(float, required): Expected value (population mean) in the null hypothesis.
Returns (list[list]): 2D list [[statistic, p_value]], or error message string.
Examples
Example 1: Sample mean greater than population mean
Inputs:
| data | popmean |
|---|---|
| 2.3 | 2 |
| 2.7 | |
| 2.9 | |
| 3.1 | |
| 2.8 |
Excel formula:
=TTEST_1SAMP({2.3;2.7;2.9;3.1;2.8}, 2)
Expected output:
| Result | ||
|---|---|---|
| 5.729 | 0.004597 | 4 |
Example 2: Sample mean less than population mean
Inputs:
| data | popmean |
|---|---|
| 1.2 | 2 |
| 1.5 | |
| 1.7 | |
| 1.9 | |
| 2.1 |
Excel formula:
=TTEST_1SAMP({1.2;1.5;1.7;1.9;2.1}, 2)
Expected output:
| Result | ||
|---|---|---|
| -2.049 | 0.1099 | 4 |
Example 3: Larger sample with 10 observations
Inputs:
| data | popmean |
|---|---|
| 5.1 | 5 |
| 5.3 | |
| 5.2 | |
| 5.4 | |
| 5 | |
| 5.5 | |
| 5.3 | |
| 5.2 | |
| 5.1 | |
| 5.4 |
Excel formula:
=TTEST_1SAMP({5.1;5.3;5.2;5.4;5;5.5;5.3;5.2;5.1;5.4}, 5)
Expected output:
| Result | ||
|---|---|---|
| 5 | 0.000739 | 9 |
Example 4: Negative t-statistic when sample mean below popmean
Inputs:
| data | popmean |
|---|---|
| 10.2 | 11 |
| 9.8 | |
| 10.5 | |
| 9.9 | |
| 10.1 |
Excel formula:
=TTEST_1SAMP({10.2;9.8;10.5;9.9;10.1}, 11)
Expected output:
| Result | ||
|---|---|---|
| -7.3485 | 0.001826 | 4 |
Python Code
from scipy.stats import ttest_1samp as scipy_ttest_1samp
import math
def ttest_1samp(data, popmean):
"""
Perform a one-sample t-test for the mean of a group of scores.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_1samp.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): 2D array of sample observations (must contain at least two numeric values).
popmean (float): Expected value (population mean) in the null hypothesis.
Returns:
list[list]: 2D list [[statistic, p_value]], or error message string.
"""
def to2d(x):
return [[x]] if not isinstance(x, list) else x
def flatten(arr):
flat = []
for row in arr:
if isinstance(row, list):
flat.extend(row)
else:
flat.append(row)
return flat
data = to2d(data)
if not isinstance(data, list) or not all(isinstance(row, list) for row in data):
return "Error: Invalid input: data must be a 2D list."
flat = flatten(data)
try:
flat = [float(x) for x in flat]
except (TypeError, ValueError):
return "Error: Invalid input: data must contain numeric values."
if len(flat) < 2:
return "Error: Invalid input: data must contain at least two values."
try:
popmean = float(popmean)
except (TypeError, ValueError):
return "Error: Invalid input: popmean must be a number."
try:
result = scipy_ttest_1samp(flat, popmean)
stat = float(result.statistic)
pval = float(result.pvalue)
df = len(flat) - 1
if math.isnan(stat) or math.isinf(stat) or math.isnan(pval) or math.isinf(pval):
return "Error: Result contains NaN or Inf: sample may have zero variance."
return [[stat, pval, df]]
except Exception as e:
return f"Error: scipy.stats.ttest_1samp error: {e}"