Sample independence test¶
In this example we are going to perform tests to assess whether two 1-d samples are independent or not.
The following tests are available:
the ChiSquared test: it tests if both scalar samples (discrete ones only) are independent. If is the number of values of the sample in the modality , , and the ChiSquared test evaluates the decision variable:
which tends towards the distribution. The hypothesis of independence is rejected if is too high (depending on the p-value threshold).
the Pearson test: it tests if there exists a linear relation between two scalar samples which form a gaussian vector (which is equivalent to have a linear correlation coefficient not equal to zero). If both samples are and , and and , the Pearson test evaluates the decision variable:
The variable tends towards a , under the hypothesis of normality of both samples. The hypothesis of a linear coefficient equal to 0 is rejected (which is equivalent to the independence of the samples) if D is too high (depending on the p-value threshold).
the Spearman test: it tests if there exists a monotonous relation between two scalar samples. If both samples are and ,, the Spearman test evaluates the decision variable:
where and . is such that tends towards the gaussian (0,1) distribution.
from __future__ import print_function import openturns as ot
# Create continuous samples sample1 = ot.Normal().getSample(100) sample2 = ot.Normal().getSample(100)
# Using the Pearson test ot.HypothesisTest.Pearson(sample1, sample2, 0.10)
class=TestResult name=Unnamed type=Pearson binaryQualityMeasure=true p-value threshold=0.1 p-value=0.360164 statistic=0.919362 description=
# Using the Spearman test ot.HypothesisTest.Spearman(sample1, sample2, 0.10)
class=TestResult name=Unnamed type=Spearman binaryQualityMeasure=true p-value threshold=0.1 p-value=0.193143 statistic=1.30926 description=
# Create discrete samples sample1 = ot.Poisson(0.2).getSample(100) sample2 = ot.Poisson(0.2).getSample(100)
# Using the Chi2 test ot.HypothesisTest.ChiSquared(sample1, sample2, 0.10)
class=TestResult name=Unnamed type=ChiSquared binaryQualityMeasure=true p-value threshold=0.1 p-value=0.790072 statistic=0.0708717 description=