コード例 #1
0
ファイル: test_hrp.py プロジェクト: tschm/hrp
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'
    ]
コード例 #2
0
ファイル: obsolete.py プロジェクト: seanahmad/hrp
def hrp(prices, node=None, method="single"):
    """
    Computes the expected variance and the weights for the hierarchical risk parity portfolio
    :param cov: This is the covariance matrix that shall be used
    :param node: Optional. This is the rootnode of the graph describing the dendrogram
    :return: variance, weights
    """
    returns = prices.pct_change().dropna(axis=0, how="all")
    cov, cor = returns.cov(), returns.corr()
    links = linkage(dist(cor.values), method=method)
    node = node or tree(links)

    return __hrp(node, cov.values, weights=np.ones(cov.shape[1]))
コード例 #3
0
ファイル: marcos.py プロジェクト: seanahmad/hrp
def marcos(prices, node=None, method=None):
    # make sure the prices are a DataFrame
    assert isinstance(prices, pd.DataFrame)

    # convert into returns
    returns = prices.pct_change().dropna(axis=0, how="all")

    # compute covariance matrix and correlation matrices (both as DataFrames)
    cov, cor = returns.cov(), returns.corr()

    # Compute the root node of the tree
    method = method or "ward"
    node = node or tree(linkage(dist(cor.values), method=method))

    # this is an interesting step
    ids = node.pre_order()
    # apply bisection, root is now a ClusterNode of the graph
    root = bisection(ids=ids)

    # It's not clear to me why Marcos is going down this route. Rather than sticking with the graph computed above.
    return _hrp(node=root, cov=cov)