def create_image_training_epoch(X_data_tr, Y_data_tr, X_data_val, Y_data_val, tr_loss, val_loss, x_grid, y_grid, cf_a, video_fotograms_folder, epoch_i): """ Creates the image of the training and validation accuracy """ gl.init_figure(); ax1 = gl.subplot2grid((2,1), (0,0), rowspan=1, colspan=1) ax2 = gl.subplot2grid((2,1), (1,0), rowspan=1, colspan=1) plt.title("Training") ## First plot with the data and predictions !!! ax1 = gl.scatter(X_data_tr, Y_data_tr, ax = ax1, lw = 3,legend = ["tr points"], labels = ["Analysis of training", "X","Y"]) gl.scatter(X_data_val, Y_data_val, lw = 3,legend = ["val points"]) gl.plot (x_grid, y_grid, legend = ["Prediction function"]) gl.set_zoom(xlimPad = [0.2, 0.2], ylimPad = [0.2,0.2], X = X_data_tr, Y = Y_data_tr) ## Second plot with the evolution of parameters !!! ax2 = gl.plot([], tr_loss, ax = ax2, lw = 3, labels = ["RMSE. lr: %.3f"%cf_a.lr, "epoch","RMSE"], legend = ["train"]) gl.plot([], val_loss, lw = 3, legend = ["validation"], loc = 3) gl.set_fontSizes(ax = [ax1,ax2], title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 12, yticks = 12) # Set final properties and save figure gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.30) gl.savefig(video_fotograms_folder +'%i.png'%epoch_i, dpi = 100, sizeInches = [14, 10], close = True, bbox_inches = None)
def get_normalized_ll_byCluster_EM(X_train, X_test, y_train, y_test, Ks_params): Likelihoods_by_Cluster_train = dp.get_likelihoods_byClusters_EM(X_train, Ks_params) Likelihoods_by_Cluster_test = dp.get_likelihoods_byClusters_EM(X_test, Ks_params) # print Likelihoods_by_Cluster_train pos_list = np.where(np.array(y_train) == 0)[0] neg_list = np.where(np.array(y_train) == 1)[0] gl.scatter(Likelihoods_by_Cluster_train[pos_list,0], Likelihoods_by_Cluster_train[pos_list,2]) gl.scatter(Likelihoods_by_Cluster_train[neg_list,0], Likelihoods_by_Cluster_train[neg_list,2], nf= 0) #%% Normalize data Xtrain = Likelihoods_by_Cluster_train Xtest = Likelihoods_by_Cluster_test Ytrain = y_train Ytest = y_test Ntrain,Ndim = Xtrain.shape Ntest, Ndim = Xtest.shape mx = np.mean(Xtrain,axis=0,dtype=np.float64) stdx = np.std(Xtrain,axis=0,dtype=np.float64) # print mx # print stdx Xtrain = np.divide(Xtrain-np.tile(mx,[Ntrain,1]),np.tile(stdx,[Ntrain,1])) Xtest = np.divide(Xtest-np.tile(mx,[Ntest,1]),np.tile(stdx,[Ntest,1])) # print Xtrain return Xtrain, Xtest, Ytrain, Ytest
def plot_learnt_function(X_data_tr, Y_data_tr, X_data_val, Y_data_val, x_grid, y_grid, cf_a, folder_images): gl.init_figure() ax1 = gl.scatter(X_data_tr, Y_data_tr, lw=3, legend=["tr points"], labels=["Data", "X", "Y"], alpha=0.2) ax2 = gl.scatter(X_data_val, Y_data_val, lw=3, legend=["val points"], alpha=0.2) gl.set_fontSizes(ax=[ax1, ax2], title=20, xlabel=20, ylabel=20, legend=20, xticks=12, yticks=12) gl.plot(x_grid, y_grid, legend=["training line"]) gl.savefig(folder_images + 'Training_Example_Data.png', dpi=100, sizeInches=[14, 4])
def plots_weights_layer(mu_W, sigma_W, sigma_b, mu_b, ax1, ax2, legend_layer, plot_pdf=1): """ Plot the given weights of the layer """ # For each of the weights we plot them !! color = gl.get_color(None) if (plot_pdf): for i in range(sigma_W.size): x_grid, y_val = bMA.gaussian1D_points(mean=mu_W[i], std=sigma_W[i], std_K=3) gl.plot( x_grid, y_val, ax=ax1, fill=1, alpha=0.15, color=color, labels=["Bayesian weights", "", "p(w)"], alpha_line=0.15 # ,legend = ["W:%i"%(i+1)] ) ###legend = ["M: %.2e, std: %.2e"%(mu_W2[i], sigma_W2[i])]) gl.scatter(mu_W, sigma_W, ax=ax2, labels=["", r"$\mu_w$", r"$\sigma_w$"], color=color, legend=legend_layer, alpha=0.3) if (plot_pdf): for i in range(sigma_b.size): x_grid, y_val = bMA.gaussian1D_points(mean=mu_b[i], std=sigma_b[i], std_K=3) # color = gl.get_color(None) gl.plot( x_grid, y_val, ax=ax1, color=color, fill=1, alpha=0.3, alpha_line=0.15, AxesStyle="Normal - No xaxis", ls="--" # ,legend = ["b:%i"%(i+1)] ) ###legend = ["M: %.2e, std: %.2e"%(mu_W2[i], sigma_W2[i])]) gl.scatter(mu_b, sigma_b, ax=ax2, color=color, marker="s", alpha=0.3)
def create_image_training_epoch(X_data_tr, Y_data_tr, X_data_val, Y_data_val, tr_loss, val_loss, x_grid, y_grid, cf_a, video_fotograms_folder, epoch_i): """ Creates the image of the training and validation accuracy """ gl.init_figure() ax1 = gl.subplot2grid((2, 1), (0, 0), rowspan=1, colspan=1) ax2 = gl.subplot2grid((2, 1), (1, 0), rowspan=1, colspan=1) plt.title("Training") ## First plot with the data and predictions !!! ax1 = gl.scatter(X_data_tr, Y_data_tr, ax=ax1, lw=3, legend=["tr points"], labels=["Analysis of training", "X", "Y"]) gl.scatter(X_data_val, Y_data_val, lw=3, legend=["val points"]) gl.plot(x_grid, y_grid, legend=["Prediction function"]) gl.set_zoom(xlimPad=[0.2, 0.2], ylimPad=[0.2, 0.2], X=X_data_tr, Y=Y_data_tr) ## Second plot with the evolution of parameters !!! ax2 = gl.plot([], tr_loss, ax=ax2, lw=3, labels=["RMSE. lr: %.3f" % cf_a.lr, "epoch", "RMSE"], legend=["train"]) gl.plot([], val_loss, lw=3, legend=["validation"], loc=3) gl.set_fontSizes(ax=[ax1, ax2], title=20, xlabel=20, ylabel=20, legend=20, xticks=12, yticks=12) # Set final properties and save figure gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.30) gl.savefig(video_fotograms_folder + '%i.png' % epoch_i, dpi=100, sizeInches=[14, 10], close=True, bbox_inches=None)
def create_plot_variational_weights(model, ax1, ax2, plot_pdf=True): """ This function plots the variational weights in the 2 axes given """ l = 0 for VBmodel in model.VBmodels: l += 1 if (VBmodel.type_layer == "linear"): sigma_W = Vil.softplus( VBmodel.rho_weight).detach().cpu().numpy().flatten() mu_W = VBmodel.mu_weight.detach().cpu().numpy().flatten() sigma_b = Vil.softplus( VBmodel.rho_bias).detach().cpu().numpy().flatten() mu_b = VBmodel.mu_bias.detach().cpu().numpy().flatten() legend_final = ["Layer %i" % (l)] plots_weights_layer(mu_W, sigma_W, sigma_b, mu_b, ax1, ax2, legend_final, plot_pdf) else: sigma_W = Vil.softplus( VBmodel.rho_weight_ih).detach().cpu().numpy().flatten() mu_W = VBmodel.mu_weight_ih.detach().cpu().numpy().flatten() sigma_b = Vil.softplus( VBmodel.rho_bias_ih).detach().cpu().numpy().flatten() mu_b = VBmodel.mu_bias_ih.detach().cpu().numpy().flatten() legend_final = ["LSTM layer ih: %i" % (l)] plots_weights_layer(mu_W, sigma_W, sigma_b, mu_b, ax1, ax2, legend_final, plot_pdf) # Now the hidden weights sigma_W = Vil.softplus( VBmodel.rho_weight_hh).detach().cpu().numpy().flatten() mu_W = VBmodel.mu_weight_hh.detach().cpu().numpy().flatten() sigma_b = Vil.softplus( VBmodel.rho_bias_hh).detach().cpu().numpy().flatten() mu_b = VBmodel.mu_bias_hh.detach().cpu().numpy().flatten() legend_final = ["LSTM layer hh: %i" % (l)] plots_weights_layer(mu_W, sigma_W, sigma_b, mu_b, ax1, ax2, legend_final, plot_pdf) prior = VBmodel.prior gl.colorIndex -= 1 # So that the color is the same as the weights gl.scatter(0, prior.sigma1, lw=3, ax=ax2, legend=["Prior layer %i" % l], marker="x")
def scatter_allocations(self,allocations, labels = ['Porfolios', "Risk (std)", "Return"], legend = ["Portfolios"], lw = 2, alpha = 0.5, nf = 1): ## Given a set of allocations, this function # plots them into a graph. returns, risks = self.compute_allocations(allocations) ## Scatter the random portfolios gl.scatter(risks, returns,labels = labels, legend = legend, nf = nf, lw = lw, alpha = alpha)
def plots_weights_layer(mu_W, sigma_W, mu_b, sigma_b, ax1, title, legend = ["Weights and biases"],alpha = 0.2): """ Plot the given weights of the layer """ # For each of the weights we plot them !! color = gl.get_color(None) gl.scatter( mu_W, sigma_W, ax = ax1, labels = [title,r"$\mu_w$",r"$\sigma_w$"], color = color, legend = legend, alpha = alpha) gl.scatter(mu_b, sigma_b, ax = ax1, color = color, marker = "s", alpha =alpha)
def IFE_a(self, year_start=1996, year_finish=2016, window=10): ## Basic, just look at the bloody graphs self.pf.set_interval(dt.datetime(year_start, 1, 1), dt.datetime(year_finish, 1, 1)) dates = self.get_dates() prices = self.pf.get_timeSeries(self.period) returns = self.get_Returns() # print returns.shape gl.plot( dates, prices, labels=["Monthly price of Symbols", "Time (years)", "Price (dolar)"], legend=self.pf.symbols.keys(), loc=2) gl.savefig(folder_images + 'pricesAll.png', dpi=150, sizeInches=[2 * 8, 1.5 * 6]) gl.plot( dates, returns, labels=["Monthly return of the Symbols", "Time (years)", "Return (%)"], legend=self.pf.symbols.keys()) gl.savefig(folder_images + 'returnsAll.png', dpi=150, sizeInches=[2 * 8, 1.5 * 6]) ## Distribution obtaining gl.set_subplots(2, 2) for i in range(4): gl.histogram(returns[:, i], labels=[self.symbol_names[i]]) gl.savefig(folder_images + 'returnDistribution.png', dpi=150, sizeInches=[2 * 8, 1.5 * 6]) ############## Posible Transformations ################## ws = [3, 4, 6, 8] gl.set_subplots(2, 2) for w in ws: means, ranges = bMl.get_meanRange(prices[:, 1], w) gl.scatter(means, ranges, lw=4, labels=["", "mean", "range"], legend=["w = %i" % (w)]) gl.savefig(folder_images + 'rangeMean.png', dpi=150, sizeInches=[2 * 8, 1.5 * 6])
def plot_learnt_function(X_data_tr, Y_data_tr, X_data_val, Y_data_val, x_grid, y_grid, cf_a, folder_images): gl.init_figure() ax1 = gl.scatter(X_data_tr, Y_data_tr, lw = 3,legend = ["tr points"], labels = ["Data", "X","Y"], alpha = 0.2) ax2 = gl.scatter(X_data_val, Y_data_val, lw = 3,legend = ["val points"], alpha = 0.2) gl.set_fontSizes(ax = [ax1,ax2], title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 12, yticks = 12) gl.plot (x_grid, y_grid, legend = ["training line"]) gl.savefig(folder_images +'Training_Example_Data.png', dpi = 100, sizeInches = [14, 4])
def plot_data_regression_1d_2axes(X_data_tr, Y_data_tr, xgrid_real_func, ygrid_real_func, X_data_val, Y_data_val, x_grid,all_y_grid, most_likely_ygrid, alpha_points, color_points_train, color_points_val, color_most_likey,color_mean, color_truth, ax1,ax2): """ This function plots the outputs of the Regression model for the 1D example """ ## Compute mean and std of regression std_samples_grid = np.std(all_y_grid, axis = 1) mean_samples_grid = np.mean(all_y_grid, axis = 1) ############## ax1: Data + Mostlikely + Real + Mean !! ######################## if(type(ax1) != type(None)): gl.scatter(X_data_tr, Y_data_tr, ax = ax1, lw = 3, #legend = ["tr points"], labels = ["Data and predictions", "","Y"], alpha = alpha_points, color = color_points_train) gl.scatter(X_data_val, Y_data_val, ax = ax1, lw = 3, #legend = ["val points"], alpha = alpha_points, color = color_points_val) gl.plot (xgrid_real_func, ygrid_real_func, ax = ax1, alpha = 0.90, color = color_truth, legend = ["Truth"]) gl.plot (x_grid, most_likely_ygrid, ax = ax1, alpha = 0.90, color = color_most_likey, legend = ["Most likely"]) gl.plot (x_grid, mean_samples_grid, ax = ax1, alpha = 0.90, color = color_mean, legend = ["Posterior mean"], AxesStyle = "Normal - No xaxis") ############## ax2: Data + Realizations of the function !! ###################### if(type(ax2) != type(None)): gl.scatter(X_data_tr, Y_data_tr, ax = ax2, lw = 3, # legend = ["tr points"], labels = ["", "X","Y"], alpha = alpha_points, color = color_points_train) gl.scatter(X_data_val, Y_data_val, ax = ax2, lw = 3, # legend = ["val points"], alpha = alpha_points, color = color_points_val) gl.plot (x_grid, all_y_grid, ax = ax2, alpha = 0.15, color = "k") gl.plot (x_grid, mean_samples_grid, ax = ax2, alpha = 0.90, color = "b", legend = ["Mean realization"]) gl.set_zoom(xlimPad = [0.2,0.2], ylimPad = [0.2,0.2], ax = ax2, X = X_data_tr, Y = Y_data_tr)
def marketTiming(self,returns = [], ind_ret = [], mode = "Treynor-Mazuy"): # Investigate if the model is good. # We put a cuatric term of the error. returns = ul.fnp(returns) ind_ret = ul.fnp(ind_ret) if (returns.size == 0): returns = self.get_PortfolioReturn() if (ind_ret.size == 0): ind_ret = self.get_indexReturns() # Instead of fitting a line, we fit a parabola, to try to see # if we do better than the market return. If when Rm is higher, we have # higher beta, and if when Rm is lower, we have lower beta. So higher # and lowr return fitting a curve, cuatric, gl.scatter(ind_ret, returns, labels = ["Treynor-Mazuy", "Index Return", "Portfolio Return"], legend = ["Returns"]) ## Linear regression: Xres = ind_ret coeffs = bMl.get_linearRef(Xres, returns) Npoints = 10000 x_grid = np.array(range(Npoints))/float(Npoints) x_grid = x_grid*(max(ind_ret) - min(ind_ret)) + min(ind_ret) x_grid = x_grid.reshape(Npoints,1) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid), axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) gl.plot(x_grid, y_grid, legend = ["Linear Regression"], nf = 0) Xres = np.concatenate((ind_ret,np.power(ind_ret,2)),axis = 1) coeffs = bMl.get_linearRef(Xres, returns) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid,np.power(x_grid,2).reshape(Npoints,1) ),axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) # print y_grid.shape gl.plot(x_grid, y_grid, legend = ["Quadratic Regression"], nf = 0) print coeffs return 1
def plot_projections_VAE(model, X_data_tr, ax1): """ This function should plot the PCA mus to 2 Dimensions of the space """ device = model.cf_a.device dtype = model.cf_a.dtype Xtrain = torch.tensor(X_data_tr, device=device, dtype=dtype) model.eval() reconstructions, mus, rhos = model.forward(Xtrain) ## We have the mus and std of every sample !! mus = mus.detach().cpu().numpy() ## [Nsamples, Dim] std = Vil.softplus(rhos).detach().cpu().numpy() pca = PCA(n_components=2) pca.fit(mus) ## PCA projection to 2 Dimensions ## TODO, for now we just get the 2 variables mus_to_2_dim = pca.transform(mus) #mus[:,[0,1]] stds_to_2_dim = std[:, [0, 1]] alpha = 0.2 target = X_data_tr[:, -1] - X_data_tr[:, 0] ###### COLOR ######### for i in range(X_data_tr.shape[0]): if target[i] > 0: gl.scatter(mus_to_2_dim[i, 0], mus_to_2_dim[i, 1], legend=[], labels=["Projeciton of points", "var1", "var2"], alpha=alpha, ax=ax1, color="r") else: gl.scatter(mus_to_2_dim[i, 0], mus_to_2_dim[i, 1], legend=[], labels=["Projeciton of points", "var1", "var2"], alpha=alpha, ax=ax1, color="b")
def scatter_deltaDailyMagic(self): ## PLOTS DAILY HEIKE ASHI ddelta = self.get_timeSeriesbyName("RangeCO") hldelta = self.get_timeSeriesbyName("RangeHL") mdelta = self.get_magicDelta() labels = ["Delta Magic Scatter", "Magic", "Delta"] gl.scatter(mdelta, ddelta, labels=labels, legend=[self.symbolID], nf=1) # gl.set_subplots(1,1) gl.scatter_3D(mdelta, ddelta, hldelta, labels=labels, legend=[self.symbolID], nf=1)
def scatter_deltaDailyMagic(self): ## PLOTS DAILY HEIKE ASHI ddelta = self.get_timeSeriesbyName("RangeCO") hldelta = self.get_timeSeriesbyName("RangeHL") mdelta = self.get_magicDelta() labels = ["Delta Magic Scatter","Magic","Delta"] gl.scatter(mdelta,ddelta, labels = labels, legend = [self.symbolID], nf = 1) # gl.set_subplots(1,1) gl.scatter_3D(mdelta,ddelta, hldelta, labels = labels, legend = [self.symbolID], nf = 1)
def scatter_allocations(self, allocations, labels=['Porfolios', "Risk (std)", "Return"], legend=["Portfolios"], lw=2, alpha=0.5, nf=1): ## Given a set of allocations, this function # plots them into a graph. returns, risks = self.compute_allocations(allocations) ## Scatter the random portfolios gl.scatter(risks, returns, labels=labels, legend=legend, nf=nf, lw=lw, alpha=alpha)
def plot_data_classification_2d_2axes(X_data_tr, Y_data_tr, xgrid_real_func, ygrid_real_func, X_data_val, Y_data_val, xx,yy,all_y_grid, most_likely_ygrid, alpha_points, color_points_train, color_points_val, color_most_likey,color_mean, color_truth, ax1,ax2): """ This function plots the outputs of the Classification model for the 2D example """ alpha_points = 1 ## Compute mean and std of regression std_samples_grid = np.std(all_y_grid, axis = 1) mean_samples_grid = np.mean(all_y_grid, axis = 1) ############## ax1: Data + Mostlikely + Real + Mean !! ######################## classes = np.unique(Y_data_tr).flatten(); colors = ["r","g","b"] for i in range(classes.size): X_data_tr_class = X_data_tr[np.where(Y_data_tr == classes[i])[0],:] X_data_val_class = X_data_val[np.where(Y_data_val == classes[i])[0],:] # print (X_data_tr_class.shape) # print (classes) # print (X_data_tr) if ((X_data_tr_class.size > 0) and (X_data_val_class.size > 0)): gl.scatter(X_data_tr_class[:,0].flatten().tolist(), X_data_tr_class[:,1].flatten().tolist(), ax = ax1, lw = 3, #legend = ["tr points"], labels = ["Data and predictions", "","Y"], alpha = alpha_points, color = colors[i]) gl.scatter(X_data_val_class[:,0].flatten(), X_data_val_class[:,1].flatten(), ax = ax1, lw = 3,color = colors[i], #legend = ["val points"], alpha = alpha_points, marker = ">") out = ax1.contourf(xx, yy, most_likely_ygrid.reshape(xx.shape), cmap=plt.cm.coolwarm, alpha=0.5) # ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k') # gl.plot (xgrid_real_func, ygrid_real_func, ax = ax1, alpha = 0.90, color = color_truth, legend = ["Truth"]) for i in range(classes.size): X_data_tr_class = X_data_tr[np.where(Y_data_tr == classes[i])[0],:] X_data_val_class = X_data_val[np.where(Y_data_val == classes[i])[0],:] # print (X_data_tr_class.shape) # print (classes) # print (X_data_tr) if ((X_data_tr_class.size > 0) and (X_data_val_class.size > 0)): gl.scatter(X_data_tr_class[:,0].flatten().tolist(), X_data_tr_class[:,1].flatten().tolist(), ax = ax2, lw = 3, #legend = ["tr points"], labels = ["", "X","Y"], alpha = alpha_points, color = colors[i]) gl.scatter(X_data_val_class[:,0].flatten(), X_data_val_class[:,1].flatten(), ax = ax2, lw = 3,color = colors[i], #legend = ["val points"], alpha = alpha_points, marker = ">") for ygrid in all_y_grid: out = ax2.contourf(xx, yy, ygrid.reshape(xx.shape), cmap=plt.cm.coolwarm, alpha=0.5) ############## ax2: Data + Realizations of the function !! ###################### gl.set_zoom(xlimPad = [0.3,0.3], ylimPad = [0.3,0.3], ax = ax2, X = X_data_tr[:,0], Y = X_data_tr[:,1])
def IFE_a(self, year_start = 1996, year_finish = 2016, window = 10): ## Basic, just look at the bloody graphs self.pf.set_interval(dt.datetime(year_start,1,1),dt.datetime(year_finish,1,1)) dates = self.get_dates() prices = self.pf.get_timeSeries(self.period) returns = self.get_Returns() # print returns.shape gl.plot(dates, prices, labels = ["Monthly price of Symbols", "Time (years)", "Price (dolar)"], legend = self.pf.symbols.keys(), loc = 2) gl.savefig(folder_images +'pricesAll.png', dpi = 150, sizeInches = [2*8, 1.5*6]) gl.plot(dates, returns, labels = ["Monthly return of the Symbols", "Time (years)", "Return (%)"], legend = self.pf.symbols.keys()) gl.savefig(folder_images +'returnsAll.png', dpi = 150, sizeInches = [2*8, 1.5*6]) ## Distribution obtaining gl.set_subplots(2,2) for i in range(4): gl.histogram(returns[:,i], labels = [self.symbol_names[i]]) gl.savefig(folder_images +'returnDistribution.png', dpi = 150, sizeInches = [2*8, 1.5*6]) ############## Posible Transformations ################## ws = [3, 4, 6, 8] gl.set_subplots(2,2) for w in ws: means, ranges = bMl.get_meanRange(prices[:,1], w) gl.scatter(means, ranges, lw = 4, labels = ["", "mean","range"], legend = ["w = %i" %(w)]) gl.savefig(folder_images +'rangeMean.png', dpi = 150, sizeInches = [2*8, 1.5*6])
def create_plot_variational_weights(model, ax1, ax2, plot_pdf = True): """ This function plots the variational weights in the 2 axes given """ l = 0 for VBmodel in model.VBmodels: l+=1 if (VBmodel.type_layer == "linear"): sigma_W = Vil.softplus(VBmodel.rho_weight).detach().cpu().numpy().flatten() mu_W = VBmodel.mu_weight.detach().cpu().numpy().flatten() sigma_b = Vil.softplus(VBmodel.rho_bias).detach().cpu().numpy().flatten() mu_b = VBmodel.mu_bias.detach().cpu().numpy().flatten() legend_final = ["Layer %i"%(l)] plots_weights_layer(mu_W, sigma_W,sigma_b, mu_b, ax1, ax2, legend_final, plot_pdf) else: sigma_W = Vil.softplus(VBmodel.rho_weight_ih).detach().cpu().numpy().flatten() mu_W = VBmodel.mu_weight_ih.detach().cpu().numpy().flatten() sigma_b = Vil.softplus(VBmodel.rho_bias_ih).detach().cpu().numpy().flatten() mu_b = VBmodel.mu_bias_ih.detach().cpu().numpy().flatten() legend_final = ["LSTM layer ih: %i"%(l)] plots_weights_layer(mu_W, sigma_W,sigma_b, mu_b, ax1, ax2, legend_final,plot_pdf) # Now the hidden weights sigma_W = Vil.softplus(VBmodel.rho_weight_hh).detach().cpu().numpy().flatten() mu_W = VBmodel.mu_weight_hh.detach().cpu().numpy().flatten() sigma_b = Vil.softplus(VBmodel.rho_bias_hh).detach().cpu().numpy().flatten() mu_b = VBmodel.mu_bias_hh.detach().cpu().numpy().flatten() legend_final = ["LSTM layer hh: %i"%(l)] plots_weights_layer(mu_W, sigma_W,sigma_b, mu_b, ax1, ax2, legend_final,plot_pdf) prior = VBmodel.prior gl.colorIndex -= 1; # So that the color is the same as the weights gl.scatter(0, prior.sigma1, lw = 3, ax = ax2, legend = ["Prior layer %i"%l], marker = "x")
def plots_weights_layer(mu_W, sigma_W, mu_b, sigma_b, ax1, title, legend=["Weights and biases"], alpha=0.2): """ Plot the given weights of the layer """ # For each of the weights we plot them !! color = gl.get_color(None) gl.scatter(mu_W, sigma_W, ax=ax1, labels=[title, r"$\mu_w$", r"$\sigma_w$"], color=color, legend=legend, alpha=alpha) gl.scatter(mu_b, sigma_b, ax=ax1, color=color, marker="s", alpha=alpha)
def get_normalized_ll_byCluster_EM(X_train, X_test, y_train, y_test, Ks_params): Likelihoods_by_Cluster_train = dp.get_likelihoods_byClusters_EM( X_train, Ks_params) Likelihoods_by_Cluster_test = dp.get_likelihoods_byClusters_EM( X_test, Ks_params) # print Likelihoods_by_Cluster_train pos_list = np.where(np.array(y_train) == 0)[0] neg_list = np.where(np.array(y_train) == 1)[0] gl.scatter(Likelihoods_by_Cluster_train[pos_list, 0], Likelihoods_by_Cluster_train[pos_list, 2]) gl.scatter(Likelihoods_by_Cluster_train[neg_list, 0], Likelihoods_by_Cluster_train[neg_list, 2], nf=0) #%% Normalize data Xtrain = Likelihoods_by_Cluster_train Xtest = Likelihoods_by_Cluster_test Ytrain = y_train Ytest = y_test Ntrain, Ndim = Xtrain.shape Ntest, Ndim = Xtest.shape mx = np.mean(Xtrain, axis=0, dtype=np.float64) stdx = np.std(Xtrain, axis=0, dtype=np.float64) # print mx # print stdx Xtrain = np.divide(Xtrain - np.tile(mx, [Ntrain, 1]), np.tile(stdx, [Ntrain, 1])) Xtest = np.divide(Xtest - np.tile(mx, [Ntest, 1]), np.tile(stdx, [Ntest, 1])) # print Xtrain return Xtrain, Xtest, Ytrain, Ytest
def plot_VB_weights_mu_std_2D(VBmodel, ax1, type = "LinearVB", title = ""): """ This function plots the variational weights in the 2 axes given """ l = 0 if (type == "LinearVB"): [mu_W, sigma_W, mu_b, sigma_b] = get_LinearVB_weights(VBmodel) shape_weights = VBmodel.mu_weight.detach().cpu().numpy().shape title +=" " + str(shape_weights) # title = ["linear layer: %i"%(l)] plots_weights_layer(mu_W, sigma_W, mu_b, sigma_b, ax1, title) prior = VBmodel.prior max_mu,min_mu,max_std,min_std,max_abs = get_boundaries_plot(mu_W, sigma_W, mu_b,sigma_b) gl.scatter(0, prior.sigma1, lw = 3, ax = ax1, legend = ["Prior 1 (%.3f)"%(prior.sigma1)], color = "k", marker = "x",) gl.scatter(0, prior.sigma2, lw = 3,ax = ax1, legend = ["Prior 2 (%.3f)"%(prior.sigma2)], color = "b",marker = "x" ) plot_signifant_region(ax1, max_mu,min_mu,max_std,min_std,max_abs) gl.set_zoom (ax = ax1, xlimPad = [0.1, 0.1], ylimPad = [0.1,0.1], X = np.array([min_mu,max_mu]), Y = np.array([min_std,max_std]) ) if (type == "HighwayVB"): [mu_W, sigma_W, mu_b, sigma_b] = get_LinearVB_weights(VBmodel) shape_weights = VBmodel.mu_weight.detach().cpu().numpy().shape title +=" " + str(shape_weights) # title = ["linear layer: %i"%(l)] print(mu_W.shape, mu_b.shape) # plots_weights_layer(mu_W[:200,:], sigma_W[:200,:], mu_b[:200], sigma_b[:200], ax1, title + " $G(x)$") # plots_weights_layer(mu_W[200:,:], sigma_W[200:,:], mu_b[200:], sigma_b[200:], ax1, title+ " $H(x)$") plots_weights_layer(mu_W.reshape(shape_weights)[:200,:].flatten(), sigma_W.reshape(shape_weights)[:200,:].flatten(), mu_b[:200], sigma_b[:200], ax1, title, legend = ["Weights and biases G(x)"]) plots_weights_layer(mu_W.reshape(shape_weights)[200:,:].flatten(), sigma_W.reshape(shape_weights)[200:,:].flatten(), mu_b[200:], sigma_b[200:], ax1, title, legend = ["Weights and biases H(x)"]) prior = VBmodel.prior max_mu,min_mu,max_std,min_std,max_abs = get_boundaries_plot(mu_W, sigma_W, mu_b,sigma_b) gl.scatter(0, prior.sigma1, lw = 3, ax = ax1, legend = ["Prior 1 (%.3f)"%(prior.sigma1)], color = "k", marker = "x",) gl.scatter(0, prior.sigma2, lw = 3,ax = ax1, legend = ["Prior 2 (%.3f)"%(prior.sigma2)], color = "b",marker = "x" ) plot_signifant_region(ax1, max_mu,min_mu,max_std,min_std,max_abs) gl.set_zoom (ax = ax1, xlimPad = [0.1, 0.1], ylimPad = [0.1,0.1], X = np.array([min_mu,max_mu]), Y = np.array([min_std,max_std]) )
def plots_weights_layer(mu_W, sigma_W,sigma_b, mu_b, ax1, ax2, legend_layer, plot_pdf = 1): """ Plot the given weights of the layer """ # For each of the weights we plot them !! color = gl.get_color(None) if (plot_pdf): for i in range(sigma_W.size): x_grid, y_val = bMA.gaussian1D_points(mean = mu_W[i], std = sigma_W[i], std_K = 3) gl.plot(x_grid, y_val, ax = ax1, fill = 1, alpha = 0.15, color = color, labels = ["Bayesian weights","","p(w)"],alpha_line = 0.15 # ,legend = ["W:%i"%(i+1)] ) ###legend = ["M: %.2e, std: %.2e"%(mu_W2[i], sigma_W2[i])]) gl.scatter( mu_W, sigma_W, ax = ax2, labels = ["",r"$\mu_w$",r"$\sigma_w$"], color = color, legend = legend_layer, alpha = 0.3) if (plot_pdf): for i in range(sigma_b.size): x_grid, y_val = bMA.gaussian1D_points(mean = mu_b[i], std = sigma_b[i], std_K = 3) # color = gl.get_color(None) gl.plot(x_grid, y_val, ax = ax1, color = color, fill = 1, alpha = 0.3, alpha_line = 0.15, AxesStyle = "Normal - No xaxis", ls = "--" # ,legend = ["b:%i"%(i+1)] ) ###legend = ["M: %.2e, std: %.2e"%(mu_W2[i], sigma_W2[i])]) gl.scatter(mu_b, sigma_b, ax = ax2, color = color, marker = "s", alpha = 0.3)
def plot_corrab(self, symbol, nf = 1): # This function plots the returns of a symbol compared # to the index, and computes the regresion and correlation parameters. index = self.Sindex # The index sym_ret = self.pf.symbols[symbol].TDs[self.period].get_timeSeriesReturn() ind_ret = self.get_indexReturns() # Mean and covariance data = np.concatenate((sym_ret,ind_ret),axis = 1) means = np.mean(data, axis = 0) cov = np.cov(data) # Regression coeffs = bMl.get_linearRef(ind_ret, sym_ret) gl.scatter(ind_ret, sym_ret, labels = ["Gaussianity study", "Index: " + self.Sindex,symbol], legend = ["Returns"], nf = nf) ## Linear regression: Xres = ind_ret coeffs = bMl.get_linearRef(Xres, sym_ret) Npoints = 10000 x_grid = np.array(range(Npoints))/float(Npoints) x_grid = x_grid*(max(ind_ret) - min(ind_ret)) + min(ind_ret) x_grid = x_grid.reshape(Npoints,1) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid), axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) gl.plot(x_grid, y_grid, legend = ["b: %.2f ,a: %.2f" % (coeffs[1], coeffs[0])], nf = 0)
def plot_multiple_iterations(Xs,mus,covs, Ks ,myDManager, logl,theta_list,model_theta_list, folder_images): ######## Plot the original data ##### gl.init_figure(); gl.set_subplots(2,3); Ngraph = 6 colors = ["r","b","g"] K_G,K_W,K_vMF = Ks for i in range(Ngraph): indx = int(i*((len(theta_list)-1)/float(Ngraph-1))) nf = 1 for xi in range(len( Xs)): ## First cluster labels = ['EM Evolution. Kg:'+str(K_G)+ ', Kw:' + str(K_W) + ', K_vMF:' + str(K_vMF), "X1","X2"] ax1 = gl.scatter(Xs[xi][0,:],Xs[xi][1,:],labels = ["","",""] , color = colors[xi] ,alpha = 0.2, nf = nf) nf =0 mean,w,h,theta = bMA.get_gaussian_ellipse_params( mu = mus[xi], Sigma = covs[xi], Chi2val = 2.4477) r_ellipse = bMA.get_ellipse_points(mean,w,h,theta) gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax1, ls = "--", lw = 2 ,AxesStyle = "Normal2", color = colors[xi], alpha = 0.7) # Only doable if the clusters dont die for k_c in myDManager.clusterk_to_Dname.keys(): k = myDManager.clusterk_to_thetak[k_c] distribution_name = myDManager.clusterk_to_Dname[k_c] # G W if (distribution_name == "Gaussian"): ## Plot the ecolution of the mu #### Plot the Covariance of the clusters ! mean,w,h,theta = bMA.get_gaussian_ellipse_params( mu = theta_list[indx][k][0], Sigma = theta_list[indx][k][1], Chi2val = 2.4477) r_ellipse = bMA.get_ellipse_points(mean,w,h,theta) gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax1, ls = "-.", lw = 3, AxesStyle = "Normal2", legend = ["Kg(%i). pi:%0.2f"%(k, float(model_theta_list[indx][0][0,k]))]) elif(distribution_name == "Watson"): #### Plot the pdf of the distributino ! ## Distribution parameters for Watson kappa = float(theta_list[indx][k][1]) mu = theta_list[indx][k][0] Nsa = 1000 # Draw 2D samples as transformation of the angle Xalpha = np.linspace(0, 2*np.pi, Nsa) Xgrid= np.array([np.cos(Xalpha), np.sin(Xalpha)]) probs = [] # Vector with probabilities for i in range(Nsa): probs.append(np.exp(Wad.Watson_pdf_log(Xgrid[:,i],[mu,kappa]) )) probs = np.array(probs) # Plot it in polar coordinates X1_w = (1 + probs) * np.cos(Xalpha) X2_w = (1 + probs) * np.sin(Xalpha) gl.plot(X1_w,X2_w, alpha = 1, lw = 3, ls = "-.",legend = ["Kw(%i). pi:%0.2f"%(k, float(model_theta_list[indx][0][0,k]))]) elif(distribution_name == "vonMisesFisher"): #### Plot the pdf of the distributino ! ## Distribution parameters for Watson kappa = float(theta_list[indx][k][1]); mu = theta_list[indx][k][0] Nsa = 1000 # Draw 2D samples as transformation of the angle Xalpha = np.linspace(0, 2*np.pi, Nsa) Xgrid= np.array([np.cos(Xalpha), np.sin(Xalpha)]) probs = [] # Vector with probabilities for i in range(Nsa): probs.append(np.exp(vMFd.vonMisesFisher_pdf_log(Xgrid[:,i],[mu,kappa]) )) probs = np.array(probs) probs = probs.reshape((probs.size,1)).T # Plot it in polar coordinates X1_w = (1 + probs) * np.cos(Xalpha) X2_w = (1 + probs) * np.sin(Xalpha) # print X1_w.shape, X2_w.shape gl.plot(X1_w,X2_w, alpha = 1, lw = 3, ls = "-.", legend = ["Kvmf(%i). pi:%0.2f"%(k, float(model_theta_list[indx][0][0,k]))]) ax1.axis('equal') gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.2, hspace=0.01) gl.savefig(folder_images +'Final_State2. K_G:'+str(K_G)+ ', K_W:' + str(K_W) + '.png', dpi = 100, sizeInches = [18, 8])
def plot_data_classification_2d_2axes(X_data_tr, Y_data_tr, xgrid_real_func, ygrid_real_func, X_data_val, Y_data_val, xx, yy, all_y_grid, most_likely_ygrid, alpha_points, color_points_train, color_points_val, color_most_likey, color_mean, color_truth, ax1, ax2): """ This function plots the outputs of the Classification model for the 2D example """ alpha_points = 1 ## Compute mean and std of regression std_samples_grid = np.std(all_y_grid, axis=1) mean_samples_grid = np.mean(all_y_grid, axis=1) ############## ax1: Data + Mostlikely + Real + Mean !! ######################## classes = np.unique(Y_data_tr).flatten() colors = ["r", "g", "b"] for i in range(classes.size): X_data_tr_class = X_data_tr[np.where(Y_data_tr == classes[i])[0], :] X_data_val_class = X_data_val[np.where(Y_data_val == classes[i])[0], :] # print (X_data_tr_class.shape) # print (classes) # print (X_data_tr) if ((X_data_tr_class.size > 0) and (X_data_val_class.size > 0)): gl.scatter( X_data_tr_class[:, 0].flatten().tolist(), X_data_tr_class[:, 1].flatten().tolist(), ax=ax1, lw=3, #legend = ["tr points"], labels=["Data and predictions", "", "Y"], alpha=alpha_points, color=colors[i]) gl.scatter( X_data_val_class[:, 0].flatten(), X_data_val_class[:, 1].flatten(), ax=ax1, lw=3, color=colors[i], #legend = ["val points"], alpha=alpha_points, marker=">") out = ax1.contourf(xx, yy, most_likely_ygrid.reshape(xx.shape), cmap=plt.cm.coolwarm, alpha=0.5) # ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k') # gl.plot (xgrid_real_func, ygrid_real_func, ax = ax1, alpha = 0.90, color = color_truth, legend = ["Truth"]) for i in range(classes.size): X_data_tr_class = X_data_tr[np.where(Y_data_tr == classes[i])[0], :] X_data_val_class = X_data_val[np.where(Y_data_val == classes[i])[0], :] # print (X_data_tr_class.shape) # print (classes) # print (X_data_tr) if ((X_data_tr_class.size > 0) and (X_data_val_class.size > 0)): gl.scatter( X_data_tr_class[:, 0].flatten().tolist(), X_data_tr_class[:, 1].flatten().tolist(), ax=ax2, lw=3, #legend = ["tr points"], labels=["", "X", "Y"], alpha=alpha_points, color=colors[i]) gl.scatter( X_data_val_class[:, 0].flatten(), X_data_val_class[:, 1].flatten(), ax=ax2, lw=3, color=colors[i], #legend = ["val points"], alpha=alpha_points, marker=">") for ygrid in all_y_grid: out = ax2.contourf(xx, yy, ygrid.reshape(xx.shape), cmap=plt.cm.coolwarm, alpha=0.5) ############## ax2: Data + Realizations of the function !! ###################### gl.set_zoom(xlimPad=[0.3, 0.3], ylimPad=[0.3, 0.3], ax=ax2, X=X_data_tr[:, 0], Y=X_data_tr[:, 1])
if plotting_zones: plot_flag = 1 for iCO in range(5): subset = df_list[iCO] OC = DifferenOC[iCO] Ns, Nd = subset.shape List_of_points = [] for i in range(Ns): lattitud1 = subset["Latitude(DDD.dddd)"][i] longitud1 = subset["Longitude(DDD.dddd)"][i] List_of_points.append(get_XYcoordinates(lattitud1, longitud1)) List_of_points = np.array(List_of_points) gl.scatter(List_of_points[:, 0], List_of_points[:, 1], nf=plot_flag, legend=[OC], alpha=0.8) plot_flag = 0 ####################################################################### #################################################################v ## "EquipmentOwnership" ## TrackingTimeOrder. If bigger than 0, it is iddle and we can use it ## It also has to be empty OperationalStatus. #df["TrackingTimeOrder"] if (spot_hired_analysis): ####### Administration sharing !! ##################### # For a given region, we detect a Spot hired, and we see from the IDLE
def IFE_e(self, ObjectiveR=0.003, Rf=0.0, year_start=1996, year_finish=2016, window=10): # Just, choose a desired return, # Using training Samples calculate using the market line # the optimal porfolio for that. # Then, using also the last year ( test), recalculate the portfolio needed # for that return, and the difference between is the turnover self.set_Rf(Rf) nf_flag = 1 desired_Portfolios = [] all_dates = [] for year_test in range(year_start, year_finish - window + 1): # +1 !! # Set the dates self.pf.set_interval(dt.datetime(year_test, 1, 1), dt.datetime(year_test + window, 1, 1)) # Obtain the market line !! w = self.TangentPortfolio(Rf=Rf) # Obtain allocation # Obtain the expected return and std when using all our money ! self.set_allocation(w) expRet, stdRet = self.get_metrics(investRf="no") param = bMl.obtain_equation_line(Rf, expRet, stdRet) bias, slope = param # Once we have the equation of the line, we obtain how much money # we need to use to reach the desired Expecred Return. # Rt = (1 - X)Rf + XRp with X = sum(w) # For a desired Rt we solve the X X = (ObjectiveR - Rf) / (expRet - Rf) # print X # So the desired porfolio is: wdesired = w * X desired_Portfolios.append(wdesired) gl.plot([0, 1.3 * abs(X * stdRet)], [bias, bias + 1.3 * abs(slope * stdRet * X)], labels=["Desired Portfolios", "Risk (std)", "Return (%)"], legend=["%s, X: %0.3f" % ((year_test + window), X[0])], nf=nf_flag, loc=2) nf_flag = 0 gl.scatter([abs(X * stdRet)], [ObjectiveR], nf=0) dates = self.get_dates() all_dates.append(dates[-1]) # print wdesired gl.savefig(folder_images + 'desiredPortfolios.png', dpi=150, sizeInches=[2 * 8, 2 * 6]) # Now we calculate the turnovers Turnovers = [] prev_abs_alloc = [] # Previous, absolute allocation percentaje_changed = [] Nport = len(desired_Portfolios) for i in range(Nport - 1): to = bMl.get_TurnOver(desired_Portfolios[i], desired_Portfolios[i + 1]) Turnovers.append(to) prev_abs_alloc.append(np.sum(np.abs(desired_Portfolios[i]))) percentaje_changed.append(Turnovers[-1] / prev_abs_alloc[-1]) print Turnovers gl.set_subplots(1, 3) gl.bar(all_dates[1:], Turnovers, color="g", labels=["Portfolio turnovers", "Year", "Value"]) gl.add_text([all_dates[1:][3], max(Turnovers) * 0.80], "Mean: %0.2f" % np.mean(Turnovers), 30) gl.bar(all_dates[0:-1], prev_abs_alloc, color="r", labels=["Absolute allocations", "Year", "Value"]) gl.bar(all_dates[1:], percentaje_changed, color="b", labels=["Percentage turnover", "Year", "Value"]) gl.add_text( [all_dates[1:][3], max(percentaje_changed) * 0.80], "Mean: %0.2f" % np.mean(percentaje_changed), 30) gl.savefig(folder_images + 'turnovers.png', dpi=150, sizeInches=[2 * 8, 1 * 6])
mu2 = np.array([[-2], [-2]]) cov2 = np.array([[1.5, -0.2], [-0.2, 1.5]]) mu3 = np.array([[3], [-4]]) cov3 = np.array([[2, -0.8], [-0.8, 2]]) X1 = np.random.multivariate_normal(mu1.flatten(), cov1, N1).T X2 = np.random.multivariate_normal(mu2.flatten(), cov2, N2).T X3 = np.random.multivariate_normal(mu3.flatten(), cov3, N3).T ######## Plotting ##### gl.init_figure() ## First cluster ax1 = gl.scatter(X1[0, :], X1[1, :], labels=["Gaussian Generated Data", "x1", "x2"], legend=["K = 1"], color="r", alpha=0.5) mean, w, h, theta = bMA.get_gaussian_ellipse_params(mu=mu1, Sigma=cov1, Chi2val=2.4477) r_ellipse = bMA.get_ellipse_points(mean, w, h, theta) gl.plot(r_ellipse[:, 0], r_ellipse[:, 1], ax=ax1, ls="--", lw=2, AxesStyle="Normal2", color="r") ## Second cluster
tf = 1 # Final time delta_t = 0.03 # Period of sampling # Create the t- values tgrid = np.linspace(t0,tf, int(float(tf-t0)/delta_t)) tgrid = tgrid.reshape(tgrid.size,1) N = tgrid.size # Create the signal X = mean_function(tgrid, f1 = 1, f2 = 5, a1 = 0.4, a2 = 0.1, phi2 = 2*np.pi/7, m = 0.1 ) if (plot_mean_signal and plot_flag): ## Plot the orginal function gl.scatter(tgrid,X, lw = 1, alpha = 0.9, color = "k", nf = 1, labels = ["The true determinist signal mu(t)", "t", "mu(t)" ]) gl.plot(tgrid,X, lw = 2, color = "k", ls = "--", legend = ["True signal"]) gl.set_fontSizes( title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 20, yticks = 20) gl.savefig(folder_images +'GP_mean.png', dpi = 100, sizeInches = [2*8, 2*2]) ########################################################################### ############### Generate the structural noise ############################# ########################################################################### """ Now we generate the stocastic process that we add to X(t), generating noisy signal Y(t) = X(t) + e(t) Where we will assume e(t) is Gaussian with mean 0 e(t) \sim N(0,\sigma_t) So we have a Gaussian process, since each set of samples forms a jointly gaussian distribution. The relation between the noises will be given by the
gl.barchart(dates, dataHLOC) gl.plot(dates, EMAslow, legend=["Slow"], lw=1, color="b") gl.plot(dates, EMAfast, legend=["fast"], lw=1, color="r", xaxis_mode="hidden") ## Entry Axes ax2 = gl.plot(dates, ul.scale(EMAfast - EMAslow), legend=["Difference"], nf=1, sharex=ax1, labels=["", "", "Events"], fill=1, alpha=0.3) gl.stem(dates, crosses, legend=["TradeSignal"]) gl.scatter(dates, ul.scale(EMAfast - EMAslow), lw=0.5, alpha=0.5) gl.plot(dates, np.zeros(crosses.shape)) ## Exit Axes gl.add_hlines(datesExit, all_stops, ax=ax1, legend=["TrailingStop"], alpha=0.8, lw=0.2) ax3 = gl.plot(datesExit, ul.scale(-(all_stops - pCheckCross)), legend=["TrailingStop"], nf=1, sharex=ax1, labels=["", "", "TrailingStop"],
delta_t = 0.03 # Period of sampling # Create the t- values tgrid = np.linspace(t0, tf, int(float(tf - t0) / delta_t)) tgrid = tgrid.reshape(tgrid.size, 1) N = tgrid.size # Create the signal X = mean_function(tgrid, f1=1, f2=5, a1=0.4, a2=0.1, phi2=2 * np.pi / 7, m=0.1) if (plot_mean_signal and plot_flag): ## Plot the orginal function gl.scatter(tgrid, X, lw=1, alpha=0.9, color="k", nf=1, labels=["The true determinist signal mu(t)", "t", "mu(t)"]) gl.plot(tgrid, X, lw=2, color="k", ls="--", legend=["True signal"]) gl.set_fontSizes(title=20, xlabel=20, ylabel=20, legend=20, xticks=20, yticks=20) gl.savefig(folder_images + 'GP_mean.png', dpi=100, sizeInches=[2 * 8, 2 * 2]) ########################################################################### ############### Generate the structural noise #############################
X_1, X_2 = X[:, [i_1]], X[:, [i_2]] mu_1, mu_2 = mus[i_1], mus[i_2] Xjoint = np.concatenate((X_1, X_2), axis=1).T std_1, std_2 = stds[i_1], stds[i_2] mu = mus[[i_1, i_2]] cov = np.cov(Xjoint) std_K = 3 ## Do stuff now ax1 = gl.subplot2grid((1, 2), (0, 0), rowspan=1, colspan=1) gl.scatter(Xjoint[0, :], Xjoint[1, :], alpha=0.5, ax=ax1, lw=4, AxesStyle="Normal", labels=["", "U1", "U2"]) ax1.axis('equal') xx, yy, zz = bMA.get_gaussian2D_pdf(xbins=40j, ybins=40j, mu=mu, cov=cov, std_K=std_K, x_grid=None) ax1.contour(xx, yy, zz,
def IFE_f (self, ObjectiveR = 0.003, Rf = 0.0, year_start = 1996, year_finish = 2016, window = 10): ### The official one can be done executing the exercise c with another Rf ## Just another graph to show that now we should not use all the data. # Just, choose a desired return, # Using training Samples calculate using the market line # the optimal porfolio for that. # Then calculate for the next year, the real return # for that portfolio. # Do this for several years as well. self.set_Rf(Rf) nf_flag = 1 All_stds = [] PortfolioReturns = [] IndexReturns = [] all_dates = [] for year_test in range(year_start,year_finish - window + 1 - 1): # +1 !! # Set the dates self.pf.set_interval(dt.datetime(year_test,1,1),dt.datetime(year_test + window,1,1)) # Obtain the market line !! w = self.TangentPortfolio(Rf = Rf) # Obtain allocation self.set_allocation(w) # Obtain the expected return and std when using all our money ! expRet, stdRet = self.get_metrics (investRf = "no") param = bMl.obtain_equation_line(Rf, expRet, stdRet) bias, slope = param X = (ObjectiveR - Rf)/(expRet - Rf) wdesired = w*X ## Check that the output of this portfolio is the desired one. self.set_allocation(wdesired) # Set the allocation expRet, stdRet = self.get_metrics() # Get the expected return for that year # print ret ## Now that we have the desired w*X, we will calculate the resturn of ## the portfolio in the following year. # To do so, we set the dates, only to the next year, set the portfolio allocation # And calculate the yearly expected return !! # Set the dates to only the next year !! # Also, one month before in order to get the returns of the first month. self.pf.set_interval(dt.datetime(year_test + window,1,1),dt.datetime(year_test + window + 1,1,1)) self.set_allocation(wdesired) # Set the allocation expRet, stdRet = self.get_metrics() # Get the expected return for that year PortfolioRet = self.yearly_Return(expRet) # Get yearly returns PortfolioReturns.append(PortfolioRet) All_stds.append(self.yearly_covMatrix(stdRet)) indexRet = self.get_indexMeanReturn() indexRet = self.yearly_Return(indexRet) IndexReturns.append(indexRet) # dates = self.get_dates() all_dates.append(year_test + window + 1) ## Graph with the evolutio of the portfolio price after the assignment gl.plot(range(1,13), np.cumsum(self.get_PortfolioReturn()), nf = nf_flag, labels = ["Evolution of returns by month", "Months passed", "Cumulative Return"], legend = [str(year_test + window +1)]) nf_flag = 0 # print ret gl.savefig(folder_images +'returnsEvolMonth.png', dpi = 150, sizeInches = [2*8, 2*6]) ## Graph with the desired, the obtained returns and the returns of the index gl.bar(all_dates[:], IndexReturns, labels = ["Obtained returns", "Time (years)", "Return (%)"], legend = ["Index Return"], alpha = 0.8, nf = 1) gl.bar(all_dates[:], PortfolioReturns, labels = ["Returns of year", "Year","Value"], legend = ["Porfolio Return"], alpha = 0.8, nf = 0) gl.scatter(all_dates[:], self.yearly_Return(ObjectiveR) * np.ones((len(all_dates[:]),1)), legend = ["Objective Return"], nf = 0) gl.scatter(all_dates[:], All_stds, legend = ["Std of the portfolio return"], nf = 0) gl.savefig(folder_images +'returnsEvolYears.png', dpi = 150, sizeInches = [2*8, 2*6]) #### Crazy idea !! Lets plot where the f*****g efficient frontier went nf_flag = 1 PortfolioReturns = [] IndexReturns = [] all_dates = [] gl.set_subplots(2,3) for year_test in range(year_start,year_start + 6): # +1 !! # Set the dates self.pf.set_interval(dt.datetime(year_test,1,1),dt.datetime(year_test + window,1,1)) optimal, portfolios = self.efficient_frontier(kind = "Tangent") self.plot_allocations(portfolios, labels = ["Evolution of the efficient frontier"], legend = ["Frontier " + str(year_test + window) + " before"], color = "k", nf = 1) self.pf.set_interval(dt.datetime(year_test + window,1,1),dt.datetime(year_test + window + 1,1,1)) self.set_allocation(self.TangentPortfolio(Rf = Rf)) self.plot_allocations(portfolios, legend = ["Frontier " + str(year_test + window) + " after"], color = "r",nf = 0) gl.savefig(folder_images +'effEvol.png', dpi = 80, sizeInches = [4*8, 3*6])
def IFE_e (self, ObjectiveR = 0.003, Rf = 0.0, year_start = 1996, year_finish = 2016, window = 10): # Just, choose a desired return, # Using training Samples calculate using the market line # the optimal porfolio for that. # Then, using also the last year ( test), recalculate the portfolio needed # for that return, and the difference between is the turnover self.set_Rf(Rf) nf_flag = 1 desired_Portfolios = [] all_dates = [] for year_test in range(year_start,year_finish - window + 1): # +1 !! # Set the dates self.pf.set_interval(dt.datetime(year_test,1,1),dt.datetime(year_test + window,1,1)) # Obtain the market line !! w = self.TangentPortfolio(Rf = Rf) # Obtain allocation # Obtain the expected return and std when using all our money ! self.set_allocation(w) expRet, stdRet = self.get_metrics (investRf = "no") param = bMl.obtain_equation_line(Rf, expRet, stdRet) bias, slope = param # Once we have the equation of the line, we obtain how much money # we need to use to reach the desired Expecred Return. # Rt = (1 - X)Rf + XRp with X = sum(w) # For a desired Rt we solve the X X = (ObjectiveR - Rf)/(expRet - Rf) # print X # So the desired porfolio is: wdesired = w*X desired_Portfolios.append(wdesired) gl.plot([0,1.3*abs(X*stdRet)],[bias, bias + 1.3*abs(slope*stdRet*X)], labels = ["Desired Portfolios", "Risk (std)", "Return (%)"], legend = ["%s, X: %0.3f" %((year_test + window ), X[0])], nf = nf_flag, loc = 2) nf_flag = 0 gl.scatter([abs(X*stdRet)],[ObjectiveR], nf = 0) dates = self.get_dates() all_dates.append(dates[-1]) # print wdesired gl.savefig(folder_images +'desiredPortfolios.png', dpi = 150, sizeInches = [2*8, 2*6]) # Now we calculate the turnovers Turnovers = [] prev_abs_alloc = [] # Previous, absolute allocation percentaje_changed = [] Nport = len(desired_Portfolios) for i in range(Nport-1): to = bMl.get_TurnOver(desired_Portfolios[i], desired_Portfolios[i+1]) Turnovers.append(to) prev_abs_alloc.append(np.sum(np.abs(desired_Portfolios[i]))) percentaje_changed.append(Turnovers[-1]/prev_abs_alloc[-1]) print Turnovers gl.set_subplots(1,3) gl.bar(all_dates[1:], Turnovers, color = "g", labels = ["Portfolio turnovers", "Year","Value"]) gl.add_text([all_dates[1:][3],max(Turnovers)*0.80], "Mean: %0.2f" % np.mean(Turnovers), 30) gl.bar(all_dates[0:-1], prev_abs_alloc, color = "r", labels = ["Absolute allocations", "Year","Value"]) gl.bar(all_dates[1:], percentaje_changed, color = "b", labels = ["Percentage turnover", "Year","Value"]) gl.add_text([all_dates[1:][3],max(percentaje_changed)*0.80], "Mean: %0.2f" % np.mean(percentaje_changed), 30) gl.savefig(folder_images +'turnovers.png', dpi = 150, sizeInches = [2*8, 1*6])
def plot_VB_weights_mu_std_2D(VBmodel, ax1, type="LinearVB", title=""): """ This function plots the variational weights in the 2 axes given """ l = 0 if (type == "LinearVB"): [mu_W, sigma_W, mu_b, sigma_b] = get_LinearVB_weights(VBmodel) shape_weights = VBmodel.mu_weight.detach().cpu().numpy().shape title += " " + str(shape_weights) # title = ["linear layer: %i"%(l)] plots_weights_layer(mu_W, sigma_W, mu_b, sigma_b, ax1, title) prior = VBmodel.prior max_mu, min_mu, max_std, min_std, max_abs = get_boundaries_plot( mu_W, sigma_W, mu_b, sigma_b) gl.scatter( 0, prior.sigma1, lw=3, ax=ax1, legend=["Prior 1 (%.3f)" % (prior.sigma1)], color="k", marker="x", ) gl.scatter(0, prior.sigma2, lw=3, ax=ax1, legend=["Prior 2 (%.3f)" % (prior.sigma2)], color="b", marker="x") plot_signifant_region(ax1, max_mu, min_mu, max_std, min_std, max_abs) gl.set_zoom(ax=ax1, xlimPad=[0.1, 0.1], ylimPad=[0.1, 0.1], X=np.array([min_mu, max_mu]), Y=np.array([min_std, max_std])) if (type == "HighwayVB"): [mu_W, sigma_W, mu_b, sigma_b] = get_LinearVB_weights(VBmodel) shape_weights = VBmodel.mu_weight.detach().cpu().numpy().shape title += " " + str(shape_weights) # title = ["linear layer: %i"%(l)] print(mu_W.shape, mu_b.shape) # plots_weights_layer(mu_W[:200,:], sigma_W[:200,:], mu_b[:200], sigma_b[:200], ax1, title + " $G(x)$") # plots_weights_layer(mu_W[200:,:], sigma_W[200:,:], mu_b[200:], sigma_b[200:], ax1, title+ " $H(x)$") plots_weights_layer(mu_W.reshape(shape_weights)[:200, :].flatten(), sigma_W.reshape(shape_weights)[:200, :].flatten(), mu_b[:200], sigma_b[:200], ax1, title, legend=["Weights and biases G(x)"]) plots_weights_layer(mu_W.reshape(shape_weights)[200:, :].flatten(), sigma_W.reshape(shape_weights)[200:, :].flatten(), mu_b[200:], sigma_b[200:], ax1, title, legend=["Weights and biases H(x)"]) prior = VBmodel.prior max_mu, min_mu, max_std, min_std, max_abs = get_boundaries_plot( mu_W, sigma_W, mu_b, sigma_b) gl.scatter( 0, prior.sigma1, lw=3, ax=ax1, legend=["Prior 1 (%.3f)" % (prior.sigma1)], color="k", marker="x", ) gl.scatter(0, prior.sigma2, lw=3, ax=ax1, legend=["Prior 2 (%.3f)" % (prior.sigma2)], color="b", marker="x") plot_signifant_region(ax1, max_mu, min_mu, max_std, min_std, max_abs) gl.set_zoom(ax=ax1, xlimPad=[0.1, 0.1], ylimPad=[0.1, 0.1], X=np.array([min_mu, max_mu]), Y=np.array([min_std, max_std]))
def generate_images_iterations_ll(Xs,mus,covs, Ks ,myDManager, logl,theta_list,model_theta_list,folder_images_gif): # os.remove(folder_images_gif) # Remove previous images if existing """ WARNING: MEANT FOR ONLY 3 Distributions due to the color RGB """ import shutil ul.create_folder_if_needed(folder_images_gif) shutil.rmtree(folder_images_gif) ul.create_folder_if_needed(folder_images_gif) ######## Plot the original data ##### Xdata = np.concatenate(Xs,axis = 1).T colors = ["r","b","g"] K_G,K_W,K_vMF = Ks ### FOR EACH ITERATION for i in range(len(theta_list)): # theta_list indx = i gl.init_figure() ax1 = gl.subplot2grid((1,2), (0,0), rowspan=1, colspan=1) ## Get the relative ll of the Gaussian denoising cluster. ll = myDManager.pdf_log_K(Xdata,theta_list[indx]) N,K = ll.shape # print ll.shape for j in range(N): # For every sample #TODO: Can this not be done without a for ? # Normalize the probability of the sample being generated by the clusters Marginal_xi_probability = gf.sum_logs(ll[j,:]) ll[j,:] = ll[j,:]- Marginal_xi_probability ax1 = gl.scatter(Xdata[j,0],Xdata[j,1], labels = ['EM Evolution. Kg:'+str(K_G)+ ', Kw:' + str(K_W) + ', K_vMF:' + str(K_vMF), "X1","X2"], color = (np.exp(ll[j,1]), np.exp(ll[j,0]), np.exp(ll[j,2])) , ### np.exp(ll[j,2]) alpha = 1, nf = 0) # Only doable if the clusters dont die for k_c in myDManager.clusterk_to_Dname.keys(): k = myDManager.clusterk_to_thetak[k_c] distribution_name = myDManager.clusterk_to_Dname[k_c] # G W if (distribution_name == "Gaussian"): ## Plot the ecolution of the mu #### Plot the Covariance of the clusters ! mean,w,h,theta = bMA.get_gaussian_ellipse_params( mu = theta_list[indx][k][0], Sigma = theta_list[indx][k][1], Chi2val = 2.4477) r_ellipse = bMA.get_ellipse_points(mean,w,h,theta) gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax1, ls = "-.", lw = 3, AxesStyle = "Normal2", legend = ["Kg(%i). pi:%0.2f"%(k, float(model_theta_list[indx][0][0,k]))]) elif(distribution_name == "Watson"): #### Plot the pdf of the distributino ! ## Distribution parameters for Watson kappa = float(theta_list[indx][k][1]); mu = theta_list[-1][k][0] Nsa = 1000 # Draw 2D samples as transformation of the angle Xalpha = np.linspace(0, 2*np.pi, Nsa) Xgrid= np.array([np.cos(Xalpha), np.sin(Xalpha)]) probs = [] # Vector with probabilities for i in range(Nsa): probs.append(np.exp(Wad.Watson_pdf_log(Xgrid[:,i],[mu,kappa]) )) probs = np.array(probs) # Plot it in polar coordinates X1_w = (1 + probs) * np.cos(Xalpha) X2_w = (1 + probs) * np.sin(Xalpha) gl.plot(X1_w,X2_w, alpha = 1, lw = 3, ls = "-.", legend = ["Kw(%i). pi:%0.2f"%(k, float(model_theta_list[indx][0][0,k]))]) elif(distribution_name == "vonMisesFisher"): #### Plot the pdf of the distributino ! ## Distribution parameters for Watson kappa = float(theta_list[indx][k][1]); mu = theta_list[indx][k][0] Nsa = 1000 # Draw 2D samples as transformation of the angle Xalpha = np.linspace(0, 2*np.pi, Nsa) Xgrid= np.array([np.cos(Xalpha), np.sin(Xalpha)]) probs = [] # Vector with probabilities for i in range(Nsa): probs.append(np.exp(vMFd.vonMisesFisher_pdf_log(Xgrid[:,i],[mu,kappa]) )) probs = np.array(probs) probs = probs.reshape((probs.size,1)).T # Plot it in polar coordinates X1_w = (1 + probs) * np.cos(Xalpha) X2_w = (1 + probs) * np.sin(Xalpha) # print X1_w.shape, X2_w.shape gl.plot(X1_w,X2_w, alpha = 1, lw = 3, ls = "-.", legend = ["Kvmf(%i). pi:%0.2f"%(k, float(model_theta_list[indx][0][0,k]))]) gl.set_zoom(xlim = [-6,6], ylim = [-6,6], ax = ax1) ax2 = gl.subplot2grid((1,2), (0,1), rowspan=1, colspan=1) if (indx == 0): gl.add_text(positionXY = [0.1,.5], text = r' Initilization Incomplete LogLike: %.2f'%(logl[0]),fontsize = 15) pass elif (indx >= 1): gl.plot(range(1,np.array(logl).flatten()[1:].size +1),np.array(logl).flatten()[1:(indx+1)], ax = ax2, legend = ["Iteration %i, Incom LL: %.2f"%(indx, logl[indx])], labels = ["Convergence of LL with generated data","Iterations","LL"], lw = 2) gl.scatter(1, logl[1], lw = 2) pt = 0.05 gl.set_zoom(xlim = [0,len(logl)], ylim = [logl[1] - (logl[-1]-logl[1])*pt,logl[-1] + (logl[-1]-logl[1])*pt], ax = ax2) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.2, hspace=0.01) gl.savefig(folder_images_gif +'gif_'+ str(indx) + '.png', dpi = 100, sizeInches = [16, 8], close = "yes",bbox_inches = None) gl.close("all")
spf.print_final_clusters(myDManager, clusters_relation, theta_list[-1], model_theta_list[-1]) ####################################################################################################################### #### Plot the evolution of the centroids likelihood ! ##################################################### ####################################################################################################################### gl.plot(range(1,np.array(logl).flatten()[1:].size +1),np.array(logl).flatten()[1:], legend = ["HMM LogLikelihood"], labels = ["Convergence of LL with generated data","Iterations","LL"], lw = 2) gl.savefig(folder_images +'Likelihood_Evolution_' + clusters_relation+ "_"+str(periods[0])+ '.png', dpi = 100, sizeInches = [12, 6]) if (final_clusters_graph): gl.init_figure() ax1 = gl.scatter(ret1, np.zeros(ret1.size), alpha = 0.2, color = "k", legend = ["Data points %i"%(ret1.size)], labels = ["EM algorithm %i Gaussian fits"%(K),"Return",""]) for k in range(len(theta_list[-1])): mu_k = theta_list[-1][k][0] std_k = theta_list[-1][k][1] model_theta_last = model_theta_list[-1] x_grid, y_values = bMA.gaussian1D_points(mean = float( mu_k), std = float(np.sqrt(std_k)), std_K = 3.5) color = gl.get_color() if (len(model_theta_last) == 1): pi = model_theta_last[0] gl.plot(x_grid, y_values, color = color, fill = 1, alpha = 0.1, legend = ["Kg(%i), pi:%.2f"%(k+1,pi[0,k])]) else: pi = model_theta_last[0]
def generate_gaussian_data(folder_images, plot_original_data, N1 = 200, N2 = 300, N3 = 50): mu1 = np.array([[0],[0]]) cov1 = np.array([[0.8,-1.1], [-1.1,1.6]]) mu2 = np.array([[0],[0]]) cov2 = np.array([[0.3,0.45], [0.45,0.8]]) mu3 = np.array([[0],[0]]) cov3 = np.array([[0.1,0.0], [0.0,0.1]]) X1 = np.random.multivariate_normal(mu1.flatten(), cov1, N1).T X2 = np.random.multivariate_normal(mu2.flatten(), cov2, N2).T X3 = np.random.multivariate_normal(mu3.flatten(), cov3, N3).T # samples_X1 = np.array(range(X1.shape[1]))[np.where([X1[0,:] > 0])[0]] # samples_X1 = np.where(X1[0,:] > 0)[0] # np.array(range(X1.shape[1])) # print samples_X1 # X1 = X1[:,samples_X1] # X2 = np.concatenate((X2,X3),axis = 1) ######## Plotting ##### if (plot_original_data): gl.init_figure(); ## First cluster ax1 = gl.scatter(X1[0,:],X1[1,:], labels = ["Gaussian Generated Data", "x1","x2"], legend = ["K = 1"], color = "r",alpha = 0.5) mean,w,h,theta = bMA.get_gaussian_ellipse_params( mu = mu1, Sigma = cov1, Chi2val = 2.4477) r_ellipse = bMA.get_ellipse_points(mean,w,h,theta) gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax1, ls = "--", lw = 2 ,AxesStyle = "Normal2", color = "r") ## Second cluster ax1 = gl.scatter(X2[0,:],X2[1,:], legend = ["K = 2"], color = "b", alpha = 0.5) mean,w,h,theta = bMA.get_gaussian_ellipse_params( mu = mu2, Sigma = cov2, Chi2val = 2.4477) r_ellipse = bMA.get_ellipse_points(mean,w,h,theta) gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax1, ls = "--", lw = 2,AxesStyle = "Normal2", color = "b") ## Third cluster ax1 = gl.scatter(X3[0,:],X3[1,:], legend = ["K = 3"], color = "g", alpha = 0.5) mean,w,h,theta = bMA.get_gaussian_ellipse_params( mu = mu3, Sigma = cov3, Chi2val = 2.4477) r_ellipse = bMA.get_ellipse_points(mean,w,h,theta) gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax1, ls = "--", lw = 2,AxesStyle = "Normal2", color = "g") ax1.axis('equal') gl.savefig(folder_images +'Original data.png', dpi = 100, sizeInches = [12, 6]) ############ ESTIMATE THEM ################ theta1 = Gae.get_Gaussian_muSigma_ML(X1.T, parameters = dict([["Sigma","full"]])) print ("mu1:") print (theta1[0]) print ("Sigma1") print(theta1[1]) ############## Estimate Likelihood ################### ll = Gad.Gaussian_pdf_log (X1, [mu1,cov1]) ll2 = [] for i in range (ll.size): ll2.append( multivariate_normal.logpdf(X1[:,i], mean=mu1.flatten(), cov=cov1)) ll2 = np.array(ll2).reshape(ll.shape) print ("ll ours") print (ll.T) print ("ll scipy") print (ll2.T) print ("Difference in ll") print ((ll - ll2).T) ###### Multiple clusters case ll_K = Gad.Gaussian_K_pdf_log(X1, [[mu1,cov1],[mu2,cov2]]) if(0): X1 = gf.remove_module(X1.T).T X2 = gf.remove_module(X2.T).T X3 = gf.remove_module(X3.T).T Xdata = np.concatenate((X1,X2,X3), axis =1).T return X1,X2,X3,Xdata, mu1,mu2,mu3, cov1,cov2, cov3
mu_Z = mu_Y ## TODO: AS we can see, the angle is actually positive, but when proejcting that is what you lose. ############################################################ ################# PLOT DATA ############################### ############################################################ if(distribution_graph): # Get the histogram and gaussian estimations ! ## Scatter plot of the points gl.init_figure() ax1 = gl.subplot2grid((4,4), (1,0), rowspan=3, colspan=3) gl.scatter(Y[0,:],Y[1,:], alpha = 0.5, ax = ax1, lw = 4, AxesStyle = "Normal", labels = ["","U1","U2"], legend = ["%i points"%Nsam]) # Plot the projection vectors n = 6; gl.plot([-n*R[0,0],n*R[0,0]],[- n*R[0,1],n*R[0,1]],color = "y", lw = 3); gl.plot([-n*R[1,0],n*R[1,0]],[-n*R[1,1],n*R[1,1]],color = "r", lw = 3); ## Plot the projections !! # V1_pr= [] # V2_pr= [] # # for i in range(Y.shape[1]): # V1_pr.append([Y[:,i],Y[:,i]- R[0,:].dot(Y[:,i]) * R[0,:]]) # V2_pr.append([Y[:,i],Y[:,i] - R[1,:].dot(Y[:,i]) * R[1,:]]) #
gl.plot(dates, EMAfast, legend=["fast"]) ax2 = gl.plot(dates, ul.scale(EMAfast - EMAslow), legend=["Difference"], nf=1, sharex=ax1, labels=["", "", "Events"], fill=1, alpha=0.3) gl.stem( dates, crosses, legend=["TradeSignal"], ) gl.scatter(dates, ul.scale(EMAfast - EMAslow), lw=0.5, alpha=0.5) gl.plot(dates, np.zeros(crosses.shape)) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.10, hspace=0.01) trade_events = myEstrategia.get_TradeEvents() # We can observe the apparent delay ! #crosses, dates = Hitler.RobustXingAverages(symbols[0], Ls_list,Ll_list) #
corr = bMA.get_corrMatrix(data) cov = bMA.get_covMatrix(data) ############################################################ ################# PLOT DATA ############################### ############################################################ if(distribution_graph): # Get the histogram and gaussian estimations ! ## Scatter plot of the points n_grids = 40 kde_K = 3 gl.init_figure() ax1 = gl.subplot2grid((4,4), (1,0), rowspan=3, colspan=3) gl.scatter(ret1,ret2, alpha = 0.5, ax = ax1, lw = 4, AxesStyle = "Normal", labels = ["",symbolIDs[0], symbolIDs[1]], legend = ["%i points"%ret1.size]) ## X distribution ax2 = gl.subplot2grid((4,4), (0,0), rowspan=1, colspan=3, sharex = ax1) gl.histogram(X = ret1, ax = ax2, AxesStyle = "Normal - No xaxis", color = "k", alpha = 0.5) x_grid = np.linspace(min(ret1),max(ret1),n_grids) y_val = bMA.kde_sklearn(ret1, x_grid, bandwidth=np.std(ret1)/kde_K) gl.plot(x_grid, y_val, color = "k", labels = ["","",""], legend = ["M: %.2e, std: %.2e"%(mean[0], cov[0,0])]) x_grid, y_val = bMA.gaussian1D_points(X = ret1, std_K = 3) gl.plot(x_grid, y_val, color = "b",
def yieldPriceStudy(self, initial_price = 80): # The initial price is for the aproximation of the # funciton with a cuadratic equation in one point. #### Obtain the yield-price curve from the structure Np = 100 ytm_list = np.linspace(0.001, 0.40, Np) prices = [] mdurations = [] convexities = [] for ytm in ytm_list: price = self.get_price(ytm = ytm) mduration = self.get_mduration(price = price) convexity = self.get_convexity(price = price) prices.append(self.get_price(ytm = ytm)) mdurations.append(mduration) convexities.append(convexity) gl.set_subplots(2,2) gl.plot(ytm_list,prices, labels = ["Yield curve", "Yield to maturity" ,"Price of Bond"], legend = ["Yield curve"], loc = 3) gl.plot(ytm_list,prices, labels = ["Duration and Yield", "Yield to maturity" ,"Price of Bond"], legend = ["Yield curve"], loc = 3) gl.plot(ytm_list,mdurations, na = 1, nf = 0, legend = ["Duration"], loc = 1) gl.plot(ytm_list,prices, labels = ["Convexity and Yield","Yield to maturity" ,"Price of Bond"], legend = ["Yield curve"], loc = 3) gl.plot(ytm_list,convexities, na = 1, nf = 0, legend = ["Convexity"], loc = 1) ### Estimation of the yield courve around a point using the ## Duration and convexity. price = initial_price ytm = self.get_ytm(price) dytmList = np.linspace(-0.10, 0.10, 100) ## Obtain estimations estimations = [] for dytm in dytmList: eprice = self.estimate_price_DC(price = price, dytm = dytm, dy = 0.01) estimations.append(eprice) ## Obtain real values rael_values = [] for dytm in dytmList: rprice = self.get_price(ytm = ytm + dytm) rael_values.append(rprice) # Calculate the error ! rael_values = ul.fnp(rael_values) estimations = ul.fnp(estimations) error = np.abs(np.power(rael_values - estimations, 1)) gl.plot(ytm_list,prices, labels = ["Convexity and Yield","Yield to maturity" ,"Price of Bond"], legend = ["Yield curve"], loc = 3, lw = 4, color = "r") gl.scatter([ytm],[price], nf = 0, legend = ["Initial price"], loc = 3, lw = 4, color = "g") gl.plot(dytmList + ytm,estimations, nf = 0, legend = ["Estimation"], loc = 3, lw = 2, color = "b") gl.plot(dytmList + ytm,error, nf = 0, na = 1, legend = ["Error"], loc = 1, lw = 2, color = "b") ## The limit is the Spot Rate !! When we can do it continuously ## In this case the return is e^(RT)
gl.plot(Klusters, mean_tr_ll - 2 * std_tr_ll, color="k", nf=0, lw=1, ls="--") gl.fill_between(Klusters, mean_tr_ll - 2 * std_tr_ll, mean_tr_ll + 2 * std_tr_ll, c="k", alpha=0.5) for i in range(len(logl_tr_CVs)): for k_i in range(len(Klusters)): gl.scatter(np.ones((len(logl_tr_CVs[i][k_i]), 1)) * Klusters[k_i], logl_tr_CVs[i][k_i], color="k", alpha=0.2, lw=1) gl.plot(Klusters, mean_val_ll, nf=0, color="r", legend=["Mean Validation LL"], lw=3) gl.plot(Klusters, mean_val_ll + 2 * std_val_ll, color="r", nf=0, lw=1, ls="--",
keys.sort() set_indexes = days_dict[keys[0]] gl.set_subplots(2,1) ax1 = gl.plot([],[], nf = 1) ax2 = gl.plot([],[], nf = 1, sharex = ax1) for key in keys: set_indexes = days_dict[key] set_indexes.sort() set_indexes = timeData.time_mask[set_indexes] # Since it changed. times = timeData.get_dates(set_indexes).time values = timeData.get_timeSeriesCumReturn(["Close"],set_indexes) gl.scatter(times, values, ax = ax1) volume = timeData.get_timeSeries(["Volume"],set_indexes) gl.scatter(times, volume, ax = ax2) """ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$ Outdated shit $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ """ ############## OWN BASIC PLOTING FUNC ######################################### if (own_plotting_func_f): # There is a small set of functions to plot the time series and some more # complex stuff but it is very underdeveloped and it is better to use the graph library price = timeData.get_timeSeries(["RangeHL","RangeCO"]); timeData.plot_timeSeries(nf = 1)
def yieldPriceStudy(self, initial_price=80): # The initial price is for the aproximation of the # funciton with a cuadratic equation in one point. #### Obtain the yield-price curve from the structure Np = 100 ytm_list = np.linspace(0.001, 0.40, Np) prices = [] mdurations = [] convexities = [] for ytm in ytm_list: price = self.get_price(ytm=ytm) mduration = self.get_mduration(price=price) convexity = self.get_convexity(price=price) prices.append(self.get_price(ytm=ytm)) mdurations.append(mduration) convexities.append(convexity) gl.set_subplots(2, 2) gl.plot(ytm_list, prices, labels=["Yield curve", "Yield to maturity", "Price of Bond"], legend=["Yield curve"], loc=3) gl.plot( ytm_list, prices, labels=["Duration and Yield", "Yield to maturity", "Price of Bond"], legend=["Yield curve"], loc=3) gl.plot(ytm_list, mdurations, na=1, nf=0, legend=["Duration"], loc=1) gl.plot( ytm_list, prices, labels=["Convexity and Yield", "Yield to maturity", "Price of Bond"], legend=["Yield curve"], loc=3) gl.plot(ytm_list, convexities, na=1, nf=0, legend=["Convexity"], loc=1) ### Estimation of the yield courve around a point using the ## Duration and convexity. price = initial_price ytm = self.get_ytm(price) dytmList = np.linspace(-0.10, 0.10, 100) ## Obtain estimations estimations = [] for dytm in dytmList: eprice = self.estimate_price_DC(price=price, dytm=dytm, dy=0.01) estimations.append(eprice) ## Obtain real values rael_values = [] for dytm in dytmList: rprice = self.get_price(ytm=ytm + dytm) rael_values.append(rprice) # Calculate the error ! rael_values = ul.fnp(rael_values) estimations = ul.fnp(estimations) error = np.abs(np.power(rael_values - estimations, 1)) gl.plot( ytm_list, prices, labels=["Convexity and Yield", "Yield to maturity", "Price of Bond"], legend=["Yield curve"], loc=3, lw=4, color="r") gl.scatter([ytm], [price], nf=0, legend=["Initial price"], loc=3, lw=4, color="g") gl.plot(dytmList + ytm, estimations, nf=0, legend=["Estimation"], loc=3, lw=2, color="b") gl.plot(dytmList + ytm, error, nf=0, na=1, legend=["Error"], loc=1, lw=2, color="b")
def IFE_f(self, ObjectiveR=0.003, Rf=0.0, year_start=1996, year_finish=2016, window=10): ### The official one can be done executing the exercise c with another Rf ## Just another graph to show that now we should not use all the data. # Just, choose a desired return, # Using training Samples calculate using the market line # the optimal porfolio for that. # Then calculate for the next year, the real return # for that portfolio. # Do this for several years as well. self.set_Rf(Rf) nf_flag = 1 All_stds = [] PortfolioReturns = [] IndexReturns = [] all_dates = [] for year_test in range(year_start, year_finish - window + 1 - 1): # +1 !! # Set the dates self.pf.set_interval(dt.datetime(year_test, 1, 1), dt.datetime(year_test + window, 1, 1)) # Obtain the market line !! w = self.TangentPortfolio(Rf=Rf) # Obtain allocation self.set_allocation(w) # Obtain the expected return and std when using all our money ! expRet, stdRet = self.get_metrics(investRf="no") param = bMl.obtain_equation_line(Rf, expRet, stdRet) bias, slope = param X = (ObjectiveR - Rf) / (expRet - Rf) wdesired = w * X ## Check that the output of this portfolio is the desired one. self.set_allocation(wdesired) # Set the allocation expRet, stdRet = self.get_metrics( ) # Get the expected return for that year # print ret ## Now that we have the desired w*X, we will calculate the resturn of ## the portfolio in the following year. # To do so, we set the dates, only to the next year, set the portfolio allocation # And calculate the yearly expected return !! # Set the dates to only the next year !! # Also, one month before in order to get the returns of the first month. self.pf.set_interval(dt.datetime(year_test + window, 1, 1), dt.datetime(year_test + window + 1, 1, 1)) self.set_allocation(wdesired) # Set the allocation expRet, stdRet = self.get_metrics( ) # Get the expected return for that year PortfolioRet = self.yearly_Return(expRet) # Get yearly returns PortfolioReturns.append(PortfolioRet) All_stds.append(self.yearly_covMatrix(stdRet)) indexRet = self.get_indexMeanReturn() indexRet = self.yearly_Return(indexRet) IndexReturns.append(indexRet) # dates = self.get_dates() all_dates.append(year_test + window + 1) ## Graph with the evolutio of the portfolio price after the assignment gl.plot(range(1, 13), np.cumsum(self.get_PortfolioReturn()), nf=nf_flag, labels=[ "Evolution of returns by month", "Months passed", "Cumulative Return" ], legend=[str(year_test + window + 1)]) nf_flag = 0 # print ret gl.savefig(folder_images + 'returnsEvolMonth.png', dpi=150, sizeInches=[2 * 8, 2 * 6]) ## Graph with the desired, the obtained returns and the returns of the index gl.bar(all_dates[:], IndexReturns, labels=["Obtained returns", "Time (years)", "Return (%)"], legend=["Index Return"], alpha=0.8, nf=1) gl.bar(all_dates[:], PortfolioReturns, labels=["Returns of year", "Year", "Value"], legend=["Porfolio Return"], alpha=0.8, nf=0) gl.scatter(all_dates[:], self.yearly_Return(ObjectiveR) * np.ones( (len(all_dates[:]), 1)), legend=["Objective Return"], nf=0) gl.scatter(all_dates[:], All_stds, legend=["Std of the portfolio return"], nf=0) gl.savefig(folder_images + 'returnsEvolYears.png', dpi=150, sizeInches=[2 * 8, 2 * 6]) #### Crazy idea !! Lets plot where the f*****g efficient frontier went nf_flag = 1 PortfolioReturns = [] IndexReturns = [] all_dates = [] gl.set_subplots(2, 3) for year_test in range(year_start, year_start + 6): # +1 !! # Set the dates self.pf.set_interval(dt.datetime(year_test, 1, 1), dt.datetime(year_test + window, 1, 1)) optimal, portfolios = self.efficient_frontier(kind="Tangent") self.plot_allocations( portfolios, labels=["Evolution of the efficient frontier"], legend=["Frontier " + str(year_test + window) + " before"], color="k", nf=1) self.pf.set_interval(dt.datetime(year_test + window, 1, 1), dt.datetime(year_test + window + 1, 1, 1)) self.set_allocation(self.TangentPortfolio(Rf=Rf)) self.plot_allocations( portfolios, legend=["Frontier " + str(year_test + window) + " after"], color="r", nf=0) gl.savefig(folder_images + 'effEvol.png', dpi=80, sizeInches=[4 * 8, 3 * 6])
X = np.concatenate((X),axis = 1) Nsim = 1000 x_grid = np.linspace(-6,8,Nsim) if(distribution_graph): ## Plot the 3 distributions ! gl.init_figure() for i in range(Nx): X_i = X[:,[i]] x_grid, y_values = bMA.gaussian1D_points(mean = mus[i], std = stds[i], x_grid = x_grid) color = gl.get_color() gl.scatter(X_i, np.zeros(X_i.shape), alpha = 0.1, lw = 4, AxesStyle = "Normal", color = color, labels = ["3 independent Gaussian distributions","x","pdf(x)"]) gl.plot(x_grid, y_values, color = color, fill = 1, alpha = 0.1, legend = ["X%i: m:%.1f, std:%.1f"%(i+1,mus[i],stds[i])]) gl.savefig(folder_images +'Gaussians.png', dpi = 100, sizeInches = [18, 10]) ############################################################ ################# PLOT DATA ############################### ############################################################ if(distribution_graph_2D): # Get the histogram and gaussian estimations ! ## Scatter plot of the points
def plot_data_regression_1d_2axes(X_data_tr, Y_data_tr, xgrid_real_func, ygrid_real_func, X_data_val, Y_data_val, x_grid, all_y_grid, most_likely_ygrid, alpha_points, color_points_train, color_points_val, color_most_likey, color_mean, color_truth, ax1, ax2): """ This function plots the outputs of the Regression model for the 1D example """ ## Compute mean and std of regression std_samples_grid = np.std(all_y_grid, axis=1) mean_samples_grid = np.mean(all_y_grid, axis=1) ############## ax1: Data + Mostlikely + Real + Mean !! ######################## if (type(ax1) != type(None)): gl.scatter( X_data_tr, Y_data_tr, ax=ax1, lw=3, #legend = ["tr points"], labels=["Data and predictions", "", "Y"], alpha=alpha_points, color=color_points_train) gl.scatter( X_data_val, Y_data_val, ax=ax1, lw=3, #legend = ["val points"], alpha=alpha_points, color=color_points_val) gl.plot(xgrid_real_func, ygrid_real_func, ax=ax1, alpha=0.90, color=color_truth, legend=["Truth"]) gl.plot(x_grid, most_likely_ygrid, ax=ax1, alpha=0.90, color=color_most_likey, legend=["Most likely"]) gl.plot(x_grid, mean_samples_grid, ax=ax1, alpha=0.90, color=color_mean, legend=["Posterior mean"], AxesStyle="Normal - No xaxis") ############## ax2: Data + Realizations of the function !! ###################### if (type(ax2) != type(None)): gl.scatter( X_data_tr, Y_data_tr, ax=ax2, lw=3, # legend = ["tr points"], labels=["", "X", "Y"], alpha=alpha_points, color=color_points_train) gl.scatter( X_data_val, Y_data_val, ax=ax2, lw=3, # legend = ["val points"], alpha=alpha_points, color=color_points_val) gl.plot(x_grid, all_y_grid, ax=ax2, alpha=0.15, color="k") gl.plot(x_grid, mean_samples_grid, ax=ax2, alpha=0.90, color="b", legend=["Mean realization"]) gl.set_zoom(xlimPad=[0.2, 0.2], ylimPad=[0.2, 0.2], ax=ax2, X=X_data_tr, Y=Y_data_tr)
nbins = None min_value_x = None gl.init_figure() ax1 = gl.subplot2grid((1, 2), (0, 0), rowspan=1, colspan=1) ax2 = gl.subplot2grid((1, 2), (0, 1), rowspan=1, colspan=1, sharex=ax1) y_values = np.array(DataSet_statistics["start_span_loss"]) + np.array( DataSet_statistics["end_span_loss"]) y_EM_values = np.array(DataSet_statistics["em"]) y_F1_values = np.array(DataSet_statistics["f1"]) ax1 = gl.scatter(x_values, y_values, ax=ax1, labels=labels_1, alpha=0.1) spl.distplot_1D_seaborn(x_values, ax2, labels=[ 'Distribution of ' + labels_1[0], labels_1[1], 'pdf(' + labels_1[1] + ")" ]) gl.set_fontSizes(ax=[ax1, ax2], title=20, xlabel=15, ylabel=18, legend=10, xticks=15, yticks=15)
x_grid_emp, y_val_emp = bMA.empirical_1D_cdf(ret1) x_grid = np.linspace(x_grid_emp[0], x_grid_emp[-1], 100) x_grid, y_val = bMA.gaussian1D_points_cdf(X=ret1, x_grid=x_grid) ax1 = gl.plot(X=x_grid, Y=y_val, AxesStyle="Normal", nf=1, color="k", alpha=0.5) gl.scatter(x_grid_emp, y_val_emp, color="k", labels=["", "", ""], legend=["empirical cdf"]) ## Now here we just plot it one againts each other like in a regression ## problem ! # ax2 = gl.plot(X = y_val_emp, Y = y_val, AxesStyle = "Normal", nf = 1, # color = "k", alpha = 0.5) x_grid, y_val = bMA.gaussian1D_points_cdf(X=ret1, x_grid=x_grid_emp) gl.scatter(X=y_val_emp, Y=y_val, color="k", labels=["", "", ""], legend=["empirical cdf"], nf=1)
def plot_final_distribution(Xs,mus,covs, Ks ,myDManager, logl,theta_list,model_theta_list, folder_images): colors = ["r","b","g"] K_G,K_W,K_vMF = Ks ################## Print the Watson and Gaussian Distribution parameters ################### for k_c in myDManager.clusterk_to_Dname.keys(): k = myDManager.clusterk_to_thetak[k_c] distribution_name = myDManager.clusterk_to_Dname[k_c] # G W if (distribution_name == "Gaussian"): print ("------------ Gaussian Cluster. K = %i--------------------"%k) print ("mu") print (theta_list[-1][k][0]) print ("Sigma") print (theta_list[-1][k][1]) elif(distribution_name == "Watson"): print ("------------ Watson Cluster. K = %i--------------------"%k) print ("mu") print (theta_list[-1][k][0]) print ("Kappa") print (theta_list[-1][k][1]) elif(distribution_name == "vonMisesFisher"): print ("------------ vonMisesFisher Cluster. K = %i--------------------"%k) print ("mu") print (theta_list[-1][k][0]) print ("Kappa") print (theta_list[-1][k][1]) print ("pimix") print (model_theta_list[-1]) mus_Watson_Gaussian = [] # k_c is the number of the cluster inside the Manager. k is the index in theta for k_c in myDManager.clusterk_to_Dname.keys(): k = myDManager.clusterk_to_thetak[k_c] distribution_name = myDManager.clusterk_to_Dname[k_c] # G W mus_k = [] for iter_i in range(len(theta_list)): # For each iteration of the algorihtm if (distribution_name == "Gaussian"): theta_i = theta_list[iter_i][k] mus_k.append(theta_i[0]) elif(distribution_name == "Watson"): theta_i = theta_list[iter_i][k] mus_k.append(theta_i[0]) elif(distribution_name == "vonMisesFisher"): theta_i = theta_list[iter_i][k] mus_k.append(theta_i[0]) mus_k = np.concatenate(mus_k, axis = 1).T mus_Watson_Gaussian.append(mus_k) ######## Plot the original data ##### gl.init_figure(); ## First cluster for xi in range(len( Xs)): ## First cluster ax1 = gl.scatter(Xs[xi][0,:],Xs[xi][1,:], labels = ['EM Evolution. Kg:'+str(K_G)+ ', Kw:' + str(K_W) + ', K_vMF:' + str(K_vMF), "X1","X2"], color = colors[xi] ,alpha = 0.2, nf = 0) mean,w,h,theta = bMA.get_gaussian_ellipse_params( mu = mus[xi], Sigma = covs[xi], Chi2val = 2.4477) r_ellipse = bMA.get_ellipse_points(mean,w,h,theta) gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax1, ls = "--", lw = 2 ,AxesStyle = "Normal2", color = colors[xi], alpha = 0.7) indx = -1 # Only doable if the clusters dont die Nit,Ndim = mus_Watson_Gaussian[0].shape for k_c in myDManager.clusterk_to_Dname.keys(): k = myDManager.clusterk_to_thetak[k_c] distribution_name = myDManager.clusterk_to_Dname[k_c] # G W if (distribution_name == "Gaussian"): ## Plot the ecolution of the mu #### Plot the Covariance of the clusters ! mean,w,h,theta = bMA.get_gaussian_ellipse_params( mu = theta_list[indx][k][0], Sigma = theta_list[indx][k][1], Chi2val = 2.4477) r_ellipse = bMA.get_ellipse_points(mean,w,h,theta) gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax1, ls = "-.", lw = 3, AxesStyle = "Normal2", legend = ["Kg(%i). pi:%0.2f"%(k, float(model_theta_list[indx][0][0,k]))]) gl.scatter(mus_Watson_Gaussian[k][:,0], mus_Watson_Gaussian[k][:,1], nf = 0, na = 0, alpha = 0.3, lw = 1, color = "y") gl.plot(mus_Watson_Gaussian[k][:,0], mus_Watson_Gaussian[k][:,1], nf = 0, na = 0, alpha = 0.8, lw = 2, color = "y") elif(distribution_name == "Watson"): #### Plot the pdf of the distributino ! ## Distribution parameters for Watson kappa = float(theta_list[indx][k][1]) mu = theta_list[indx][k][0] Nsa = 1000 # Draw 2D samples as transformation of the angle Xalpha = np.linspace(0, 2*np.pi, Nsa) Xgrid= np.array([np.cos(Xalpha), np.sin(Xalpha)]) probs = [] # Vector with probabilities for i in range(Nsa): probs.append(np.exp(Wad.Watson_pdf_log(Xgrid[:,i],[mu,kappa]) )) probs = np.array(probs) # Plot it in polar coordinates X1_w = (1 + probs) * np.cos(Xalpha) X2_w = (1 + probs) * np.sin(Xalpha) gl.plot(X1_w,X2_w, legend = ["Kw(%i). pi:%0.2f"%(k, float(model_theta_list[indx][0][0,k]))] , alpha = 1, lw = 3, ls = "-.") elif(distribution_name == "vonMisesFisher"): #### Plot the pdf of the distributino ! ## Distribution parameters for Watson kappa = float(theta_list[indx][k][1]) mu = theta_list[indx][k][0] Nsa = 1000 # Draw 2D samples as transformation of the angle Xalpha = np.linspace(0, 2*np.pi, Nsa) Xgrid= np.array([np.cos(Xalpha), np.sin(Xalpha)]) probs = [] # Vector with probabilities for i in range(Nsa): probs.append(np.exp(vMFd.vonMisesFisher_pdf_log(Xgrid[:,i],[mu,kappa]) )) probs = np.array(probs) probs = probs.reshape((probs.size,1)).T # Plot it in polar coordinates X1_w = (1 + probs) * np.cos(Xalpha) X2_w = (1 + probs) * np.sin(Xalpha) # print X1_w.shape, X2_w.shape gl.plot(X1_w,X2_w, alpha = 1, lw = 3, ls = "-.", legend = ["Kvmf(%i). pi:%0.2f"%(k, float(model_theta_list[indx][0][0,k]))]) ax1.axis('equal') gl.savefig(folder_images +'Final_State. K_G:'+str(K_G)+ ', K_W:' + str(K_W) + ', K_vMF:' + str(K_vMF) + '.png', dpi = 100, sizeInches = [12, 6])
sigmaXhatList = ul.fnp([ x[0,0] for x in SigmaXXhatList]) sigmaXpredList= ul.fnp([ x[0,0] for x in SigmaXXpredList]) sigmaXpredtestList = ul.fnp([ x[0,0] for x in SigmaXXpredtestList]) sigmaXhatList = np.sqrt(sigmaXhatList ) SigmaXpredList = np.sqrt(sigmaXpredList) # Now we plot RealPrice = Matrix_tr[:,[0]] EstimatedPrice = Yhat[:,[0]] PredictedPrice = Ypred[:,[0]] PredictedPriceTest = Ypredtest[:,[0]] # gl.plot([], diff, legend = ["Estate"]) # Plot the training data and the estimated state gl.scatter(dates, RealPrice, legend = ["Real"]) gl.plot_timeSeriesRange(dates, EstimatedPrice,sigmaXhatList, legend = ["Estate"], nf = 0) # Plot the one Step prediction dates_OneStepPred = ul.fnp(range(0, dates.size + 1)) gl.plot_timeSeriesRange(dates_OneStepPred, PredictedPrice[:,:],sigmaXpredList[:,:], legend = ["Prediction"], nf = 0) ## Plot the future prediction dates_test = ul.fnp(range(dates.size, dates.size + Ntest)) gl.plot_timeSeriesRange(dates_test, PredictedPriceTest[:-1,:],sigmaXpredtestList[:-1,:], legend = ["Prediction future"], nf = 0) # Using the state formulate, plot the prediction line from each state point. for n in range (0,Ns): start_point = XhatList[:,[n]] end_point = A.dot(start_point)
if ((step+1) % step_period == 0): # This will always happen after the first Num_iterations Network.save_variables() loss_tr, accuracy_tr = Network.get_loss_accuracy(X_data_tr, Y_data_tr) print ("The final loss:" , loss_tr) print ("The final variables:" , W_values,b_values,s_value) ## Plot the loss function against the parameters !! ## Get the surface for the loss ####### PLOT THE EVOLUTION OF RMSE AND PARAMETERS ############ gl.init_figure() ax1 = gl.scatter(X_data_tr, Y_data_tr, lw = 3,legend = ["tr points"], labels = ["Data", "X","Y"]) ax2 = gl.scatter(X_data_val, Y_data_val, lw = 3,legend = ["val points"]) gl.set_fontSizes(ax = [ax1,ax2], title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 12, yticks = 12) x_grid = np.linspace(np.min([X_data_tr]) -1, np.max([X_data_val]) +1, 100) y_grid = x_grid * W_values + b_values gl.plot (x_grid, y_grid, legend = ["training line"]) gl.savefig(folder_images +'Training_Example_Data.png', dpi = 100, sizeInches = [14, 4]) ####### PLOT THE EVOLUTION OF RMSE AND PARAMETERS ############ gl.set_subplots(2,1) ax1 = gl.plot([], tr_loss, nf = 1, lw = 3, labels = ["RMSE loss and parameters. Learning rate: %.3f"%train_config.lr, "","RMSE"], legend = ["train"])
EstimatedPrice = Yhat[:,[0]] PredictedPrice = Ypred[:,[0]] sigmaXhatList = ul.fnp([ x[0,0] for x in SigmaXXhatList]) sigmaXpredList= ul.fnp([ x[0,0] for x in SigmaXXpredList]) sigmaXhatList = np.sqrt(sigmaXhatList ) SigmaXpredList = np.sqrt(sigmaXpredList) sigmaYhatList = np.sqrt(sigmaXhatList**2 + MeasurCovNoise[0,0]) PredictedPriceTest = Ypredtest[:,[0]] sigmaXpredtestList = ul.fnp([ x[0,0] for x in SigmaXXpredtestList]) sigmaXpredtestList = np.sqrt(sigmaXpredtestList) # Plot the training data and the estimated state gl.scatter(dates, RealPrice, legend = ["Real"]) gl.plot_timeSeriesRange(dates, EstimatedPrice,sigmaXhatList, legend = ["Estate"], nf = 0) gl.plot_timeSeriesRange(dates, EstimatedPrice,sigmaYhatList, legend = ["Estate"], nf = 0) # Plot the one Step prediction dates_OneStepPred = ul.fnp(range(0, dates.size + 1)) gl.plot_timeSeriesRange(dates_OneStepPred, PredictedPrice[:,:],SigmaXpredList[:,:], legend = ["Prediction"], nf = 0) # ## Plot the future prediction dates_test = ul.fnp(range(dates.size, dates.size + Ntst +1)) gl.plot_timeSeriesRange(dates_test, PredictedPriceTest[:,:],sigmaXpredtestList[:,:], legend = ["Prediction future"], nf = 0) ###################################################################### ## Optimization !!! xopt = myKF.optimize_parameters(100,100, 100)
# gl.init_figure() i_1 = 2 i_2 = 0 X_1,X_2 = X[:,[i_1]], X[:,[i_2]] mu_1, mu_2 = mus[i_1],mus[i_2] Xjoint = np.concatenate((X_1,X_2), axis = 1).T std_1, std_2 = stds[i_1],stds[i_2] mu = mus[[i_1,i_2]] cov = np.cov(Xjoint) std_K = 3 ## Do stuff now ax1 = gl.subplot2grid((1,2), (0,0), rowspan=1, colspan=1) gl.scatter(Xjoint[0,:],Xjoint[1,:], alpha = 0.5, ax = ax1, lw = 4, AxesStyle = "Normal", labels = ["","U1", "U2"]) ax1.axis('equal') xx, yy, zz = bMA.get_gaussian2D_pdf( xbins=40j, ybins=40j, mu = mu, cov = cov, std_K = std_K, x_grid = None) ax1.contour(xx, yy, zz, linewidths = 3, linestyles = "solid", alpha = 0.8, colors = None, zorder = 100) ######## Transformation !! A = np.array([[0.9,2],[0.8,0.7]]) mu = np.array([-1.5,2]).reshape(2,1) Yjoint = A.dot(Xjoint) + mu cov = np.cov(Yjoint)
X2 = probs * np.sin(Xalpha) gl.plot(X1,X2, legend = ["pdf k:%f, mu_angle: %f"%(kappa,mu_angle)], labels = ["Watson Distribution", "Angle(rad)", "pdf"], nf = 1, na = 1) ## Generate samples RandWatson = Was.randWatson(Nsampling, mu, kappa) mu_est2, kappa_est2 = Wae.get_Watson_muKappa_ML(RandWatson) print "Real: ", mu, kappa print "Estimate: ", mu_est2, kappa_est2 gl.scatter(RandWatson[:,0],RandWatson[:,1], legend = ["pdf k:%f, mu_angle: %f"%(kappa,mu_angle)], labels = ["Watson Distribution", "Angle(rad)", "pdf"], nf = 1, na = 1, alpha = 0.1) #new_samples = sl.InvTransSampGrid(probs, Xalpha, Nsa * 2) ######################################################## """ With 3D sphere """ ######################################################## # Data generation parameters Nsa = 100 # Number of samples we will draw Nsampling = 1000 ## Distribution parameters kappa = 20 # "Variance" of the circular multivariate guassian #mu_angle = [0.3*np.pi, 0.8*np.pi] # Mean angle direction mu_angle = [0.5*np.pi, 0.5*np.pi] # Mean angle direction
if(qq_plot): # Get the histogram and gaussian estimations ! ## Scatter plot of the points gl.set_subplots(1,2) x_grid_emp, y_val_emp = bMA.empirical_1D_cdf(ret1) x_grid = np.linspace(x_grid_emp[0],x_grid_emp[-1],100) x_grid, y_val = bMA.gaussian1D_points_cdf(X = ret1, x_grid = x_grid) ax1 = gl.plot(X = x_grid, Y = y_val, AxesStyle = "Normal", nf = 1, color = "k", alpha = 0.5) gl.scatter(x_grid_emp, y_val_emp, color = "k", labels = ["","",""], legend = ["empirical cdf"]) ## Now here we just plot it one againts each other like in a regression ## problem ! # ax2 = gl.plot(X = y_val_emp, Y = y_val, AxesStyle = "Normal", nf = 1, # color = "k", alpha = 0.5) x_grid, y_val = bMA.gaussian1D_points_cdf(X = ret1, x_grid = x_grid_emp) gl.scatter(X = y_val_emp, Y = y_val, color = "k", labels = ["","",""], legend = ["empirical cdf"], nf = 1) gl.savefig(folder_images +'q-qplot.png', dpi = 100, sizeInches = [14,6])