1) Explanation of each (all) variables in the python code (important with comment out function). 2) Use Jupiter notebook of python 3) Please clearly mention questions (a) or (b) in python code not to...

1 answer below »
OLS (Ordinary Least Square) in Python


1) Explanation of each (all) variables in the python code (important with comment out function). 2) Use Jupiter notebook of python 3) Please clearly mention questions (a) or (b) in python code not to be confused.
Answered 6 days AfterSep 08, 2021

Answer To: 1) Explanation of each (all) variables in the python code (important with comment out function). 2)...

Kshitij answered on Sep 10 2021
137 Votes
9/14/21, 8:50 AM ols_numpy_output.ipynb - Colaboratory
https://colab.research.google.com/drive/1PFnUx8z2qoBQJ0UHj2ZZCFqolt3zT3MC?authuser=1#scrollTo=tyryA2XWlFam 1/10
enter t value10
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1]
#a
import pandas as pd
from matplotlib import pyplot as plt
nps.random.seed(223)
a=int(input("enter t value"))
A = nps.random.rand(a)
A =
[0 if i <=0.5 else 1 for i in A]
import numpy as nps
print(A)
#B

class Model():

def __init__(self, x, y):
self.x = x
self.y = y

def fit(self, intercept = False):
self.intercept = intercept

x = nps.hstack([nps.ones(len(self.x))[:, nps.newaxis], self.x]) if self.intercept

y = self.y
self.betas = nps.linalg.inv(x.T @ x) @ x.T @ self.y

def predict(self):

x = nps.hstack([nps.ones(len(self.x))[:, nps.newaxis], self.x])\
if self.intercept else self.x

self.y_hat = x @ self.betas

# If you want to see the predictions, you can 'return self.y_hat' here

def plot_predictions(self):
plt.scatter(self.x, self.y, c='orange', label = 'Observed values')
plt.plot(self.x, self.y_hat, label = 'Fitted values')
plt.legend()
title_label = 'Fitted OLS Regression: intercept={}, slope={}'\
.format(nps.round(self.betas[0][0],2), nps.round(self.betas[1][0],2))
plt.title(label=title_label)
plt.show()

if __name__ == '__main__':
x = nps.arange(50)[:, nps.newaxis]
y = nps.array([i + nps.random.rand() for i in range(50)])[:, nps.newaxis]
ols_test = Model(x, y)
ols_test.fit(intercept=True)
ols_test.predict()
()
9/14/21, 8:50 AM ols_numpy_output.ipynb - Colaboratory
https://colab.research.google.com/drive/1PFnUx8z2qoBQJ0UHj2ZZCFqolt3zT3MC?authuser=1#scrollTo=tyryA2XWlFam 2/10
ols_test.plot_predictions()
β = (X^T X)^{-1} X^T Y
[ 1.25802086 -0.50211536 0.89923789 1.861438 0.31105937]
#c
# generate numpy random regression data
import numpy as np
N=1000
M=5
## generate x y
X = np.random.ranf((N,M))
B = np.random.ranf(M)
eps = np.random.normal(size=N) * 10
Y = X @ B + eps
A = np.array([[2,1,-2],[3,0,1],[1,1,-1]])
b = np.transpose(np.array([[-3,5,-2]]))
x = np.linalg.solve(A,b)
Xt = np.transpose(X)
XtX = np.dot(Xt,X)
Xty = np.dot(Xt,Y)
beta = np.linalg.solve(XtX,Xty)
print("β = (X^T X)^{-1} X^T Y")
print(beta)

import statsmodels.api as sm
from sklearn import linear_model
import random
n = 1000
p =3
def sklearn_w_method():
exaecution_times = timeit.timeit(setup = setup_sklearn, stmt = model_sklearn, number =
return exaecution_times

def statsmodels_w_method():
exaecution_times = timeit.timeit(setup = setup_statsmodels, stmt = model_statsmodels,
return exaecution_times
import numpy as nps
9/14/21, 8:50 AM ols_numpy_output.ipynb - Colaboratory
https://colab.research.google.com/drive/1PFnUx8z2qoBQJ0UHj2ZZCFqolt3zT3MC?authuser=1#scrollTo=tyryA2XWlFam 3/10
/usr/local/lib/python3.7/dist-packages/statsmodels/tools/_testing.py:19: FutureWarnin
import pandas.util.testing as tm

def matmul_w_method():
exaecution_times = timeit.timeit(setup = setup_matmul, stmt = model_matmul, number = 1
return exaecution_times
sample = nps.random.normal(0,1,(n,p))
model=linear_model.LinearRegression(fit_intercept=False).fit(sample[:,0:p-1], sample[:,p-1
linear_model
#D
import time
import numpy as nps

from sklearn import datasets
import...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here