import sys
import numpy as np
import matplotlib.pyplot as plt 
import csv

if len(sys.argv) < 2:
  print "Usage: python project.py <slope value>"
  sys.exit(1)
slope = float(sys.argv[1])

data = []
with open('scatter.csv','rb') as f:
  points = csv.reader(f)
  for p in points:
    data.append([float(p[0]), float(p[1])])
data = np.array(data).transpose()

vec = np.array([0.0,1.0])
if slope != np.inf:
  vec = np.array([1,slope])
vec /= np.linalg.norm(vec)

P = np.outer(vec,vec)
print "Projection Matrix\n",P

slopeAligned = P.dot(data)

phi = -np.arctan(slope)
# clockwise rotation by phi
R = np.array([[np.cos(phi), -np.sin(phi)],[np.sin(phi), np.cos(phi)]])
xAligned = R.dot(slopeAligned)
#compute variance here
variance = np.var(xAligned[0,:])

print "Empirical Variance is %.4f"%(variance)
plt.scatter(slopeAligned[0,:],slopeAligned[1,:])
plt.show()
