Extreme Optimization™: Complexity made simple.

Math and Statistics
Libraries for .NET

  • Home
  • Features
    • Math Library
    • Vector and Matrix Library
    • Statistics Library
    • Performance
    • Usability
  • Documentation
    • Introduction
    • Math Library User's Guide
    • Vector and Matrix Library User's Guide
    • Data Analysis Library User's Guide
    • Statistics Library User's Guide
    • Reference
  • Resources
    • Downloads
    • QuickStart Samples
    • Sample Applications
    • Frequently Asked Questions
    • Technical Support
  • Blog
  • Order
  • Company
    • About us
    • Testimonials
    • Customers
    • Press Releases
    • Careers
    • Partners
    • Contact us
Introduction
Deployment Guide
Nuget packages
Configuration
Using Parallelism
Expand Mathematics Library User's GuideMathematics Library User's Guide
Expand Vector and Matrix Library User's GuideVector and Matrix Library User's Guide
Expand Data Analysis Library User's GuideData Analysis Library User's Guide
Expand Statistics Library User's GuideStatistics Library User's Guide
Expand Data Access Library User's GuideData Access Library User's Guide
Expand ReferenceReference
  • Extreme Optimization
    • Features
    • Solutions
    • Documentation
    • QuickStart Samples
    • Sample Applications
    • Downloads
    • Technical Support
    • Download trial
    • How to buy
    • Blog
    • Company
    • Resources
  • Documentation
    • Introduction
    • Deployment Guide
    • Nuget packages
    • Configuration
    • Using Parallelism
    • Mathematics Library User's Guide
    • Vector and Matrix Library User's Guide
    • Data Analysis Library User's Guide
    • Statistics Library User's Guide
    • Data Access Library User's Guide
    • Reference
  • Statistics Library User's Guide
    • Statistical Variables
    • Numerical Variables
    • Statistical Models
    • Regression Analysis
    • Analysis of Variance
    • Time Series Analysis
    • Multivariate Analysis
    • Continuous Distributions
    • Discrete Distributions
    • Multivariate Distributions
    • Kernel Density Estimation
    • Hypothesis Tests
    • Appendices
  • Hypothesis Tests
    • Hypothesis Test Basics
    • Testing Means
    • Testing Variances
    • Testing Goodness-Of-Fit
    • Testing Homogeneity of Variances
    • Non-Parametric Tests
    • Testing for Outliers
  • Testing Variances

Testing Variances

Extreme Optimization Numerical Libraries for .NET Professional

The one sample chi-square test is used to test the hypothesis that the population from which a sample is drawn has a specified variance or standard deviation. The F test compares the variances of two samples. Other tests, such as Levene's test and Bartlett's test, are used to compare the variances of multiple samples. They are covered in their own section on testing equality of variances.

The One Sample Chi-Square Test

The one sample chi-square test is used to test the hypothesis that a sample comes from a population with a specified variance or standard deviation. The test is based on the assumption that the sample is randomly selected from the population, and that the population itself follows a normal distribution. If either of these assumptions is violated, the reliability of the chi Square test may be compromised.

The null hypothesis is always that the population underlying the sample has a variance that is equal to the proposed variance. The alternative hypothesis depends on whether a one or two-tailed test is performed. For the one-tailed test, the alternative hypothesis is that the population from which the sample was drawn has a variance that is either less than (lower tailed) or greater than (upper tailed) the proposed variance. For the two-tailed version, the alternative hypothesis is that the variance of the population does not equal the proposed variance.

This test should not be confused with the chi-square goodness-of-fit test, which is used to verify that a sample comes from a specified distribution.

The one sample chi-square test is implemented by the OneSampleChiSquareTest class. It has five constructors. The first constructor takes no arguments. The data and conditions for the test must be specified by setting properties of the OneSampleChiSquareTest object.

The remaining four constructors can be divided into two pairs. The second and third constructors take 2 or 3 arguments. The first argument is a VectorT that specifies the sample. The second argument is the proposed variance. The third, optional argument is a HypothesisType value that specifies whether the test is one or two-tailed. The default value is TwoTailed.

The last two constructors take 3 or 4 arguments. The first argument is an integer that specifies the size of the sample. The second argument specifies the variance of the sample. The third parameter specifies the variance to test again. The optional fourth argument is a HypothesisType value that specifies whether the test is one or two-tailed. The default value is TwoTailed.

Example

The test scores of a class on a national test are as follows:

61, 77, 61, 90, 72, 51, 75, 83, 53, 82, 82, 66, 68, 57, 61, 61, 78, 69, 65.

We want to investigate if the variance of the scores is greater than 5. The following code performs the test:

C#
VB
C++
F#
Copy
var results = Vector.Create<double>(62, 77, 61, 94, 75, 82,
    86, 83, 64, 84, 68, 82, 72, 71, 85, 66, 61, 79, 81, 73);
var chiSquareTest = new OneSampleChiSquareTest(results, 5.0);
Console.WriteLine("Test statistic: {0:F4}", chiSquareTest.Statistic);
Console.WriteLine("P-value:        {0:F4}", chiSquareTest.PValue);
Console.WriteLine("Reject null hypothesis? {0}",
    chiSquareTest.Reject() ? "yes" : "no");
Dim results = Vector.Create(Of Double)(62, 77, 61, 94, 75, 82,
    86, 83, 64, 84, 68, 82, 72, 71, 85, 66, 61, 79, 81, 73)
Dim chiSquareTest = New OneSampleChiSquareTest(results, 5.0)
Console.WriteLine("Test statistic: {0:F4}", chiSquareTest.Statistic)
Console.WriteLine("P-value:        {0:F4}", chiSquareTest.PValue)
Console.WriteLine("Reject null hypothesis? {0}",
    IIf(chiSquareTest.Reject(), "yes", "no"))

No code example is currently available or this language may not be supported.

let results = Vector.Create(
                62., 77., 61., 94., 75., 82., 86., 83., 
                64., 84., 68., 82., 72., 71., 85., 66., 
                61., 79., 81., 73.)
let chiSquareTest = OneSampleChiSquareTest(results, 5.0)
printfn "Test statistic: %.4f" chiSquareTest.Statistic
printfn "P-value:        %.4f" chiSquareTest.PValue
printfn "Reject null hypothesis? %s"
    (if chiSquareTest.Reject() then "yes" else "no")

The value of the chi square statistic turns out to be -2.4505 giving a p-value of 0.0143. As a result, the hypothesis that on average, the students in this class score no different than the national average is rejected at the 0.05 level.

Using pre-calculated values for the variance and sample size, the above example would look like this:

C#
VB
C++
F#
Copy
double variance = results.Variance();
int sampleSize = 20;
chiSquareTest = new OneSampleChiSquareTest(sampleSize, variance, 5.0);
Dim variance = results.Variance()
Dim sampleSize = 20
chiSquareTest = New OneSampleChiSquareTest(sampleSize, variance, 5.0)

No code example is currently available or this language may not be supported.

let variance = results.Variance()
let sampleSize = 20
let chiSquareTest2 = OneSampleChiSquareTest(sampleSize, variance, 5.0)

Once a OneSampleChiSquareTest object has been created, you can access other properties and methods common to all hypothesis test classes. For instance, to obtain a 95% confidence interval around the variance, the code would be:

C#
VB
C++
F#
Copy
var varianceInterval = chiSquareTest.GetConfidenceInterval();
Console.WriteLine("95% Confidence interval for the variance: {0:F1} - {1:F1}",
    varianceInterval.LowerBound, varianceInterval.UpperBound);
Dim varianceInterval = chiSquareTest.GetConfidenceInterval()
Console.WriteLine("95% Confidence interval for the variance: {0:F1} - {1:F1}",
    varianceInterval.LowerBound, varianceInterval.UpperBound)

No code example is currently available or this language may not be supported.

let varianceInterval = chiSquareTest.GetConfidenceInterval()
printfn "95%% Confidence interval for the variance: %.1f - %.1f"
    varianceInterval.LowerBound varianceInterval.UpperBound
The F-Test

The F-test is a two sample test that is used to test the hypothesis that the variances of two populations are equal. The test is based on the assumption that the sample is randomly selected from the population, and that the population itself follows a normal distribution. If either of these assumptions is violated, the reliability of the chi square test may be compromised.

The null hypothesis is always that the two populations have the same variance. The alternative hypothesis depends on whether a one or two-tailed test is performed.

For the one-tailed test, the alternative hypothesis is that the variance of the population from which the first sample was drawn is either less than (lower tailed) or greater than (upper tailed) the variance of the population from which the second sample was drawn. For the two-tailed version, the alternative hypothesis is that the variance of the two population are not equal.

The F test was named in honor of sir Ronald Fisher who created the foundations for much of modern statistical analysis. The F test is implemented by the FTest class. It has five constructors.

The first constructor takes no arguments. All test parameters must be provided by setting the properties of the FTest object.

The remaining four constructors can be divided into two pairs. The first pair has 2 or 3 arguments. The first two arguments are VectorT objects that represent the samples the test is to be applied to. The first constructor only has these two arguments. This creates a two-tailed test for equality of variances. The second constructor of this pair takes a third parameter: a HypothesisType value that specifies whether the test is one or two-tailed. One-tailed F tests are very common.

The second pair of constructors take 4 or 5 arguments. The first four arguments are, in order, the degrees of freedom and variance of the numerator, and the degrees of freedom and variance of the denominator. The fifth parameter, if present, is once again a HypothesisType value that specifies whether the test is one or two-tailed.

Example

Once again, we use the same data as before. However, this time we compare the results of one group of students to the results of a second group of students, with these test scores:

61, 80, 98, 90, 94, 65, 79, 75, 74, 86, 76, 85, 78, 72, 76, 79, 65, 92, 76, 80

We want to test if the variances of the two populations are equal. The code below performs this test:

C#
VB
C++
F#
Copy
var results2 = Vector.Create<double>(61, 80, 98, 90, 94, 65,
    79, 75, 74, 86, 76, 85, 78, 72, 76, 79, 65, 92, 76, 80);
var fTest = new FTest(results, results2);
Console.WriteLine("Test statistic: {0:F4}", fTest.Statistic);
Console.WriteLine("P-value:        {0:F4}", fTest.PValue);
Console.WriteLine("Reject null hypothesis? {0}",
    fTest.Reject() ? "yes" : "no");
Dim results2 = Vector.Create(Of Double)(61, 80, 98, 90, 94, 65,
    79, 75, 74, 86, 76, 85, 78, 72, 76, 79, 65, 92, 76, 80)
Dim FTest = New FTest(results, results2)
Console.WriteLine("Test statistic: {0:F4}", FTest.Statistic)
Console.WriteLine("P-value:        {0:F4}", FTest.PValue)
Console.WriteLine("Reject null hypothesis? {0}",
    IIf(FTest.Reject(), "yes", "no"))

No code example is currently available or this language may not be supported.

let results2 = Vector.Create(
                61., 80., 98., 90., 94., 65., 79., 
                75., 74., 86., 76., 85., 78., 72., 
                76., 79., 65., 92., 76., 80.)
let fTest = FTest(results, results2)
printfn "Test statistic: %.4f" fTest.Statistic
printfn "P-value:        %.4f" fTest.PValue
printfn "Reject null hypothesis? %s"
    (if fTest.Reject() then "yes" else "no")

The value of the F-statistic is 0.9573 giving a p-value of 0.5374. As a result, the hypothesis that the variance of the scores of students from the first group is no different than that of the second group is not rejected at the 0.05 level.

C#
VB
C++
F#
Copy
double numeratorDegreesOfFreedom = results.Length - 1;
double numeratorVariance = results.Variance();
double denominatorDegreesOfFreedom = results2.Length - 1;
double denominatorVariance = results2.Variance();
fTest = new FTest(numeratorDegreesOfFreedom, numeratorVariance,
    denominatorDegreesOfFreedom, denominatorVariance);
Dim numeratorDegreesOfFreedom = results.Length - 1
Dim numeratorVariance = results.Variance()
Dim denominatorDegreesOfFreedom = results2.Length - 1
Dim denominatorVariance = results2.Variance()
FTest = New FTest(numeratorDegreesOfFreedom, numeratorVariance,
    denominatorDegreesOfFreedom, denominatorVariance)

No code example is currently available or this language may not be supported.

let numeratorDegreesOfFreedom = float (results.Length - 1)
let numeratorVariance = results.Variance()
let denominatorDegreesOfFreedom = float (results2.Length - 1)
let denominatorVariance = results2.Variance()
let fTest2 = FTest(numeratorDegreesOfFreedom, numeratorVariance,
                denominatorDegreesOfFreedom, denominatorVariance)

Copyright (c) 2004-2021 ExoAnalytics Inc.

Send comments on this topic to support@extremeoptimization.com

Copyright © 2004-2021, Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, Visual Studio.NET, and the Optimized for Visual Studio logo
are registered trademarks of Microsoft Corporation.