{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Hughes and Hase, Problem 2.2\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Twelve data points given. Enter them into a `numpy` array:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "data = np.array([5.33, 4.95, 4.93, 5.08, 4.95, 4.96, 5.02, 4.99, 5.24, 5.25, 5.23, 5.01])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### i) Calculating the mean: $\\quad \\mu = \\frac{1}{N} \\sum_i x_i$" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mean = 5.078333333333334\n" ] } ], "source": [ "print('mean = ', np.sum(data)/len(data))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OR" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mean = 5.078333333333334\n" ] } ], "source": [ "print('mean = ', np.mean(data))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### ii) Calculating standard deviation: $\\quad \\sigma_{N-1} = \\sqrt{\\frac{1}{N-1} \\sum_i (x_i - \\mu)^2}$" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "standard deviation = 0.14357977404617803\n" ] } ], "source": [ "print(\"standard deviation = \",np.sqrt(np.sum((data-np.mean(data))**2)/(len(data)-1)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OR" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "standard deviation = 0.13746716779734078\n" ] } ], "source": [ "print(\"standard deviation = \",np.std(data))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These results do not agree!!\n", "\n", "By default the numpy `std` function calculates $\\sigma_N$, which is similar to the $\\sigma_{N−1}$ \n", "given in Eq. (2.3) of H&H, except the denominator is $N$ instead of $N−1$. \n", "The difference doesn't usually matter, and we won't go into this in any depth now. But if we\n", "set the `ddof=1` option, `numpy` will calculate $\\sigma_{N−1}$.\n", "\n", "Remember: you can see all the details of `np.std` by typing `np.std?`.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "standard deviation = 0.14357977404617803\n" ] } ], "source": [ "print(\"standard deviation = \", np.std(data, ddof=1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### iii) Standard error, or standard deviation of the mean\n", "\n", "Use Eq. (2.7): \n", "$$\\quad \\alpha = \\frac{\\sigma_{N-1}}{\\sqrt{N}}. $$" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "standard error = 0.04144791059787326\n" ] } ], "source": [ "print(\"standard error =\", np.std(data, ddof=1)/np.sqrt(len(data)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### iv) Formatted result:\n", "\n", "Sensitivity = $5.08 \\pm 0.04\\, \\mbox{A/W}$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Version information\n", "`version_information` is from J.R. Johansson (jrjohansson at gmail.com); see Introduction to scientific computing with Python for more information and instructions for package installation.\n", "\n", "`version_information` is installed on the linux network at Bucknell" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "%load_ext version_information" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/json": { "Software versions": [ { "module": "Python", "version": "3.7.8 64bit [GCC 7.5.0]" }, { "module": "IPython", "version": "7.17.0" }, { "module": "OS", "version": "Linux 3.10.0 1127.19.1.el7.x86_64 x86_64 with centos 7.9.2009 Core" }, { "module": "numpy", "version": "1.19.1" } ] }, "text/html": [ "
SoftwareVersion
Python3.7.8 64bit [GCC 7.5.0]
IPython7.17.0
OSLinux 3.10.0 1127.19.1.el7.x86_64 x86_64 with centos 7.9.2009 Core
numpy1.19.1
Sun Jan 16 14:26:17 2022 EST
" ], "text/latex": [ "\\begin{tabular}{|l|l|}\\hline\n", "{\\bf Software} & {\\bf Version} \\\\ \\hline\\hline\n", "Python & 3.7.8 64bit [GCC 7.5.0] \\\\ \\hline\n", "IPython & 7.17.0 \\\\ \\hline\n", "OS & Linux 3.10.0 1127.19.1.el7.x86\\_64 x86\\_64 with centos 7.9.2009 Core \\\\ \\hline\n", "numpy & 1.19.1 \\\\ \\hline\n", "\\hline \\multicolumn{2}{|l|}{Sun Jan 16 14:26:17 2022 EST} \\\\ \\hline\n", "\\end{tabular}\n" ], "text/plain": [ "Software versions\n", "Python 3.7.8 64bit [GCC 7.5.0]\n", "IPython 7.17.0\n", "OS Linux 3.10.0 1127.19.1.el7.x86_64 x86_64 with centos 7.9.2009 Core\n", "numpy 1.19.1\n", "Sun Jan 16 14:26:17 2022 EST" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%version_information numpy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.8" } }, "nbformat": 4, "nbformat_minor": 2 }