Click or drag to resize
Accord.NET (logo)

TwoSampleKolmogorovSmirnovTest Class

Two-sample Kolmogorov-Smirnov (KS) test.
Inheritance Hierarchy
SystemObject
  Accord.Statistics.TestingHypothesisTestKolmogorovSmirnovDistribution
    Accord.Statistics.TestingTwoSampleKolmogorovSmirnovTest

Namespace:  Accord.Statistics.Testing
Assembly:  Accord.Statistics (in Accord.Statistics.dll) Version: 3.7.0
Syntax
[SerializableAttribute]
public class TwoSampleKolmogorovSmirnovTest : HypothesisTest<KolmogorovSmirnovDistribution>
Request Example View Source

The TwoSampleKolmogorovSmirnovTest type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyCriticalValue
Gets the critical value for the current significance level.
(Inherited from HypothesisTestTDistribution.)
Public propertyEmpiricalDistribution1
Gets the first empirical distribution being tested.
Public propertyEmpiricalDistribution2
Gets the second empirical distribution being tested.
Public propertyHypothesis
Gets the alternative hypothesis under test. If the test is Significant, the null hypothesis can be rejected in favor of this alternative hypothesis.
Public propertyPValue
Gets the P-value associated with this test.
(Inherited from HypothesisTestTDistribution.)
Public propertySignificant
Gets whether the null hypothesis should be rejected.
(Inherited from HypothesisTestTDistribution.)
Public propertySize
Gets the significance level for the test. Default value is 0.05 (5%).
(Inherited from HypothesisTestTDistribution.)
Public propertyStatistic
Gets the test statistic.
(Inherited from HypothesisTestTDistribution.)
Public propertyStatisticDistribution
Gets the distribution associated with the test statistic.
(Inherited from HypothesisTestTDistribution.)
Public propertyTail
Gets the test type.
(Inherited from HypothesisTestTDistribution.)
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodOnSizeChanged
Called whenever the test significance level changes.
(Inherited from HypothesisTestTDistribution.)
Public methodPValueToStatistic
Converts a given p-value to a test statistic.
(Overrides HypothesisTestTDistributionPValueToStatistic(Double).)
Public methodStatisticToPValue
Converts a given test statistic to a p-value.
(Overrides HypothesisTestTDistributionStatisticToPValue(Double).)
Public methodToString
Converts the numeric P-Value of this test to its equivalent string representation.
(Inherited from HypothesisTestTDistribution.)
Public methodToString(String, IFormatProvider)
Converts the numeric P-Value of this test to its equivalent string representation.
(Inherited from HypothesisTestTDistribution.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodToT
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.)
Top
Remarks

The Kolmogorov-Smirnov test tries to determine if two samples have been drawn from the same probability distribution. The Kolmogorov-Smirnov test has an interesting advantage in which it does not requires any assumptions about the data. The distribution of the K-S test statistic does not depend on which distribution is being tested.

The K-S test has also the advantage of being an exact test (other tests, such as the chi-square goodness-of-fit 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 K-S test will be no longer valid.

The two-sample 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 high-accuracy algorithm based on work by Richard Simard (2010). Please see KolmogorovSmirnovDistribution for more details.

References:

Examples

In the following example, we will be creating a K-S 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 K-S 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 K-S 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 t-test fails to see a difference between the samples because of the non-normality of the sample's distributions. The Kolmogorov-Smirnov 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 t-test as a first attempt.
var t = new TwoSampleTTest(redwell, whitney);

Console.WriteLine("T-Test");
Console.WriteLine("Test p-value: " + t.PValue);    // ~0.837
Console.WriteLine("Significant? " + t.Significant); // false

// Create a non-parametric Kolmogorov-Smirnov test
var ks = new TwoSampleKolmogorovSmirnovTest(redwell, whitney);

Console.WriteLine("KS-Test");
Console.WriteLine("Test p-value: " + ks.PValue);    // ~0.038
Console.WriteLine("Significant? " + ks.Significant); // true
See Also