Risk & Return Analysis
This page documents functions that are useful for general risk and return analysis.
- contribution_cvar(pnl, exposures, probability=None, alpha=0.95, include_mean=False, include_gross=False)
Function for computing portfolio CVaR contribution and marginals.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.exposures (
DataFrame) – Fortitudo Technologies Exposure pd.DataFrame with shape (I, P) containing portfolio exposures.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.alpha (
float) – Alpha value for computing the alpha-CVaR. Default: 0.95.include_mean (
bool) – Boolean specifying whether the portfolio mean should be included or not. Default: False.include_gross (
bool) – Option to return gross and diversification contributions and marginals. Default: False.
- Return type:
Union[Tuple[DataFrame,DataFrame],Tuple[DataFrame,DataFrame,DataFrame,DataFrame,DataFrame,DataFrame]]- Returns:
pd.DataFrames with portfolio CVaR contributions and marginal CVaR.
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If there is a mismatch between pnl and exposure dimensions.
- contribution_lsad(pnl, exposures, probability=None, include_gross=False)
Function for computing portfolio LSAD contribution and marginal LSAD.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.exposures (
DataFrame) – Fortitudo Technologies Exposure pd.DataFrame with shape (I, P) containing portfolio exposures.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.include_gross (
bool) – Option to return gross and diversification contributions and marginals. Default: False.
- Return type:
Union[Tuple[DataFrame,DataFrame],Tuple[DataFrame,DataFrame,DataFrame,DataFrame,DataFrame,DataFrame]]- Returns:
pd.DataFrames with portfolio LSAD contributions and marginals.
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If there is a mismatch between pnl and exposure dimensions.
- contribution_mean(pnl, exposures, probability=None)
Function for computing portfolio mean contribution and marginal mean.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.exposures (
DataFrame) – Fortitudo Technologies Exposure pd.DataFrame with shape (I, P) containing portfolio exposures.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.
- Return type:
Tuple[DataFrame,DataFrame]- Returns:
pd.DataFrames with portfolio mean contributions and marginal mean.
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If there is a mismatch between pnl and exposure dimensions.
- contribution_var(pnl, exposures, probability=None, alpha=0.95, include_mean=False, include_gross=False)
Function for computing portfolio VaR contribution and marginal VaR.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.exposures (
DataFrame) – Fortitudo Technologies Exposure pd.DataFrame with shape (I, P) containing portfolio exposures.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.alpha (
float) – Alpha value for computing the alpha-VaR. Default: 0.95.include_mean (
bool) – Boolean specifying whether the portfolio mean should be included or not. Default: False.include_gross (
bool) – Option to return gross and diversification contributions and marginals. Default: False.
- Return type:
Union[Tuple[DataFrame,DataFrame],Tuple[DataFrame,DataFrame,DataFrame,DataFrame,DataFrame,DataFrame]]- Returns:
pd.DataFrames with portfolio VaR contributions and marginals.
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If there is a mismatch between pnl and exposure dimensions.
- contribution_vol(pnl, exposures, probability=None, include_gross=False)
Function for computing portfolio vol contribution and marginal vol.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.exposures (
DataFrame) – Fortitudo Technologies Exposure pd.DataFrame with shape (I, P) containing portfolio exposures.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.include_gross (
bool) – Option to return gross and diversification contributions and marginals. Default: False.
- Return type:
Union[Tuple[DataFrame,DataFrame],Tuple[DataFrame,DataFrame,DataFrame,DataFrame,DataFrame,DataFrame]]- Returns:
pd.DataFrames with portfolio vol contributions and marginals.
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If there is a mismatch between pnl and exposure dimensions.
- correlation_matrix(pnl, probability=None)
Function for computing the correlation matrix.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.
- Return type:
DataFrame- Returns:
Correlation matrix with shape (I, I).
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
- covariance_matrix(pnl, probability=None)
Function for computing the covariance matrix.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.
- Return type:
DataFrame- Returns:
Covariance matrix with shape (I, I).
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
- diversification_distribution(pnl, exposures, probability=None, method=None)
Function for computing the diversification distribution as well as variance and volatility concentrations.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.exposures (
DataFrame) – Fortitudo Technologies Exposure pd.DataFrame with shape (I, P) containing portfolio exposures.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.method (
str) – Method for computing the the uncorrelated factors: {‘minimum-torsion’, ‘minimum-torsion-approximate’, ‘pca’}. Default: ‘pca’.
- Return type:
Tuple[DataFrame,DataFrame,DataFrame]- Returns:
pd.DataFrames with diversification distribution as well as variance and volatility concentrations.
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If there is a mismatch between pnl and exposure dimensions.
ValueError – If method is not in {‘minimum-torsion’, ‘minimum-torsion-approximate’, ‘pca’}.
- effective_number(probability, round=True)
Function for computing the effective number of scenarios or bets (depending on application).
- Parameters:
probability (
Union[ndarray,DataFrame]) – Probability vector for P&L / risk factors scenarios or diversification distribution.round (
bool) – Boolean indicating whether to round the effective number to an integer value or not. Default: True.
- Return type:
Union[int,float]- Returns:
Effective number of scenarios or bets.
- Raises:
ValueError – If probability is not a vector containing strictly positive elements that sum to 1.
- individual_stats(pnl, probability=None, alpha=0.95, include_mean=False)
Function for computing individual statistics of the P&L / risk factor simulation.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.alpha (
float) – Alpha value for VaR and CVaR calculations. Default: 0.95.include_mean (
bool) – Boolean indicating whether the mean should be included in VaR and CVaR computations. Default: False.
- Return type:
DataFrame- Returns:
Statistics overview with shape (I, 6) (mean, vol, skew, kurt, VaR, CVaR).
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
- portfolio_cvar(pnl, exposures, probability=None, alpha=0.95, include_mean=False)
Function for computing portfolio Conditional Value-at-Risk (CVaR).
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.exposures (
DataFrame) – Fortitudo Technologies Exposure pd.DataFrame with shape (I, P) containing portfolio exposures.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.alpha (
float) – Alpha value for computing the alpha-CVaR. Default: 0.95.include_mean (
bool) – Boolean specifying whether the portfolio mean should be included or not. Default: False.
- Return type:
Union[float,ndarray]- Returns:
Portfolio CVaR float for P=1 or np.ndarray with shape (P,) for P>1.
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If there is a mismatch between pnl and exposure dimensions.
- portfolio_lsad(pnl, exposures, probability=None)
Function for computing portfolio lower semi-absolute deviation (LSAD).
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.exposures (
DataFrame) – Fortitudo Technologies Exposure pd.DataFrame with shape (I, P) containing portfolio exposures.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.
- Return type:
Union[float,ndarray]- Returns:
Portfolio LSAD float for P=1 or np.ndarray with shape (P,) for P>1.
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If there is a mismatch between pnl and exposure dimensions.
- portfolio_mean(pnl, exposures, probability=None)
Function for computing portfolio mean.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.exposures (
DataFrame) – Fortitudo Technologies Exposure pd.DataFrame with shape (I, P) containing portfolio exposures.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.
- Return type:
Union[float,ndarray]- Returns:
Portfolio mean float for P=1 or np.ndarray with shape (P,) for P>1.
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If there is a mismatch between pnl and exposure dimensions.
- portfolio_var(pnl, exposures, probability=None, alpha=0.95, include_mean=False)
Function for computing portfolio Value-at-Risk (VaR).
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.exposures (
DataFrame) – Fortitudo Technologies Exposure pd.DataFrame with shape (I, P) containing portfolio exposures.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.alpha (
float) – Alpha value for computing the alpha-VaR. Default: 0.95.include_mean (
bool) – Variable specifying whether the portfolio mean should be included or not. Default: False.
- Return type:
Union[float,ndarray]- Returns:
Portfolio VaR float for P=1 or np.ndarray with shape (P,) for P>1.
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If there is a mismatch between pnl and exposure dimensions.
- portfolio_vol(pnl, exposures, probability=None)
Function for computing portfolio volatility.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.exposures (
DataFrame) – Fortitudo Technologies Exposure pd.DataFrame with shape (I, P) containing portfolio exposures.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.
- Return type:
Union[float,ndarray]- Returns:
Portfolio volatility float for P=1 or np.ndarray with shape (P,) for P>1.
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If there is a mismatch between pnl and exposure dimensions.
Function for computing the uncorrelated factors with minimum-torsion or PCA.
- Parameters:
pnl (
DataFrame) – pd.DataFrame with shape (S, I) containing a P&L / risk factor simulation.probability (
ndarray) – np.ndarray with shape (S,) containing the probabilites of the S scenarios in pnl. Set to np.ones(S) / S by default.method (
str) – Method for computing the the uncorrelated factors: {‘minimum-torsion’, ‘minimum-torsion-approximate’, ‘pca’}. Default: ‘pca’.
- Return type:
Tuple[ndarray,ndarray]- Returns:
Factors matrix with shape (S, I) and torsion matrix with shape (I, I).
- Raises:
ValueError – If probability is not a vector with shape (S,) containing strictly positive elements that sum to 1.
ValueError – If method is not in {‘minimum-torsion’, ‘minimum-torsion-approximate’, ‘pca’}.