#!/usr/bin/python """ Contrast function """ import argparse import numpy as np from matplotlib import pyplot as plt def setup_data(): mu = 0 sigma = 1 samples = 500 s1 = np.random.random(size=samples) s2 = np.random.random(size=samples) return np.array([s1, s2]) def main(args): s = setup_data() A = np.array([[1,2],[3,1]]) x = A.dot(s) x_mu = 1./len(x[0])*np.sum(x, axis=1) Cx = np.zeros((len(x), len(x[0]))) Cx_intermediate = np.zeros((len(x), len(x[0]))) # Calculate Error Vectors for i in xrange(0, len(Cx[0,:])): Cx_intermediate[0,i] = x[0,i]-x_mu[0] Cx_intermediate[1,i] = x[1,i]-x_mu[1] #Cx_intermediate[2,i] = x[2,i]-x_mu[2] # Calculate Covariance matrix based on error vectors Cx = np.dot(Cx_intermediate, (Cx_intermediate.T)) # Calc eigen values and vectors w, M = np.linalg.eig(Cx) M = M.T # try this line print x[0] print "\n\n" print "-- 12.3c --" print "Covariance Matrix, Cx" print np.around(Cx, decimals=3) print "Eigenvalues" print np.around(w, decimals=3) print "\n" print "-- 12.3d --" print "Eigenvectors of Covariance matrix" print M.T # Calculate Covariance Matrix of Transformation y = np.dot(M, x) Cy = np.dot(np.dot(M, Cx), M.T) print len(y) print "Cy" print np.around(Cy, decimals=3) wy, My = np.linalg.eig(Cy) My = My.T print "Eigenvalues of Cy" print np.around(wy, decimals=3) fig = plt.figure() # 12.4a #plt.scatter(s[0],s[1]) # 12.4b #plt.scatter(x[0],x[1]) # 12.4c plt.scatter(y[0],y[1]) plt.show() if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("-n", "--n", type=int, default=100, help="number of points") parser.add_argument("-s", "--s", type=int, default=0.5, help="standard deviation") args = parser.parse_args() main(args)