Пример #1
0
def test_max_quadratic_utility_with_shorts():
    es = EfficientSemivariance(*setup_efficient_semivariance(data_only=True),
                               weight_bounds=(-1, 1))
    es.max_quadratic_utility()
    np.testing.assert_almost_equal(es.weights.sum(), 1)

    np.testing.assert_allclose(
        es.portfolio_performance(),
        (3.2806086360944846, 1.0219729896939227, 3.190503730505662),
        rtol=1e-4,
        atol=1e-4,
    )
Пример #2
0
def test_max_quadratic_utility_market_neutral():
    es = EfficientSemivariance(*setup_efficient_semivariance(data_only=True),
                               weight_bounds=(-1, 1),
                               solver="ECOS")
    es.max_quadratic_utility(market_neutral=True)
    np.testing.assert_almost_equal(es.weights.sum(), 0)
    np.testing.assert_allclose(
        es.portfolio_performance(),
        (3.106826930927578, 0.9789014670659041, 3.153358161960706),
        rtol=1e-4,
        atol=1e-4,
    )
def test_es_errors():
    df = get_data()
    mu = expected_returns.mean_historical_return(df)
    historical_rets = expected_returns.returns_from_prices(df)

    with pytest.warns(UserWarning):
        EfficientSemivariance(mu, historical_rets)

    historical_rets = historical_rets.dropna(axis=0, how="any")
    es = EfficientSemivariance(mu, historical_rets)

    with pytest.raises(NotImplementedError):
        es.min_volatility()

    with pytest.raises(NotImplementedError):
        es.max_sharpe()

    with pytest.raises(ValueError):
        # Must be > 0
        es.max_quadratic_utility(risk_aversion=-0.01)

    with pytest.raises(ValueError):
        # Must be > 0
        es.efficient_return(target_return=-0.01)

    with pytest.raises(ValueError):
        # Must be <= max expected return
        es.efficient_return(target_return=np.abs(mu).max() + 0.01)

    with pytest.raises(AttributeError):
        # list not supported.
        EfficientSemivariance(mu, historical_rets.to_numpy().tolist())

    historical_rets = historical_rets.iloc[:, :-1]
    with pytest.raises(ValueError):
        EfficientSemivariance(mu, historical_rets)