history <>

use <>

hypothesis <>

Unpaired and paired double samples t-test

independent ( unpaired ) sample

Paired samples

calculation <>

Single sample t test

Slope of regression line

Independent double sample t test

replace t- Test location problem

Multivariate testing

Algorithm implementation

example <>


T A test is any statistical hypothesis test , In this test , The test statistics follow a student's zero hypothesis t distribution .

T The test is most often applied when the test statistic will follow a normal distribution <> If a Value of
Scaling terms <> The test statistics are known . When the scaling item is unknown and is based on data
<> Estimated replacement time of , Test statistics ( on certain conditions ) follow t distribution . The T test
have access to , for example , To determine whether the two sets of data are significant <>
Different from each other .



Statistics were compiled by 1908 Introduced William · Cili · Gosset , Chemist works at Guinness brewery in Dublin , Ireland .“ student ” It's his pseudonym .

because Claude Guinness The policy is to recruit the best graduates from Oxford and Cambridge , Applying biochemistry and statistics to industrial processes in Guinness , So we hired Gosset
.Gosset Designed t Inspection as an economic method for monitoring bulky quality . This one T Test work has been submitted to Biometrika Journal and 1908 Published in . Guinness's policy forbids its chemists from publishing their results , therefore Gosset Under a pseudonym “ student ” Published his statistical work ( Detailed history of this kana , Please refer to the student's t distribution , It should not be confused with literal students ).

Guinness has a policy that allows technicians to leave school ( The so-called “ Study vacation ”), Gost is here 1906-1907 Professor Karl Pearson's biometric laboratory at University College London was used in the first two semesters of the academic year .
Gosset He was later a statistician and editor in chief Karl Pearson What we know .



Most commonly used t- test There are :

* Single sample position test , Test whether the mean value of the population has the value specified in the null hypothesis .
* Double sample position test for null hypothesis , Make the mean value of the two groups equal . All of these tests are often referred to as students t test , But strictly speaking , This name should only be used if the variance of two populations is assumed to be equal ;
The form of testing used when this assumption is removed is sometimes referred to as Welch Of t test . These tests are often referred to as “ unpaired ” or “ Independent sample ”
t- test , Because they are usually applied when the basic statistical units of the two samples being compared do not overlap .[8]
* Test of null hypothesis
, That is, the difference between two responses measured on the same statistical unit has a zero average . for example , Suppose we measure the size of cancer patients before and after treatment . If the treatment works , We expect the tumor size to decrease in many patients after treatment . This is often referred to as “ pair ” or “ Repeated measurement ”
t- test :[8] [9] See paired difference test .
* Whether the slope of the regression line is consistent with 0 Significantly different .


majority t -test The statistical data are in the form of , among Z and s It's a function of data . usually ,Z Designed to be sensitive to alternative assumptions ( Namely , When the alternative hypothesis is true, the range tends to be larger ), and s Yes, it is allowed to determine t Distribution of
Scaling parameters <>.

formula :

Among them are samples X 1,X 2,...X n Sample mean value of
<>, The size is n,s It's the mean
<> Standard error of
<>,σ Is the overall standard deviation of the data
<>,μ It's the overall mean


t The hypothesis of the test is

* X It follows the normal distribution with mean value  μ Sum variance  
* follow   Distribution and  p  Zero hypothesis of degree of freedom , Middle and lower  p It's a positive constant
* Z and  s It's independent .

In particular types of t Under test , These conditions are the consequence of the population studied , And the way of data sampling . for example , When comparing the mean of two independent samples t Under test , The following assumptions should be satisfied :

Each of the two populations to be compared should follow a normal distribution . This can be tested using a normal test , for example Shapiro-Wilk or Kolmogorov-Smirnov test , Or it can be evaluated graphically using a normal quantile plot .
If you use the t Original definition of inspection , The two populations should have the same variance ( Available F-test,Levene test ,Bartlett Inspection or Brown-Forsythe Inspection and inspection  ; Or use Q-Q Graphs are evaluated graphically ). If the sample size in the two groups being compared is equal , The original of students t The test is very robust to the existence of unequal variance . Whether the sample size is similar or not , Welch's t The test is not sensitive to the equality of the difference .
The data used for testing should be sampled independently of the two populations being compared . This usually cannot be tested from the data , But if the known data is dependent sampling ( Namely , If they are sampled as clusters ), So the classic discussed here t- Tests can produce misleading results .
Most double samples t- The test deviates greatly from all assumptions .


Unpaired and paired double samples t-test

Double sample t- The difference in the mean value of the test involves independent samples ( Unpaired samples ) Or paired samples . pair t- Testing is a form of blocking , And when the pairing unit and “ Noise factor ” Similar time , It has more power than the unpaired test , The “ Noise factor ” Independent of membership in the two groups being compared . In different contexts , pair t-test Can be used to reduce the impact of confounding factors in observational studies .


independent ( unpaired ) sample

When two groups of independent samples with the same distribution are obtained , Using independent samples t -test
, One group came from each of the two groups being compared . for example , Suppose we're evaluating the effectiveness of medical treatment , We will 100 Subjects were included in our study , Then the 50 Subjects were randomly assigned to the treatment group , take 50 Subjects were randomly assigned to the control group . under these circumstances , We have two separate samples , And will use unpaired t
-test form . Randomization is not necessary here -
If we contact by phone 100 Individuals and get everyone's age and gender , Then double samples are used t Test to determine if the average age varies by gender , It will also be a separate sample t- test , Even if the data is observational .


Paired samples

Paired samples t- The test usually consists of a sample of a matched pair of similar units or a set of units that have been tested twice (“ Repeated measurement ” t- test ) form .

Typical examples of repeated measurements t- The trial was to test subjects before treatment , For example, hypertension , The same subjects were tested again after treatment with antihypertensive drugs . By comparing the same number of patients before and after treatment , We effectively use each patient as our own control . such , Correct rejection of null hypothesis ( here : There was no difference in treatment ) Become more likely , Statistical power increases only because random variation between patients has now been eliminated . But please note , Statistical power increases at a cost : More testing is needed , Each topic must be tested twice . Because now half of the sample depends on the other half , So students t
-test Only the paired version of n/2- 1 freedom ( n It's the total number of observations ). In pairs, they become separate test units , And the sample must be doubled to achieve the same number of degrees of freedom . usually , Yes n - 1 Degrees of freedom (
n It's the total number of observations ).

be based on “ Matching pairs of samples ”
Paired samples of t- The test results were from unmatched samples , It is then used to form paired samples , By using additional variables measured with variables of interest . The matching is achieved by identifying observations from one of the two samples , In the case of similar values in other measured variables in the pair . This method is sometimes used in observational studies , To reduce or eliminate the influence of confounding factors .

Paired samples t- Tests are often referred to as “ Dependent sample t- test ”.



The following is a list of the various t- Explicit expression of test . In each case , It is shown that under the null hypothesis, it is exactly followed or very close t- The formula of test statistics of distribution . and , The appropriate degrees of freedom are given in each case . Each of these statistics can be used to perform single tailed or double tailed tests .

once t Values and degrees of freedom are determined , One p Values can use the table of values found , From students t distribution . If p The value is below the threshold selected for statistical significance ( Usually 0.10,0.05 or 0.01 level ), The original hypothesis is rejected to support the alternative hypothesis .

Single sample t test

At zero of the test, the population average is assumed to be equal to the specified value


Is the sample mean ,s Is the sample standard deviation of the sample <>,
n Is the sample size . The degrees of freedom used in this test are n - 1.

Although the parent population does not need a normal distribution , But the distribution of the sample population means it is considered normal . Through the central limit theorem
, If the sample of the parent group is independent and the second moment of the parent group exists , Then the sample mean will be approximately normal in the large sample limit .( The degree of approximation depends on the closeness of parent population to normal distribution and sample size n.)


Slope of regression line

Suppose a suitable model

among X It's known ,α and β It's unknown , also ε Yes, the mean is 0, Random variables of normal distribution with unknown variance , and ÿ It's the result of interest . The slope of the zero hypothesis that we're going to test β Equal to a specified value
( Usually taken as 0, under these circumstances , The zero hypothesis is x and y It's not relevant ).


There is one t Distribution if null hypothesis holds , Then there are n-2 Degree of freedom t distribution .

Standard error of slope coefficient :

It can be written in terms of residuals

Then it is given by the following equation :

Another way to determine :

among r yes Pearson correlation coefficient <>.

The method can be determined from

Where is the sample variance



Independent double sample t test

Equal sample size , The variance is equal

Give two groups (1,2), This test only applies to :

* Two sample sizes ( The number of participants in each group n) equal ;
* It can be assumed that the two distributions have the same variance ;
Violations of these assumptions are discussed below .

Is the mean value different t The statistics can be calculated as follows :


Here is n = n1 = n2 and s 2 The sum of the combined standard deviations is an unbiased estimate of the variance of two samples . t The denominator of is the standard error of the difference between the two methods .

For significance test , The degree of freedom of the test is 2n-2, among n Is the number of participants in each group .


Equal or unequal sample size , The variance is equal

Use this test only if you can assume that two distributions have the same variance .( When this assumption is violated , See below .) Please note that , The previous formula is a special case of the following formula , When the two samples are equal in size , They can be restored :n
 = n 1 = n 2.

Is the mean value different t The statistics can be calculated as follows :


Is an estimate of the aggregate standard deviation of two samples : It is defined in this way , So that its square is an unbiased estimate of the common variance , Whether or not the overall mean is the same . In these formulas ,ni -
1 Is the number of degrees of freedom for each group , Total sample size minus 2( Namely n1 + n2 - 2) Is the total number of degrees of freedom used . In the importance test .


Equal or unequal sample size , Variance inequality

This test is also known as Welch t test , It is only assumed that the variances of the two populations are equal ( The two samples may be the same size or not ) When using , Therefore, it must be estimated separately . Used to test whether the population mean is different t The statistics are calculated as follows :


here , Is an unbiased estimate of the variance of each of the two samples , among ni = group i Number of participants in (1 or 2).

be careful , under these circumstances ,s2 It's not a set variance . For use in testing , The distribution of test statistics is similar to that of ordinary students t Degrees of freedom for distribution and use  

This is called Welch-Satterthwaite equation . The true distribution of test statistics is actually ( slightly ) Depends on two unknown demographic differences ( See Behrens-Fisher problem ).


Correlation of paired samples t test

Use this test when the sample is dependent ; in other words , When only one sample has been tested twice ( Repeated measurement ) Or two samples have been matched or “ pair ” Time . This is an example of a paired difference test .

For this equation , The difference between all pairs must be calculated . These pairs are a person's scores before and after the test , Or a pair of people who match into meaningful groups ( for example , From the same family or age group : See table ). The average of these differences (
) And standard deviation () Used in equations . Constant is nonzero , If we want to test whether the average of the differences is from a significant difference . Degrees of freedom used 是n -1,其中n表示对的数量.




对于正确性,所述t -test和z -test要求样品的正常装置,并且t -test另外需要,对于样本方差如下缩放的χ






对于一示例多变量测试中,假设是平均向量(μ)是等于给定的矢量().测试统计数据是Hotelling的t 2

其中n是样本大小,是列均值的向量,S是m × m 样本协方差矩阵

对于两样品多元测试中,假设是均值向量(,)的两个样品是相等的.测试统计数据是Hotelling的双样本t 2




scipy.stats.ttest_ind(a, b, axis=0, equal_var=True)



a,b : array_like


axis : int或None,可选


equal_var : bool,可选



nan_policy : {'propagate','raise','omit'},可选


statistic : 浮点数或数组


pvalue : float或array



from scipy import stats np.random.seed(12345678)
rvs1 = stats.norm.rvs(loc=5,scale=10,size=500) rvs2 =
stats.norm.rvs(loc=5,scale=10,size=500) stats.ttest_ind(rvs1,rvs2)
#(0.26833823296239279, 0.78849443369564776) stats.ttest_ind(rvs1,rvs2,
equal_var = False) #(0.26833823296239279, 0.78849452749500748)
ttest_ind 低估了不等方差的p:
rvs3 = stats.norm.rvs(loc=5, scale=20, size=500) stats.ttest_ind(rvs1, rvs3)
#(-0.46580283298287162, 0.64145827413436174) stats.ttest_ind(rvs1, rvs3,
equal_var = False) #(-0.46580283298287162, 0.64149646246569292)
当n1!= n2时,等方差t-统计量不再等于不等方差t-统计量:
rvs4 = stats.norm.rvs(loc=5, scale=20, size=100) stats.ttest_ind(rvs1, rvs4)
#(-0.99882539442782481, 0.3182832709103896) stats.ttest_ind(rvs1, rvs4,
equal_var = False) #(-0.69712570584654099, 0.48716927725402048)
rvs5 = stats.norm.rvs(loc=8, scale=20, size=100) stats.ttest_ind(rvs1, rvs5)
#(-1.4679669854490653, 0.14263895620529152) stats.ttest_ind(rvs1, rvs5,
equal_var = False) #(-0.94365973617132992, 0.34744170334794122)