Test samples independence using regressionΒΆ

In this example we are going to estimate whether samples are independent using the regression test.

It consists in detecting a linear relation between two scalar samples.

from __future__ import print_function
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)

Generate a sample of dimension 3 with component 0 correlated to component 2

marginals = [ot.Normal()] * 3
S = ot.CorrelationMatrix(3)
S[0, 2] = 0.9
copula = ot.NormalCopula(S)
distribution = ot.ComposedDistribution(marginals, copula)
sample = distribution.getSample(30)

Split it in two samples: firstSample of dimension=2, secondSample of dimension=1

firstSample = sample[:, :2]
secondSample = sample[:, 2]

Test independance of each components of firstSample against secondSample

test_results = ot.LinearModelTest.FullRegression(firstSample, secondSample)
for i in range(len(test_results)):
    print('Component', i, 'is independent?', test_results[i].getBinaryQualityMeasure(),
          'p-value=%.6g' % test_results[i].getPValue(),
          'threshold=%.6g' % test_results[i].getThreshold())

Out:

Component 0 is independent? True p-value=0.998225 threshold=0.05
Component 1 is independent? False p-value=3.56363e-15 threshold=0.05
Component 2 is independent? True p-value=0.111968 threshold=0.05

Total running time of the script: ( 0 minutes 0.003 seconds)

Gallery generated by Sphinx-Gallery