returns¶
Functions for computing, transforming, and summarizing portfolio returns. These metrics cover the full return workflow, from converting prices into returns and rebuilding price series to calculating cumulative, annualized, excess, and benchmark-relative performance.
returns ¶
Return transformations and return-based summary statistics.
Convention: every scalar function accepts as_result=True to get back a portpy.explain.MetricResult (a float that also knows how to explain itself) instead of a plain float. Annualization everywhere in PortPy uses the period-count convention (years = n_periods / periods_per_year), matching empyrical/quantstats defaults, rather than actual elapsed calendar time.
simple_returns ¶
Period-over-period simple (arithmetic) returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
Series | DataFrame
|
Price series or DataFrame of price columns. |
required |
Returns:
| Type | Description |
|---|---|
Series | DataFrame
|
Simple returns aligned to |
Source code in src/portpy/metrics/returns.py
log_returns ¶
Period-over-period continuously-compounded (log) returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
Series | DataFrame
|
Price series or DataFrame of price columns. |
required |
Returns:
| Type | Description |
|---|---|
Series | DataFrame
|
Log returns aligned to |
Source code in src/portpy/metrics/returns.py
cumulative_returns ¶
Compounded cumulative return series from a series of simple returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series | DataFrame
|
Simple per-period returns. |
required |
Returns:
| Type | Description |
|---|---|
Series | DataFrame
|
Cumulative return at each period, e.g. 0.25 for a 25% total gain. |
Source code in src/portpy/metrics/returns.py
prices_from_returns ¶
Reconstruct a price-like series from simple returns, anchored at base
one period before the first return.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series | DataFrame
|
Simple per-period returns. |
required |
base
|
float
|
Starting price level. Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
Series | DataFrame
|
A price series (or DataFrame) one period longer than |
Series | DataFrame
|
first value is |
Source code in src/portpy/metrics/returns.py
total_return ¶
Total compounded return over the full return series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Per-period simple returns. |
required |
as_result
|
bool
|
If True, return a MetricResult instead of a plain float. |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The total compounded return, e.g. 0.35 for a 35% gain. |
Source code in src/portpy/metrics/returns.py
annualized_return ¶
annualized_return(
returns: Series,
periods_per_year: int = TRADING_DAYS_PER_YEAR,
geometric: bool = True,
as_result: bool = False,
) -> float | MetricResult
Annualized return computed from a return series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Per-period simple returns. |
required |
periods_per_year
|
int
|
Number of periods in a year, used to annualize. |
TRADING_DAYS_PER_YEAR
|
geometric
|
bool
|
If True (default), compounds the total return and raises it to periods_per_year / n_periods (the CAGR of the series). If False, scales the arithmetic mean return by periods_per_year. |
True
|
as_result
|
bool
|
If True, return a MetricResult instead of a plain float. |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The annualized return. When geometric and the compounded growth |
float | MetricResult
|
factor is non-positive (a total loss or worse), returns -1.0. |
Source code in src/portpy/metrics/returns.py
cagr ¶
cagr(
prices: Series,
periods_per_year: int = TRADING_DAYS_PER_YEAR,
as_result: bool = False,
) -> float | MetricResult
Compound Annual Growth Rate, computed directly from a price series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
Series
|
Price series spanning the measurement period. |
required |
periods_per_year
|
int
|
Number of periods in a year, used to annualize. |
TRADING_DAYS_PER_YEAR
|
as_result
|
bool
|
If True, return a MetricResult instead of a plain float. |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The CAGR. Returns -1.0 if the start/end price ratio is non-positive, |
float | MetricResult
|
rather than raising. |
Source code in src/portpy/metrics/returns.py
average_return ¶
average_return(
returns: Series,
geometric: bool = False,
as_result: bool = False,
) -> float | MetricResult
Average return per period, arithmetic (default) or geometric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Per-period simple returns. |
required |
geometric
|
bool
|
If True, computes exp(mean(log1p(returns))) - 1 instead of the arithmetic mean. Defaults to arithmetic. |
False
|
as_result
|
bool
|
If True, return a MetricResult instead of a plain float. |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The average per-period return. |
Source code in src/portpy/metrics/returns.py
rebased_returns ¶
Rescale a price series so it starts at base, preserving percentage changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
Series | DataFrame
|
Price series or DataFrame of price columns. |
required |
base
|
float
|
Starting value for the rescaled series. Defaults to 100. |
100.0
|
Returns:
| Type | Description |
|---|---|
Series | DataFrame
|
Rescaled price series, same shape as |
Source code in src/portpy/metrics/returns.py
excess_returns ¶
Per-period return in excess of a benchmark series or a constant rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Per-period simple returns. |
required |
benchmark_or_rf
|
Series | float
|
A benchmark return series (aligned by date) or a constant per-period rate to subtract. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Per-period excess returns. |
Source code in src/portpy/metrics/returns.py
active_returns ¶
Per-period return relative to a benchmark series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Per-period simple returns. |
required |
benchmark
|
Series
|
Benchmark per-period simple returns. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Per-period active (portfolio minus benchmark) returns. |