Skip to content

Instantly share code, notes, and snippets.

View SwampThingPaul's full-sized avatar

Paul Julian SwampThingPaul

View GitHub Profile
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
plt.style.use('ggplot')
fig, (ax1, ax2) = plt.subplots(ncols=2,figsize=(16,6))
plt.xlim((0, 10))
plt.ylim((0, 7))
plt.tight_layout(w_pad=1.5)
#red line
import scipy.odr as odr
def odr_line(B, x):
y = B[0]*x + B[1]*x**2
return y
def perform_odr(x, y, xerr, yerr):
quadr = odr.Model(odr_line)
mydata = odr.Data(x, y, wd=1./xerr, we=1./yerr)
#mydata = odr.Data(x, y)
import numpy.linalg as la
def tls(X,y):
if X.ndim is 1:
n = 1 # the number of variable of X
X = X.reshape(len(X),1)
else:
n = np.array(X).shape[1]
Z = np.vstack((X.T,y)).T
import numpy as np
from numpy.linalg import inv
import statsmodels.api as sm
# from scratch
x = sm.add_constant(x) # add constant in the 0 index
b = inv(x.T.dot(x)).dot(x.T).dot(y)
yest_ols = np.array([b[2]*v**2 + b[1]*v + b[0] for v in x.T[0]])
# with using numpy.linalg.lstsq
N=60
mu=0
sd=2
np.random.seed(0)
ran = np.random.normal(size=N)
error1 = sd**2 * ran + mu
error2 = sd*.5 * ran + mu
lin = np.linspace(-15., 15., num=N)