def test_symbol_ab(self,symbol, nf = 1): ## This function tests that the residuals behaves properly. ## That is, that the alpha (how we behave compared to the market) ## has a nice gaussian distribution. ## Slide 7 index = self.Sindex # The index sym_ret = self.pf.symbols[symbol].TDs[self.period].get_timeSeriesReturn() ind_ret = self.get_indexReturns() # Get coefficients for the symbol coeffs = self.get_symbol_ab(symbol) ##### GET THE RESIDUAL X = np.concatenate((np.ones((sym_ret.shape[0],1)),sym_ret),axis = 1) pred = X.dot(np.array(coeffs)) # Pred = X * Phi pred = pred.reshape(pred.shape[0],1) residual = pred - ind_ret print "Mean of residual %f" % np.mean(residual) ### Now we test the residual print "Statistical test of residual" ttest = stats.ttest_1samp(a = residual, # Sample data popmean = 0) # Pop mean print ttest ######## DOUBLE REGRESSION OF PAGE 7. Early empirical test Xres = np.concatenate((ind_ret,np.power(residual,2)),axis = 1) coeff = bMl.get_linearRef(Xres, sym_ret) print "Early empirical test of CAPM is wrong" print coeff hist, bin_edges = np.histogram(residual, density=True) gl.bar(bin_edges[:-1], hist, labels = ["Distribution","Return", "Probability"], legend = [symbol], alpha = 0.5, nf = nf) ## Lets get some statistics using stats m, v, s, k = stats.t.stats(10, moments='mvsk') n, (smin, smax), sm, sv, ss, sk = stats.describe(residual) print "****** MORE STATISTIC ************" print "Mean " + str(sm) tt = (sm-m)/np.sqrt(sv/float(n)) # t-statistic for mean pval = stats.t.sf(np.abs(tt), n-1)*2 # two-sided pvalue = Prob(abs(t)>tt) print 't-statistic = %6.3f pvalue = %6.4f' % (tt, pval) return coeff
for i in range(len(question_types)): question_types_ammount.append( np.where(question_types_list == i)[0].size) ## Normalize and order question_types_ammount, question_types_ordered = zip( *sorted(zip(question_types_ammount, question_types), reverse=True)) question_types_ammount = np.array(question_types_ammount) question_types_ammount = question_types_ammount / np.sum( question_types_ammount) ## Do the actual plotting gl.init_figure() ax1 = gl.bar( question_types_ordered, question_types_ammount, align="center", labels=["Question Types Proportions SQUAD 1.1", "", "Proportion"]) gl.set_fontSizes(ax=[ax1], title=20, xlabel=20, ylabel=20, legend=10, xticks=15, yticks=15) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.10)
def IFE_h(self, Rf=0, mktcap=[], year_start=1996, year_finish=2016, window=10): ## Black litterman question !! # The optimal portolio, lets say is the one given by Markovitz # mktcap is a dicktionary with the market capitalizaion of the equities self.pf.set_interval(dt.datetime(year_start, 1, 1), dt.datetime(year_finish, 1, 1)) ## Get the actual stuff !! ExpRet = self.get_MeanReturns() Sigma = self.get_covMatrix() woptimal = self.TangentPortfolio() self.set_allocation(woptimal) R, S = self.get_metrics() delta = (R - self.Rf) / np.power(S, 2) # Optimal risk adversion ## Get the weights by the market capitalization if (len(mktcap) > 0): weq = [] for sym in self.symbol_names: weq.append(mktcap[sym]) weq = ul.fnp(weq) / np.sum(weq) weq = weq.T.tolist()[0] # print weq else: weq = woptimal # Initial prior ############### PUT FECKING BL prior instead ########## # Calculate initial portfolio from the market capitalization # Risk aversion of the market. We say it is the one of the portfolio # The optimal portfolio is the market. # weq = np.ones((1,self.Nsym))/self.Nsym # weq = weq.tolist()[0] # Coefficient of uncertainty in the prior estimate of the mean tau = 10 ### Prior of our Views !!! P1 = np.zeros((2, self.Nsym)) P1[0, 0] = -1 P1[0, 1] = 1 P1[1, 1] = -1 P1[1, 2] = 1 P1 = ul.fnp(P1) # If we invert P1 and Q1 at the same time we get the same Q1 = [0.0002, 0.0001] Q1 = ul.fnp(Q1) Omega1 = np.dot(np.dot(P1, Sigma), P1.T) * np.eye(Q1.shape[0]) postPi, weqpost = self.BlackLitterman( weq, Sigma, delta, # Prior portfolio variables tau, # Uncertainty coefficient of the porfolio priors P1, Q1, Omega1) # Prior views variables # Reference returns of the portfolio of the market # They can just be calculated using the portfolio # A priory the expected return Posteriori does not have to be bigger # Just more accuarate to reality if our views are right :) refPi = delta * np.dot(Sigma, weq) Ereturn = np.dot(refPi, weq) EreturnPost = np.dot(postPi, weqpost) ## Plot the returns !!! # We will plot the real w returns, the Pi Returns, and the Post- Returns gl.set_subplots(2, 3) gl.bar(self.pf.symbols.keys(), ExpRet, labels=["Optimal initial returns"]) gl.bar(self.pf.symbols.keys(), refPi, labels=["Prior Returns"]) gl.bar(self.pf.symbols.keys(), postPi, labels=["Posterior Returns"]) # gl.savefig(folder_images +'returnsBL.png', # dpi = 150, sizeInches = [2*8, 2*6]) ## Plot the weights !!! # We will plot the real w returns, the Pi Returns, and the Post- Returns # gl.set_subplots(1,3) gl.bar(self.pf.symbols.keys(), woptimal, labels=["Optimal intial weights"]) gl.bar(self.pf.symbols.keys(), weq, labels=["Prior Weights"]) gl.bar(self.pf.symbols.keys(), weqpost, labels=["Posterior Weights"]) # gl.savefig(folder_images +'weightsBL.png', # dpi = 150, sizeInches = [2*8, 2*6]) gl.savefig(folder_images + 'weightsreturnsBL.png', dpi=150, sizeInches=[2 * 8, 2 * 6]) pass
def IFE_f2(self, ObjectiveRlist=[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) All_returns = [] All_vars = [] windowslist = range(1, 13) ObjectiveR = 0.03 for window in windowslist: PortfolioReturns = [] 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 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) dates = self.get_dates() all_dates.append(dates[0]) All_returns.append(np.mean(PortfolioReturns)) All_vars.append(np.std(PortfolioReturns) / np.sqrt(np.sqrt(12 * 12))) # All_returns = np.array(All_returns).reshape(len(ObjectiveRlist),10) # print All_returns All_means = All_returns print All_returns # All_means = np.mean(All_returns, axis = 1) print ul.fnp(All_returns).shape print All_means # print All_means - ObjectiveRlist # All_means = np.divide((All_means - ObjectiveRlist),ObjectiveRlist) # print All_means ## Graph with the desired, the obtained returns and the returns of the index gl.bar(windowslist, All_means, labels=["Obtained returns", "Time (years)", "Return (%)"], legend=["Index Return"], alpha=0.8, nf=1) gl.plot(windowslist, All_vars, labels=["Obtained returns", "Time (years)", "Return (%)"], legend=["Index Return"], alpha=0.8, nf=0) gl.savefig(folder_images + 'best_Objective.png', dpi=150, sizeInches=[2 * 8, 2 * 6])
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])
legend = ["price"], ws = ws, color = "red", nf = 0) timeSeries = timeData.get_timeSeries(["Volume"]); # gl.step(timeData.dates,timeSeries, # legend = ["price"], # ws = ws, # color = "red", # nf = 0, na = 1, fill = 1) gl.bar(dates,timeSeries, legend = ["price"], ws = ws, color = "red", nf = 0, na = 1) gl.add_slider(args = {"wsize":ws}, plots_affected = []) gl.add_hidebox() list_values = [] gl.add_selector(list_values) gl.add_onKeyPress() # type(timeData.TD.index) # cad = pd.DataFrame(index = time_index) # caca = pd.to_datetime(time_index.T.tolist()) # print caca #
def IFE_h (self, Rf = 0, mktcap = [], year_start = 1996, year_finish = 2016, window = 10): ## Black litterman question !! # The optimal portolio, lets say is the one given by Markovitz # mktcap is a dicktionary with the market capitalizaion of the equities self.pf.set_interval(dt.datetime(year_start,1,1),dt.datetime(year_finish,1,1)) ## Get the actual stuff !! ExpRet = self.get_MeanReturns() Sigma = self.get_covMatrix() woptimal = self.TangentPortfolio() self.set_allocation(woptimal) R,S = self.get_metrics() delta = (R - self.Rf)/np.power(S,2) # Optimal risk adversion ## Get the weights by the market capitalization if (len(mktcap) > 0): weq = [] for sym in self.symbol_names: weq.append(mktcap[sym]) weq = ul.fnp(weq) /np.sum(weq) weq = weq.T.tolist()[0] # print weq else: weq = woptimal # Initial prior ############### PUT FECKING BL prior instead ########## # Calculate initial portfolio from the market capitalization # Risk aversion of the market. We say it is the one of the portfolio # The optimal portfolio is the market. # weq = np.ones((1,self.Nsym))/self.Nsym # weq = weq.tolist()[0] # Coefficient of uncertainty in the prior estimate of the mean tau = 10 ### Prior of our Views !!! P1 = np.zeros((2,self.Nsym)) P1[0,0] = -1; P1[0,1] = 1 P1[1,1] = -1; P1[1,2] = 1 P1 = ul.fnp(P1) # If we invert P1 and Q1 at the same time we get the same Q1 = [0.0002, 0.0001] Q1 = ul.fnp(Q1) Omega1 = np.dot(np.dot(P1,Sigma),P1.T) * np.eye(Q1.shape[0]) postPi,weqpost = self.BlackLitterman(weq, Sigma, delta, # Prior portfolio variables tau, # Uncertainty coefficient of the porfolio priors P1, Q1, Omega1) # Prior views variables # Reference returns of the portfolio of the market # They can just be calculated using the portfolio # A priory the expected return Posteriori does not have to be bigger # Just more accuarate to reality if our views are right :) refPi = delta * np.dot(Sigma, weq) Ereturn = np.dot(refPi,weq) EreturnPost = np.dot(postPi,weqpost) ## Plot the returns !!! # We will plot the real w returns, the Pi Returns, and the Post- Returns gl.set_subplots(2,3) gl.bar(self.pf.symbols.keys(),ExpRet, labels = ["Optimal initial returns"]) gl.bar(self.pf.symbols.keys(),refPi, labels = ["Prior Returns"]) gl.bar(self.pf.symbols.keys(),postPi, labels = ["Posterior Returns"]) # gl.savefig(folder_images +'returnsBL.png', # dpi = 150, sizeInches = [2*8, 2*6]) ## Plot the weights !!! # We will plot the real w returns, the Pi Returns, and the Post- Returns # gl.set_subplots(1,3) gl.bar(self.pf.symbols.keys(),woptimal, labels = ["Optimal intial weights"]) gl.bar(self.pf.symbols.keys(),weq, labels = ["Prior Weights"]) gl.bar(self.pf.symbols.keys(),weqpost, labels = ["Posterior Weights"]) # gl.savefig(folder_images +'weightsBL.png', # dpi = 150, sizeInches = [2*8, 2*6]) gl.savefig(folder_images +'weightsreturnsBL.png', dpi = 150, sizeInches = [2*8, 2*6]) pass
def IFE_f2 (self, ObjectiveRlist = [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) All_returns = [] All_vars = [] windowslist = range(1,13) ObjectiveR = 0.03 for window in windowslist: PortfolioReturns = [] 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 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) dates = self.get_dates() all_dates.append(dates[0]) All_returns.append(np.mean(PortfolioReturns)) All_vars.append(np.std(PortfolioReturns)/np.sqrt(np.sqrt(12*12))) # All_returns = np.array(All_returns).reshape(len(ObjectiveRlist),10) # print All_returns All_means = All_returns print All_returns # All_means = np.mean(All_returns, axis = 1) print ul.fnp(All_returns).shape print All_means # print All_means - ObjectiveRlist # All_means = np.divide((All_means - ObjectiveRlist),ObjectiveRlist) # print All_means ## Graph with the desired, the obtained returns and the returns of the index gl.bar(windowslist, All_means, labels = ["Obtained returns", "Time (years)", "Return (%)"], legend = ["Index Return"], alpha = 0.8, nf = 1) gl.plot(windowslist, All_vars, labels = ["Obtained returns", "Time (years)", "Return (%)"], legend = ["Index Return"], alpha = 0.8, nf = 0) gl.savefig(folder_images +'best_Objective.png', dpi = 150, sizeInches = [2*8, 2*6])
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])
N_IDLE.append(0) for trailerid in different_trailers: if (np.mean((registers_day[registers_day["EquipmentItemId"] == trailerid])["IDLE"]) == 1): N_IDLE[-1] = N_IDLE[-1] + 1 # gl.step(days, N_IDLE, legend = ["IDLE Trailers"], alpha = 0.5, fill = 1, labels = ["Usage of Trailers","",DifferenOC[j]]) # gl.step(days, N_trucks, legend = ["Total Trailers"], nf = 0, alpha = 0.5, fill = 1) # propor_IDLE = np.sum(N_IDLE) / float(np.sum(N_trucks)) IDLE_prop.append(propor_IDLE) # gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.20, hspace=0) # caca = ["d","df","d","$","f"] gl.bar(DifferenOC.tolist(), IDLE_prop, labels=["Proportion of IDLE Trailers", "", "Proportion"]) # DifferenOC.tolist() #TODO: Gang number 99403034 29103929 Nregion = DifferenOC.size precompute_trailers_IDLE = 0 if (precompute_trailers_IDLE): total_days_onhire = 0 total_onhire = 0 list_IDLE = [] Nregion = DifferenOC.size list_IDLE_Regions = [ ] # Nregions x Ndays x Trailers that are IDLE the whole day bitch list_days = [] # Nregions x Ndays list_OSH_Regions = [] for j in range(Nregion): #Nregion
# gl.plot_wid(timeData.get_dates(), timeSeries, scrolling = 200) ws = 50 gl.step(dates, timeSeries, legend=["price"], ws=ws) timeSeries = timeData.get_timeSeries(["Low"]) gl.step(dates, timeSeries, legend=["price"], ws=ws, color="red", nf=0) timeSeries = timeData.get_timeSeries(["Volume"]) # gl.step(timeData.dates,timeSeries, # legend = ["price"], # ws = ws, # color = "red", # nf = 0, na = 1, fill = 1) gl.bar(dates, timeSeries, legend=["price"], ws=ws, color="red", nf=0, na=1) gl.add_slider(args={"wsize": ws}, plots_affected=[]) gl.add_hidebox() list_values = [] gl.add_selector(list_values) gl.add_onKeyPress() # type(timeData.TD.index) # cad = pd.DataFrame(index = time_index) # caca = pd.to_datetime(time_index.T.tolist()) # print caca # # ld = CGraph() # ld = copy.copy(gl)
eu_dist = hf.get_doc_sims(All_bow, BoW) best_common, best_common_i = uf.sort_and_get_order(eu_dist[:-1].tolist(), reverse=False) # print best_common_i[:5] # print dict_files # plt.close("all") gl.set_subplots(1, 3) for i in range(3): BoW_values = All_bow[str(best_common_i[i])].values BoW_index = All_bow.index Nwords = 30 b_v, b_vi = uf.sort_and_get_order(BoW_values, reverse=True) gl.bar(BoW_index[b_vi[0:Nwords]].tolist(), BoW_values[b_vi[0:Nwords]], legend=[dict_files[best_common_i[i]].split("/")[-1]], labels=["", "", "Num"], nf=1) ## Get the BoW of the most common: plotting_thins = 0 if (plotting_thins == 1): query_dirs = ["./books/0/Winchester", "Yorkshire Battles"] query_dirs = ["1342-0.txt", "11-0.txt"] labels = [ "Pride and Prejudice", "Alice's Adventures in Wonderland", "Trading Book" ] gl.set_subplots(3, 1) for i in range(3):