QuickStart Samples

# Variance Tests QuickStart Sample (IronPython)

Illustrates how to perform hypothesis tests involving the standard deviation or variance using classes in our .NET statistical library in IronPython.

C# code Visual Basic code F# code Back to QuickStart Samples

import numerics from Extreme.Mathematics import * from Extreme.Statistics import * from Extreme.Statistics.Tests import * #/ Demonstrates how to use hypothesis tests for the variance #/ of one or two distributions. # This QuickStart Sample uses the scores obtained by the students # in two groups of students on a national test. # # We want to know if the variance of the scores is greater than # a specific value. We use the one sample Chi-square test for this # purpose. print "Tests for class 1" # First we create a NumericalVariable that holds the test results. group1Data = Vector([ \ 62, 77, 61, 94, 75, 82, 86, 83, 64, 84, \ 68, 82, 72, 71, 85, 66, 61, 79, 81, 73 ]) group1Results = NumericalVariable("Class 1", group1Data) # We can get the mean and standard deviation of the class right away: print "Mean for the class: {0:.1f}".format(group1Results.Mean) print "Standard deviation: {0:.1f}".format(group1Results.StandardDeviation) # # One Sample Chi-square Test # print "\nUsing chi-square test:" # We want to know if the standard deviation is larger than 15. # Therefore, we use a one-tailed chi-square test: chiSquareTest = OneSampleChiSquareTest(group1Results, 225, HypothesisType.OneTailedUpper) # We can obtan the value of the test statistic through the Statistic property, # and the corresponding P-value through the Probability property: print "Test statistic: {0:.4f}".format(chiSquareTest.Statistic) print "P-value: {0:.4f}".format(chiSquareTest.PValue) # The significance level is the default value of 0.05: print "Significance level: {0:.2f}".format(chiSquareTest.SignificanceLevel) # We can now print the test results: print "Reject null hypothesis?", "yes" if chiSquareTest.Reject() else "no" # We can get a confidence interval for the current significance level: varianceInterval = chiSquareTest.GetConfidenceInterval() print "95% Confidence interval for the variance: {0:.1f} - {1:.1f}".format( \ varianceInterval.LowerBound, varianceInterval.UpperBound) # We can get the same results for the 0.01 significance level by explicitly # passing the significance level as a parameter to these methods: print "Significance level: {0:.2f}".format(0.01) print "Reject null hypothesis?", "yes" if chiSquareTest.Reject(0.01) else "no" # The GetConfidenceInterval method needs the confidence level, which equals # 1 - the significance level: varianceInterval = chiSquareTest.GetConfidenceInterval(0.99) print "99% Confidence interval for the variance: {0:.1f} - {1:.1f}".format( \ varianceInterval.LowerBound, varianceInterval.UpperBound) # # Two sample F-test # print "\nUsing F-test:" # We want to compare the scores of the first group to the scores # of a second group from another school. We want to verify that the # variances of the scores from the two schools are equal. Once again, # we start by creating a NumericalVariable, this time containing # the scores for the second group: group2Data = Vector([ \ 61, 80, 98, 90, 94, 65, 79, 75, 74, 86, 76, \ 85, 78, 72, 76, 79, 65, 92, 76, 80 ]) group2Results = NumericalVariable("Class 2", group2Data) # To compare the variances of the two groups, we need the two sample # F test, implemented by the FTest class: fTest = FTest(group1Results, group2Results) # We can obtan the value of the test statistic through the Statistic property, # and the corresponding P-value through the Probability property: print "Test statistic: {0:.4f}".format(fTest.Statistic) print "P-value: {0:.4f}".format(fTest.PValue) # The significance level is the default value of 0.05: print "Significance level: {0:.2f}".format(fTest.SignificanceLevel) # We can now print the test results: print "Reject null hypothesis?", "yes" if fTest.Reject() else "no"

Copyright Â© 2003-2018, 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.