Note
Click here to download the full example code
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 standard normal distribution.
from __future__ import print_function
import openturns as ot
ot.Log.Show(ot.Log.NONE)
continuous samples
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.697478 statistic=0.389871 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.485495 statistic=0.699007 description=[]
discrete samples
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.165616 statistic=3.59616 description=[]
Total running time of the script: ( 0 minutes 0.002 seconds)