TwoSampleKolmogorovSmirnovTest Class 
Namespace: Accord.Statistics.Testing
[SerializableAttribute] public class TwoSampleKolmogorovSmirnovTest : HypothesisTest<KolmogorovSmirnovDistribution>
The TwoSampleKolmogorovSmirnovTest type exposes the following members.
Name  Description  

TwoSampleKolmogorovSmirnovTest(Double, Double) 
Creates a new TwoSample Kolmogorov test.
 
TwoSampleKolmogorovSmirnovTest(Double, Double, TwoSampleKolmogorovSmirnovTestHypothesis) 
Creates a new TwoSample Kolmogorov test.

Name  Description  

CriticalValue 
Gets the critical value for the current significance level.
(Inherited from HypothesisTestTDistribution.)  
EmpiricalDistribution1 
Gets the first empirical distribution being tested.
 
EmpiricalDistribution2 
Gets the second empirical distribution being tested.
 
Hypothesis 
Gets the alternative hypothesis under test. If the test is
Significant, the null hypothesis can be rejected
in favor of this alternative hypothesis.
 
PValue 
Gets the Pvalue associated with this test.
(Inherited from HypothesisTestTDistribution.)  
Significant 
Gets whether the null hypothesis should be rejected.
(Inherited from HypothesisTestTDistribution.)  
Size 
Gets the significance level for the
test. Default value is 0.05 (5%).
(Inherited from HypothesisTestTDistribution.)  
Statistic 
Gets the test statistic.
(Inherited from HypothesisTestTDistribution.)  
StatisticDistribution 
Gets the distribution associated
with the test statistic.
(Inherited from HypothesisTestTDistribution.)  
Tail 
Gets the test type.
(Inherited from HypothesisTestTDistribution.) 
Name  Description  

Equals  Determines whether the specified object is equal to the current object. (Inherited from Object.)  
Finalize  Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)  
GetHashCode  Serves as the default hash function. (Inherited from Object.)  
GetType  Gets the Type of the current instance. (Inherited from Object.)  
MemberwiseClone  Creates a shallow copy of the current Object. (Inherited from Object.)  
OnSizeChanged 
Called whenever the test significance level changes.
(Inherited from HypothesisTestTDistribution.)  
PValueToStatistic 
Converts a given pvalue to a test statistic.
(Overrides HypothesisTestTDistributionPValueToStatistic(Double).)  
StatisticToPValue 
Converts a given test statistic to a pvalue.
(Overrides HypothesisTestTDistributionStatisticToPValue(Double).)  
ToString 
Converts the numeric PValue of this test to its equivalent string representation.
(Inherited from HypothesisTestTDistribution.)  
ToString(String, IFormatProvider) 
Converts the numeric PValue of this test to its equivalent string representation.
(Inherited from HypothesisTestTDistribution.) 
Name  Description  

HasMethod 
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)  
IsEqual  Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices. (Defined by Matrix.)  
ToT 
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) 
The KolmogorovSmirnov test tries to determine if two samples have been drawn from the same probability distribution. The KolmogorovSmirnov test has an interesting advantage in which it does not requires any assumptions about the data. The distribution of the KS test statistic does not depend on which distribution is being tested.
The KS test has also the advantage of being an exact test (other tests, such as the chisquare goodnessoffit test depends on an adequate sample size). One disadvantage is that it requires a fully defined distribution which should not have been estimated from the data. If the parameters of the theoretical distribution have been estimated from the data, the critical region of the KS test will be no longer valid.
The twosample KS test is one of the most useful and general nonparametric methods for comparing two samples, as it is sensitive to differences in both location and shape of the empirical cumulative distribution functions of the two samples.
This class uses an efficient and highaccuracy algorithm based on work by Richard Simard (2010). Please see KolmogorovSmirnovDistribution for more details.
References:
In the following example, we will be creating a KS test to verify if two samples have been drawn from different populations. In this example, we will first generate a number of samples from two different distributions and then check if the KS can indeed see the difference between them:
// Generate 15 points from a Normal distribution with mean 5 and sigma 2 double[] sample1 = new NormalDistribution(mean: 5, stdDev: 1).Generate(25); // Generate 15 points from an uniform distribution from 0 to 10 double[] sample2 = new UniformContinuousDistribution(a: 0, b: 10).Generate(25); // Now we can create a KS test and test the unequal hypothesis: var test = new TwoSampleKolmogorovSmirnovTest(sample1, sample2, TwoSampleKolmogorovSmirnovTestHypothesis.SamplesDistributionsAreUnequal); bool significant = test.Significant; // outputs true
The following example comes from the stats page of the College of Saint Benedict and Saint John's University (Kirkman, 1996). It is a very interesting example as it shows a case in which a ttest fails to see a difference between the samples because of the nonnormality of the sample's distributions. The KolmogorovSmirnov nonparametric test, on the other hand, succeeds.
The example deals with the preference of bees between two nearby blooming trees in an empty field. The experimenter has collected data measuring how much time does a bee spent near a particular tree. The time starts to be measured when a bee first touches the tree, and is stopped when the bee moves more than 1 meter far from it. The samples below represents the measured time, in seconds, of the observed bees for each of the trees.
double[] redwell = { 23.4, 30.9, 18.8, 23.0, 21.4, 1, 24.6, 23.8, 24.1, 18.7, 16.3, 20.3, 14.9, 35.4, 21.6, 21.2, 21.0, 15.0, 15.6, 24.0, 34.6, 40.9, 30.7, 24.5, 16.6, 1, 21.7, 1, 23.6, 1, 25.7, 19.3, 46.9, 23.3, 21.8, 33.3, 24.9, 24.4, 1, 19.8, 17.2, 21.5, 25.5, 23.3, 18.6, 22.0, 29.8, 33.3, 1, 21.3, 18.6, 26.8, 19.4, 21.1, 21.2, 20.5, 19.8, 26.3, 39.3, 21.4, 22.6, 1, 35.3, 7.0, 19.3, 21.3, 10.1, 20.2, 1, 36.2, 16.7, 21.1, 39.1, 19.9, 32.1, 23.1, 21.8, 30.4, 19.62, 15.5 }; double[] whitney = { 16.5, 1, 22.6, 25.3, 23.7, 1, 23.3, 23.9, 16.2, 23.0, 21.6, 10.8, 12.2, 23.6, 10.1, 24.4, 16.4, 11.7, 17.7, 34.3, 24.3, 18.7, 27.5, 25.8, 22.5, 14.2, 21.7, 1, 31.2, 13.8, 29.7, 23.1, 26.1, 25.1, 23.4, 21.7, 24.4, 13.2, 22.1, 26.7, 22.7, 1, 18.2, 28.7, 29.1, 27.4, 22.3, 13.2, 22.5, 25.0, 1, 6.6, 23.7, 23.5, 17.3, 24.6, 27.8, 29.7, 25.3, 19.9, 18.2, 26.2, 20.4, 23.3, 26.7, 26.0, 1, 25.1, 33.1, 35.0, 25.3, 23.6, 23.2, 20.2, 24.7, 22.6, 39.1, 26.5, 22.7 }; // Create a ttest as a first attempt. var t = new TwoSampleTTest(redwell, whitney); Console.WriteLine("TTest"); Console.WriteLine("Test pvalue: " + t.PValue); // ~0.837 Console.WriteLine("Significant? " + t.Significant); // false // Create a nonparametric KolmogorovSmirnov test var ks = new TwoSampleKolmogorovSmirnovTest(redwell, whitney); Console.WriteLine("KSTest"); Console.WriteLine("Test pvalue: " + ks.PValue); // ~0.038 Console.WriteLine("Significant? " + ks.Significant); // true