distributions¶
Functions for analyzing the statistical distribution of portfolio returns. These metrics summarize return characteristics, assess normality, measure the frequency and magnitude of gains and losses, and provide data for tables and visualizations.
distributions ¶
Descriptive statistics and shape of the return distribution.
describe ¶
Standard descriptive statistics (count, mean, std, min/max, quartiles) plus skew and kurtosis, using pandas' bias-adjusted Fisher-Pearson sample estimators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Periodic returns. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
A Series indexed by statistic name (count, mean, std, min, 25%, |
Series
|
50%, 75%, max, skew, kurtosis). |
Source code in src/portpy/metrics/distributions.py
normality_test ¶
Jarque-Bera test of whether returns look Normally distributed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Periodic returns. Requires at least 8 observations. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dict with "statistic" (the JB test statistic), "p_value", and |
dict
|
"is_normal" (True if p_value > 0.05). |
Source code in src/portpy/metrics/distributions.py
best_worst_periods ¶
The n best and n worst individual periods, each as a Series sorted from most extreme to least extreme.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Periodic returns. |
required |
n
|
int
|
Number of best/worst periods to return. |
5
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict with keys "best" and "worst", each a Series of the top-n |
dict
|
returns. |
Source code in src/portpy/metrics/distributions.py
win_rate ¶
Fraction of periods with a strictly positive return.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Periodic returns. |
required |
as_result
|
bool
|
If True, return a |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The win rate, or a |
Source code in src/portpy/metrics/distributions.py
win_loss_ratio ¶
Average winning period's return divided by the average (absolute) losing period's return. Zero-return periods count toward neither bucket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Periodic returns. |
required |
as_result
|
bool
|
If True, return a |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The win/loss ratio, or a |
float | MetricResult
|
are no wins or no losses. |
Source code in src/portpy/metrics/distributions.py
positive_periods_pct ¶
Alias of win_rate, kept for API compatibility with the common naming
convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Periodic returns. |
required |
as_result
|
bool
|
If True, return a |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The fraction of positive periods, or a |
Source code in src/portpy/metrics/distributions.py
monthly_returns_table ¶
Calendar table of compounded returns: rows are years, columns are Jan-Dec, plus a Year total column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Periodic returns, finer than monthly (e.g. daily/weekly). |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
A DataFrame indexed by year with one column per calendar month |
DataFrame
|
(compounded return) plus a "Year" column (compounded annual return). |
Source code in src/portpy/metrics/distributions.py
return_histogram_data ¶
Histogram of the return distribution, computed via numpy.histogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Periodic returns. |
required |
bins
|
int
|
Number of histogram bins. |
50
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
A tuple of (counts, bin_edges). |