def test_quasi_diag(): prices = get_data() # compute returns returns = prices.pct_change().dropna(axis=0, how="all").fillna(0.0) np.testing.assert_allclose(returns.cov().values, np.genfromtxt(resource("covariance2.csv"))) np.testing.assert_allclose(returns.corr().values, np.genfromtxt(resource("correlation2.csv"))) cor = returns.corr().values links = linkage(dist(cor), method="single") # uncomment this line if you want to generate a new test resource # np.savetxt(resource("links.csv"), links, delimiter=",") np.testing.assert_array_almost_equal( links, np.loadtxt(resource("links.csv"), delimiter=',')) node = tree(links) ids = node.pre_order() assert ids == [ 11, 7, 19, 6, 14, 5, 10, 13, 3, 1, 4, 16, 0, 2, 17, 9, 8, 18, 12, 15 ] ordered_tickers = prices.keys()[ids].to_list() print(ordered_tickers) assert ordered_tickers == [ 'UAA', 'WMT', 'SBUX', 'AMD', 'RRC', 'GE', 'T', 'XOM', 'BABA', 'AAPL', 'AMZN', 'MA', 'GOOG', 'FB', 'PFE', 'GM', 'BAC', 'JPM', 'SHLD', 'BBY' ]
def test_hrp(): prices = get_data() root = hrp(prices=prices, method="ward") x = pd.read_csv(resource("weights_hrp.csv"), squeeze=True, index_col=0, header=0) x.index.name = None pd.testing.assert_series_equal(x, root.weights, check_exact=False)
def test_hrp(): prices = get_data() variance, weights = hrp(prices=prices) w = pd.Series(index=prices.keys(), data=weights, name="Weights").sort_index() x = pd.read_csv(resource("weights_hrp.csv"), squeeze=True, index_col=0, header=None) x.name = "Weights" x.index.name = None pd.testing.assert_series_equal(x, w, check_exact=False)
def test_hrp(): prices = get_data() root = hrp(prices=prices) # uncomment this line if you want generating a new file # root.weights_series(index=list(prices.keys())).to_csv(resource("weights_hrp.csv"), header=False) x = pd.read_csv(resource("weights_hrp.csv"), squeeze=True, index_col=0, header=None) x.name = "Weights" x.index.name = None pd.testing.assert_series_equal(x, root.weights, check_exact=False)