{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import scipy.stats" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's write a routine to compute the covariance between two variables. Remember, this is caluclated using: $\\sigma_{xy} = {\\sum (x_i-\\bar x)(y_i- \\bar y) \\over n-1}$" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def cov(x,y) :\n", " \"\"\"Compute the covariance between two sets of values\"\"\"\n", " n=len(x)\n", " return ((x*y).sum()-x.sum()*y.sum()/n ) / (n-1) # add code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you were to make a 2D probability distribution function with independent variables, using a uniform distribution for each variable, what would you expect to get for the covariance? How about the full covariance matrix? What would you expect for a normal distribution?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ANSWER HERE: The variance for a uniform distribution (between 0 and 1) is, as derived in previous notebook, 1/12. If the variables are independent, then the covariances should be zero. If it is a normal distribution, the variances should be the Gaussian $\\sigma^2$ " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's generate a 2D probabilty distribution function with uncorrelated variables, and use your routine to caculate the covariance. Let's compare it to the [numpy.cov()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.cov.html) routine, which calculates the covariance matrix. " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "covariance: 0.0021789884310597827\n", "covariance matrix:\n", "[[1.06299978 0.00217899]\n", " [0.00217899 1.01959439]]\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQ0AAAD4CAYAAAD2OrMWAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAA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AALJSURBVEick03CQ0RrAP+slHqta1uJaQijZGQTNWeJaRDRTzT++csAnoo5njACJtLtqGUiI2pjB6z9GRG9GsB1AAcAnD0nwohJtT6sUDQyR6iQjhF2O04ZmSNUyM+Euh2njIiGkI4JdTtOGRENIR0T6nacMiIaQjokJ2QSyHR/Qlpkqr7RI5aGIAheiGgIguCFiIYgCF6IaAiC4IWIhiAIXvSSRk5Ez6IaqzJm7gTwXN+V6JEpX/9Yrn2tlLqr/WMvojEFiGhfl7c/FaZ8/WO/dnFPBEHwQkRDEAQvRDTycaHvCvTMlK9/1NcuMQ1BELwQS0MQBC9ENARB8EJEIyNE9GEieurG0pUfJ6I7+q5TbojoXiL6ChE9TUQf6Ls+XUJE9xDRp4noy0T0JBHd33edciAxjYwQ0dsA/JtS6gdE9CEAUEr9Qc/VygYRzQF8FcBbATwD4PMAflMp9aVeK9YRRHQ3gLuVUl8gotsBXATwK2O7frE0MqKU+pRS6gc3/vlZAK/qsz4d8LMAnlZKfU0p9RKAvwPw9p7r1BlKqW8qpb5w4+/fBfBlAK/st1bpEdHojt8G8C99VyIzrwTw9ca/n8EIGw0HItoC8DoAn+u3JumRmbsiIaJHAfyo5r/OKaU+cWObcwB+gGrpyjFDmt8m5/8S0SkAfw/g/a1F0keBiEYkSqm32P6fiN4N4JcAnFbjDyA9A+Cexr9fBeAbPdWlF4hoA5Vg7Cml/qHv+uRAAqEZIaJ7AfwFgF9USo1+1WsiOoEqEHoawP+iCoT+llLqyV4r1hFERAA+BuDbSqn3912fXIhoZISIngZwC4DDGz99Vik16qUrieg+AB8BMAfwUaXUZNYvIKI3AvgPAP+NaqlSAPhDpdQj/dUqPSIagiB4Ib0ngiB4IaIhCIIXIhqCIHghoiEIghciGoIgeCGiIQiCFyIagiB48f9LJdqUGeZpcQAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "n=1000\n", "# Generate a set of n randomly distributed points (uniform or gaussian)\n", "x=np.random.normal(size=n)\n", "# generate an independent set of randomly distributed points\n", "y=np.random.normal(size=n)\n", "# plot them in a square plot\n", "plt.figure()\n", "axes=plt.gca()\n", "axes.set_aspect('equal')\n", "plt.plot(x,y,'ro')\n", "# calculate and print the covariance. Compare with numpy.cov\n", "c=cov(x,y)\n", "print('covariance: ',c)\n", "print('covariance matrix:')\n", "c_matrix=np.cov(x,y)\n", "print(c_matrix)\n", "if not np.isclose(c,c_matrix[0,1]) : print('Problem with covariance calculation?')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How do the values compare with your expectation? How does the numpy routine calculate covariance by default (as compared to how it calculates variance)?
\n", " \n", " ANSWER HERE: We get the expected variances as the sample size tends towards infinity. At smaller sample sizes, there is noise around the expected values. There does not seem to be a difference between my covariance routine and numpy.cov(); the default for np.cov() is to use dof=1 (unlike np.variance()!)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's generate a perfectly correlated (or anticorrelated) data set. What do you predict for the covariance matrix in this case, again, both for a uniform distribution and a normal distribution in the variables?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER HERE: The covariance should be equal to the variances for perfectly correlated data, and negative of the variances for perfectly uncorrelated data." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "covariance: -0.09679730934183998\n", "covariance matrix:\n", "[[ 0.09679731 -0.09679731]\n", " [-0.09679731 0.09679731]]\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQcAAAD4CAYAAADhGCPfAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAWIUlEQVR4nO3dfYxcV33G8e/jOAZZBMiLE1JgvaAYCSeqCgwRaQW02KmCK+FQQRs0NhtIaxyXKhLij0juHx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f5L+pPE7PCLpn6vUP0ljkr4t6f7G3/GWtM/pUt8+L+lpSQ+2eV+SPtvo+08kvbHwh0dEpb+A84CfAq8F1gA/Bja2tNkF7Gs8vwH4csX69wfA2sbzm6vWv0a7C0gKBd8H1KrUP2ADcD9wYeP1pRXr3xRwc+P5RuB4D/v3NuCNwINt3t8CfIOkIttbgO8X/exBOHMoUqB2KzDdeP5VYJOUVpa3P/2LiG9HRLM8z30k1bB6pWiB348DnwT+t4d9g2L9+3Pgtoj4JUBEPF2x/gXw0sbzl9Gm0lk3RMR3gF9kNNkK3B6J+4CXS7q8yGcPQjgUKVD7mzaRFJf5NXBxT3q3/AK6N5Ekea/k9k/SG4BXR8R/9LBfTUV+f68DXifpu5Luk3Rdz3pXrH9/DWxrFD66C/jL3nStkBUXeC5V7KVHihSoLVzEtguWU0B3G1AD3t7VHrUcNuV7v+mfpFXAp4Ebe9WhFkV+f6tJLi1+n+Ss678kXRURv+py36BY/94HfDEi/lbSNcAdjf61KQfdUyv+tzEIZw5FCtT+po2k1SSndlmnWp1UqICupM3AbuBdEfF/Peob5PfvAuAq4F5Jx0muSw/1cFCy6N/vwYh4LiL+G3iYJCyq0r+bgDsBIuJ7wItJbnqqgsIFnl+gVwMnJQZcVgOPAa/h7IDQlS1t/oJzByTvrFj/3kAyqLWhir+/lvb30tsBySK/v+uA6cbzS0hOky+uUP++AdzYeP76xj8+9fB3OE77Ack/4twByR8U/txe/QAlf/gtwCONf2C7G9/7GMn/hSFJ6q8AjwI/AF5bsf7NAE8BP2p8HapS/1ra9jQcCv7+BPwdcBR4ALihYv3bCHy3ERw/Av6wh337F+BJ4DmSs4SbgJ3AziW/u9safX9gOX+3Xj5tZqkGYczBzPrA4WBmqRwOZpbK4WBmqRwOZpbK4WBmqRwOZpbq/wFTBOKQKJbMLgAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "n=100\n", "# generate a randomly distributed data set in one variable\n", "x=np.random.uniform(size=n)\n", "# set the other variable to be THE SAME (i.e., not an independent random sample), or THE SAME with opposite sign\n", "y=-x # add code here\n", "plt.figure()\n", "axes=plt.gca()\n", "axes.set_aspect('equal')\n", "plt.plot(x,y,'ro')\n", "# calculate and print the covariance matrix\n", "c=cov(x,y)\n", "print('covariance: ',c)\n", "c_matrix=np.cov(x,y)\n", "print('covariance matrix:')\n", "print(c_matrix)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's generate a correlated data set, but not with a perfect correlation. Do this by injecting some random noise on top of the underlying correlation." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "covariance: 0.08522014496508686\n", "covariance matrix:\n", "[[0.08269301 0.08522014]\n", " [0.08522014 1.10109394]]\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAFlCAYAAAD292MqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO2df4xm11nfv+ednfF6ZuwEv95WgDPvQhOimqhSyYqmatWWTETDFsWC0ipoHFISdeOxaC2hqgKNRH+g+YNWbXEliLsqCYln+JG2oqRoKypvbCWKEmBMQnASgly647hBybIuYLIx6+ye/nHneu/cOb/vc3+ce78f6Whm3rnvveece+73POc5zzlXaa1BCCEkX2Z9Z4AQQkgzKOSEEJI5FHJCCMkcCjkhhGQOhZwQQjKHQk4IIZlzqo+L3nvvvfrs2bN9XJoQQrLl6aef/iOt9Zn6570I+dmzZ3FwcNDHpQkhJFuUUoemz+laIYSQzKGQE0JI5lDICSEkcyjkhBCSORRyQgjJHAo5IYRkDoWcEEIyR0zIlVJLSqlPKaV+TeqchBBC/Eha5I8A+Lzg+QghhAQgIuRKqfsA/D0A/1nifIQQ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vt5qj9nGWWSk1A/AfAPyjrjLUMiH3+BQK98rfQTHi+phS6g1a6z9uOW9tEVLmHwTw81rrf6eU+usAHj8q8632s9cbovo1VIv8eQCvqfx9H04Ot145Ril1CsWQzDWUGTohZYZS6i0AdgC8TWv95x3lrS18Zb4LwBsAPKWUuoLCl/jhjCc8Q9v1r2qtX9Za/x8AX0Ah7LkSUuZ3A/gQAGitPwHgNIr9SMZM0PMeylCF/LcAvE4p9S1KqRUUk5kfrh3zYQDvPPr9BwB8RB/NImSKt8xHbob/hELEc/edAp4ya63/RGt9r9b6rNb6LIp5gbdprQ/6yW5jQtr1f0cxqQ2l1L0oXC1/0GkuZQkp83MANgFAKfWXUQj51U5z2T0fBvBDR9ErbwLwJ1rrP0w+W9+zu45Z3/MAfh/FjPfO0Wf/GsWDDBQ3+78AeBbAbwL41r7z3EGZnwDwZQCfPkof7jvPbZe5duxTyDhqJfAeKwD/HsDnAPwugLf3necOynw/gI+jiGj5NIDv7jvPAmX+RQB/COBlFNb3uwE8BOChyn3+maM6+d2m7ZorOwkhJHOG6lohhBASCIWcEEIyh0JOCCGZQyEnhJDMoZATQkjmUMgJISRzKOSEEJI5FHJCCMmc/w8y9p8+kis0FwAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "def get_correlated_sample(n,uniform=True,xmin=0,xmax=1,mean=0,sigma=1,noise=1) :\n", " \"\"\"Generated correlated sample data set\"\"\"\n", " if uniform :\n", " x=np.random.uniform(xmin,xmax,size=n)\n", " else :\n", " x=np.random.normal(mean,sigma,size=n)\n", " y=x+np.random.normal(0,noise,size=len(x)) # add some random noise here, Gaussian with input noise standard deviation\n", " return x,y\n", "\n", "x,y=get_correlated_sample(10000,noise=1)\n", "plt.figure(figsize=(6,6))\n", "plt.plot(x,y,'ro')\n", "\n", "# calculate and print the covariance matrix\n", "c=cov(x,y)\n", "print('covariance: ',c)\n", "c_matrix=np.cov(x,y)\n", "print('covariance matrix:')\n", "print(c_matrix)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How do you expect your results for the covariance matrices change if you changed the width of the underlying distributions, both for uniform and normally distributed variables?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ANSWER HERE:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Play around with your data sets, changing the widths and types (uniform and normal) of the underlying distributions to build up some intuition about the covariance matrix." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "# code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OK, now let's work the other direction, i.e., given a covariance matrix, generate a multivariate distribution. For normal distributions, we can use the [numpy.random.multivariate_normal()](https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.random.multivariate_normal.html) routine. Play around with this for different values of the input means and covariance matrix:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/holtz/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:13: RuntimeWarning: covariance is not positive-semidefinite.\n", " del sys.path[0]\n" ] }, { "data": { "text/plain": [ "[]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAFlCAYAAAD292MqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAbHElEQVR4nO3db4xcV3nH8d+za6ftEiIae+mfJDsbJEQbRVQhKwSNhBChUjBRwgsqQSfIhUpWUUvTCkRDV2pfVJYqtULkBYq0CqRRPAJVgQpUuZQ/pa36goh1oCVgaKPUdgyhWdtt88dIJvbTF3cmnh3fO3Nn7rl/zr3fj7Ta7PX63jMh/ObMc59zrrm7AADxWqp7AACAYghyAIgcQQ4AkSP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/lNJn5f06zWPqUr/bWa/JEnD78/Oe4IYg/ybkl5rZjea2VVKbop8seYxlcrMTEnN9Li7f7zu8VTF3T/m7te7+7qS/53/0d1bPVNz9x9LetrMXjc8dLuk79U4pCqckvQmM1sZ/rd+u1p+g3fCFyUdHP7zQUlfmPcEe4IOpwLu/pKZ/b6kf1Byd/vT7v7dmodVttskvU/Sd8zs28Njf+LuR2scE8rzIUmD4UTlKUnvr3k8pXL3x8zsUUmPK+nQ+pZausLTzD4j6a2S9pvZaUl/JukvJP2Nmf2Okje135z7vKzsBIC4xVhaAQCMIcgBIHIEOQBEjiAHgMgR5AAQOYIcACJHkANA5AhyAIjc/wMsdFZ7TiLU2QAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "n= 100\n", "\n", "# The desired mean values of the sample.\n", "mu = np.array([5.0, 0.0])\n", "\n", "# The desired covariance matrix.\n", "r = np.array([\n", " [ 3.40, -05.05 ],\n", " [ -05.05, 5.50]\n", " ])\n", "\n", "# Generate the random samples.\n", "y = np.random.multivariate_normal(mu, r, size=n)\n", "plt.figure(figsize=(6,6))\n", "plt.plot(y[:,0],y[:,1],'ro')\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "OK, now let's work with quantifying correlation for distributions with arbitrary width, using the correlation coefficient, which is essentially a normalized covariance.\n", "\n", "[scipy.stats.pearsonr](https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.pearsonr.html) computes the (Pearson) correlation coeffient, and returns it plus the probability of getting the value given the number of points, given the null hypothesis. [scipy.stats.spearmanr](https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.spearmanr.html) does the same thing for Spearman's correlation coefficient. Let's write a quick routine that calculates both for an input data set:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pearsons: (-0.9088473503370985, 5.3988638206682304e-39)\n", "Spearmans: SpearmanrResult(correlation=-0.9007260726072607, pvalue=2.894703262889244e-37)\n" ] } ], "source": [ "def corrcoef(x,y) :\n", " \"\"\" Compute Pearsons and Spearmans correlation coefficients and probabilities\"\"\"\n", " print('Pearsons: ',scipy.stats.pearsonr(x,y))\n", " print('Spearmans: ',scipy.stats.spearmanr(x,y))\n", " \n", "corrcoef(y[:,0],y[:,1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Experiment with generating sets of data with different covariance matrices and measuring the correlation coefficients" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if we want to make a nonlinear relation? One way to accomplish this is to generate a random sample from a distribution of each x point, with some nonlinear relation. Play around with the relation and the scatter around it, and see the effect on the measured correlation coefficients." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.18" } }, "nbformat": 4, "nbformat_minor": 4 }