rolling¶
rolling_metric / expanding_metric are generic engines - they roll any PortPy metric function (or your own) over a window. The rest are convenience wrappers over the same engine.
rolling ¶
Rolling and expanding-window metric engines.
rolling_metric / expanding_metric are the generic engines the more specific helpers below are built on - use them directly to roll any PortPy metric function (or your own) over a window.
rolling_metric ¶
rolling_metric(
returns: Series,
func: Callable[..., float],
window: int,
min_periods: int | None = None,
**kwargs: Any,
) -> pd.Series
Apply any Series -> float metric function over a rolling window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Per-period returns. |
required |
func
|
Callable[..., float]
|
A callable taking a pd.Series window (plus **kwargs) and returning a float - any PortPy metric function works directly (pass as_result=False, the default). |
required |
window
|
int
|
Window size, in periods. |
required |
min_periods
|
int | None
|
Minimum observations required to produce a value.
Defaults to |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed through to |
{}
|
Returns:
| Type | Description |
|---|---|
Series
|
A Series of |
Source code in src/portpy/metrics/rolling.py
rolling_sharpe ¶
rolling_sharpe(
returns: Series,
window: int,
rf: float = DEFAULT_RISK_FREE_RATE,
periods_per_year: int = TRADING_DAYS_PER_YEAR,
) -> pd.Series
Sharpe ratio recomputed over a trailing window.
Shows whether risk-adjusted performance is stable or decaying.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Per-period returns. |
required |
window
|
int
|
Window size, in periods. |
required |
rf
|
float
|
Risk-free rate, as an annual rate. Defaults to DEFAULT_RISK_FREE_RATE. |
DEFAULT_RISK_FREE_RATE
|
periods_per_year
|
int
|
Number of periods in a year, used to annualize. |
TRADING_DAYS_PER_YEAR
|
Returns:
| Type | Description |
|---|---|
Series
|
A Series of rolling Sharpe ratios, named "rolling_sharpe". |
Source code in src/portpy/metrics/rolling.py
rolling_volatility ¶
rolling_volatility(
returns: Series,
window: int,
annualized: bool = True,
periods_per_year: int = TRADING_DAYS_PER_YEAR,
) -> pd.Series
Standard deviation recomputed over a trailing window; reveals volatility clustering and regime shifts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Per-period returns. |
required |
window
|
int
|
Window size, in periods. |
required |
annualized
|
bool
|
If True (default), scale by sqrt(periods_per_year). |
True
|
periods_per_year
|
int
|
Number of periods in a year, used to annualize. |
TRADING_DAYS_PER_YEAR
|
Returns:
| Type | Description |
|---|---|
Series
|
A Series of rolling volatility, named "rolling_volatility". |
Source code in src/portpy/metrics/rolling.py
rolling_beta ¶
Beta recomputed over a trailing window; reveals whether market sensitivity is stable over time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Per-period returns. |
required |
benchmark
|
Series
|
Per-period benchmark returns. |
required |
window
|
int
|
Window size, in periods. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
A Series of rolling beta, named "rolling_beta". |
Source code in src/portpy/metrics/rolling.py
rolling_correlation ¶
Pearson correlation recomputed over a trailing window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns_a
|
Series
|
First per-period return series. |
required |
returns_b
|
Series
|
Second per-period return series. |
required |
window
|
int
|
Window size, in periods. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
A Series of rolling correlation, named "rolling_correlation". |
Source code in src/portpy/metrics/rolling.py
expanding_metric ¶
expanding_metric(
returns: Series,
func: Callable[..., float],
min_periods: int = 2,
**kwargs: Any,
) -> pd.Series
Apply any Series -> float metric function over an expanding (growing) window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Per-period returns. |
required |
func
|
Callable[..., float]
|
A callable taking a pd.Series window (plus **kwargs) and returning a float. |
required |
min_periods
|
int
|
Minimum observations required to produce a value. Defaults to 2. |
2
|
**kwargs
|
Any
|
Additional keyword arguments passed through to |
{}
|
Returns:
| Type | Description |
|---|---|
Series
|
A Series of |