{ "metadata": { "name": "", "signature": "sha256:369195402e993e526912fb436d4393da87d88b4cefad395e9b891b41d82299f8" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Making a correlation function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the next exercise, we will implement our own correlation function.\n", "\n", "For this exercise, you will be working with your text editor and the terminal.\n", "\n", "This notebook just sets out the problem." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to implement [Pearson product-moment correlation](https://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have two vectors, $\\mathbf{x}$ and $\\mathbf{y}$, each with $N$ values, $\\mathbf{x} = x_0, x_1, ..., x_{N-1}$, $\\mathbf{y} = y_0, y_1, ..., y_{N-1}$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We write the sample mean of each vector with a bar across the top, as in:\n", "\n", "$$\n", "\\bar{x}=\\frac{1}{N}\\sum_{i=0}^{N-1} x_i\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course, in `numpy` this would just be `np.mean(x)`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Pearson product-moment correlation coefficient between two vectors $\\mathbf{x}, \\mathbf{y}$ is defined as:\n", "\n", "$$\n", "r_{xy} =\\frac{\\sum ^{N-1} _{i=0}(x_i - \\bar{x})(y_i - \\bar{y})}{\\sqrt{\\sum ^{N-1} _{i=0}(x_i - \\bar{x})^2} \\sqrt{\\sum ^{N-1} _{i=0}(y_i - \\bar{y})^2}}\n", "$$\n", "\n", "(This is the same formula as the one on the Wikipedia page, but using $0 ... N-1$ index convention instead of $1 ... N$ convention)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$(x_i - \\bar{x})$ is the vector $\\mathbf{x}$ after it has been *mean-centered* (it has a sample mean of zero)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now open the files `pearson.py` and `test_pearson_1d.py`.\n", "\n", "Read the instructions in both these files, and, shoot at will." ] } ], "metadata": {} } ] }