You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 KiB

None <html lang="en"> <head> </head>
In [12]:
# data for the OLS
import numpy as np
import matplotlib.pyplot as plt

theta0 = 2.6486
theta1 = 4.98
m = 50
disp = 4
x = np.linspace(0,5,m)
y = theta0+theta1*x+disp*np.random.rand(m)
In [13]:
plt.plot(x,y, '.k')
plt.show()
No description has been provided for this image
In [18]:
xSum = 0
ySum = 0

for i in range(m):
    #print(x[i])
    xSum = xSum + x[i]
    ySum = ySum + y[i]
    pass
xMean = xSum/m
yMean = ySum/m
print(xMean)
print(yMean)
2.5
17.115760540070784
In [20]:
num = 0
den = 0
for i in range(m):
    num = num + x[i]*(yMean-y[i])
    den = den + x[i]*(xMean-x[i])
    pass
theta1H = num/den
theta0H = yMean-theta1H*xMean
print("theta 0: ", theta0H)
print("theta 1: ", theta1H)
theta 0:  4.012173816371986
theta 1:  5.24143468947952
In [21]:
yH = theta0H+theta1H*x
plt.plot(x, yH, ':r', label="Model")
plt.plot(x, y, '.k', label="Data")
plt.legend()
plt.show()
No description has been provided for this image
</html>