Lecture 02 Linear Regression

Jan 26, 2021

Auto-Regressive Model

In [1]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import toeplitz

N     = 500
y     = np.cumsum(0.2*np.random.randn(N)) + 0.05*np.random.randn(N)

L     = 100
c     = np.hstack((0, y[0:400-1]))
r     = np.zeros(L)
X     = toeplitz(c,r)
theta = np.linalg.lstsq(X, y[0:400], rcond=None)[0]

yhat        = np.zeros(N)
yhat[0:400] = np.dot(X, theta)
# for i in range(400,500):
#   yhat[i] = np.dot(yhat[i-L:i], theta)

plt.plot(y, 'o')
plt.plot(yhat,linewidth=4)
Out[1]:
[<matplotlib.lines.Line2D at 0x7f1b7f35cf28>]
In [ ]:
for i in range(400,500):
  yhat[i] = np.dot(yhat[i-L:i], theta)

plt.plot(y, 'o')
plt.plot(yhat,linewidth=4)
Out[ ]:
[<matplotlib.lines.Line2D at 0x7f4262d2a3c8>]

Linear Programming Example

In [ ]:
# Least Squares with clean data
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import eval_legendre

N = 50
x = np.linspace(-1,1,N)
a = np.array([-0.001, 0.01, 0.55, 1.5, 1.2])
y = a[0]*eval_legendre(0,x) + a[1]*eval_legendre(1,x) + \
  a[2]*eval_legendre(2,x) + a[3]*eval_legendre(3,x) + \
  a[4]*eval_legendre(4,x) + 0.2*np.random.randn(N)

X = np.column_stack((eval_legendre(0,x), eval_legendre(1,x), \
                     eval_legendre(2,x), eval_legendre(3,x), \
                     eval_legendre(4,x)))
theta = np.linalg.lstsq(X, y, rcond=None)[0]
t     = np.linspace(-1, 1, 200);
yhat  = theta[0]*eval_legendre(0,t) + theta[1]*eval_legendre(1,t) + \
        theta[2]*eval_legendre(2,t) + theta[3]*eval_legendre(3,t) + \
        theta[4]*eval_legendre(4,t)
plt.plot(x,y,'o',markersize=12)
plt.plot(t,yhat, linewidth=8)
plt.show()
In [ ]:
# Least squares with outliers
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import eval_legendre

N = 50
x = np.linspace(-1,1,N)
a = np.array([-0.001, 0.01, 0.55, 1.5, 1.2])
y = a[0]*eval_legendre(0,x) + a[1]*eval_legendre(1,x) + \
    a[2]*eval_legendre(2,x) + a[3]*eval_legendre(3,x) + \
    a[4]*eval_legendre(4,x) + 0.2*np.random.randn(N)
idx = [10,16,23,37,45]    # Outlier locations
y[idx] = 5                # Outlier values

X = np.column_stack((np.ones(N), x, x**2, x**3, x**4))
beta = np.linalg.lstsq(X, y, rcond=None)[0]
t    = np.linspace(-1, 1, 200);
yhat = beta[0]*np.ones(200) + beta[1]*t + beta[2]*t**2 + beta[3]*t**3 + beta[4]*t**4
plt.plot(x,y,'o',markersize=12)
plt.plot(t,yhat, linewidth=8)
plt.show()
In [ ]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import eval_legendre
from scipy.optimize import linprog

N = 50
x = np.linspace(-1,1,N)
a = np.array([-0.001, 0.01, 0.55, 1.5, 1.2])
y = a[0]*eval_legendre(0,x) + a[1]*eval_legendre(1,x) + \
    a[2]*eval_legendre(2,x) + a[3]*eval_legendre(3,x) + \
    a[4]*eval_legendre(4,x) + 0.2*np.random.randn(N)
idx = [10,16,23,37,45]
y[idx] = 5

# Please complete the rest for Homework 1
In [ ]:
%%shell
jupyter nbconvert --to html /content/ECE595_lecture03_lin_prog.ipynb
[NbConvertApp] Converting notebook /content/ECE595_lecture03_lin_prog.ipynb to html
[NbConvertApp] Writing 387344 bytes to /content/ECE595_lecture03_lin_prog.html
Out[ ]: