sf_quant.research.beta_scale_ports#
- sf_quant.research.beta_scale_ports(df: DataFrame, market_col: str = 'bmk_return', target_beta: float = 1.0, lookback: int = 60) DataFrame#
Scale portfolio returns to a target beta relative to a market benchmark in place.
This function computes rolling beta estimates for each portfolio relative to a market benchmark, then scales the portfolios to achieve a specified target beta. Beta is calculated using rolling covariance and variance over a specified lookback window. Original return columns are replaced with their beta-scaled versions.
Parameters#
- dfpl.DataFrame
Portfolio returns data containing:
Portfolio columns starting with
p_or namedspread.A market benchmark column specified by
market_col.
- market_colstr, optional
Name of the column containing market benchmark returns (default: “bmk_return”).
- target_betafloat, optional
Target beta level for scaled portfolios (default: 1.0). A value of 1.0 means the portfolio moves in line with the market.
- lookbackint, optional
Rolling window size in days for beta calculation (default: 60). Typically set to 2-3 months of trading days.
Returns#
- pl.DataFrame
The DataFrame with beta-scaled return columns:
p_1,p_2, …,p_{num_bins}(float): Beta-scaled returns.Other columns remain unchanged.
Notes#
Beta = rolling_cov(portfolio, market) / rolling_var(market)
Scaling factor = target_beta / estimated_beta
Only columns starting with
p_or namedspreadare scaled.Original return values are replaced with scaled versions.
Early rows (< lookback window) will have null values.
Examples#
>>> import polars as pl >>> import sf_quant.research as sfr >>> import datetime as dt >>> ports = pl.DataFrame({ ... 'date': [dt.date(2024, 1, i) for i in range(1, 100)], ... 'p_1': [0.01] * 99, ... 'p_10': [0.02] * 99, ... 'spread': [0.01] * 99, ... 'bmk_return': [0.015] * 99 ... }) >>> beta_scaled = sfr.beta_scale_ports(ports, target_beta=0.5, lookback=30) >>> beta_scaled.columns ['date', 'p_1', 'p_10', 'spread', 'bmk_return']