Scipy Significance Tests
A significance test is a method that first makes an assumption about the parameters or distribution form of a population (random variable), then uses sample information to judge whether this assumption (the alternative hypothesis) is reasonable, that is, whether there is a significant difference between the population's true situation and the null hypothesis. In other words, a significance test judges whether the difference between the sample and the assumption we made about the population is purely due to chance variation, or is caused by inconsistency between our assumption and the population's true situation. A significance test tests the assumption we made about the population; its principle is to accept or reject the hypothesis based on the "practical impossibility principle of small-probability events."
A significance test is a method used to determine whether there is a difference between the experimental treatment group and the control group, or between the effects of two different treatments, and whether this difference is significant.
SciPy provides the scipy.stats module to perform significance test functions.
Statistical Hypothesis
A statistical hypothesis is an assumption about the unknown distribution of one or more random variables. When the distribution form of the random variable is known and the hypothesis involves only one or a few unknown parameters of the distribution, it is called a parametric hypothesis. The process of testing a statistical hypothesis is called hypothesis testing, and the test that discriminates parametric hypotheses is called a parametric test.
Null Hypothesis
The null hypothesis, a statistical term also called the original hypothesis, refers to the hypothesis established in advance when conducting a statistical test. When the null hypothesis holds, the relevant statistic should follow a known probability distribution.
When the calculated value of the statistic falls into the rejection region, it indicates that a small-probability event has occurred, and the original hypothesis should be rejected.
A hypothesis to be tested is usually denoted H0, called the original hypothesis (or null hypothesis), and the hypothesis opposite to H0 is denoted H1, called the alternative hypothesis.
- When the null hypothesis is true but we decide to reject it, it is called a Type I error, whose probability is usually denoted α;
- When the null hypothesis is false but we decide not to reject it, it is called a Type II error, whose probability is usually denoted β
- α+β does not necessarily equal 1.
Usually only the maximum probability α of a Type I error is limited, without considering the probability β of a Type II error. Such hypothesis testing is also called a significance test, and the probability α is called the significance level.
The most commonly used α values are 0.01, 0.05, 0.10, etc. Generally, depending on the research question, if rejecting the true hypothesis causes large loss, to reduce such errors, α is set smaller; conversely, α is set larger.
Alternative Hypothesis
The alternative hypothesis is one of the basic concepts in statistics; it contains all propositions about the population distribution that make the null hypothesis invalid. The alternative hypothesis is also called the opposite hypothesis or the competing hypothesis.
The alternative hypothesis can replace the null hypothesis.
For example, for our evaluation of students, we will take:
“学生比平均水平差” -— 作为零假设
“学生优于平均水平” —— 作为替代假设。
One-Sided Test
A one-sided test, also called a one-tailed test or single-sided test, is a test in hypothesis testing where the critical region is constructed using the one-sided tail area enclosed by the density curve of the test statistic and the two axes.
When our hypothesis tests only one side of the value, it is called a "one-tailed test".
Example:
For the null hypothesis:
“均值等于 k”
We can have the alternative hypothesis:
“平均值小于 k”
或
“平均值大于 k”
Two-Sided Test
A two-sided test, also called a two-tailed test or two-sided test, is a test in hypothesis testing where the critical region is constructed using the tail areas on both the left and right sides of the area enclosed by the density curve of the test statistic and the x-axis.
When our hypothesis tests both sides of the value.
Example:
For the null hypothesis:
“均值等于 k”
We can have the alternative hypothesis:
“均值不等于k”
In this case, whether the mean is less than or greater than k, both sides must be checked.
Alpha Value
The alpha value is the significance level.
The significance level is the probability of making an error when estimating that a population parameter falls within a certain interval, denoted by α.
How close the data must be to the extreme to reject the null hypothesis.
It is usually set to 0.01, 0.05, or 0.1.
P-value
The P-value indicates how close the data actually is to the extreme.
Compare the P-value and the alpha value to determine the level of statistical significance.
If the p-value <= alpha, we reject the null hypothesis and say the data is statistically significant; otherwise we accept the null hypothesis.
T-Test
The T-test is used to determine whether there is a significant difference between the means of two variables, and to judge whether they belong to the same distribution.
This is a two-tailed test.
The function ttest_ind() takes two samples of the same size and returns a tuple of the t-statistic and the p-value.
Check whether the given values v1 and v2 come from the same distribution:
Example
import
numpy
as
np
from
scipy.
stats
import
ttest_ind
v1
=
np.
random
.
normal
(
size
=
100
)
v2
=
np.
random
.
normal
(
size
=
100
)
res
=
ttest_ind
(
v1
,
v2
)
(
res
)
The output is:
Ttest_indResult(statistic=0.40833510339674095, pvalue=0.68346891833752133)
If you only want to return the p-value, use the pvalue attribute:
Example
import
numpy
as
np
from
scipy.
stats
import
ttest_ind
v1
=
np.
random
.
normal
(
size
=
100
)
v2
=
np.
random
.
normal
(
size
=
100
)
res
=
ttest_ind
(
v1
,
v2
)
.
pvalue
(
res
)
The output is:
0.68346891833752133
KS Test
The KS test is used to check whether given values conform to a distribution.
This function takes two parameters: the tested values and the CDF.
CDF stands for Cumulative Distribution Function, also known as the distribution function.
The CDF can be a string or a callable that returns a probability.
It can be used as a one-tailed or two-tailed test.
By default it is a two-tailed test. We can pass a string as the alternative parameter: "two-sided", "less", or "greater".
Check whether the given values conform to a normal distribution:
Example
import
numpy
as
np
from
scipy.
stats
import
kstest
v
=
np.
random
.
normal
(
size
=
100
)
res
=
kstest
(
v
,
'norm'
)
(
res
)
The output is:
KstestResult(statistic=0.047798701221956841, pvalue=0.97630967161777515)
Data Statistical Description
Using the describe() function, you can view information about an array, including the following values:
- nobs -- number of observations
- minmax -- minimum and maximum values
- mean -- arithmetic mean
- variance -- variance
- skewness -- skewness
- kurtosis -- kurtosis
Display the statistical description of the array:
Example
import
numpy
as
np
from
scipy.
stats
import
describe
v
=
np.
random
.
normal
(
size
=
100
)
res
=
describe
(
v
)
(
res
)
The output is:
DescribeResult(
nobs=100,
minmax=(-2.0991855456740121, 2.1304142707414964),
mean=0.11503747689121079,
variance=0.99418092655064605,
skewness=0.013953400984243667,
kurtosis=-0.671060517912661
)
Normality Test (Skewness and Kurtosis)
A test that uses observed data to judge whether a population follows a normal distribution is called a normality test; it is an important special type of goodness-of-fit hypothesis test in statistical decision-making.
The normality test is based on skewness and kurtosis.
The normaltest() function returns the p-value for the null hypothesis:
“x 来自正态分布”
Skewness
A measure of the symmetry of the data.
For a normal distribution, it is 0.
If it is negative, the data is skewed to the left.
If it is positive, it means the data is right-skewed.
Kurtosis
A measure of whether the data has a heavy-tailed or light-tailed normal distribution.
Positive kurtosis means a heavy tail.
Negative kurtosis means a light tail.
Find the skewness and kurtosis of the values in the array:
Example
import
numpy
as
np
from
scipy.
stats
import
skew
,
kurtosis
v
=
np.
random
.
normal
(
size
=
100
)
(
skew
(
v
)
)
(
kurtosis
(
v
)
)
The output is:
0.11168446328610283
-0.1879320563260931
Check whether the data comes from a normal distribution:
Example
import
numpy
as
np
from
scipy.
stats
import
normaltest
v
=
np.
random
.
normal
(
size
=
100
)
(
normaltest
(
v
)
)
The output is:
NormaltestResult(statistic=4.4783745697002848, pvalue=0.10654505998635538)