# 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.

In [2]:
from __future__ import print_function
import openturns as ot

continuous samples

In [3]:
# Create continuous samples
sample1 = ot.Normal().getSample(100)
sample2 = ot.Normal().getSample(100)
In [9]:
# Using the Pearson test
ot.HypothesisTest.Pearson(sample1, sample2, 0.90)
Out[9]:

class=TestResult name=Unnamed type=TwoSamplePearson binaryQualityMeasure=true p-value threshold=0.1 p-value=0.521269 description=[]

In [11]:
# Using the Spearman test
ot.HypothesisTest.Spearman(sample1, sample2, 0.90)
Out[11]:

class=TestResult name=Unnamed type=TwoSampleSpearman binaryQualityMeasure=true p-value threshold=0.1 p-value=0.719843 description=[]

discrete samples

In [4]:
# Create discrete samples
sample1 = ot.Poisson(0.2).getSample(100)
sample2 = ot.Poisson(0.2).getSample(100)
In [6]:
# Using the Chi2 test
ot.HypothesisTest.ChiSquared(sample1, sample2, 0.90)
Out[6]:

class=TestResult name=Unnamed type=TwoSampleChiSquared binaryQualityMeasure=true p-value threshold=0.1 p-value=0.878545 description=[]