{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sample independence test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example we are going to perform tests to assess whether two 1-d samples are independent or not.\n",
    "\n",
    "Several methods can be used:\n",
    "\n",
    "- Pearson test (for continuous values)\n",
    "- Spearman test (for continuous values)\n",
    "- Chi2 test (for discrete values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**continuous samples**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create continuous samples\n",
    "sample1 = ot.Normal().getSample(100)\n",
    "sample2 = ot.Normal().getSample(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>class=TestResult name=Unnamed type=TwoSamplePearson binaryQualityMeasure=true p-value threshold=0.1 p-value=0.521269 description=[]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=TestResult name=Unnamed type=TwoSamplePearson binaryQualityMeasure=true p-value threshold=0.1 p-value=0.521269 description=[]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Using the Pearson test\n",
    "ot.HypothesisTest.Pearson(sample1, sample2, 0.90)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>class=TestResult name=Unnamed type=TwoSampleSpearman binaryQualityMeasure=true p-value threshold=0.1 p-value=0.719843 description=[]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=TestResult name=Unnamed type=TwoSampleSpearman binaryQualityMeasure=true p-value threshold=0.1 p-value=0.719843 description=[]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Using the Spearman test\n",
    "ot.HypothesisTest.Spearman(sample1, sample2, 0.90)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**discrete samples**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create discrete samples\n",
    "sample1 = ot.Poisson(0.2).getSample(100)\n",
    "sample2 = ot.Poisson(0.2).getSample(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>class=TestResult name=Unnamed type=TwoSampleChiSquared binaryQualityMeasure=true p-value threshold=0.1 p-value=0.878545 description=[]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=TestResult name=Unnamed type=TwoSampleChiSquared binaryQualityMeasure=true p-value threshold=0.1 p-value=0.878545 description=[]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Using the Chi2 test\n",
    "ot.HypothesisTest.ChiSquared(sample1, sample2, 0.90)"
   ]
  }
 ],
 "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
}
