{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 1\n", "\n", "An introduction to the Jupyter Notebook and some practice with probability ideas from Chapter 1.\n", "## 1.1 Probability\n", "### 1.1.1 Moments of Measured Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Jupyter Notebook has two primary types of cells \"Markdown\" cells for text (like this one) and \"Code\" cells for running python code. The cell below this one is a code cell that loads the plotting functions into the `plt` namespace and loads several functions from the `numpy` library. The last line requests that all plots show up inline in the notebook (instead of in other windows or as files on your computer)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "from numpy import array, sin, sqrt, dot, outer\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arrays of data, and their average and sum in Python. We use some definitions from `numpy`. Notice the way these operators can be applied to arrays (with the \".\" operator)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "x = array([1,2,3])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.sum()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The formal definition (and to make sure we match with the book) is to take the sum and divide by the number of items in the sample:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.sum()/len(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Higher order moments\n", "Operate on the sample, python does this element-by-element, then do the same thing as above. You may be surprised that to raise a power is \"**\" instead of \"^\". This is a difference between python and other languages, just something to keep in mind!" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 4, 9])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x**2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(x**2).sum() # Note the parenthesis!" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.666666666666667" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(x**2).sum()/len(x)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "inputHidden": false, "outputHidden": false }, "outputs": [ { "data": { "text/plain": [ "4.666666666666667" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(x**2).mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### It works for functions too\n", "`sin(x)` will find the `sin` of each element in the array `x`:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.84147098, 0.90929743, 0.14112001])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sin(x)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6306294732311485" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sin(x).sum()/len(x)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6306294732311485" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sin(x).mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Variance" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6666666666666666" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.var() # Variance" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.816496580927726" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.std() # Standard Deviation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can test if two quanties are equal with the `==` operator. Not the same as `=` since the `=` is an assignment operator. This will trip you up if you are new to programming, but you'll get over it." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.std()**2 == x.var() # Related by a square root" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 1.1" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "x_m = array([9,5,25,23,10,22,8,8,21,20])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15.1" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_m.mean()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "281.3" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(x_m**2).sum()/len(x_m)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.300000000000002" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sqrt(281.3 - (15.1)**2)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.3" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_m.std()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sqrt(281.3 - (15.1)**2) == x_m.std()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Close enough!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.2 Probability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 1.2\n", "This is an illustration of how to implement the histogram from Example 1.2 in the text. Note the use of setting the number of bins. The `hist` command will pick for you, and you should try other values to see the impact. There is no one correct value, but the too many bins doesn't illustrate clusters of data, and too-few bins tends to oversimplify the data." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAARu0lEQVR4nO3da4xcd33G8e+DYy4tEQG8LW5is7SgSoC4hFUIpUURUBoCStoSKqMWwk0WiAiQqNoEpAB5RVoVKi4iMiQiUASh4VIDiSAUEPAiho3rmARDMSgoblJiEnCIuNX01xdzUg2TmZ2z9szu8uf7kUZ75pz/zDw6c/z4zJnLSVUhSfr1d5/1DiBJmg0LXZIaYaFLUiMsdElqhIUuSY04Yb0eeMuWLbW4uLheDy9Jv5auv/76H1TVwrhl61boi4uLLC8vr9fDS9KvpSTfm7TMQy6S1AgLXZIaYaFLUiMsdElqhIUuSY2w0CWpEb0LPcmmJP+R5FNjlt0vyZVJDibZk2RxliElSdOtZg/9NcCBCcteBvywqh4JvA245HiDSZJWp1ehJzkFeA7w3glDzgGu6KavAp6RJMcfT5LUV99viv4z8HfAiROWnwzcAlBVR5McAR4K/GB4UJKdwE6A7du3H0veX0uLF3x6vSP0dvNbnrPeESQdo6l76EmeC9xeVdevNGzMvHudCqmqdlXVUlUtLSyM/SkCSdIx6nPI5anA2UluBj4MPD3Jv4yMOQRsA0hyAvAg4M4Z5pQkTTG10Kvqwqo6paoWgR3A56vqb0aG7QbO66bP7cZ4slJJWkPH/GuLSS4Gl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FLUiP+D8O11B/Db53pAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "n, bins, patches = plt.hist(x_m,bins=7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The hist function has several possible arguments, we use bins=7 to match the example." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1., 4., 0., 0., 0., 3., 2.])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# an array of the counts in each bin:\n", "n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.6, 3.6, 0. , 0. , 0. , 6.3, 4.8])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n/10.0*array([6,9,12,15,18,21,24]) # counts times each bin-center value" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15.3" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# sum of the last cell should be the mean:\n", "sum(_)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n/10.0*array([6,9,12,15,18,21,24])**2 # counts times each bin-center value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# sum of the last cell should be the second moment:\n", "sum(_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Both of these results are close to the previous value, but not exact. Remember, the historgram is a representation of the data and the agreement will improve for larger data sets." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.2 Linear Algebra\n", "### 1.2.1 Vectors and Basis sets\n", "We'll use the qutip library later, even though this is all just linear algebra. For now, try using standard-python for vector math:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rvec = array([1,2]) # A row vector" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rvec" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cvec = array([[1],[2]]) # A column vector" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cvec" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cvec*rvec # Actually the outer product:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "inputHidden": false, "outputHidden": false }, "outputs": [], "source": [ "rvec*cvec # still the outer product... so this simple `*` doesn't respect the rules of linear algebra!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `dot` function properly computes the dot product that we know and love from workshop physics:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dot(rvec,cvec)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "outer(cvec,rvec)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dot(cvec,rvec) # This doesn't work, because `dot` knows what shape the vectors should be" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This probably isn't your first error message, but it's important to look at what Python is telling you. First, it lists the type of error (ValueError). Then it shows where the error occured (in the dot function). This is helpful when you have larger cells with more lines in them. Obviously we already know what line caused this error since there is only one. Finally, the error is explained as follows: the shapes are not aligned, meaning the vectors don't have the right dimensions for a dot product. Some error messages are more helpful than others, but they all look like this. An error and a traceback." ] } ], "metadata": { "kernel_info": { "name": "python3" }, "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.7.4" }, "nteract": { "version": "0.15.0" } }, "nbformat": 4, "nbformat_minor": 1 }