({{, (RefLink[ZTest, paclet:ref/ZTest])[data] 
tests whether the mean of the data is zero. }, {, (RefLink[ZTest, paclet:ref/ZTest])[{(Subscript[data, 1]),(Subscript[data, 2])}]
tests whether the means of (Subscript[data, 1]) and (Subscript[data, 2]) are equal.}, {, (RefLink[ZTest, paclet:ref/ZTest])[dspec,(σ^2)]
tests for zero or equal means assuming a population variance (σ^2).}, {, (RefLink[ZTest, paclet:ref/ZTest])[dspec,(σ^2),(Subscript[μ, 0])]
tests the mean against (Subscript[μ, 0]).}, {, (RefLink[ZTest, paclet:ref/ZTest])[dspec,(σ^2),(Subscript[μ, 0]),"property"]
returns the value of "property".}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[ZTest, paclet:ref/ZTest]) tests the null hypothesis (Subscript[H, 0]) against the alternative hypothesis (Subscript[H, a]):

where (Subscript[μ, i]) is the population mean for (Subscript[data, i]).

By default, a probability value or p-value is returned.

A small p-value suggests that it is unlikely that (Subscript[H, 0]) is true.

The data in dspec can be univariate {(Subscript[x, 1]),(Subscript[x, 2]),…} or multivariate {{(Subscript[x, 1]),(Subscript[y, 1]),…},{(Subscript[x, 2]),(Subscript[y, 2]),…},…}.

Given one dataset, the argument (σ^2) can be any positive real number or a positive definite matrix with dimension equal to the dimension of data.

Given two datasets, the argument (σ^2) can be any positive real number, a positive definite matrix with dimension equal to the dimension of dspec, or two such numbers or matrices.

The argument (Subscript[μ, 0]) can be a real number or a real vector with length equal to the dimension of the data.

(RefLink[ZTest, paclet:ref/ZTest]) assumes that the data is normally distributed and that the variance is known and not estimated from the data.

If variances or covariance matrices are not provided, (RefLink[ZTest, paclet:ref/ZTest]) treats the sample estimate as the known variance or covariance.

(RefLink[ZTest, paclet:ref/ZTest])[dspec,(σ^2),(Subscript[μ, 0]),"HypothesisTestData"] returns a (RefLink[HypothesisTestData, paclet:ref/HypothesisTestData]) object htd that can be used to extract additional test results and properties using the form htd["property"].

(RefLink[ZTest, paclet:ref/ZTest])[dspec,(σ^2),(Subscript[μ, 0]),"property"] can be used to directly give the value of "property".

Properties related to the reporting of test results include:

If a known variance (σ^2) is not provided, (RefLink[ZTest, paclet:ref/ZTest]) performs a z-test assuming the sample variance is the known variance for univariate data, and Hotelling's (T^2) test assuming the sample covariance is the known covariance for multivariate data.

Options include:

For tests of location, a cutoff α is chosen such that (Subscript[H, 0]) is rejected if and only if p<α. The value of α used for the "TestConclusion" and "ShortTestConclusion" properties is controlled by the (RefLink[SignificanceLevel, paclet:ref/SignificanceLevel]) option. This value α is also used in diagnostic tests of assumptions, including tests for normality, equal variance, and symmetry. By default, α is set to 0.05.

Named settings for (RefLink[VerifyTestAssumptions, paclet:ref/VerifyTestAssumptions]) in (RefLink[ZTest, paclet:ref/ZTest]) include:

OpenersPrimaryExamplesSection(Spacer1)Examples  (40)

OpenersExampleSection0(Spacer1)Basic Examples  (4)

Test for zero mean, assuming a known variance of 1:

SeedRandom[1];
data=RandomVariate[NormalDistribution[],100];

ZTest[data,1]

SmoothHistogram[data]

The mean of the dataset:

Mean[data]

Compare the means of two populations, assuming known variances of 4 and 16:

SeedRandom[1];
data1=RandomVariate[NormalDistribution[0,(Sqrt[4])],1000];
data2=RandomVariate[NormalDistribution[2,(Sqrt[16])],1000];

ZTest[{data1,data2},{4,16}]

The means are significantly different:

SmoothHistogram[{data1,data2}]

Compare the mean vectors of two multivariate populations:

SeedRandom[1];
data1=RandomVariate[MultinormalDistribution[{1,2,3},IdentityMatrix[3]],1000];
data2=RandomVariate[MultinormalDistribution[{1,2,3.1},IdentityMatrix[3]],2500];

ZTest[{data1,data2},IdentityMatrix[3]]

Compare the mean vector of a multivariate population to {1,2,3}:

SeedRandom[1];
data=RandomVariate[MultinormalDistribution[{3,2,1},IdentityMatrix[3]],(10^2)];

Mean[data]-{1,2,3}

The difference is significant:

ZTest[data,IdentityMatrix[3],{1,2,3}]

OpenersExampleSection1(Spacer1)Scope  (17)

OpenersExampleSubsection0(Spacer1)Testing  (14)

Set the known variance to 1:

SeedRandom[1];
data=RandomVariate[NormalDistribution[],100];

ZTest[data,1]

Setting the known variance to (RefLink[Automatic, paclet:ref/Automatic]) is equivalent to using the sample variance:

SeedRandom[1];
data=RandomVariate[NormalDistribution[],100];

ZTest[data,Variance[data]]

The result is the same:

ZTest[data,Automatic]

Specify a single common variance for two groups, or give a list of variances:

SeedRandom[1];
data1=RandomVariate[NormalDistribution[0,1],100];
data2=RandomVariate[NormalDistribution[0,1.5],100];

ZTest[{data1,data2},1]

ZTest[{data1,data2},{1,(1.5^2)}]

For multivariate data, covariance matrices can be given:

SeedRandom[1];
data1=RandomVariate[BinormalDistribution[.5],100];
data2=RandomVariate[BinormalDistribution[.25],100];

A single group covariance:

ZTest[{data1,data2},Covariance[BinormalDistribution[.5]]]

Separate covariance matrices for each dataset:

ZTest[{data1,data2},{Covariance[BinormalDistribution[.5]],Covariance[BinormalDistribution[.25]]}]

Test (Subscript[H, 0]): μ==0 versus (Subscript[H, a]): μ≠0:

SeedRandom[1];data1=RandomVariate[NormalDistribution[0,1],500];
data2=RandomVariate[NormalDistribution[3,1],500];

The p-values are typically large when the mean is close to (Subscript[μ, 0]):

ZTest[data1,1]

The p-values are typically small when the location is far from (Subscript[μ, 0]):

TTest[data2,1]

Using (RefLink[Automatic, paclet:ref/Automatic]) is equivalent to testing for a mean of zero:

data=RandomVariate[NormalDistribution[0,1],500];

ZTest[data,1,0]

ZTest[data,1,Automatic]

Test  (Subscript[H, 0]):μ==3 versus (Subscript[H, a]):μ≠3:

SeedRandom[1];
data1=RandomVariate[NormalDistribution[3,1],500];
data2=RandomVariate[NormalDistribution[0,1],200];

The p-values are typically large when the mean is close to (Subscript[μ, 0]):

ZTest[data1,1,3]

The p-values are typically small when the location is far from (Subscript[μ, 0]):

ZTest[data2,1,3]

Test whether the mean vector of a multivariate population is the zero vector:

SeedRandom[1];
data=RandomVariate[MultinormalDistribution[{.1,0,-.05,0},IdentityMatrix[4]],(10^3)];

ZTest[data,IdentityMatrix[4]]

Alternatively, test against {0.1,0,-0.05,0}:

ZTest[data,IdentityMatrix[4],{0.1,0,-.05,0}]

Test (Subscript[H, 0]):(Subscript[μ, 1])-(Subscript[μ, 2])==0  versus (Subscript[H, a]):(Subscript[μ, 1])-(Subscript[μ, 2])≠0:

SeedRandom[1];
data1=RandomVariate[NormalDistribution[0,1],100];
data2=RandomVariate[NormalDistribution[1,1],150];
data3=RandomVariate[NormalDistribution[0,2],130];

The p-values are generally small when the locations are not equal:

ZTest[{data1,data2},{1,1}]

The p-values are generally large when the locations are equal:

ZTest[{data1,data3},{1,4}]

Test (Subscript[H, 0]):(Subscript[μ, 1])-(Subscript[μ, 2])==3  versus (Subscript[H, a]):(Subscript[μ, 1])-(Subscript[μ, 2])≠3:

SeedRandom[1];
data1=RandomVariate[NormalDistribution[3,1],135];
data2=RandomVariate[NormalDistribution[0,1],100];

The order of the datasets affects the test results:

ZTest[{data1,data2},1,3]

ZTest[{data2,data1},1,3]

Test whether the mean difference vector of two multivariate populations is the zero vector:

SeedRandom[1];
data1=RandomVariate[MultinormalDistribution[{.5,0,-.5,0},IdentityMatrix[4]],122];

data2=RandomVariate[MultinormalDistribution[{-.5,0,.5,0},IdentityMatrix[4]],135];

ZTest[{data1,data2},IdentityMatrix[4]]

Alternatively, test against {1,0,-1,0}:

ZTest[{data1,data2},IdentityMatrix[4],{1,0,-1,0}]

Create a (RefLink[HypothesisTestData, paclet:ref/HypothesisTestData]) object for repeated property extraction:

SeedRandom[1];
data=RandomVariate[NormalDistribution[],{2,(10^4)}];

ℋ=ZTest[data,1,0,"HypothesisTestData"];

The properties available for extraction:

ℋ["Properties"]

Extract some properties from a (RefLink[HypothesisTestData, paclet:ref/HypothesisTestData]) object:

SeedRandom[2];
data=RandomVariate[NormalDistribution[],{2,(10^4)}];

ℋ=ZTest[data,1,0,"HypothesisTestData"];

The p-value and test statistic:

ℋ["PValue"]

ℋ["TestStatistic"]

Extract any number of properties simultaneously:

SeedRandom[2];
data=RandomVariate[NormalDistribution[],{2,(10^4)}];

ℋ=ZTest[data,1,0,"HypothesisTestData"];

The p-value and test statistic:

ℋ["PValue","TestStatistic"]

OpenersExampleSubsection1(Spacer1)Reporting  (3)

Tabulate the test results:

SeedRandom[1];
data=RandomVariate[NormalDistribution[],{2,20}];

ℋ=ZTest[data,1,0,"HypothesisTestData"];

ℋ["TestDataTable"]

Retrieve the entries from a test table for customized reporting:

SeedRandom[1];
data=RandomVariate[NormalDistribution[],{1000,25}];

res=Table[Quiet@ZTest[i,1,0,"TestData"],{i,data}];

ListPlot[res,FrameLabel->{"Z","p-value"},Frame->True,PlotRange->All]

Tabulate p-values or test statistics:

SeedRandom[1];
data=RandomVariate[NormalDistribution[],{2,(10^2)}];

ℋ=ZTest[data,1,0,"HypothesisTestData"];

ℋ["PValueTable"]

The p-value from the table:

ℋ["PValue"]

ℋ["TestStatisticTable"]

The test statistic from the table:

ℋ["TestStatistic"]

OpenersExampleSection2(Spacer1)Options  (9)

OpenersExampleSubsection2(Spacer1)AlternativeHypothesis  (3)

A two-sided test is performed by default:

SeedRandom[1];
data=RandomVariate[NormalDistribution[],100];

Test (Subscript[H, 0]):μ==0 versus (Subscript[H, a]):μ≠0:

ZTest[data,1,0,AlternativeHypothesis->"Unequal"]

ZTest[data,1,0,AlternativeHypothesis->Automatic]

Perform a two-sided test or a one-sided alternative:

SeedRandom[1];
data=RandomVariate[NormalDistribution[],100];

Test (Subscript[H, 0]):μ==0 versus (Subscript[H, a]):μ≠0:

ZTest[data,1,0,AlternativeHypothesis->"Unequal"]

Test (Subscript[H, 0]):μ≥0 versus (Subscript[H, a]):μ<0:

ZTest[data,1,0,AlternativeHypothesis->"Less"]

Test (Subscript[H, 0]):μ≤0 versus (Subscript[H, a]):μ>0:

ZTest[data,1,0,AlternativeHypothesis->"Greater"]

Perform tests with one-sided alternatives when (Subscript[μ, 0]) is given:

SeedRandom[1];
data1=RandomVariate[NormalDistribution[2.9,1],1000];
data2=RandomVariate[NormalDistribution[0,1],1000];

Mean[data1]-Mean[data2]

Test (Subscript[H, 0]):(Subscript[μ, 1])-(Subscript[μ, 2])≥3  versus (Subscript[H, a]):(Subscript[μ, 1])-(Subscript[μ, 2])<3:

ZTest[{data1,data2},2,3,AlternativeHypothesis->"Less"]

Test (Subscript[H, 0]):(Subscript[μ, 1])-(Subscript[μ, 2])≥2.9  versus (Subscript[H, a]):(Subscript[μ, 1])-(Subscript[μ, 2])<2.9:

ZTest[{data1,data2},2,2.9,AlternativeHypothesis->"Less"]

OpenersExampleSubsection3(Spacer1)SignificanceLevel  (2)

Set the significance level for diagnostic tests:

data=BlockRandom[SeedRandom[1];RandomVariate[StudentTDistribution[3],50]];

ZTest[data,3,0,SignificanceLevel->.0001]

By default, 0.05 is used:

ZTest[data,3,0]

The significance level is also used for "TestConclusion" and "ShortTestConclusion":

BlockRandom[SeedRandom[1];data=RandomVariate[NormalDistribution[0,1],100]];

Mean[data]

ℋ1=ZTest[data,1,.2,"HypothesisTestData",SignificanceLevel->.1];

ℋ2=ZTest[data,1,.2,"HypothesisTestData",SignificanceLevel->.005];

ℋ1["TestConclusion"]//TraditionalForm

ℋ2["TestConclusion"]//TraditionalForm

ℋ1["ShortTestConclusion"]

ℋ2["ShortTestConclusion"]

OpenersExampleSubsection4(Spacer1)VerifyTestAssumptions  (4)

Control whether to verify the assumption of normality:

SeedRandom[1];
data1=RandomVariate[NormalDistribution[0,1],100];
data2=RandomVariate[NormalDistribution[0,2],100];

Assume normality:

ZTest[{data1,data2},{1,4},0,VerifyTestAssumptions->None]

Check for normality:

ZTest[{data1,data2},{1,4},0,VerifyTestAssumptions->"Normality"]

Alternatively:

ZTest[{data1,data2},{1,4},0,VerifyTestAssumptions->All]

Unlisted assumptions are not tested:

SeedRandom[1];
data=RandomVariate[CauchyDistribution[0,1],100];

Normality is assumed:

ZTest[data,1,0,VerifyTestAssumptions->None]

The result is the same but a warning is issued:

ZTest[data,1,0,VerifyTestAssumptions->All]

Bypassing diagnostic tests can save compute time:

SeedRandom[1];
data=RandomVariate[MultinormalDistribution[{0,0,0},IdentityMatrix[3]],10^4];

ZTest[data,IdentityMatrix[3],Automatic,"TestDataTable",VerifyTestAssumptions->All]//AbsoluteTiming

ZTest[data,IdentityMatrix[3],Automatic,"TestDataTable",VerifyTestAssumptions->None]//AbsoluteTiming

It is often useful to bypass diagnostic tests for simulation purposes:

SeedRandom[1];
data=RandomVariate[NormalDistribution[],{1000,100}];

AbsoluteTiming[T=Quiet@ZTest[#,1,Automatic,"TestStatistic"]&/@data;]

The assumptions of the test hold by design, so a great deal of time can be saved:

AbsoluteTiming[T2=ZTest[#,1,Automatic,"TestStatistic",VerifyTestAssumptions->None]&/@data;]

The results are identical:

SmoothHistogram[{T,T2}]

OpenersExampleSection3(Spacer1)Applications  (1)

A factory creates 10-inch fuse casings on two separate lines. The length of these casings has a known variance of 0.005 inches squared. In a quality control test, 25 casings were randomly selected from each of the two lines. A line is shut down and recalibrated if there is a significant deviation from 10 inches:

sample1=QuantityArray[{9.9001,9.878,9.982,9.9628,10.0053,10.1056,10.0212,10.0037,9.905,9.898,10.052,10.0076,9.9747,10.0453,9.9844,9.9881,10.0139,9.9870,10.0779,10.1126,10.0445,9.9927,9.9901,10.0117,9.9705},"Inches"];

sample2=QuantityArray[{10.0711,10.1043,10.0809,10.0456,10.0241,10.1772,10.0078,10.2123,10.0523,10.1894,10.1357,10.1753,10.1479,10.2104,10.085,10.1268,10.0214,10.1708,10.0627,10.0506,10.0398,10.0476,10.1446,10.0742,10.1573},"Inches"];

SmoothHistogram[{sample1,sample2}//QuantityMagnitude]

The first line performs as expected; the second needs to be recalibrated:

ZTest[sample1,(Quantity[0.005, (("in")^2), inches squared, (("Inches")^2)]),(Quantity[10, "in", inches, "Inches"])]

ZTest[sample2,(Quantity[0.005, (("in")^2), inches squared, (("Inches")^2)]),(Quantity[10, "in", inches, "Inches"])]

Suppose it is known that the factory machinery produces longer casings after prolonged use. The one-sided alternative "Greater" is appropriate:

ZTest[sample1,(Quantity[0.005, (("in")^2), inches squared, (("Inches")^2)]),(Quantity[10, "in", inches, "Inches"]),AlternativeHypothesis->"Greater"]

ZTest[sample2,(Quantity[0.005, (("in")^2), inches squared, (("Inches")^2)]),(Quantity[10, "in", inches, "Inches"]),AlternativeHypothesis->"Greater"]

OpenersExampleSection4(Spacer1)Properties & Relations  (6)

The univariate test statistic for one sample:

n=100;σ=2;μ0=.5;

SeedRandom[1];
data=RandomVariate[NormalDistribution[0,σ],n];

μ=Mean[data]

z=((μ-μ0)/(Sqrt[(σ^2)/n]))

The test statistic follows a (RefLink[NormalDistribution, paclet:ref/NormalDistribution])[0,1] under (Subscript[H, 0]):

pvalue=2SurvivalFunction[NormalDistribution[],Abs@z]

ZTest[data,(σ^2),μ0,"TestDataTable"]

The multivariate test statistic for one sample:

n=100;Σ={{1,-.25},{-.25,2}};μ0={.5,2};

SeedRandom[1];
data=RandomVariate[MultinormalDistribution[{1,2},Σ],n];

μ=Mean[data]

z=n(μ-μ0).Inverse[Σ].(μ-μ0)

Under (Subscript[H, 0]), z\[Distributed](RefLink[ChiSquareDistribution, paclet:ref/ChiSquareDistribution])[p] where p is the dimension of the data:

dim=Dimensions[data][[2]];

SurvivalFunction[ChiSquareDistribution[dim],z]

ZTest[data,Σ,μ0,"TestDataTable"]

If variance is not specified, the test statistic is equivalent to that of (RefLink[TTest, paclet:ref/TTest]):

SeedRandom[1];
data=RandomVariate[NormalDistribution[],100];

ZTest[data,Automatic,0,"TestStatistic"]//Quiet

TTest[data,0,"TestStatistic"]

The p-values are not equivalent:

ZTest[data,Automatic,0]//Quiet

TTest[data]

In general, (RefLink[ZTest, paclet:ref/ZTest]) is more powerful than (RefLink[TTest, paclet:ref/TTest]):

μ0=1;

SeedRandom[1];
data=RandomVariate[NormalDistribution[],{1000,10}];

pZ=ZTest[#,1,μ0,VerifyTestAssumptions->None]&/@data;

pT=TTest[#,μ0,VerifyTestAssumptions->None]&/@data;

Probability[z<t,{z,t}\[Distributed]EmpiricalDistribution[Transpose[{pZ,pT}]]]

(RefLink[ZTest, paclet:ref/ZTest]) works with the values only when the input is a (RefLink[TimeSeries, paclet:ref/TimeSeries]):

ts=TimeSeries[];

ZTest[ts]

ZTest[ts["Values"]]

(RefLink[ZTest, paclet:ref/ZTest]) works with all the values together when the input is a (RefLink[TemporalData, paclet:ref/TemporalData]):

td=TemporalData[];

ZTest[td]

Test all the values only:

data=td["ValueList"]//Flatten;
ZTest[data]

Test whether the means of the two paths are equal:

{data1,data2}=td["ValueList"];

ZTest[{data1,data2}]

OpenersExampleSection5(Spacer1)Possible Issues  (2)

The test statistic does not follow a (RefLink[NormalDistribution, paclet:ref/NormalDistribution])[0,1] if the sample variance is used:

data=RandomVariate[NormalDistribution[],10];

ZTest[data,Automatic,0,"TestDataTable"]

(RefLink[TTest, paclet:ref/TTest]) gives the appropriate p-value when the sample variance is used:

TTest[data,0,"TestDataTable"]

The data should follow a (RefLink[NormalDistribution, paclet:ref/NormalDistribution]):

SeedRandom[1];data=RandomVariate[StudentTDistribution[3],100];

ZTest[data,3,0,"TestDataTable"]

Use median-based tests if the data is not normally distributed:

SignTest[data,0,"TestDataTable"]

SignedRankTest[data,0,"TestDataTable"]

(RefLink[LocationTest, paclet:ref/LocationTest]) will pick an appropriate test automatically:

LocationTest[data,0,"TestDataTable"]

OpenersExampleSection6(Spacer1)Neat Examples  (1)

Compute the statistic when the null hypothesis (Subscript[H, 0]) is true:

data1=RandomVariate[NormalDistribution[],{250,100}];
data2=RandomVariate[NormalDistribution[1,2],{250,100}];

T1=MapThread[ZTest[{#1,#2},1,0,"TestStatistic",VerifyTestAssumptions->None]&,{data1,data2}];

The test statistic given a particular alternative:

T2=MapThread[ZTest[{#1,#2},1,2,"TestStatistic",VerifyTestAssumptions->None]&,{data1,data2}];

Compare the distributions of the test statistics:

SmoothHistogram[{T1,T2},Filling->Axis,PlotLegends->{"(Subscript[H, 0]) is True","(Subscript[H, 0]) is False"}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[HypothesisTestData, paclet:ref/HypothesisTestData])  ▪  (RefLink[LocationTest, paclet:ref/LocationTest])  ▪  (RefLink[LocationEquivalenceTest, paclet:ref/LocationEquivalenceTest])  ▪  (RefLink[VarianceTest, paclet:ref/VarianceTest])  ▪  (RefLink[VarianceEquivalenceTest, paclet:ref/VarianceEquivalenceTest])  ▪  (RefLink[DistributionFitTest, paclet:ref/DistributionFitTest])  ▪  (RefLink[MannWhitneyTest, paclet:ref/MannWhitneyTest])  ▪  (RefLink[PairedTTest, paclet:ref/PairedTTest])  ▪  (RefLink[PairedZTest, paclet:ref/PairedZTest])  ▪  (RefLink[SignTest, paclet:ref/SignTest])  ▪  (RefLink[SignedRankTest, paclet:ref/SignedRankTest])  ▪  (RefLink[TTest, paclet:ref/TTest])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Hypothesis Tests, paclet:guide/HypothesisTests])}})}})