def getFrontier(na_data): '''Function gets a 100 sample point frontier for given returns''' # Special Case with fTarget=None, just returns average rets. (na_avgrets, na_std, b_error) = tsu.OptPort(na_data, None) # Declaring bounds on the optimized portfolio na_lower = np.zeros(na_data.shape[1]) na_upper = np.ones(na_data.shape[1]) # Getting the range of possible returns with these bounds (f_min, f_max) = tsu.getRetRange(na_data, na_lower, na_upper, na_avgrets, s_type="long") # Getting the step size and list of returns to optimize for. f_step = (f_max - f_min) / 100.0 lf_returns = [f_min + x * f_step for x in range(101)] # Declaring empty lists lf_std = [] lna_portfolios = [] # Calling the optimization for all returns for f_target in lf_returns: (na_weights, f_std, b_error) = tsu.OptPort(na_data, f_target, na_lower, na_upper, s_type="long") lf_std.append(f_std) lna_portfolios.append(na_weights) return (lf_returns, lf_std, lna_portfolios, na_avgrets, na_std)
def get_frontier(basic_portfolio, ref_symbol, filename="EquitiesvFrontier.pdf", target_return=0.015): """ @param basic_portfolio @param ref_symbol reference symbol """ assert isinstance(basic_portfolio, BasicPortfolio) stock_close_price = basic_portfolio.get_stock_close_prices() stock_normalized_price = stock_close_price.values / stock_close_price.values[ 0, :] ref_close_price = load_stock_close_price(basic_portfolio.start_date, basic_portfolio.end_date, [ref_symbol]) ref_normalized_price = ref_close_price.values / ref_close_price.values[ 0, :] daily_return0 = get_daily_return0(stock_normalized_price) (na_avgrets, na_std, b_error) = tsu.OptPort(daily_return0, None) # Declaring bounds on the optimized portfolio na_lower = np.zeros(daily_return0.shape[1]) na_upper = np.ones(daily_return0.shape[1]) # Getting the range of possible returns with these bounds (f_min, f_max) = tsu.getRetRange(daily_return0, na_lower, na_upper, na_avgrets, s_type="long") # Getting the step size and list of returns to optimize for. f_step = (f_max - f_min) / 100.0 lf_returns = [f_min + x * f_step for x in range(101)] # Declaring empty lists lf_std = [] lna_portfolios = [] # Calling the optimization for all returns for f_target in lf_returns: (na_weights, f_std, b_error) = \ tsu.OptPort(daily_return0, f_target, na_lower, na_upper, s_type="long") lf_std.append(f_std) lna_portfolios.append(na_weights) f_target = target_return (na_weights, f_std, b_error) = \ tsu.OptPort(daily_return0, f_target, na_lower, na_upper, s_type="long") print 'Optimized portfolio for target return', f_target print 'Volatility is ', f_std for ticker_name, weight in zip(basic_portfolio.ticker_names, na_weights): if weight > 0.00001: print ticker_name, ':', weight plt.clf() plt.figure(figsize=(8, 10), dpi=100) # Plot individual stock risk/return as green + for i in range(len(basic_portfolio.ticker_names)): # plt.plot(na_std[i], f_ret, 'g+') # plt.text(na_std[i], f_ret, ls_names[i], fontsize = 10) ave = np.average(daily_return0[:, i]) std = np.std(daily_return0[:, i]) plt.plot(std, ave, 'g+') plt.text(std, ave, basic_portfolio.ticker_names[i], fontsize=5) ref_daily_return = get_daily_return0(ref_normalized_price) ave = np.average(ref_daily_return) std = np.std(ref_daily_return) plt.plot(std, ave, 'r+') plt.text(std, ave, 'CAC 40', fontsize=6) plt.plot(lf_std, lf_returns, 'b') plt.title('Efficient Frontier For CAC 40') # plt.legend(['2013 Frontier'], loc = 'lower left') plt.ylabel('Expected Return') plt.xlabel('StDev') if filename is None: plt.show() else: plt.savefig(filename, format='pdf') return na_weights
if s in symbols_2013_2015: tickers.append(s) else: continue # Remove SPY index to avoid bias tickers.remove('SPY') # Use data from 2010 to 2012 to find optimal portfolios Adj_Close_Train = Adj_Close_2010_2012[tickers] opt_data = Adj_Close_Train.values.copy() #Create NumPy array tsu.returnize0(opt_data) # Normalize with respect to first value # Get average returns, upper and lower bounds (opt_avgrets, opt_std, b_error) = tsu.OptPort(opt_data, None) opt_lower = np.zeros(opt_data.shape[1]) opt_upper = np.ones(opt_data.shape[1]) (f_min, f_max) = tsu.getRetRange(opt_data, opt_lower, opt_upper,opt_avgrets, s_type="long") #%% Create optimal portfolios based on target return for all periods k = [0.5,0.6,0.7,0.8,0.9,1.0] portfolios = [] portfolios_std = [] optimal_tickers = [] for i in range(len(k)): target_return = k[i] * f_max (opt_weights, f_std, b_error) = tsu.OptPort(opt_data, target_return,opt_lower, opt_upper, s_type="long") opt_weights[opt_weights < 0.001] = 0.0 # Remove any tiny values portfolios.append(opt_weights) portfolios_std.append(f_std)