ttest_1samp(a, popmean, axis=0, nan_policy='propagate', alternative='two-sided', *, keepdims=False)
This is a test for the null hypothesis that the expected value (mean) of a sample of independent observations a is equal to the given population mean, popmean.
The statistic is calculated as (np.mean(a) - popmean)/se
, where se
is the standard error. Therefore, the statistic will be positive when the sample mean is greater than the population mean and negative when the sample mean is less than the population mean.
Beginning in SciPy 1.9, np.matrix
inputs (not recommended for new code) are converted to np.ndarray
before the calculation is performed. In this case, the output will be a scalar or np.ndarray
of appropriate shape rather than a 2D np.matrix
. Similarly, while masked elements of masked arrays are ignored, the output will be a scalar or np.ndarray
rather than a masked array with mask=False
.
Sample observation.
If an int, the axis of the input along which to compute the statistic. The statistic of each axis-slice (e.g. row) of the input will appear in a corresponding element of the output. If None
, the input will be raveled before computing the statistic.
Defines how to handle input NaNs.
propagate
: if a NaN is present in the axis slice (e.g. row) along which the statistic is computed, the corresponding entry of the output will be NaN.omit
: NaNs will be omitted when performing the calculation. If insufficient data remains in the axis slice along which the statistic is computed, the corresponding entry of the output will be NaN.raise
: if a NaN is present, a ValueError
will be raised.Defines the alternative hypothesis. The following options are available (default is 'two-sided'):
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
An object with the following attributes:
statistic
statistic
pvalue
pvalue
df
df
The object also has the following method:
confidence_interval(confidence_level=0.95)
Computes a confidence interval around the population mean for the given confidence level. The confidence interval is returned in a namedtuple
with fields low and high.
Calculate the T-test for the mean of ONE group of scores.
import numpy as np
from scipy import stats
rng = np.random.default_rng()
rvs = stats.uniform.rvs(size=50, random_state=rng)
stats.ttest_1samp(rvs, popmean=0.5)
rvs = stats.norm.rvs(size=50, random_state=rng)
stats.ttest_1samp(rvs, popmean=0.5)
stats.ttest_1samp(rvs, popmean=0.5, alternative='greater')
rvs = stats.uniform.rvs(size=(100, 50), random_state=rng)
res = stats.ttest_1samp(rvs, popmean=0.5, axis=1)
np.sum(res.pvalue < 0.01)
rvs = stats.norm.rvs(size=50, random_state=rng)
res = stats.ttest_1samp(rvs, popmean=0)
ci = res.confidence_interval(confidence_level=0.95)
ci
res = stats.ttest_1samp(rvs, popmean=ci.low)
np.testing.assert_allclose(res.pvalue, 0.05)
res = stats.ttest_1samp(rvs, popmean=ci.high)
np.testing.assert_allclose(res.pvalue, 0.05)
rvs = stats.norm.rvs(size=(50, 1000), loc=1, random_state=rng)
res = stats.ttest_1samp(rvs, popmean=0)
ci = res.confidence_interval()
contains_pop_mean = (ci.low < 1) & (ci.high > 1)
contains_pop_mean.sum()
The following pages refer to to this document either explicitly or contain code examples using this.
scipy.stats._mstats_basic:ttest_1samp
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them