def test_es_return_sample():
    df = get_data()
    mu = expected_returns.mean_historical_return(df)
    S = risk_models.sample_cov(df)

    # Generate a 1y sample of daily data
    np.random.seed(0)
    mu_daily = (1 + mu)**(1 / 252) - 1
    S_daily = S / 252
    sample_rets = pd.DataFrame(np.random.multivariate_normal(
        mu_daily, S_daily, 300),
                               columns=mu.index)

    cd = EfficientCDaR(mu, sample_rets)
    w = cd.efficient_return(0.2)

    assert isinstance(w, dict)
    assert set(w.keys()) == set(cd.tickers)
    np.testing.assert_almost_equal(cd.weights.sum(), 1)
    assert all([i >= -1e-5 for i in w.values()])

    np.testing.assert_allclose(
        cd.portfolio_performance(),
        (0.20, 0.045414),
        rtol=1e-4,
        atol=1e-4,
    )
    # Cover verbose param case
    np.testing.assert_equal(cd.portfolio_performance(verbose=True),
                            cd.portfolio_performance())
def test_cdar_example():
    beta = 0.95
    df = get_data()
    mu = expected_returns.mean_historical_return(df)
    historical_rets = expected_returns.returns_from_prices(df).dropna()

    cd = EfficientCDaR(mu, historical_rets, beta=beta)
    w = cd.min_cdar()

    assert isinstance(w, dict)
    assert set(w.keys()) == set(cd.tickers)
    np.testing.assert_almost_equal(cd.weights.sum(), 1)
    assert all([i >= -1e-5 for i in w.values()])

    np.testing.assert_allclose(
        cd.portfolio_performance(),
        (0.21502, 0.056433),
        rtol=1e-4,
        atol=1e-4,
    )

    cdar = cd.portfolio_performance()[1]
    portfolio_rets = historical_rets @ cd.weights
    cum_rets = portfolio_rets.cumsum(0)
    drawdown = cum_rets.cummax() - cum_rets

    dar_hist = drawdown.quantile(beta)
    cdar_hist = drawdown[drawdown > dar_hist].mean()
    np.testing.assert_almost_equal(cdar_hist, cdar, decimal=3)
def test_efficient_return_short():
    cd = EfficientCDaR(*setup_efficient_cdar(data_only=True),
                       weight_bounds=(-3.0, 3.0))
    w = cd.efficient_return(0.28)
    assert isinstance(w, dict)
    assert set(w.keys()) == set(cd.tickers)
    np.testing.assert_almost_equal(cd.weights.sum(), 1)
    np.testing.assert_allclose(
        cd.portfolio_performance(),
        (0.28, 0.045999),
        rtol=1e-4,
        atol=1e-4,
    )
    cdar = cd.portfolio_performance()[1]

    ef_long_only = EfficientCDaR(*setup_efficient_cdar(data_only=True),
                                 weight_bounds=(0.0, 1.0))
    ef_long_only.efficient_return(0.26)
    long_only_cdar = ef_long_only.portfolio_performance()[1]

    assert long_only_cdar > cdar
def test_efficient_risk_market_neutral():
    cd = EfficientCDaR(*setup_efficient_cdar(data_only=True),
                       weight_bounds=(-1, 1))
    w = cd.efficient_risk(0.025, market_neutral=True)
    assert isinstance(w, dict)
    assert set(w.keys()) == set(cd.tickers)
    np.testing.assert_almost_equal(cd.weights.sum(), 0)
    assert (cd.weights < 1).all() and (cd.weights > -1).all()
    np.testing.assert_allclose(
        cd.portfolio_performance(),
        (0.219306, 0.025),
        rtol=1e-4,
        atol=1e-4,
    )
def test_cdar_example_weekly():
    beta = 0.90
    df = get_data()
    df = df.resample("W").first()
    mu = expected_returns.mean_historical_return(df, frequency=52)
    historical_rets = expected_returns.returns_from_prices(df).dropna()
    cd = EfficientCDaR(mu, historical_rets, beta=beta)
    cd.efficient_return(0.21)
    np.testing.assert_allclose(
        cd.portfolio_performance(),
        (0.21, 0.045085),
        rtol=1e-4,
        atol=1e-4,
    )

    cdar = cd.portfolio_performance()[1]
    portfolio_rets = historical_rets @ cd.weights

    cum_rets = portfolio_rets.cumsum(0)
    drawdown = cum_rets.cummax() - cum_rets

    dar_hist = drawdown.quantile(beta)
    cdar_hist = drawdown[drawdown > dar_hist].mean()
    np.testing.assert_almost_equal(cdar_hist, cdar, decimal=3)
def test_cdar_example_short():
    df = get_data()
    mu = expected_returns.mean_historical_return(df)
    historical_rets = expected_returns.returns_from_prices(df).dropna()
    cd = EfficientCDaR(
        mu,
        historical_rets,
        weight_bounds=(-1, 1),
    )
    w = cd.efficient_return(0.2, market_neutral=True)
    assert isinstance(w, dict)
    assert set(w.keys()) == set(cd.tickers)
    np.testing.assert_almost_equal(cd.weights.sum(), 0)

    np.testing.assert_allclose(
        cd.portfolio_performance(),
        (0.2, 0.047152),
        rtol=1e-4,
        atol=1e-4,
    )