N= 20
np.random.seed(345)
x = randn(N)
y = 5*x+0.2 + randn(N)*2.
fig,ax=plt.subplots(1,1)
ax.plot(x,y,'.',markersize=10)
b = np.sum(x*y)-N*np.mean(x)*np.mean(y)
b = b/(np.sum(x**2)-N*np.mean(x)**2)
print b
a = np.mean(y)-b*np.mean(x)
print a
yhat=b*x+a
ax.plot(x,yhat,label='Linear Fit')
ind = 3
ax.plot(x[[ind,ind]],[y[ind],yhat[ind]],'-')
ax.text(x[ind],np.mean([y[ind],yhat[ind]]) ,r'$\ \epsilon_i$',fontsize=16,verticalalignment='top')
ax.text(x[ind],y[ind] ,r'$\ x_i, y_i$',fontsize=16,verticalalignment='top')
ax.set_xlabel('x [V]')
ax.set_ylabel('y [V]')
plt.legend(loc=0)
savefig('images/Regress.png')
4.8459645835 -0.290747620753
import scipy.stats as stats
bdn,bup=stats.t.interval(0.95,df=N-2)
bb=np.array([bdn,bup])
bb/np.sqrt(np.sum((x-np.mean(x))**2))
yhathigh=(b+bb[1])*(x-np.mean(x))+np.mean(y)
ax.plot(x,yhathigh)
yhathigh=(b+bb[0])*(x-np.mean(x))+np.mean(y)
ax.plot(x,yhathigh)
display(fig)