** Confidence interval on an average
:PROPERTIES:
:categories: statistics
:date: 2013/02/10 09:00:00
:updated: 2013/04/09 08:54:05
:END:
mod:scipy has a statistical package available for getting statistical distributions. This is useful for computing confidence intervals using the student-t tables. Here is an example of computing a 95% confidence interval on an average.
#+BEGIN_SRC python :results output :exports both
import numpy as np
from scipy.stats.distributions import t
n = 10 # number of measurements
dof = n - 1 # degrees of freedom
avg_x = 16.1 # average measurement
std_x = 0.01 # standard deviation of measurements
# Find 95% prediction interval for next measurement
alpha = 1.0 - 0.95
pred_interval = t.ppf(1-alpha/2.0, dof) * std_x / np.sqrt(n)
s = ['We are 95% confident the next measurement',
' will be between {0:1.3f} and {1:1.3f}']
print ''.join(s).format(avg_x - pred_interval, avg_x + pred_interval)
#+END_SRC
#+RESULTS:
: We are 95% confident the next measurement will be between 16.093 and 16.107