def plot_sharpe_ratios(data, w_vec, gammas, show=False): for i, gamma in enumerate(gammas): plt.plot(gamma, compute_scores(data, w_vec[i])[2], 'bo') plt.xlabel('Gamma') plt.ylabel('Sharpe Ratio') if show: plt.show()
def plot_equally_weighted(data, show=False): N = len(data) equal_w_vec = [1 / float(N)] * N mean, std, sharpe = compute_scores(data, equal_w_vec) plt.plot(std, mean, 'co') if show: plt.show()
def plot_individual_assets(data, show=False): for i in range(len(data)): new_w_vec = [0] * len(data) new_w_vec[i] = 1 mean, std, sharpe = compute_scores(data, new_w_vec) plt.plot(std, mean, 'ro') plt.annotate('coin {}'.format(i), xy=(std, mean)) if show: plt.show()
def test_scores(prices): """DEPRECATED""" N = len(prices) results = [] for i in range(N): new_w_vec = [0] * N new_w_vec[i] = 1 results.append([i] + list(compute_scores(prices, new_w_vec))) # MPT gammas = np.logspace(-2, 2, num=100) w_vec_results, ret_results, risk_results = mpt_opt(prices, gammas) results.append(['MVO'] + list(compute_scores(prices, w_vec_results[0]))) # Market Cap mcap_vec = mktcap_opt(load_marketcaps()) results.append(['MCAP'] + list(compute_scores(prices, mcap_vec))) # Equal Weights eq_w_vec = [1 / float(N)] * N results.append(['EQ'] + list(compute_scores(prices, eq_w_vec))) sorted_results = sorted(results, key=lambda x: -x[-1]) for result in sorted_results: print(result)
def train(self, start_index=None, end_index=None): sliced_prices = slice_index_params(self.prices, start_index, end_index) gammas = np.logspace(-2, 2, num=100) w_vec_results, ret_results, risk_results = mpt_opt(sliced_prices, gammas) # This is slow - bisect so it runs in log(N) time sharpe_ratios = [compute_scores(sliced_prices, w_vec_results[i])[-1] for i, gamma in enumerate(gammas)] max_sharpe_ratio = max(sharpe_ratios) max_sharpe_ratio_index = sharpe_ratios.index(max_sharpe_ratio) self.weights = w_vec_results[max_sharpe_ratio_index] self.gamma = gammas[max_sharpe_ratio_index] self.trained_sharpe_ratio = max_sharpe_ratio return (self.weights, self.gamma, ret_results[max_sharpe_ratio_index], risk_results[max_sharpe_ratio_index], self.trained_sharpe_ratio)
def test(self, start_index=None, end_index=None): sliced_prices = slice_index_params(self.prices, start_index, end_index) return compute_scores(sliced_prices, self.weights)
def plot_marketcap_weighted(data, show=False): mktcap_w_vec = mktcap_opt(load_marketcaps()) mean, std, sharpe = compute_scores(data, mktcap_w_vec) plt.plot(std, mean, 'mo') if show: plt.show()