{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sample manipulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example will describe the main statistical functionalities on data through the Sample object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<TABLE><TR><TD></TD><TH>X0</TH><TH>X1</TH><TH>X2</TH></TR><TR><TH>0</TH><TD>0.6082016512187646</TD><TD>-1.2661731022166567</TD><TD>-0.43826561996041397</TD></TR><TR><TH>1</TH><TD>1.2054782008285756</TD><TD>-2.1813852346165143</TD><TD>0.3500420865302907</TD></TR><TR><TH>2</TH><TD>-0.3550070491856397</TD><TD>1.437249310140903</TD><TD>0.8106679824694837</TD></TR><TR><TH>3</TH><TD>0.79315601145977</TD><TD>-0.4705255986325704</TD><TD>0.26101793529769673</TD></TR><TR><TH>4</TH><TD>-2.2900619818700854</TD><TD>-1.2828852904549808</TD><TD>-1.311781115463341</TD></TR></TABLE>"
      ],
      "text/plain": [
       "class=Sample name=Unnamed implementation=class=SampleImplementation name=Unnamed size=5 dimension=3 description=[X0,X1,X2] data=[[0.608202,-1.26617,-0.438266],[1.20548,-2.18139,0.350042],[-0.355007,1.43725,0.810668],[0.793156,-0.470526,0.261018],[-2.29006,-1.28289,-1.31178]]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Generate a sample of dimension 3\n",
    "sample = ot.Normal(3).getSample(500)\n",
    "sample[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(class=Point name=Unnamed dimension=3 values=[-2.4067,-3.24637,-3.09834],\n",
       " class=Point name=Unnamed dimension=3 values=[3.15958,3.01263,2.63821])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get min and max per component\n",
    "sample.getMin(), sample.getMax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[5.56628,6.25899,5.73655]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=Point name=Unnamed dimension=3 values=[5.56628,6.25899,5.73655]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the range per component (xmax-xmin)\n",
    "sample.computeRange()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[-0.0421682,-0.0168704,0.0580127]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=Point name=Unnamed dimension=3 values=[-0.0421682,-0.0168704,0.0580127]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the mean per component\n",
    "sample.computeMean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[0.96048,1.01449,0.995846]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=Point name=Unnamed dimension=3 values=[0.96048,1.01449,0.995846]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the standard deviation per component\n",
    "sample.computeStandardDeviationPerComponent()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[0.922521,1.02919,0.991709]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=Point name=Unnamed dimension=3 values=[0.922521,1.02919,0.991709]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the Variance per component\n",
    "sample.computeVariance()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[0.200045,-0.0429991,-0.0712751]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=Point name=Unnamed dimension=3 values=[0.200045,-0.0429991,-0.0712751]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the Skewness per component\n",
    "sample.computeSkewness()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[3.15748,3.03583,3.02072]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=Point name=Unnamed dimension=3 values=[3.15748,3.03583,3.02072]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the Kurtosis per component\n",
    "sample.computeKurtosis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[-0.0497493,-0.0559353,0.0312677]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=Point name=Unnamed dimension=3 values=[-0.0497493,-0.0559353,0.0312677]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the median per component\n",
    "sample.computeMedian()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[1.54329,1.64875,1.69843]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=Point name=Unnamed dimension=3 values=[1.54329,1.64875,1.69843]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the empirical 0.95 quantile per component\n",
    "sample.computeQuantilePerComponent(0.95)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[[ 0.922521  0.0383477 0.051918  ]<br>\n",
       " [ 0.0383477 1.02919   0.0143437 ]<br>\n",
       " [ 0.051918  0.0143437 0.991709  ]]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=CovarianceMatrix dimension=3 implementation=class=MatrixImplementation name=Unnamed rows=3 columns=3 values=[0.922521,0.0383477,0.051918,0.0383477,1.02919,0.0143437,0.051918,0.0143437,0.991709]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the sample covariance\n",
    "sample.computeCovariance()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[[ 0.96048   0         0         ]<br>\n",
       " [ 0.0399256 1.01371   0         ]<br>\n",
       " [ 0.0540543 0.0120208 0.994305  ]]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=TriangularMatrix dimension=3 implementation=class=MatrixImplementation name=Unnamed rows=3 columns=3 values=[0.96048,0.0399256,0.0540543,0,1.01371,0.0120208,0,0,0.994305]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the sample standard deviation\n",
    "sample.computeStandardDeviation()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[[ 1         0.0393553 0.0542798 ]<br>\n",
       " [ 0.0393553 1         0.0141978 ]<br>\n",
       " [ 0.0542798 0.0141978 1         ]]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=CorrelationMatrix dimension=3 implementation=class=MatrixImplementation name=Unnamed rows=3 columns=3 values=[1,0.0393553,0.0542798,0.0393553,1,0.0141978,0.0542798,0.0141978,1]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the sample Pearson correlation matrix\n",
    "sample.computePearsonCorrelation()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[[ 1         0.0367936 0.0292906 ]<br>\n",
       " [ 0.0367936 1         0.0209539 ]<br>\n",
       " [ 0.0292906 0.0209539 1         ]]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=CorrelationMatrix dimension=3 implementation=class=MatrixImplementation name=Unnamed rows=3 columns=3 values=[1,0.0367936,0.0292906,0.0367936,1,0.0209539,0.0292906,0.0209539,1]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get  the sample Kendall correlation matrix\n",
    "sample.computeKendallTau()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[[ 1         0.0544864 0.0444143 ]<br>\n",
       " [ 0.0544864 1         0.0313186 ]<br>\n",
       " [ 0.0444143 0.0313186 1         ]]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=CorrelationMatrix dimension=3 implementation=class=MatrixImplementation name=Unnamed rows=3 columns=3 values=[1,0.0544864,0.0444143,0.0544864,1,0.0313186,0.0444143,0.0313186,1]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get  the sample Spearman  correlation matrix\n",
    "sample.computeSpearmanCorrelation()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.862"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the value of the empirical CDF at a point\n",
    "point = [1.1, 2.2, 3.3]\n",
    "sample.computeEmpiricalCDF(point)"
   ]
  }
 ],
 "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
}
