Hughes and Hase, Problem 2.2

In [1]:
import numpy as np

Twelve data points given. Enter them into a numpy array:

In [2]:
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])

i) Calculating the mean: $\quad \mu = \frac{1}{N} \sum_i x_i$

In [3]:
print('mean = ', np.sum(data)/len(data))
mean =  5.078333333333334

OR

In [4]:
print('mean = ', np.mean(data))
mean =  5.078333333333334

ii) Calculating standard deviation: $\quad \sigma_{N-1} = \sqrt{\frac{1}{N-1} \sum_i (x_i - \mu)^2}$

In [5]:
print("standard deviation = ",np.sqrt(np.sum((data-np.mean(data))**2)/(len(data)-1)))
standard deviation =  0.14357977404617803

OR

In [6]:
print("standard deviation = ",np.std(data))
standard deviation =  0.13746716779734078

These results do not agree!!

By default the numpy std function calculates $\sigma_N$, which is similar to the $\sigma_{N−1}$ given in Eq. (2.3) of H&H, except the denominator is $N$ instead of $N−1$. The difference doesn't usually matter, and we won't go into this in any depth now. But if we set the ddof=1 option, numpy will calculate $\sigma_{N−1}$.

Remember: you can see all the details of np.std by typing np.std?.

In [7]:
print("standard deviation = ", np.std(data, ddof=1))
standard deviation =  0.14357977404617803

iii) Standard error, or standard deviation of the mean

Use Eq. (2.7): $$\quad \alpha = \frac{\sigma_{N-1}}{\sqrt{N}}. $$

In [8]:
print("standard error =", np.std(data, ddof=1)/np.sqrt(len(data)))
standard error = 0.04144791059787326

iv) Formatted result:

Sensitivity = $5.08 \pm 0.04\, \mbox{A/W}$

Version information

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.

version_information is installed on the linux network at Bucknell

In [9]:
%load_ext version_information
In [10]:
%version_information numpy
Out[10]:
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
In [ ]: