This is a great intuitive explanation. I coded this up using numpy and it works. Thanks for illustrating the steps to solve for xhat!
def la1():
‘’’OLS explained!‘’’
day = np.array([[1],[2],[3]])
nof = np.array([[1],[2],[2]])# b = C+Dx
# 3col x 2row linear equation
day_C = np.ones((len(day), 1))
day_CD = np.concatenate( (day_C, day), axis=1 )# find x^ containing [C, D]
xhat = np.linalg.inv(day_CD.T@day_CD)@day_CD.T@nof# plot dataset and C+Dx regression line
plt.plot(day, nof, ‘bo’)
plt.plot(day, xhat[0]+day*xhat[1], ‘r — ‘)
plt.show()