示例#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()
e_return = {}
e_cov = {}

for x in epochs.keys():
    sub_price = assets.loc[epochs[x]['start']:epochs[x]['end']]
    e_return[x] = mean_historical_return(assets, frequency=252)
    e_cov[x] = CovarianceShrinkage(sub_price).ledoit_wolf()

# Display the efficient covariance matrices for all epochs
print("Efficient Covariance Matrices\n", e_cov)

# Initialize the Crtical Line Algorithm object
efficient_portfolio_during = CLA(e_return["during"], e_cov["during"])

# Find the minimum volatility portfolio weights and display them
print(efficient_portfolio_during.min_volatility())

# Compute the efficient frontier
(ret, vol, weights) = efficient_portfolio_during.efficient_frontier()

# Add the frontier to the plot showing the 'before' and 'after' frontiers
plt.scatter(vol, ret, s=4, c='g', marker='.', label='During')
plt.legend()
plt.show()

# plotting using PyPortfolioOpt
pplot.plot_covariance(cs, plot_correlation=False, show_tickers=True)
pplot.plot_efficient_frontier(efficient_portfolio_during,
                              points=100,
                              show_assets=True)
pplot.plot_weights(cw)