{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  Test samples independence using regression test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example we are going to estimate whether samples are independent using the regression test."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate a sample of dimension 3 with component 0 correlated to component 2\n",
    "marginals = [ot.Normal()] * 3\n",
    "S = ot.CorrelationMatrix(3)\n",
    "S[0, 2] = 0.9\n",
    "copula = ot.NormalCopula(S)\n",
    "distribution = ot.ComposedDistribution(marginals, copula)\n",
    "sample = distribution.getSample(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Split it in two samples: firstSample of dimension=2, secondSample of dimension=1\n",
    "firstSample = sample[:, :2]\n",
    "secondSample = sample[:, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Component 0 is independent? False p-value=4.83187e-13 threshold=0.05\n",
      "Component 1 is independent? True p-value=0.210156 threshold=0.05\n"
     ]
    }
   ],
   "source": [
    "# Test independance of each components of firstSample against secondSample\n",
    "test_results = ot.HypothesisTest.FullRegression(firstSample, secondSample)\n",
    "for i in range(len(test_results)):\n",
    "    print('Component', i, 'is independent?', test_results[i].getBinaryQualityMeasure(),\n",
    "          'p-value=%.6g' % test_results[i].getPValue(),\n",
    "          'threshold=%.6g' % test_results[i].getThreshold())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
