Click or drag to resize
Accord.NET (logo)

KolmogorovSmirnovTest Class

One-sample Kolmogorov-Smirnov (KS) test.
Inheritance Hierarchy
SystemObject
  Accord.Statistics.TestingHypothesisTestKolmogorovSmirnovDistribution
    Accord.Statistics.TestingKolmogorovSmirnovTest

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

The KolmogorovSmirnovTest type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyCriticalValue
Gets the critical value for the current significance level.
(Inherited from HypothesisTestTDistribution.)
Public propertyEmpiricalDistribution
Gets the empirical distribution measured from the sample.
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.)
Public propertyTheoreticalDistribution
Gets the theoretical, hypothesized distribution for the samples, which should have been stated before any measurements.
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 methodStatic memberGetStatistic
Gets the appropriate Kolmogorov-Sminorv D statistic for the samples and target distribution.
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.)
Public methodStatic memberOneSideLower
Gets the one-sided "Dn-" Kolmogorov-Sminorv statistic for the samples and target distribution.
Public methodStatic memberOneSideUpper
Gets the one-sided "Dn+" Kolmogorov-Sminorv statistic for the samples and target distribution.
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.)
Public methodStatic memberTwoSide
Gets the two-sided "Dn" Kolmogorov-Sminorv statistic for the samples and target distribution.
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 MethodTo(Type)Overloaded.
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.)
Public Extension MethodToTOverloaded.
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 a sample differs significantly from an hypothesized theoretical 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.

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 this first example, suppose we got a new sample, and we would like to test whether this sample has been originated from a uniform continuous distribution.

double[] sample = 
{ 
    0.621, 0.503, 0.203, 0.477, 0.710, 0.581, 0.329, 0.480, 0.554, 0.382
};

// First, we create the distribution we would like to test against:
// 
var distribution = UniformContinuousDistribution.Standard;

// Now we can define our hypothesis. The null hypothesis is that the sample
// comes from a standard uniform distribution, while the alternate is that
// the sample is not from a standard uniform distribution.
// 
var kstest = new KolmogorovSmirnovTest(sample, distribution);

double statistic = kstest.Statistic; // 0.29
double pvalue = kstest.PValue;       // 0.3067

bool significant = kstest.Significant; // false

Since the null hypothesis could not be rejected, then the sample can perhaps be from a uniform distribution. However, please note that this doesn't means that the sample *is* from the uniform, it only means that we could not rule out the possibility.

Before we could not rule out the possibility that the sample came from a uniform distribution, which means the sample was not very far from uniform. This would be an indicative that it would be far from what would be expected from a Normal distribution:

// First, we create the distribution we would like to test against:
// 
NormalDistribution distribution = NormalDistribution.Standard;

// Now we can define our hypothesis. The null hypothesis is that the sample
// comes from a standard Normal distribution, while the alternate is that
// the sample is not from a standard Normal distribution.
// 
var kstest = new KolmogorovSmirnovTest(sample, distribution);

double statistic = kstest.Statistic; // 0.580432
double pvalue = kstest.PValue;       // 0.000999

bool significant = kstest.Significant; // true

Since the test says that the null hypothesis should be rejected, then this can be regarded as a strong indicative that the sample does not comes from a Normal distribution, just as we expected.

See Also