Shortcuts for some common statistical functions

confidence interval, correlation coefficient, and linear regression

Feb 25, 2014 · 114 words · 1 minute read python stats

Here are some useful functions when performing statistical analysis:

Confidence Interval

from scipy import stats
from numpy import mean, var
from math import sqrt

sample = [1, 2, 3, 4, 5]

#95% confidence interval
R = stats.t.interval(0.95, len(sample)-1, loc=mean(sample),
                     scale=sqrt(var(sample)/len(sample)))
>>> R
(1.2440219338298311, 4.7559780661701687)

SciPy documentation

Correlation Coefficient

from numpy import corrcoef
x = [1, 2, 3, 4, 100]
y = [6, 7, 8, 9, 10]

r = corrcoef(x, y)

>>> r
array([[ 1., 0.72499943],
       [ 0.72499943,  1.]])

SciPy documentation

Linear Regression

from scipy import stats
x = [1, 2, 3, 4, 5]
y = [6, 7, 8, 9, 10]

slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)

>>> slope, intercept
(1.0, 5.0)

SciPy documentation

tweet Share