TTEST_1SAMP
Overview
The TTEST_1SAMP
function performs a one-sample t-test for the mean of a group of scores, testing the null hypothesis that the expected value (mean) of a sample is equal to a specified population mean. This is commonly used to determine if a sample mean significantly differs from a known or hypothesized value. The function returns the test statistic, p-value, and degrees of freedom. For more details, see the scipy.stats.ttest_1samp documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=TTEST_1SAMP(data, popmean)
data
(2D list, required): Table or column vector of sample observations (must contain two or more elements).popmean
(float, required): Expected value in the null hypothesis.
The function returns a 2D list with three values: test statistic (float), p-value (float), and degrees of freedom (float), or an error message (string) if the input is invalid.
Examples
Example 1: Sample mean greater than population mean
Inputs:
data | popmean |
---|---|
2.3 | 2.0 |
2.7 | |
2.9 | |
3.1 | |
2.8 |
Excel formula:
=TTEST_1SAMP({2.3;2.7;2.9;3.1;2.8}, 2.0)
Expected output:
Test Statistic | P-value | Degrees of Freedom |
---|---|---|
5.7287 | 0.0046 | 4 |
Example 2: Sample mean less than population mean
Inputs:
data | popmean |
---|---|
1.2 | 2.0 |
1.5 | |
1.7 | |
1.9 | |
2.1 |
Excel formula:
=TTEST_1SAMP({1.2;1.5;1.7;1.9;2.1}, 2.0)
Expected output:
Test Statistic | P-value | Degrees of Freedom |
---|---|---|
-2.0486 | 0.1099 | 4 |
Example 3: Sample mean equals population mean
Inputs:
data | popmean |
---|---|
2.0 | 2.0 |
2.0 | |
2.0 | |
2.0 | |
2.0 |
Excel formula:
=TTEST_1SAMP({2.0;2.0;2.0;2.0;2.0}, 2.0)
Expected output:
Test Statistic | P-value | Degrees of Freedom |
---|---|---|
nan | nan | nan |
Example 4: Larger sample size
Inputs:
data | popmean |
---|---|
5.1 | 5.0 |
5.3 | |
5.2 | |
5.4 | |
5.0 | |
5.5 | |
5.3 | |
5.2 | |
5.1 | |
5.4 |
Excel formula:
=TTEST_1SAMP({5.1;5.3;5.2;5.4;5.0;5.5;5.3;5.2;5.1;5.4}, 5.0)
Expected output:
Test Statistic | P-value | Degrees of Freedom |
---|---|---|
5.0000 | 0.0007 | 9 |
Python Code
from scipy.stats import ttest_1samp as scipy_ttest_1samp
def ttest_1samp(data, popmean):
"""
Perform a one-sample t-test for the mean of a group of scores.
Args:
data: 2D list. Sample observations (must contain two or more elements).
popmean: float. Expected value in null hypothesis.
Returns:
2D list. Test statistic, p-value, and degrees of freedom.
This example function is provided as-is without any representation of accuracy.
"""
# Validate data
if not isinstance(data, list) or len(data) < 2:
return "Invalid input: data must be a 2D list with at least two rows."
# Flatten data to 1D list
try:
flat = []
for row in data:
if isinstance(row, list):
flat.extend(row)
else:
flat.append(row)
flat = [float(x) for x in flat]
except Exception:
return "Invalid input: data must contain numeric values."
if len(flat) < 2:
return "Invalid input: data must contain at least two values."
try:
popmean = float(popmean)
except Exception:
return "Invalid input: popmean must be a number."
try:
stat, pval = scipy_ttest_1samp(flat, popmean)[:2]
df = len(flat) - 1
# Disallow nan/inf
if any([
isinstance(x, float) and (x != x or x == float('inf') or x == float('-inf'))
for x in [stat, pval, df]
]):
return [["nan", "nan", "nan"]]
return [[stat, pval, df]]
except Exception as e:
return f"scipy.stats.ttest_1samp error: {e}"