normalTwoSample¶
Computes statistics for mean and variance inferences using samples from two normal populations.
Synopsis¶
normalTwoSample (x1, x2)
Required Arguments¶
- float
x1[]
(Input) - Array of length
n1Observations
containing the first sample. - float
x2[]
(Input) - Array of length
n2Observations
containing the second sample.
Return Value¶
Difference in means, x1Mean
− x2Mean
.
Optional Arguments¶
means
,x1Mean
,x2Mean
(Output)- Means of the first and second samples.
confidenceMean
, float (Input)Confidence level for two-sided interval estimate of the mean of
x1
minus the mean ofx2
, in percent. ArgumentconfidenceMean
must be between 0.0 and 100.0 and is often 90.0, 95.0, or 99.0. For a one-sided confidence interval with confidence level c (at least 50 percent), setconfidenceMean
= \(100.0-2.0\times(100.0-c)\).Default:
confidenceMean
= 95.0
NOTE: The following three optional arguments allow the analysis to be applied to subsets of the original data sets and then later combined for final results. These optional arguments may be useful when analyzing data sets too large to fit into memory, and also allow subsets of the data to be analyzed in separate threads and later combined for final results. |
intermediateResults
, float[]
(Input/Output)Array of length 25 containing intermediate results. On input,
intermediateResults
contains intermediate statistics about a previous function invocation. When invoking the function the first time, set allintermediateResults
elements to 0.0. On output,normalTwoSample
combines the results on the current data sets and the intermediate statistics inintermediateResults
.This optional argument can be applied to separate blocks of data when physical memory cannot hold the entire data sets.
Note that when
intermediateResults
optional argument is used,normalTwoSample
function outputs are valid only iffinalResults
optional argument is specified to calculate the final statistics. See Example 3.Default:
intermediateResults = None
.union
,stats1[]
,stats2[]
(Input)stats1
andstats2
are arrays of length 25 containing the intermediate results about previous computations.stats1
andstats2
are the variables provided to theintermediateResults
optional argument in previous function invocations.normalTwoSample
combines the results on the current data sets and the intermediate statistics instats1
andstats2
.stats1
andstats2
can beNone
. See Example 3.This option would typically be used in conjunction with the
intermediateResults
option to process a large data set using separate threads or compute nodes. For example, a data set could be split into two subsets, where each subset of data is passed into a separate thread or compute node and processed throughnormalTwoSample
with theintermediateResults
option. The output from each thread is then saved and input to a final call ofnormalTwoSample
using optionunion
andfinalResults
.Default:
stats1 = None
andstats2 = None
.finalResults
(Output)Array of length 25 containing the final statistics. See Example 3.
Elements of
finalResults
are:
index |
finalResults[i] |
---|---|
0 | Mean of the first sample. |
1 | Mean of the second sample. |
2 | Variance of the first sample. |
3 | Variance of the second sample. |
4 | Number of observations in the first sample. |
5 | Number of observations in the second sample. |
*Note*: finalResults[6]
through finalResults[13]
depend on the assumption of equal
variances. |
|
6 | Pooled variance. |
7 | t value, assuming equal variances. |
8 | Probability of a larger t in absolute value, assuming normality, equal means, and equal variances. |
9 | Degrees of freedom assuming equal variances. |
10 | Lower confidence limit for the mean of the first population minus the mean of the second, assuming equal variances. |
11 | Upper confidence limit for the mean of the first population minus the mean of the second, assuming equal variances. |
12 | Lower confidence limit for the common variance. |
13 | Upper confidence limit for the common variance. |
*Note*:
finalResults[14] through
finalResults[18] use
approximations that do not depend
on an assumption of equal
variances. |
|
14 | t value, assuming unequal variances. |
15 | Approximate probability of a larger t in absolute value, assuming normality, equal means, and unequal variances. |
16 | Degrees of freedom assuming unequal variances, for Satterthwaite’s approximation. |
17 | Approximate lower confidence limit for the mean of the first population minus the mean of the second, assuming equal variances. |
18 | Approximate upper confidence limit for the mean of the first population minus the mean of the second, assuming equal variances. |
19 | F value (greater than or equal to 1.0). |
20 | Probability of a larger F in absolute value, assuming normality and equal variances. |
21 | Lower confidence limit for the ratio of the variance of the first population to the second. |
22 | Upper confidence limit for the ratio of the variance of the first population to the second. |
23 | Number of missing values of first sample. |
24 | Number of missing values of second sample. |
ciDiffForEqualVars
,lowerLimit
,upperLimit
(Output)- Argument
lowerLimit
contains the lower confidence limit, andupperLimit
contains the upper limit for the mean of the first population minus the mean of the second, assuming equal variances. ciDiffForUnequalVars
,lowerLimit
,upperLimit
(Output)- Argument
lowerLimit
contains the approximate lower confidence limit, andupperLimit
contains the approximate upper limit for the mean of the first population minus the mean of the second, assuming unequal variances. tTestForEqualVars
,df
,t
,pValue
(Output)- A t test for \(\mu_1-\mu_2=c\), where c is the null hypothesis
value. (See the description of
tTestNull
.) Argumentdf
contains the degrees of freedom, argumentt
contains the t value, and argumentpValue
contains the probability of a larger t in absolute value, assuming equal means. This test assumes equal variances. tTestForUnequalVars
,df
,t
,pValue
(Output)- A t test for \(\mu_1-\mu_2=c\), where c is the null hypothesis
value. (See the description of
tTestNull
.) Argumentdf
contains the degrees of freedom for Satterthwaite’s approximation, argumentt
contains the t value, and argumentpValue
contains the approximate probability of a larger t in absolute value, assuming equal means. This test does not assume equal variances. tTestNull
, float (Input)Null hypothesis value for the t test.
Default:
tTestNull
= 0.0pooledVariance
(Output)- Pooled variance for the two samples.
confidenceVariance
, float (Input)Confidence level for inference on variances. Under the assumption of equal variances, the pooled variance is used to obtain a two-sided
confidenceVariance
percent confidence interval for the common variance ifciCommonVariance
is specified. Without making the assumption of equal variances, the ratio of the variances is of interest. A two-sidedconfidenceVariance
percent confidence interval for the ratio of the variance of the first sample to that of the second sample is computed and is returned ifciRatioVariances
is specified. The confidence intervals are symmetric in probability.Default:
confidenceVariance
= 95.0ciCommonVariance
,lowerLimit
,upperLimit
(Output)- Argument
lowerLimit
contains the lower confidence limit, andupperLimit
contains the upper limit for the common, or pooled, variance. chiSquaredTest
,df
,chiSquared
,pValue
(Output)The chi-squared test for
\[\sigma^2 = \sigma_0^2 \text{ where } \sigma^2\]is the common, or pooled, variance, and
\[\sigma_0^2\]is the null hypothesis value. (See description of
chiSquaredTestNull
.) Argumentdf
contains the degrees of freedom, argumentchiSquared
contains the chi-squared value, and argumentpValue
contains the probability of a larger chi-squared in absolute value, assuming equal means.chiSquaredTestNull
, float (Input)Null hypothesis value for the chi-squared test.
Default:
chiSquaredTestNull
= 1.0stdDevS
,x1StdDev
,x2StdDev
(Output)- Standard deviations of the first and second samples.
ciRatioVariances
,lowerLimit
,upperLimit
(Output)- Argument
lowerLimit
contains the approximate lower confidence limit, andupperLimit
contains the approximate upper limit for the ratio of the variance of the first population to the second. fTest
,dfNumerator
,dfDenominator
, f,pValue
(Output)- The F test for equality of variances. Argument
dfNumerator
anddfDenominator
contain the numerator and denominator degrees of freedom, argument f contains the F test value, and argumentpValue
contains the probability of a larger F in absolute value, assuming equal variances.
Description¶
Function normalTwoSample
computes statistics for making inferences about
the means and variances of two normal populations, using independent samples
in x1
and x2
. For inferences concerning parameters of a single
normal population, see function normalOneSample.
Let \(\mu_1\) and \(\sigma_1^2\) be the mean and variance of the first population, and let \(\mu_2\) and \(\sigma_2^2\) be the corresponding quantities of the second population. The function contains test confidence intervals for difference in means, equality of variances, and the pooled variance.
The means and variances for the two samples are as follows:
and
Inferences about the Means¶
The test that the difference in means equals a certain value, for example,
\(\mu_0\), depends on whether or not the variances of the two populations
can be considered equal. If the variances are equal and tTestNull
equals
0, the test is the two-sample t test, which is equivalent to an
analysis-of-variance test. The pooled variance for the difference-in-means
test is as follows:
The t statistic is as follows:
Also, the confidence interval for the difference in means can be obtained by
specifying ciDiffForEqualVars
.
If the population variances are not equal, the ordinary t statistic does
not have a t distribution and several approximate tests for the equality
of means have been proposed. (See, for example, Anderson and Bancroft 1952,
and Kendall and Stuart 1979.) One of the earliest tests devised for this
situation is the Fisher-Behrens test, based on Fisher’s concept of fiducial
probability. A procedure used if tTestForUnequalVars
and/or
ciDiffForUnequalVars
are specified is the Satterthwaite’s procedure, as
suggested by H.F. Smith and modified by F.E. Satterthwaite (Anderson and
Bancroft 1952, p. 83).
The test statistic is
where
Under the null hypothesis of \(\mu_1-\mu_2=c\), this quantity has an
approximate t distribution with degrees of freedom df
(in
tTestForUnequalVars
), given by the following equation:
Inferences about Variances¶
The F statistic for testing the equality of variances is given by \(F=s_{\max}^2/s_{\min}^2\), where \(s_{\mathrm{max}}^2\) is the larger of \(s_1^2\) and \(s_2^2\). If the variances are equal, this quantity has an F distribution with \(n_1-1\) and \(n_2-1\) degrees of freedom.
It is generally not recommended that the results of the F test be used to decide whether to use the regular t test or the modified tʹ on a single set of data. The modified tʹ (Satterthwaite’s procedure) is the more conservative approach to use if there is doubt about the equality of the variances.
Examples¶
Example 1¶
This example, taken from Conover and Iman (1983, p. 294), involves scores on arithmetic tests of two grade-school classes. The question is whether a group taught by an experimental method has a higher mean score. Only the difference in means is output. The data are shown below.
Scores for Standard Group | Scores for Experimental Group |
---|---|
72 | 111 |
75 | 118 |
77 | 128 |
80 | 138 |
104 | 140 |
110 | 150 |
125 | 163 |
164 | |
169 |
from __future__ import print_function
from numpy import *
from pyimsl.stat.normalTwoSample import normalTwoSample
x1 = array([72.0, 75.0, 77.0, 80.0, 104.0, 110.0, 125.0])
x2 = array([111.0, 118.0, 128.0, 138.0, 140.0, 150.0, 163.0, 164.0, 169.0])
# Perform analysis
diff_means = normalTwoSample(x1, x2)
# Print results
print("x1_mean - x2_mean = %5.2f" % diff_means)
Output¶
x1_mean - x2_mean = -50.48
Example 2¶
The same data is used for this example as for the initial example. Here, the results of the t test are output. The variances of the two populations are assumed to be equal. It is seen from the output that there is strong reason to believe that the two means are different (t value of −4.804). Since the lower 97.5-percent confidence limit does not include 0, the null hypothesis is that \(\mu_1\leq\mu _2\) would be rejected at the 0.05 significance level. (The closeness of the values of the sample variances provides some qualitative substantiation of the assumption of equal variances.)
from __future__ import print_function
from numpy import *
from pyimsl.stat.normalTwoSample import normalTwoSample
x1 = array([72.0, 75.0, 77.0, 80.0, 104.0, 110.0, 125.0])
x2 = array([111.0, 118.0, 128.0, 138.0, 140.0, 150.0, 163.0, 164.0, 169.0])
pooledVariance = []
ciDiff = {}
tTest = {}
# Perform analysis
diff_means = normalTwoSample(x1, x2,
pooledVariance=pooledVariance,
ciDiffForEqualVars=ciDiff,
tTestForEqualVars=tTest)
# Print results
print("x1_mean - x2_mean = %5.2f" % diff_means)
print("Pooled variance = %5.2f" % (pooledVariance[0]))
print("95%% CI for x1_mean - x2_mean is (%5.2f,%5.2f)" %
(ciDiff["lowerLimit"], ciDiff["upperLimit"]))
print("df = %3d" % (tTest["df"]))
print("t = %5.2f" % (tTest["t"]))
print("p-value = %8.5f" % (tTest["pValue"]))
Output¶
x1_mean - x2_mean = -50.48
Pooled variance = 434.63
95% CI for x1_mean - x2_mean is (-73.01,-27.94)
df = 14
t = -4.80
p-value = 0.00028
Example 3¶
The same data is used for this example as for the initial example. This
example illustrates the use of the intermediateResults
, union
, and
finalResults
optional arguments with “x1
” and “x2
” divided into
three sub-groups. Since there are more “x2
” values than “x1
” values,
n1Observations
is set to zero on later calls to the function.
This example demonstrates how the analysis can be applied to subsets of the original data sets and then later combined for final results. These techniques may be useful when analyzing data sets too large to fit into memory, and also allow subsets of the data to be analyzed in separate threads (though this example does not show the use of separate threads) and later combined for final results.
from __future__ import print_function
from numpy import *
from pyimsl.stat.normalTwoSample import normalTwoSample
x1 = array([72.0, 75.0, 77.0, 80.0, 104.0, 110.0, 125.0])
x2 = array([111.0, 118.0, 128.0, 138.0, 140.0, 150.0, 163.0, 164.0, 169.0])
stats1 = zeros(25, dtype=double)
stats2 = zeros(25, dtype=double)
finalStats = zeros(25, dtype=double)
# Bring in first group of observations on x1 and x2.
# Save intermediate results into variable, stat1.
#
# Total second group:
# n1_observations = 3, n2_observations = 3
# First call using: n1_observations = 2, n2_observations = 2
diff_means = normalTwoSample(x1[0:2], x2[0:2],
intermediateResults=stats1)
# Second call using: n1_observations = 1, n2_observations = 1
diff_means = normalTwoSample(x1[2:3], x2[2:3],
intermediateResults=stats1)
# Save intermediate results into variable, stat2.
#
# Total second group:
# n1_observations = 4, n2_observations = 4.
diff_means = normalTwoSample(x1[3:7], x2[3:7],
intermediateResults=stats2)
# Bring in third group of observations on x1 and x2
# and combine the results in variables, stats1 and stats2,
# from the first and second groups.
#
# Total third group:
# n1_observations = 0, n2_observations = 2.
pooledVariance = []
diff_means = normalTwoSample(None, x2[7:9],
union={'stats1': stats1, 'stats2': stats2},
finalResults=finalStats,
pooledVariance=pooledVariance)
# Print results
print("x1_mean - x2_mean = %5.2f" % diff_means)
print("pooled variance = %5.2f" % pooledVariance[0])
Output¶
x1_mean - x2_mean = -50.48
pooled variance = 434.63