########################################################################## ## Set GAP return as NAN gap_ret = np.where(dates.time == dates[0].time())[0] ret1[gap_ret, :] = np.NaN ret2[gap_ret, :] = np.NaN # Remove the NaNs NonNan_index = np.logical_not(np.isnan(ret1)) ret1 = ret1[NonNan_index[:, 0], :] ret2 = ret2[NonNan_index[:, 0], :] ## Final data dates = dates[NonNan_index[:, 0]] data = np.concatenate((ret1, ret2), axis=1) mean = np.mean(data, axis=0) corr = bMA.get_corrMatrix(data) cov = bMA.get_covMatrix(data) ############################################################ ################# PLOT DATA ############################### ############################################################ import numpy as np import scipy.stats as stats from matplotlib import pyplot as plt cdf = stats.norm.cdf data = ret1 if (qq_plot): # Get the histogram and gaussian estimations ! ## Scatter plot of the points
if (preprocess_data): ## Remove the NaNs NonNan_index = np.logical_not(np.isnan(ret1)) ret1 = ret1[NonNan_index[:,0],:] dates = dates[NonNan_index[:,0]] #### Apply some shifting if we want crosscorrelations though time lag_sigmals = 0 if(lag_sigmals != 0 ): lag_sigmals = ret1[:-lag_sigmals,:] ## Final data data = ret1 mean = np.mean(data, axis = 0) corr = bMA.get_corrMatrix(data) cov = bMA.get_covMatrix(data) ############################################################ ################# PLOT DATA ############################### ############################################################ if (perform_EM): # N,D = Xdata.shape data = data - np.mean(data, axis = 0) Xdata = [data] ######## Create the Distribution object ############################################## #### 1st Gaussian Distribution Gaussian_d = Cdist.CDistribution(name = "Gaussian"); Gaussian_d.set_distribution("Gaussian")
X_1,X_2 = X2D[:,[0]], X2D[:,[1]] mu_1, mu_2 = np.mean(Xproj, axis =0) std_1,std_2 = np.std(Xproj,axis =0) cov = np.cov(np.concatenate((X_1,X_2),axis = 1).T).T std_K = 3 ## Do stuff now ax0 = gl.subplot2grid((1,2), (0,0), rowspan=1, colspan=1) gl.scatter(X_1,X_2, alpha = 0.5, ax = ax0, lw = 4, AxesStyle = "Normal", labels = ["Original 2D data","X1: %s" %(symbolIDs[AAPL_id]), "X2: %s" %(symbolIDs[GOOGL_id])]) ax0.axis('equal') ################# Draw the error ellipse ################# mean,w,h,theta = bMA.get_gaussian_ellipse_params(X2D, Chi2val = 2.4477) corr = bMA.get_corrMatrix(X2D) cov = bMA.get_covMatrix(X2D) vecs,vals = bMA.get_eigenVectorsAndValues(X2D) r_ellipse = bMA.get_ellipse_points(mean,w,h,theta) gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax0, ls = "--",color = "k", lw = 2, legend = ["Corr: %.2f"%(corr[0,1])]) gl.plot([mean[0], mean[0] + vecs[0,0]*w], [mean[1], mean[1] + vecs[0,1]*w], ax = ax0, ls = "--",color = "k") gl.plot([mean[0], mean[0] + vecs[1,0]*h], [mean[1], mean[1] + vecs[1,1]*h], ax = ax0, ls = "--",color = "k") gl.plot([mean[0], mean[0] - vecs[0,0]*w], [mean[1], mean[1] - vecs[0,1]*w], ax = ax0, ls = "--",color = "k") gl.plot([mean[0], mean[0] - vecs[1,0]*h], [mean[1], mean[1] - vecs[1,1]*h], ax = ax0, ls = "--",color = "k")