This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |