Skip to content

Downside Beta

Intuição

O Downside Beta mede a sensibilidade do ativo ao benchmark apenas em períodos em que o benchmark está em queda (retorno negativo). É uma proxy de risco assimétrico.

Definição

Em uma janela n, usando apenas observações com r_b < 0:

beta_down = Cov(r_a, r_b | r_b < 0) / Var(r_b | r_b < 0)

Uso

from quantmaster.features.statistical import downside_beta

beta_d = downside_beta(df, benchmark_series, window=60)

API

Source code in src/quantmaster/features/statistical.py
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
def downside_beta(
    data: pd.DataFrame | pd.Series,
    benchmark: pd.Series,
    *,
    window: int = 60,
    price_col: str = "close",
    log_returns: bool = True,
) -> pd.Series:
    window = validate_positive_int(window, name="window")

    asset_price = get_price_series(data, price_col=price_col).astype(float)
    bench_price = pd.to_numeric(benchmark, errors="coerce").astype(float)

    if log_returns:
        r_asset = np.log(asset_price.where(asset_price > 0)).diff()
        r_bench = np.log(bench_price.where(bench_price > 0)).diff()
    else:
        r_asset = asset_price.pct_change()
        r_bench = bench_price.pct_change()

    df = pd.concat([r_asset.rename("asset"), r_bench.rename("bench")], axis=1)
    out = pd.Series(np.nan, index=df.index, dtype=float)
    out.name = f"downside_beta_{window}"

    if len(df) < window:
        return out

    x = df["asset"].to_numpy(dtype=float)
    y = df["bench"].to_numpy(dtype=float)

    xw = np.lib.stride_tricks.sliding_window_view(x, window_shape=window)
    yw = np.lib.stride_tricks.sliding_window_view(y, window_shape=window)

    beta_arr = np.full(xw.shape[0], np.nan, dtype=float)
    for i in range(xw.shape[0]):
        mask = np.isfinite(xw[i]) & np.isfinite(yw[i]) & (yw[i] < 0.0)
        if mask.sum() < 2:
            continue
        beta_arr[i] = _beta_from_windows(xw[i][mask], yw[i][mask])

    out.iloc[window - 1 :] = beta_arr
    return out