covariance¶
Multi-asset, portfolio-level risk. This is the one category with no equivalent in empyrical or quantstats - both operate on a single, already-aggregated return stream, never a weighted book of assets.
covariance ¶
Covariance/correlation matrices and portfolio-level risk decomposition.
covariance_matrix ¶
covariance_matrix(
returns: DataFrame,
annualized: bool = False,
periods_per_year: int = TRADING_DAYS_PER_YEAR,
) -> pd.DataFrame
Sample covariance matrix of asset returns (ddof=1), optionally annualized by linear scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
DataFrame
|
Periodic returns, one column per asset. |
required |
annualized
|
bool
|
If True, scale the covariance matrix by |
False
|
periods_per_year
|
int
|
Number of return periods per year, used when
|
TRADING_DAYS_PER_YEAR
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The (optionally annualized) covariance matrix, indexed and columned |
DataFrame
|
by asset. |
Source code in src/portpy/metrics/covariance.py
correlation_matrix ¶
Pairwise Pearson correlation matrix of asset returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
DataFrame
|
Periodic returns, one column per asset. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The correlation matrix, indexed and columned by asset. |
Source code in src/portpy/metrics/covariance.py
portfolio_variance ¶
portfolio_variance(
weights: Series | ndarray,
cov_matrix: DataFrame,
as_result: bool = False,
) -> float | MetricResult
Portfolio return variance: w^T @ Cov @ w.
The result's time-scale (periodic vs. annualized) matches whatever
cov_matrix was passed in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Series | ndarray
|
Portfolio weights, one per asset. |
required |
cov_matrix
|
DataFrame
|
Covariance matrix of asset returns, columns matching
the assets in |
required |
as_result
|
bool
|
If True, return a |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The portfolio variance, or a |
Source code in src/portpy/metrics/covariance.py
portfolio_volatility ¶
portfolio_volatility(
weights: Series | ndarray,
cov_matrix: DataFrame,
as_result: bool = False,
) -> float | MetricResult
Portfolio return standard deviation: sqrt(w^T @ Cov @ w).
The result's time-scale (periodic vs. annualized) matches whatever
cov_matrix was passed in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Series | ndarray
|
Portfolio weights, one per asset. |
required |
cov_matrix
|
DataFrame
|
Covariance matrix of asset returns, columns matching
the assets in |
required |
as_result
|
bool
|
If True, return a |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The portfolio volatility, or a |
Source code in src/portpy/metrics/covariance.py
diversification_ratio ¶
diversification_ratio(
weights: Series | ndarray,
cov_matrix: DataFrame,
as_result: bool = False,
) -> float | MetricResult
Weighted-average standalone asset volatility divided by actual portfolio volatility - a measure of how much diversification is buying you.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Series | ndarray
|
Portfolio weights, one per asset. |
required |
cov_matrix
|
DataFrame
|
Covariance matrix of asset returns, columns matching
the assets in |
required |
as_result
|
bool
|
If True, return a |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The diversification ratio, or a |
float | MetricResult
|
portfolio volatility is ~0. |
Source code in src/portpy/metrics/covariance.py
marginal_contribution_to_risk ¶
Marginal Contribution to Risk (MCTR): how much portfolio volatility changes per unit change in each asset's weight.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Series | ndarray
|
Portfolio weights, one per asset. |
required |
cov_matrix
|
DataFrame
|
Covariance matrix of asset returns, columns matching
the assets in |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
An array of MCTR values, one per asset, in |
Source code in src/portpy/metrics/covariance.py
component_contribution_to_risk ¶
Component Contribution to Risk (CCTR): each asset's slice of total
portfolio volatility, computed as weight_i * MCTR_i. Components sum
exactly to portfolio volatility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Series | ndarray
|
Portfolio weights, one per asset. |
required |
cov_matrix
|
DataFrame
|
Covariance matrix of asset returns, columns matching
the assets in |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
An array of CCTR values, one per asset, in |