Two Sample T-Test |
The Two Sample T-Test of the null hypothesis that data in the samples are random samples from independent normal distributions with equal means, against the alternative that the means are different.
This topic contains the following sections:
For one sample T-test we consider two time series {X1} with sample size N1 and sample variance s1 and {X2} with sample size N2 and sample variance s2.
Tail.Both must be set if in the alternative hypothesis mean of the first distribution is not equal to mean of the second distribution.
Tail.Left must be set if in the alternative hypothesis mean of the first distribution is less than mean of the second distribution.
Tail.Right must be set if in the alternative hypothesis mean of the first distribution is greater than mean of the second distribution.
In the case of equal variances the t-statistic is calculated as follows:
where SX1,X2 is an estimator of the common standard deviation of the two samples:
where SX1, SX2 are standard deviations of the samples.
It is defined in this way so that its square is an unbiased estimator of the common variance whether or not the population means are the same. N1 + N2 − 2 is the total number of degrees of freedom.
In the case of unequal variances the t-statistic is calculated as follows:
where s12 and s22 are the unbiased estimators of the variance of the two samples. Note that in this case, it is not a pooled variance.
Welch–Satterthwaite equation is used for approximation of degrees of freedom:
The following constructors create an instance of TTestTwoSample class.
Constructor | Description | Performance |
---|---|---|
two-tailed T-test, default significance level | Constructor without parameters. Creates TTestTwoSample instance with default significance level for two-tailed test. | |
default significance level | Creates TTestTwoSample instance with default significance level and user defined tail. | |
two-tailed test, user defined significance level | Creates TTestTwoSample instance for two-tailed test and user-defined significance level. | |
user defined tail and significance level | Creates TTestTwoSample instance with user defined significance level and tail. |
The class provides the following methods:
Method | Description | Performance |
---|---|---|
update | Updates test statistic using provided samples and supposition about variances equality. Double array series: Vector series: | |
update | Updates test statistic using provided samples which are subsets of double arrays. User specifies the first element to use and the number of elements to use in each of two arrays. |
The class provides the following properties:
Property | Description | Performance |
---|---|---|
region of acceptance | We fail to reject null hypothesis if test statistics is between left and right borders of region of acceptance. Region of acceptance left border: Region of acceptance right border: | |
p-value | The probability of obtaining a test statistic at least as extreme as the one that was actually observed, assuming that the null hypothesis is true. |
The example of TTestTwoSample class usage:
1using System; 2using FinMath.LinearAlgebra; 3using FinMath.Statistics.HypothesisTesting; 4using FinMath.Statistics.Distributions; 5 6namespace FinMath.Samples 7{ 8 class TTestTwoSamplesSample 9 { 10 static void Main() 11 { 12 //Create instances of normal distribution. 13 Normal distr1 = new Normal(0, 5); 14 Normal distr2 = new Normal(3, 5); 15 16 // Generate random series. 17 Vector series1 = Vector.Random(100, distr1); 18 Vector series2 = Vector.Random(150, distr2); 19 20 // Create an instance of TTestTwoSample 21 TTestTwoSample test = new TTestTwoSample(0.05, Tail.Both); 22 test.Update(series1, series2); 23 24 Console.WriteLine("Test Result:"); 25 // Test decision 26 Console.WriteLine($" The null hypothesis failed to be rejected: {test.Decision}"); 27 // The statistic of TTestTwoSample test. 28 Console.WriteLine($" Statistics = {test.Statistics:0.000}"); 29 // The p-value of the test statistic. 30 Console.WriteLine($" P-Value = {test.PValue:0.000}"); 31 32 } 33 } 34}