import numpy as np
import math
import matplotlib.pyplot as plt
def plot_effect(A):
assert A.shape == (2, 2)
# create points evenly spaced on a sphere
rad = np.arange(0, 2*math.pi, 0.1)
xs = np.cos(rad)
ys = np.sin(rad)
# apply "A" to the points
Ax = np.matmul(A, np.stack([xs, ys]))
lim = np.max(np.abs(A)) * 2
# plot the figure
plt.figure(figsize=(11,5))
plt.subplot(1, 2, 1)
plt.xlim([-lim, lim])
plt.ylim([-lim, lim])
plt.scatter(xs, ys, color=plt.cm.rainbow(rad/2/math.pi))
plt.title("x")
plt.subplot(1, 2, 2)
plt.scatter(Ax[0,:], Ax[1,:], color=plt.cm.rainbow(rad/2/math.pi))
plt.xlim([-lim, lim])
plt.ylim([-lim, lim])
plt.title("Ax")
plot_effect(np.array([[1., 2.],
[2., 1.]]))
plot_effect(np.array([[0.780, 0.563],
[0.913, 0.659]]))
plot_effect(np.array([[0, 2.], [2., 0.]]))
# Least Squares
import pandas as pd
df = pd.read_csv('hws.csv', sep=',')
data = df.values
np.random.shuffle(data)
X = data[:, :2]
b = data[:, 2]
# add noise before displaying any data (privacy)
X = np.maximum(5, X + np.random.normal(0, 2.5, X.shape))
X.shape
# np.round(X, 1)[:5]
np.matmul(np.matmul( np.linalg.inv(np.dot(X.T, X)) , X.T), b)
X = np.concatenate([X, np.ones([X.shape[0], 1])], 1)
X.shape
np.matmul(np.matmul( np.linalg.inv(np.dot(X.T, X)) , X.T), b)