def _plot(self, x, y, labels=None, x_label='Return [%]', y_label='-', title='Backtesting results', plot_title='', xlim=None, ylim=None, pres_name=None): if pres_name is None: pres_name = self.presname pres = Presenter(pres_name) fig = plt.figure() plt.scatter(x, y) if xlim is not None: plt.xlim(xlim) if ylim is not None: plt.ylim(ylim) plt.xlabel(x_label) plt.ylabel(y_label) plt.title(plot_title) plt.grid() img_path = 'temp.png' plt.savefig(img_path) plt.close(fig) pres.add_picture_slide(img_path, title=title) pres.save_presentation(pres_name)
def _bcbcg_blbcg_least_square_exp_b(self): """ """ self._BB_6 = np.random.random( ( self._mat.shape[0],6) ) self._BX_6 = np.ones ( (self._mat.shape[1],6) ) #line 1 blbcg_solver_obj = BLBCG() self._final_x_blbcg_m6s6, self._final_r_blbcg_m6s6, self._residual_hist_blbcg_m6s6 = \ blbcg_solver_obj.blbcg_solver_least_square(self._mat, self._BB_6, self._BX_6, 6, self._tol, self._maxiter, 0) #line 2 blbcg_solver_obj = BLBCG() self._final_x_blbcg_m6s12, self._final_r_blbcg_m6s12, self._residual_hist_blbcg_m6s12 = \ blbcg_solver_obj.blbcg_solver_least_square(self._mat, self._BB_6, self._BX_6, 12, self._tol, self._maxiter, 0) #line 3 bcbcg_solver_obj = BCBCG() self._final_x_bcbcg_m6s6, self._final_r_bcbcg_m6s6, self._residual_hist_bcbcg_m6s6 = \ bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_6, self._BX_6, 6, self._tol, self._maxiter, 0) #line 4 bcbcg_solver_obj = BCBCG() self._final_x_bcbcg_m6s12, self._final_r_bcbcg_m6s12, self._residual_hist_bcbcg_m6s12 = \ bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_6, self._BX_6, 12, self._tol, self._maxiter, 0) plot_worker = Presenter() residual_list = [self._residual_hist_blbcg_m6s6, self._residual_hist_blbcg_m6s12, \ self._residual_hist_bcbcg_m6s6, self._residual_hist_bcbcg_m6s12 ] legend_list = ["blbcg_m6s6", "blbcg_m6s12", "bcbcg_m6s6", "bcbcg_m6s12"] color_list = ["r","k","b","y"] #plot_worker.instant_plot_y_log10(residual_list, "Chem97ZtZ", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list) plot_worker.instant_plot_y_log10(residual_list, "test", "#iteration", "$\\mathbf{log_{10}\\frac{||x_1||}{||b_1||}}$", legend_list, color_list)
def _cg_bcg_exp(self, block_size): """ """ print("_cg_bcg_bcbcg_least_square_exp starting, ... ") self._BB_1 = np.random.random((self._mat.shape[0], 1)) self._BX_1 = np.ones((self._mat.shape[1], 1)) self._BB_X = np.random.random((self._mat.shape[0], block_size)) self._BX_X = np.ones((self._mat.shape[1], block_size)) #line 1 bcg_solver_obj = NativeBlockConjugateGradient(self._mat, self._BX_1, self._BB_1, self._tol, self._maxiter) self._final_X_cg, self._final_R_cg, self._residual_hist_cg = bcg_solver_obj.bcg_variant_lstsq_run( 0) ##line 2 bcg_solver_obj = NativeBlockConjugateGradient(self._mat, self._BX_X, self._BB_X, self._tol, self._maxiter) self._final_X_bcg_mX, self._final_R_bcg_mX, self._residual_hist_bcg_mX = bcg_solver_obj.bcg_variant_lstsq_run( 0) plot_worker = Presenter() residual_list = [self._residual_hist_cg, self._residual_hist_bcg_mX] legend_list = ["cg", "bcg_m" + str(block_size)] color_list = ["r", "k"] plot_worker.instant_plot_y_log10( residual_list, "MG", "#iteration", "$\\mathbf{log_{10}\\frac{||r_1||}{||b_1||}}$", legend_list, color_list)
def main(): """MVP architecture to separate logic from interface""" _view = View() _presenter = Presenter(_view) _view.set_presenter(_presenter) _presenter.run()
def legendre_poly_exp_a(self, order_lo, order_hi): """ """ x = np.linspace(-1.1, 1.1, 41) order_controller = np.zeros(order_hi + 1) y_list = [] plot_worker = Presenter() legend_list = [] color_list = [] for order_idx in range(order_lo, order_hi + 1): order_controller[order_idx] = 1 legp = np.polynomial.legendre.Legendre(order_controller) legcoef = np.polynomial.legendre.leg2poly(legp.coef) poly = np.polynomial.Polynomial(legcoef) y_list.append(poly(x)) print(order_idx, " ", poly(x)) legend_list.append("order_" + str(order_idx)) color_list.append("k") order_controller[order_idx] = 0 plot_worker.instant_plot_unified_x_axis(x, y_list, "Legendre Poly", "x", "y", legend_list, color_list)
def _lbcg_least_square_exp(self): """ """ lbcg_solver_obj = LBCG() self._final_x_a, self._final_r_a, self._residual_hist_a = \ lbcg_solver_obj.lbcg_solver_least_square(self._mat, self._SB, self._SX, 8, self._tol, self._maxiter) self._final_x_b, self._final_r_b, self._residual_hist_b = \ lbcg_solver_obj.lbcg_solver_least_square(self._mat, self._SB, self._SX, 18, self._tol, self._maxiter) cbcg_solver_obj = CBCG() self._final_x_c, self._final_r_c, self._residual_hist_c = \ cbcg_solver_obj.cbcg_solver_least_square(self._mat, self._SB, self._SX, 8, self._tol, self._maxiter) self._final_x_d, self._final_r_d, self._residual_hist_d = \ cbcg_solver_obj.cbcg_solver_least_square(self._mat, self._SB, self._SX, 18, self._tol, self._maxiter) plot_worker = Presenter() residual_list = [ self._residual_hist_a, self._residual_hist_b, self._residual_hist_c, self._residual_hist_d ] legend_list = [ "lbcg_lstsq_s8", "lbcg_lstsq_s18", "cbcg_lstsq_s8", "cbcg_lstsq_s18" ] color_list = ["r", "k", "b", "y"] #plot_worker.instant_plot_y_log10(residual_list, "crystm01", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list) #plot_worker.instant_plot_y_log10(residual_list, "wathen100", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list) plot_worker.instant_plot_y_log10(residual_list, "bodyy06", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list)
def _cm_plot(self, x, y, labels=None, x_label='Transaction Value [monetary unit]', y_label='Transaction Costs [monetary unit]', title='Transaction costs', plot_title=''): pres = Presenter(self.presname) fig = plt.figure() if isinstance(y[0], np.ndarray) | isinstance(y[0], list): for i in range(0, len(y)): plt.plot(x, y[i], label=labels[i]) plt.legend(loc='upper left') else: plt.plot(x, y, 'b', label=labels) if labels is not None: plt.legend(loc='upper left') plt.xlabel(x_label) plt.ylabel(y_label) plt.title(plot_title) img_path = 'temp.png' plt.savefig(img_path) plt.close(fig) pres.add_picture_slide(img_path, title=title) pres.save_presentation(self.presname) os.remove(img_path)
def chebyshev_poly_exp_a(self, order_lo, order_hi): """ """ x = np.linspace(-1.1, 1.1, 41) order_controller = np.zeros(order_hi + 1) y_list = [] plot_worker = Presenter() legend_list = [] color_list = [] for order_idx in range(order_lo, order_hi + 1): order_controller[order_idx] = 1 cheb = np.polynomial.chebyshev.Chebyshev(order_controller) choef = np.polynomial.chebyshev.cheb2poly(cheb.coef) poly = np.polynomial.Polynomial(choef) y_list.append(poly(x)) print(order_idx, " ", poly(x)) legend_list.append("order_" + str(order_idx)) color_list.append("k") order_controller[order_idx] = 0 plot_worker.instant_plot_unified_x_axis(x, y_list, "chebyshev Poly", "x", "y", legend_list, color_list)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.presenter = Presenter() self.setGeometry(200, 200, 300, 300) self.setWindowTitle("Qt Window example") self.initUI() self.show()
def main(): # view = View() # dim_view = DimensionalView(view.drawing_area_dimensional.get_allocation().width,view.drawing_area_dimensional.get_allocation().height) # comp_view = ComplexView(view.drawing_area_complex.get_allocation().width,view.drawing_area_complex.get_allocation().height) app = QApplication(sys.argv) p = Presenter() p.show() app.exec_()
def __init__(self): super(Multiply, self).__init__() self.resize(400, 300) self.view = MainWindow(self) self.presenter = Presenter(self.view, MultiplyModel, ResetModel) self.view.inputChanged.connect(self.presenter.onInputChanged) self.view.resetClicked.connect(self.presenter.resetToZero)
def _db_presenter_a(self): plot_worker = Presenter() residual_list = [self._residual_hist_a] residual_list.append(self._residual_hist_b) legend_list = ["bcbcg_s3", "bcbcg_s5"] color_list = ["r", "k"] # latex style notation #plot_worker.instant_plot_y_log10(residual_list, "crystm01 $x_1$") #plot_worker.instant_plot_y_log10(residual_list, "crystm01", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list) plot_worker.instant_plot_y(residual_list, "crystm01", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list)
def _pc_lbcg_vs_cbcg(self): """ """ #print(self._mat) self._pc_jacobi = diags(self._mat.diagonal()) #print ("") #print ("dia") #print(self._pc_jacobi) self._pc_jacobi = np.sqrt(self._pc_jacobi) #print ("") #print(self._pc_jacobi) self._pc_jacobi = inv(self._pc_jacobi) #print ("") #print ("inverse") #print(self._pc_jacobi) #print(type(self._pc_jacobi)) self._left_pcd_mat = aslinearoperator(self._pc_jacobi)( self._mat.toarray()) #print ("") #print ("left pcd") self._pcd_mat = np.matmul(self._left_pcd_mat, self._pc_jacobi.toarray()) #print ("") #print (csr_matrix(self._pcd_mat).diagonal()) #print ("mat cond:", np.linalg.cond(self._mat.toarray())) #print ("pcd_mat cond:", np.linalg.cond(self._pcd_mat)) self._pcd_mat = csr_matrix(self._pcd_mat) lbcg_solver_obj = LBCG() self._final_x_a, self._final_r_a, self._residual_hist_a = \ lbcg_solver_obj.lbcg_solver_least_square(self._pcd_mat, self._SB, self._SX, 8, self._tol, self._maxiter) self._final_x_b, self._final_r_b, self._residual_hist_b = \ lbcg_solver_obj.lbcg_solver_least_square(self._pcd_mat, self._SB, self._SX, 18, self._tol, self._maxiter) cbcg_solver_obj = CBCG() self._final_x_c, self._final_r_c, self._residual_hist_c = \ cbcg_solver_obj.cbcg_solver_least_square(self._pcd_mat, self._SB, self._SX, 8, self._tol, self._maxiter) self._final_x_d, self._final_r_d, self._residual_hist_d = \ cbcg_solver_obj.cbcg_solver_least_square(self._pcd_mat, self._SB, self._SX, 18, self._tol, self._maxiter) plot_worker = Presenter() residual_list = [ self._residual_hist_a, self._residual_hist_b, self._residual_hist_c, self._residual_hist_d ] legend_list = [ "pcd_lbcg_lstsq_s8", "pcd_lbcg_lstsq_s18", "pcd_cbcg_lstsq_s8", "pcd_cbcg_lstsq_s18" ] color_list = ["r", "k", "b", "y"] #plot_worker.instant_plot_y_log10(residual_list, "crystm01", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list) #plot_worker.instant_plot_y_log10(residual_list, "wathen100", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list) plot_worker.instant_plot_y_log10(residual_list, "bodyy06", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list)
def _bcbcg_blbcg_least_square_exp_b(self): """ figure 2""" print("_bcbcg_blbcg_least_square_exp_b starting ... ") m = 3 self._BB = np.random.random((self._mat.shape[0], m)) self._BX = np.ones((self._mat.shape[1], m)) #line 1 blbcg_solver_obj = BLBCG() self._final_x_blbcg_a, self._final_r_blbcg_a, self._residual_hist_blbcg_a = \ blbcg_solver_obj.blbcg_solver_least_square(self._mat, self._BB, self._BX, 16, self._tol, self._maxiter, 0) #line 2 blbcg_solver_obj = BLBCG() self._final_x_blbcg_b, self._final_r_blbcg_b, self._residual_hist_blbcg_b = \ blbcg_solver_obj.blbcg_solver_least_square(self._mat, self._BB, self._BX, 32, self._tol, self._maxiter, 0) #line addition #blbcg_solver_obj = BLBCG() #self._final_x_blbcg_c, self._final_r_blbcg_c, self._residual_hist_blbcg_c = \ # blbcg_solver_obj.blbcg_solver_least_square(self._mat, self._BB_4, self._BX_4, 32, self._tol, self._maxiter, 0) #line 3 bcbcg_solver_obj = BCBCG() self._final_x_bcbcg_a, self._final_r_bcbcg_a, self._residual_hist_bcbcg_a = \ bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB, self._BX, 16, self._tol, self._maxiter, 0) #line 4 bcbcg_solver_obj = BCBCG() self._final_x_bcbcg_b, self._final_r_bcbcg_b, self._residual_hist_bcbcg_b = \ bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB, self._BX, 32, self._tol, self._maxiter, 0) #line addition #bcbcg_solver_obj = BCBCG() #self._final_x_bcbcg_c, self._final_r_bcbcg_c, self._residual_hist_bcbcg_c = \ # bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_4, self._BX_4, 32, self._tol, self._maxiter, 0) plot_worker = Presenter() #residual_list = [self._residual_hist_blbcg_a, self._residual_hist_blbcg_b, self._residual_hist_blbcg_c, \ # self._residual_hist_bcbcg_a, self._residual_hist_bcbcg_b, self._residual_hist_bcbcg_c ] residual_list = [self._residual_hist_blbcg_a, self._residual_hist_blbcg_b, \ self._residual_hist_bcbcg_a, self._residual_hist_bcbcg_b ] #legend_list = ["blbcg_m4s4", "blbcg_m4s8", "blbcg_m4s12", "bcbcg_m4s4", "bcbcg_m4s8", "bcbcg_m4s12"] legend_list = [ "blbcg_m3s16", "blbcg_m3s32", "bcbcg_m3s16", "bcbcg_m3s32" ] #color_list = ["r","k","b","y","g","m"] color_list = ["r", "k", "b", "g"] #plot_worker.instant_plot_y_log10(residual_list, "test", "#iteration", "$\\mathbf{log_{10}\\frac{||x_1||}{||b_1||}}$", legend_list, color_list) plot_worker.instant_plot_y_log10( residual_list, "bodyy6", "#iteration", "$\\mathbf{log_{10}\\frac{||x_1||}{||b_1||}}$", legend_list, color_list)
def __init__(self, context, plot=False, plot_title='Backtesting.pptx'): self.context = context self.plot = plot if self.plot: pres = Presenter() pres.start_presentation(title='Backtesting') self.presname = plot_title pres.save_presentation(self.presname) if self.context['strategy_type'] == 'index': self.index_strategy()
def __init__(self, broker_id=None, sec_type=None, exchange_id=None, country=None, sec_price=None, no_sec=None, plot=False, location=None): # #### MAIN INPUT #### self.name = 'Transaction Cost Model' self.dbTCM = DatabaseManipulationTCM() self.broker_id = broker_id self.sec_type = sec_type self.exchange_id = exchange_id self.country = country # Ensure that sec_price is a numpy array, and create one if not provided if not isinstance(sec_price, list) and sec_price is not None: sec_price = [sec_price] self.sec_price = np.arange( 10, 500, 10) if sec_price is None else np.array(sec_price) # Ensure that no_sec is a numpy array, and create one if not provided if not isinstance(no_sec, list) and no_sec is not None: no_sec = [no_sec] self.no_sec = np.arange(10, 500, 10) if no_sec is None else np.array(no_sec) # Assign location, if not provided, it will be main file of project if location is None: self.location = '' else: self.location = ''.join([location, '/']) # #### PRESENTATION #### self.plot = plot self.presname = '' if self.plot: pres = Presenter() pres.start_presentation(title='Transaction Costs') text = ''.join([ 'broker id: ', str(self.broker_id), '\n security type: ', self.sec_type, '\n exchange id: ', self.exchange_id, '\n country: ', self.country ]) pres.add_text_slide(text, title='Input') self.presname = ''.join([ self.location, 'Transaction_Costs_', str(broker_id), '_', self.sec_type, '_', self.exchange_id, '_', self.country, '.pptx' ]) pres.save_presentation(self.presname)
def _cg_bcg_bcbcg_least_square_exp(self): """ """ print("_cg_bcg_bcbcg_least_square_exp starting, ... ") self._BB_1 = np.random.random( ( self._mat.shape[0],1) ) self._BX_1 = np.ones ( (self._mat.shape[1],1) ) self._BB_4 = np.random.random( ( self._mat.shape[0],4) ) self._BX_4 = np.ones ( (self._mat.shape[1],4) ) self._BB_12 = np.random.random( ( self._mat.shape[0],12) ) self._BX_12 = np.ones ( (self._mat.shape[1],12) ) #line 1 bcg_solver_obj = NativeBlockConjugateGradient(self._mat, self._BX_1, self._BB_1, self._tol, self._maxiter) self._final_X_cg, self._final_R_cg, self._residual_hist_cg = bcg_solver_obj.bcg_variant_lstsq_run(0) ##line 2 bcg_solver_obj = NativeBlockConjugateGradient(self._mat, self._BX_12, self._BB_12, self._tol, self._maxiter) self._final_X_bcg_m12, self._final_R_bcg_m12, self._residual_hist_bcg_m12 = bcg_solver_obj.bcg_variant_lstsq_run(0) ##line 3 #bcbcg_solver_obj = BCBCG() #self._final_x_bcbcg_m1s2, self._final_r_bcbcg_m1s2, self._residual_hist_bcbcg_m1s2 = \ # bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_1, self._BX_1, 2, self._tol, self._maxiter, 0) ##line 4 #bcbcg_solver_obj = BCBCG() #self._final_x_bcbcg_m1s8, self._final_r_bcbcg_m1s8, self._residual_hist_bcbcg_m1s8 = \ # bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_1, self._BX_1, 8, self._tol, self._maxiter, 0) ##line 5 #bcbcg_solver_obj = BCBCG() #self._final_x_bcbcg_m4s2, self._final_r_bcbcg_m4s2, self._residual_hist_bcbcg_m4s2 = \ # bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_4, self._BX_4, 2, self._tol, self._maxiter, 0) ##line 6 #bcbcg_solver_obj = BCBCG() #self._final_x_bcbcg_m4s8, self._final_r_bcbcg_m4s8, self._residual_hist_bcbcg_m4s8 = \ # bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_4, self._BX_4, 8, self._tol, self._maxiter, 0) plot_worker = Presenter() #residual_list = [self._residual_hist_cg, self._residual_hist_bcg_m12, \ # self._residual_hist_bcbcg_m1s2, self._residual_hist_bcbcg_m1s8, \ # self._residual_hist_bcbcg_m4s2, self._residual_hist_bcbcg_m4s8 ] residual_list = [self._residual_hist_cg, self._residual_hist_bcg_m12] #legend_list = ["cg","bcg_m12", "bcbcg_m1s2", "bcbcg_m1s8", "bcbcg_m4s2", "bcbcg_m4s8"] legend_list = ["cg","bcg_m12"] #color_list = ["r","k","b","y","m","g"] color_list = ["r","k"] #plot_worker.instant_plot_y_log10(residual_list, "test", "#iteration", "$\\mathbf{log_{10}\\frac{||x_1||}{||b_1||}}$", legend_list, color_list) #plot_worker.instant_plot_y_log10(residual_list, "wathen100(dim=30,401, nnz=471,601, cond=5816.01 )", "#iteration", "$\\mathbf{log_{10}\\frac{||x_1||}{||b_1||}}$", legend_list, color_list) plot_worker.instant_plot_y_log10(residual_list, "MG", "#iteration", "$\\mathbf{log_{10}\\frac{||x_1||}{||b_1||}}$", legend_list, color_list)
def _db_bcbcg_lstsq (self): """ """ bcbcg_solver_obj = BCBCG() self._final_X_a, self._final_R_a, self._residual_hist_a = \ bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB, self._BX, self._step_val, self._tol, self._maxiter,0) self._final_X_b, self._final_R_b, self._residual_hist_b = \ bcbcg_solver_obj.bcbcg_solver(self._mat, self._BB, self._BX, self._step_val, self._tol, self._maxiter,0) plot_worker = Presenter() residual_list = [self._residual_hist_a, self._residual_hist_b] legend_list = ["bcbcg_s20b4_lstsq","bcbcg_s20b4"] color_list = ["r","k"] plot_worker.instant_plot_y_log10(residual_list, "crystm02", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list)
def _db_cbcg_lstsq (self): cbcg_solver_obj = CBCG() self._final_x_a, self._final_r_a, self._residual_hist_a = \ cbcg_solver_obj.cbcg_solver_least_square(self._mat, self._SB, self._SX, self._step_val, self._tol, self._maxiter) self._final_x_b, self._final_r_b, self._residual_hist_b = \ cbcg_solver_obj.cbcg_solver_least_square(self._mat, self._SB, self._SX, self._step_val, self._tol, self._maxiter) plot_worker = Presenter() residual_list = [self._residual_hist_a, self._residual_hist_b] legend_list = ["cbcg_s2_lstsq","blbcg_s2"] color_list = ["r","k"] plot_worker.instant_plot_y_log10(residual_list, "bodyy6", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list)
def _cg_bcg_bcbcg_least_square_exp(self): """ """ self._BB_1 = np.random.random( ( self._mat.shape[0],1) ) self._BX_1 = np.ones ( (self._mat.shape[1],1) ) self._BB_6 = np.random.random( ( self._mat.shape[0],6) ) self._BX_6 = np.ones ( (self._mat.shape[1],6) ) self._BB_12 = np.random.random( ( self._mat.shape[0],12) ) self._BX_12 = np.ones ( (self._mat.shape[1],12) ) #line 1 bcg_solver_obj = NativeBlockConjugateGradient(self._mat, self._BX_1, self._BB_1, self._tol, self._maxiter) self._final_X_cg, self._final_R_cg, self._residual_hist_cg = bcg_solver_obj.bcg_variant_lstsq_run(0) #line 2 bcg_solver_obj = NativeBlockConjugateGradient(self._mat, self._BX_12, self._BB_12, self._tol, self._maxiter) self._final_X_bcg_m12, self._final_R_bcg_m12, self._residual_hist_bcg_m12 = bcg_solver_obj.bcg_variant_lstsq_run(0) #line 3 bcbcg_solver_obj = BCBCG() self._final_x_bcbcg_m1s2, self._final_r_bcbcg_m1s2, self._residual_hist_bcbcg_m1s2 = \ bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_1, self._BX_1, 2, self._tol, self._maxiter, 0) #line 4 #bcbcg_solver_obj = BCBCG() #self._final_x_bcbcg_m1s6, self._final_r_bcbcg_m1s6, self._residual_hist_bcbcg_m1s6 = \ # bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_1, self._BX_1, 6, self._tol, self._maxiter, 0) bcbcg_solver_obj = BCBCG() self._final_x_bcbcg_m1s6, self._final_r_bcbcg_m1s6, self._residual_hist_bcbcg_m1s6 = \ bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_1, self._BX_1, 8, self._tol, self._maxiter, 0) #line 5 bcbcg_solver_obj = BCBCG() self._final_x_bcbcg_m6s2, self._final_r_bcbcg_m6s2, self._residual_hist_bcbcg_m6s2 = \ bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_6, self._BX_6, 2, self._tol, self._maxiter, 0) #line 6 #bcbcg_solver_obj = BCBCG() #self._final_x_bcbcg_m6s6, self._final_r_bcbcg_m6s6, self._residual_hist_bcbcg_m6s6 = \ # bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_6, self._BX_6, 6, self._tol, self._maxiter, 0) bcbcg_solver_obj = BCBCG() self._final_x_bcbcg_m6s6, self._final_r_bcbcg_m6s6, self._residual_hist_bcbcg_m6s6 = \ bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB_6, self._BX_6, 8, self._tol, self._maxiter, 0) plot_worker = Presenter() residual_list = [self._residual_hist_cg, self._residual_hist_bcg_m12, \ self._residual_hist_bcbcg_m1s2, self._residual_hist_bcbcg_m1s6, \ self._residual_hist_bcbcg_m6s2, self._residual_hist_bcbcg_m6s6 ] legend_list = ["cg","bcg_m12", "bcbcg_m1s2", "bcbcg_m1s6", "bcbcg_m6s2", "bcbcg_m6s6"] color_list = ["r","k","b","y","m","g"] plot_worker.instant_plot_y_log10(residual_list, "test", "#iteration", "$\\mathbf{log_{10}\\frac{||x_1||}{||b_1||}}$", legend_list, color_list)
def find_poster_raw(user_id): """ Return User's Profile in JSON. """ presenter = Presenter(user_id) response = { 'user_id': user_id, 'top_favorites': presenter.get_posters('TF', 8), 'nearest_neighbours': presenter.get_posters('NN', 8), 'latent_factors': presenter.get_posters('LF', 8) } return jsonify(response)
def find_data(user_id): """ Return user's data in JSON. """ presenter = Presenter(user_id) response = { 'user_id': user_id, 'top_favorites': presenter.get_data('TF'), 'nearest_neighbours': presenter.get_data('NN'), 'latent_factors': presenter.get_data('LF') } return jsonify(response)
def _db_power_iteration(self): pi_worker = PowerIteration(); tol = 1e-4 maxiters = 1000 self._v_eigen, self._lambda_eigen, self._lambda_eigen_list = pi_worker.naive_power_iteration (self._mat, self._X, maxiters, tol) self._v_shift_eigen, self._lambda_shift_eigen, self._lambda_shift_eigen_list = pi_worker.power_iteration_with_shifting (self._mat, self._X, self._lambda_eigen, maxiters, tol) print ("lambda ", self._lambda_shift_eigen) plot_worker = Presenter() ratio_list = [ [x+self._lambda_eigen for x in self._lambda_shift_eigen_list] ] legend_list = ["naive power iteration"] color_list = ["k"] plot_worker.instant_plot_y_log10(ratio_list, "crystm01" , "#iteration", "$\\frac{\\lambda_{old} - \\lambda_{new}}{\\lambda_i{old} }$", legend_list, color_list)
def find_poster(user_id): """ User's Profile Page. """ presenter = Presenter(user_id) response = { 'header': 'OpenREC', 'user_id': user_id, 'top_favorites': presenter.get_posters('TF', 8), 'nearest_neighbours': presenter.get_posters('NN', 8), 'latent_factors': presenter.get_posters('LF', 8) } return render_template('user.html', data=response)
def _db_lbcg_least_square_exp (self): """ """ lbcg_solver_obj = LBCG() self._final_x_a, self._final_r_a, self._residual_hist_a = \ lbcg_solver_obj.lbcg_solver_least_square(self._mat, self._SB, self._SX, self._step_val, self._tol, self._maxiter) cbcg_solver_obj = CBCG() self._final_x_b, self._final_r_b, self._residual_hist_b = \ cbcg_solver_obj.cbcg_solver(self._mat, self._SB, self._SX, self._step_val, self._tol, self._maxiter) plot_worker = Presenter() residual_list = [self._residual_hist_a, self._residual_hist_b] legend_list = ["lbcg_lstsq_s2", "cbcg_s2"] color_list = ["r","k"] #plot_worker.instant_plot_y_log10(residual_list, "crystm01", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list) plot_worker.instant_plot_y_log10(residual_list, "wathen100", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list)
def _blbcg_least_square_exp(self): """ """ blbcg_solver_obj = BLBCG() self._final_x_a, self._final_r_a, self._residual_hist_a = \ blbcg_solver_obj.blbcg_solver_least_square(self._mat, self._BB, self._BX, self._step_val, self._tol, self._maxiter, 0) bcbcg_solver_obj = BCBCG() self._final_x_b, self._final_r_b, self._residual_hist_b = \ bcbcg_solver_obj.bcbcg_solver_least_square(self._mat, self._BB, self._BX, self._step_val, self._tol, self._maxiter, 0) plot_worker = Presenter() residual_list = [self._residual_hist_a, self._residual_hist_b] legend_list = ["blbcg_s64b4_lstsq","bcbcg_s64b4_lstsq"] color_list = ["r","k"] #plot_worker.instant_plot_y_log10(residual_list, "crystm01", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list) plot_worker.instant_plot_y_log10(residual_list, "bodyy6", "#iteration", "$\\frac{||x_1||}{||b_1||}$", legend_list, color_list)
def main(): window_width = 80 window_height = 50 window_title = "Janus Mining Colony" font_path = Path("fonts/libtcod/dejavu16x16_gs_tc.png") presenter = Presenter(window_width, window_height, window_title, font_path) while presenter.is_window_open(): presenter.put_tile(1, 1, '@', libtcodpy.white) presenter.present() key = libtcodpy.console_check_for_keypress() if key.vk == libtcodpy.KEY_ESCAPE: return True
def _db_bcg_least_square (self): """ """ m = 32 self._BB_m = np.random.random( ( self._mat.shape[0],m) ) self._BX_m = np.ones ( (self._mat.shape[1],m) ) bcg_solver_obj = NativeBlockConjugateGradient(self._mat, self._BX_m, self._BB_m, self._tol, self._maxiter) self._final_X_bcg, self._final_R_bcg, self._residual_hist_bcg = bcg_solver_obj.bcg_variant_one_run(0) bcg_solver_obj = NativeBlockConjugateGradient(self._mat, self._BX_m, self._BB_m, self._tol, self._maxiter) self._final_X_bcg_lstsq, self._final_R_bcg_lstsq, self._residual_hist_bcg_lstsq = bcg_solver_obj.bcg_variant_lstsq_run(0) plot_worker = Presenter() residual_list = [self._residual_hist_bcg, self._residual_hist_bcg_lstsq] legend_list = ["bcg","bcg_lstsq"] color_list = ["r","k"] plot_worker.instant_plot_y_log10(residual_list, "test", "#iteration", "$\\mathbf{log_{10}\\frac{||x_1||}{||b_1||}}$", legend_list, color_list)
def __init__(self, path="../database"): self.session_files = [] self.books = self.getBooks(path) self.preferences = Preferences() self.preferences.readConfig() self.session = copy.deepcopy(self.preferences) self.db = Database() self.openDefaultDatabase() self.presenter = Presenter() self.songs = [] self.uploaded_song = {}
def __init__(self, context, data, plot=False, plot_title='Backtesting.pptx', plot_data=None): """ Initializes Backtesting class :param context: dictionary filled with vital information for backtesting and running the strategy algorithm :param data: list of the dataframes of the various securities needed for performance measurements :param plot: boolean for whether to plot or not. Default is False :param plot_title: string title to save plot presentation as. Default is 'Backtesting.pptx' :param plot_data: dictionary with additional information to use for plotting :return: N.A. """ self.context = context self.plot = plot if self.context['start_date'] is None: self.context['start_date'] = 'default' if self.context['end_date'] is None: self.context['end_date'] = 'default' # self.perf_data is one big dataframe of all dataframes in data, cut to provided start and end date # Will be used to determine performance (returns) of strategy # self.test_data is also one big dataframe, but cut only to provided end date # Will be used to determine orders from strategy self.perf_data = [None] * len(data) self.test_data = [None] * len(data) for i, df in enumerate(data): self.perf_data[i] = dfm.cut_dates(df, self.context['start_date'], self.context['end_date']) self.test_data[i] = dfm.cut_dates(df, 'default', self.context['end_date']) self.perf_df = dfm.merge_dfs(self.perf_data) self.test_df = dfm.merge_dfs(self.test_data) if self.plot: self.plot_data = plot_data self.presname = plot_title self.pres = Presenter(new_pres=self.presname, title='Backtesting') if self.plot_data['benchmark'] is not None: self.plot_data['benchmark'] = dfm.cut_dates( plot_data['benchmark'], self.context['start_date'], self.context['end_date'])