示例#1
0
def test_cla_custom_bounds():
    bounds = [(0.01, 0.13), (0.02, 0.11)] * 10
    cla = CLA(*setup_cla(data_only=True), weight_bounds=bounds)
    df = get_data()
    cla.cov_matrix = risk_models.exp_cov(df).values
    w = cla.min_volatility()
    assert isinstance(w, dict)
    assert set(w.keys()) == set(cla.tickers)
    np.testing.assert_almost_equal(cla.weights.sum(), 1)
    assert (0.01 <= cla.weights[::2]).all() and (cla.weights[::2] <=
                                                 0.13).all()
    assert (0.02 <= cla.weights[1::2]).all() and (cla.weights[1::2] <=
                                                  0.11).all()
    # Test polymorphism of the weight_bounds param.
    bounds2 = ([bounds[0][0], bounds[1][0]] * 10,
               [bounds[0][1], bounds[1][1]] * 10)
    cla2 = CLA(*setup_cla(data_only=True), weight_bounds=bounds2)
    cla2.cov_matrix = risk_models.exp_cov(df).values
    w2 = cla2.min_volatility()
    assert dict(w2) == dict(w)
示例#2
0
def test_cla_min_volatility_exp_cov_short():
    cla = CLA(*setup_cla(data_only=True), weight_bounds=(-1, 1))
    df = get_data()
    cla.cov_matrix = risk_models.exp_cov(df).values
    w = cla.min_volatility()
    assert isinstance(w, dict)
    assert set(w.keys()) == set(cla.tickers)
    np.testing.assert_almost_equal(cla.weights.sum(), 1)
    np.testing.assert_allclose(
        cla.portfolio_performance(),
        (0.23215576461823062, 0.1325959061825329, 1.6000174569958052),
    )
示例#3
0
def test_cla_min_volatility_exp_cov_short():
    cla = CLA(*setup_cla(data_only=True), weight_bounds=(-1, 1))
    df = get_data()
    cla.cov_matrix = risk_models.exp_cov(df).values
    w = cla.min_volatility()
    assert isinstance(w, dict)
    assert set(w.keys()) == set(cla.tickers)
    np.testing.assert_almost_equal(cla.weights.sum(), 1)
    np.testing.assert_allclose(
        cla.portfolio_performance(),
        (0.2634735528776959, 0.13259590618253303, 1.8362071642131053),
    )
示例#4
0
def test_cla_custom_bounds():
    bounds = [(0.01, 0.13), (0.02, 0.11)] * 10
    cla = CLA(*setup_cla(data_only=True), weight_bounds=bounds)
    df = get_data()
    cla.cov_matrix = risk_models.exp_cov(df).values
    w = cla.min_volatility()
    assert isinstance(w, dict)
    assert set(w.keys()) == set(cla.tickers)
    np.testing.assert_almost_equal(cla.weights.sum(), 1)
    assert (0.01 <= cla.weights[::2]).all() and (cla.weights[::2] <=
                                                 0.13).all()
    assert (0.02 <= cla.weights[1::2]).all() and (cla.weights[1::2] <=
                                                  0.11).all()