Risk Functions¶
This module has functions that calculates several risk measures that are widely used by the asset management industry and academics.
Module Functions¶
- RiskFunctions.MAD(X)[source]¶
Calculate the Mean Absolute Deviation (MAD) of a returns series.
\[\text{MAD}(X) = \frac{1}{T}\sum_{t=1}^{T} | X_{t} - \mathbb{E}(X_{t}) |\]
- RiskFunctions.SemiDeviation(X)[source]¶
Calculate the Semi Deviation of a returns series.
\[\text{SemiDev}(X) = \left [ \frac{1}{T-1}\sum_{t=1}^{T} \min (X_{t} - \mathbb{E}(X_{t}), 0)^2 \right ]^{1/2}\]
- RiskFunctions.Kurtosis(X)[source]¶
Calculate the Square Root Kurtosis of a returns series.
\[\text{Kurt}(X) = \left [ \frac{1}{T}\sum_{t=1}^{T} (X_{t} - \mathbb{E}(X_{t}))^{4} \right ]^{1/2}\]
- RiskFunctions.SemiKurtosis(X)[source]¶
Calculate the Semi Square Root Kurtosis of a returns series.
\[\text{SemiKurt}(X) = \left [ \frac{1}{T}\sum_{t=1}^{T} \min (X_{t} - \mathbb{E}(X_{t}), 0)^{4} \right ]^{1/2}\]
-
RiskFunctions.EvenMoment(X, p: int =
2)[source]¶ Calculate the p-th Root of Even Moment of order 2 * p of a returns series.
\[\text{EM}_{p}(X) = \left [ \frac{1}{T}\sum_{t=1}^{T} (X_{t} - \mathbb{E}(X_{t}))^{2p} \right ]^{1/p}\]
-
RiskFunctions.EvenSemiMoment(X, p: int =
2)[source]¶ Calculate the p-th Root of Semi Even Moment of order 2 * p of a returns series.
\[\text{ESM}_{p}(X) = \left [ \frac{1}{T}\sum_{t=1}^{T} \min (X_{t} - \mathbb{E}(X_{t}), 0)^{2p} \right ]^{1/p}\]
-
RiskFunctions.VaR_Hist(X, alpha=
0.05)[source]¶ Calculate the Value at Risk (VaR) of a returns series.
\[\text{VaR}_{\alpha}(X) = -\inf_{t \in (0,T)} \left \{ X_{t} \in \mathbb{R}: F_{X}(X_{t})>\alpha \right \}\]
-
RiskFunctions.CVaR_Hist(X, alpha=
0.05)[source]¶ Calculate the Conditional Value at Risk (CVaR) of a returns series.
\[\text{CVaR}_{\alpha}(X) = \text{VaR}_{\alpha}(X) + \frac{1}{\alpha T} \sum_{t=1}^{T} \max(-X_{t} - \text{VaR}_{\alpha}(X), 0)\]
- RiskFunctions.WR(X)[source]¶
Calculate the Worst Realization (WR) or Worst Scenario of a returns series.
\[\text{WR}(X) = \max(-X)\]
-
RiskFunctions.LPM(X, MAR=
0, p=1)[source]¶ Calculate the First or Second Lower Partial Moment of a returns series.
\[\begin{split}\text{LPM}(X, \text{MAR}, 1) &= \frac{1}{T}\sum_{t=1}^{T} \max(\text{MAR} - X_{t}, 0) \\ \text{LPM}(X, \text{MAR}, 2) &= \left [ \frac{1}{T-1}\sum_{t=1}^{T} \max(\text{MAR} - X_{t}, 0)^{2} \right ]^{\frac{1}{2}} \\\end{split}\]Where:
\(\text{MAR}\) is the minimum acceptable return. \(p\) is the order of the \(\text{LPM}\).
- Parameters:¶
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – p-th Lower Partial Moment of a returns series.
- Return type:¶
-
RiskFunctions.Entropic_RM(X, z=
1, alpha=0.05)[source]¶ Calculate the Entropic Risk Measure (ERM) of a returns series.
\[\text{ERM}_{\alpha}(X) = z\ln \left (\frac{M_X(z^{-1})}{\alpha} \right )\]Where:
\(M_X(z)\) is the moment generating function of X.
- Parameters:¶
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – ERM of a returns series.
- Return type:¶
-
RiskFunctions.EVaR_Hist(X, alpha=
0.05, solver='CLARABEL')[source]¶ Calculate the Entropic Value at Risk (EVaR) of a returns series.
\[\text{EVaR}_{\alpha}(X) = \inf_{z>0} \left \{ z \ln \left (\frac{M_X(z^{-1})}{\alpha} \right ) \right \}\]Where:
\(M_X(t)\) is the moment generating function of X.
- Parameters:¶
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
(value, z) – EVaR of a returns series and value of z that minimize EVaR.
- Return type:¶
-
RiskFunctions.RLVaR_Hist(X, alpha=
0.05, kappa=0.3, solver='CLARABEL')[source]¶ Calculate the Relativistic Value at Risk (RLVaR) of a returns series. I recommend only use this function with MOSEK solver.
\[\begin{split}\text{RLVaR}^{\kappa}_{\alpha}(X) & = \left \{ \begin{array}{ll} \underset{z, t, \psi, \theta, \varepsilon, \omega}{\text{inf}} & t + z \ln_{\kappa} \left ( \frac{1}{\alpha T} \right ) + \sum^T_{i=1} \left ( \psi_{i} + \theta_{i} \right ) \\ \text{s.t.} & -X - t + \varepsilon + \omega \leq 0\\ & z \geq 0 \\ & \left ( z \left ( \frac{1+\kappa}{2\kappa} \right ), \psi_{i} \left ( \frac{1+\kappa}{\kappa} \right ), \varepsilon_{i} \right) \in \mathcal{P}_3^{1/(1+\kappa),\, \kappa/(1+\kappa)} \\ & \left ( \omega_{i}\left ( \frac{1}{1-\kappa} \right ), \theta_{i}\left ( \frac{1}{\kappa} \right), -z \left ( \frac{1}{2\kappa} \right ) \right ) \in \mathcal{P}_3^{1-\kappa,\, \kappa} \\ \end{array} \right .\end{split}\]Where:
\(\mathcal{P}_3^{\alpha,\, 1-\alpha}\) is the power cone 3D.
\(\kappa\) is the deformation parameter.
- Parameters:¶
- X : np.array¶
Returns series, must have Tx1 size.
- alpha : float, optional¶
Significance level of EVaR. The default is 0.05.
- kappa : float, optional¶
Deformation parameter of RLVaR, must be between 0 and 1. The default is 0.3.
- solver : str, optional¶
Solver available for CVXPY that supports power cone programming. Used to calculate RLVaR and RLDaR. The default value is ‘CLARABEL’.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – RLVaR of a returns series.
- Return type:¶
- RiskFunctions.MDD_Abs(X)[source]¶
Calculate the Maximum Drawdown (MDD) of a returns series using uncompounded cumulative returns.
\[\text{MDD}(X) = \max_{j \in (0,T)} \left [\max_{t \in (0,j)} \left ( \sum_{i=0}^{t}X_{i} \right ) - \sum_{i=0}^{j}X_{i} \right ]\]
- RiskFunctions.ADD_Abs(X)[source]¶
Calculate the Average Drawdown (ADD) of a returns series using uncompounded cumulative returns.
\[\text{ADD}(X) = \frac{1}{T}\sum_{j=0}^{T}\left [ \max_{t \in (0,j)} \left ( \sum_{i=0}^{t}X_{i} \right ) - \sum_{i=0}^{j}X_{i} \right ]\]
-
RiskFunctions.DaR_Abs(X, alpha=
0.05)[source]¶ Calculate the Drawdown at Risk (DaR) of a returns series using uncompounded cumulative returns.
\[\begin{split}\text{DaR}_{\alpha}(X) & = \max_{j \in (0,T)} \left \{ \text{DD}(X,j) \in \mathbb{R}: F_{\text{DD}} \left ( \text{DD}(X,j) \right )< 1-\alpha \right \} \\ \text{DD}(X,j) & = \max_{t \in (0,j)} \left ( \sum_{i=0}^{t}X_{i} \right )- \sum_{i=0}^{j}X_{i}\end{split}\]
-
RiskFunctions.CDaR_Abs(X, alpha=
0.05)[source]¶ Calculate the Conditional Drawdown at Risk (CDaR) of a returns series using uncompounded cumulative returns.
\[\text{CDaR}_{\alpha}(X) = \text{DaR}_{\alpha}(X) + \frac{1}{\alpha T} \sum_{j=0}^{T} \max \left [ \max_{t \in (0,j)} \left ( \sum_{i=0}^{t}X_{i} \right ) - \sum_{i=0}^{j}X_{i} - \text{DaR}_{\alpha}(X), 0 \right ]\]Where:
\(\text{DaR}_{\alpha}\) is the Drawdown at Risk of an uncompounded cumulated return series \(X\).
-
RiskFunctions.EDaR_Abs(X, alpha=
0.05, solver='CLARABEL')[source]¶ Calculate the Entropic Drawdown at Risk (EDaR) of a returns series using uncompounded cumulative returns.
\[\begin{split}\text{EDaR}_{\alpha}(X) & = \inf_{z>0} \left \{ z \ln \left (\frac{M_{\text{DD}(X)}(z^{-1})}{\alpha} \right ) \right \} \\ \text{DD}(X,j) & = \max_{t \in (0,j)} \left ( \sum_{i=0}^{t}X_{i} \right )- \sum_{i=0}^{j}X_{i} \\\end{split}\]- Parameters:¶
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
(value, z) – EDaR of an uncompounded cumulative returns series and value of z that minimize EDaR.
- Return type:¶
-
RiskFunctions.RLDaR_Abs(X, alpha=
0.05, kappa=0.3, solver='CLARABEL')[source]¶ Calculate the Relativistic Drawdown at Risk (RLDaR) of a returns series using uncompounded cumulative returns. I recommend only use this function with MOSEK solver.
\[\begin{split}\text{RLDaR}^{\kappa}_{\alpha}(X) & = \text{RLVaR}^{\kappa}_{\alpha}(\text{DD}(X)) \\ \text{DD}(X,j) & = \max_{t \in (0,j)} \left ( \sum_{i=0}^{t}X_{i} \right )- \sum_{i=0}^{j}X_{i} \\\end{split}\]- Parameters:¶
- X : np.array¶
Returns series, must have Tx1 size.
- alpha : float, optional¶
Significance level of EVaR. The default is 0.05.
- kappa : float, optional¶
Deformation parameter of RLDaR, must be between 0 and 1. The default is 0.3.
- solver : str, optional¶
Solver available for CVXPY that supports power cone programming. Used to calculate RLVaR, RVRG and RLDaR. The default value is ‘CLARABEL’.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – RLDaR of an uncompounded cumulative returns series.
- Return type:¶
- RiskFunctions.UCI_Abs(X)[source]¶
Calculate the Ulcer Index (UCI) of a returns series using uncompounded cumulative returns.
\[\text{UCI}(X) =\sqrt{\frac{1}{T}\sum_{j=0}^{T} \left [ \max_{t \in (0,j)} \left ( \sum_{i=0}^{t}X_{i} \right ) - \sum_{i=0}^{j}X_{i} \right ] ^2}\]
- RiskFunctions.MDD_Rel(X)[source]¶
Calculate the Maximum Drawdown (MDD) of a returns series using cumpounded cumulative returns.
\[\text{MDD}(X) = \max_{j \in (0,T)}\left[\max_{t \in (0,j)} \left ( \prod_{i=0}^{t}(1+X_{i}) \right ) - \prod_{i=0}^{j}(1+X_{i}) \right]\]
- RiskFunctions.ADD_Rel(X)[source]¶
Calculate the Average Drawdown (ADD) of a returns series using cumpounded cumulative returns.
\[\text{ADD}(X) = \frac{1}{T}\sum_{j=0}^{T} \left [ \max_{t \in (0,j)} \left ( \prod_{i=0}^{t}(1+X_{i}) \right )- \prod_{i=0}^{j}(1+X_{i}) \right ]\]
-
RiskFunctions.DaR_Rel(X, alpha=
0.05)[source]¶ Calculate the Drawdown at Risk (DaR) of a returns series using cumpounded cumulative returns.
\[\begin{split}\text{DaR}_{\alpha}(X) & = \max_{j \in (0,T)} \left \{ \text{DD}(X,j) \in \mathbb{R}: F_{\text{DD}} \left ( \text{DD}(X,j) \right )< 1 - \alpha \right \} \\ \text{DD}(X,j) & = \max_{t \in (0,j)} \left ( \prod_{i=0}^{t}(1+X_{i}) \right )- \prod_{i=0}^{j}(1+X_{i})\end{split}\]
-
RiskFunctions.CDaR_Rel(X, alpha=
0.05)[source]¶ Calculate the Conditional Drawdown at Risk (CDaR) of a returns series using cumpounded cumulative returns.
\[\text{CDaR}_{\alpha}(X) = \text{DaR}_{\alpha}(X) + \frac{1}{\alpha T} \sum_{i=0}^{T} \max \left [ \max_{t \in (0,T)} \left ( \prod_{i=0}^{t}(1+X_{i}) \right )- \prod_{i=0}^{j}(1+X_{i}) - \text{DaR}_{\alpha}(X), 0 \right ]\]Where:
\(\text{DaR}_{\alpha}\) is the Drawdown at Risk of a cumpound cumulated return series \(X\).
-
RiskFunctions.EDaR_Rel(X, alpha=
0.05, solver='CLARABEL')[source]¶ Calculate the Entropic Drawdown at Risk (EDaR) of a returns series using cumpounded cumulative returns.
\[\begin{split}\text{EDaR}_{\alpha}(X) & = \inf_{z>0} \left \{ z \ln \left (\frac{M_{\text{DD}(X)}(z^{-1})}{\alpha} \right ) \right \} \\ \text{DD}(X,j) & = \max_{t \in (0,j)} \left ( \prod_{i=0}^{t}(1+X_{i}) \right )- \prod_{i=0}^{j}(1+X_{i})\end{split}\]- Parameters:¶
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
(value, z) – EDaR of a cumpounded cumulative returns series and value of z that minimize EDaR.
- Return type:¶
-
RiskFunctions.RLDaR_Rel(X, alpha=
0.05, kappa=0.3, solver='CLARABEL')[source]¶ Calculate the Relativistic Drawdown at Risk (RLDaR) of a returns series using compounded cumulative returns. I recommend only use this function with MOSEK solver.
\[\begin{split}\text{RLDaR}^{\kappa}_{\alpha}(X) & = \text{RLVaR}^{\kappa}_{\alpha}(\text{DD}(X)) \\ \text{DD}(X,j) & = \max_{t \in (0,j)} \left ( \prod_{i=0}^{t}(1+X_{i}) \right )- \prod_{i=0}^{j}(1+X_{i}) \\\end{split}\]- Parameters:¶
- X : np.array¶
Returns series, must have Tx1 size.
- alpha : float, optional¶
Significance level of RLDaR. The default is 0.05.
- kappa : float, optional¶
Deformation parameter of RLDaR, must be between 0 and 1. The default is 0.3.
- solver : str, optional¶
Solver available for CVXPY that supports power cone programming. Used to calculate RLVaR, RVRG and RLDaR. The default value is ‘CLARABEL’.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – RLDaR of a compounded cumulative returns series.
- Return type:¶
- RiskFunctions.UCI_Rel(X)[source]¶
Calculate the Ulcer Index (UCI) of a returns series using cumpounded cumulative returns.
\[\text{UCI}(X) =\sqrt{\frac{1}{T}\sum_{j=0}^{T} \left [ \max_{t \in (0,j)} \left ( \prod_{i=0}^{t}(1+X_{i}) \right )- \prod_{i=0}^{j} (1+X_{i}) \right ] ^2}\]
-
RiskFunctions.TG(X, alpha=
0.05, a_sim=100)[source]¶ Calculate the Tail Gini of a returns series.
- Parameters:¶
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Ulcer Index of a cumpounded cumulative returns.
- Return type:¶
-
RiskFunctions.VRG(X, alpha=
0.05, beta=None)[source]¶ Calculate the VaR range of a returns series.
- Parameters:¶
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Ulcer Index of a cumpounded cumulative returns.
- Return type:¶
-
RiskFunctions.CVRG(X, alpha=
0.05, beta=None)[source]¶ Calculate the CVaR range of a returns series.
- Parameters:¶
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Ulcer Index of a cumpounded cumulative returns.
- Return type:¶
-
RiskFunctions.TGRG(X, alpha=
0.05, a_sim=100, beta=None, b_sim=None)[source]¶ Calculate the Tail Gini range of a returns series.
- Parameters:¶
- X : np.array¶
Returns series, must have Tx1 size.
- alpha : float, optional¶
Significance level of Tail Gini of losses. The default is 0.05.
- a_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of losses. The default is 100.
- beta : float, optional¶
Significance level of Tail Gini of gains. If None it duplicates alpha value. The default is None.
- b_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of gains. If None it duplicates a_sim value. The default is None.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Ulcer Index of a cumpounded cumulative returns.
- Return type:¶
-
RiskFunctions.EVRG(X, alpha=
0.05, beta=None, solver='CLARABEL')[source]¶ Calculate the CVaR range of a returns series.
- Parameters:¶
- X : np.array¶
Returns series, must have Tx1 size.
- alpha : float, optional¶
Significance level of EVaR of losses. The default is 0.05.
- beta : float, optional¶
Significance level of EVaR of gains. If None it duplicates alpha value. The default is None.
- solver : str, optional¶
Solver available for CVXPY that supports exponential cone programming. Used to calculate EVaR, EVRG and EDaR. The default value is ‘CLARABEL’.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Ulcer Index of a cumpounded cumulative returns.
- Return type:¶
-
RiskFunctions.RVRG(X, alpha=
0.05, beta=None, kappa=0.3, kappa_g=None, solver='CLARABEL')[source]¶ Calculate the CVaR range of a returns series.
- Parameters:¶
- X : np.array¶
Returns series, must have Tx1 size.
- alpha : float, optional¶
Significance level of RLVaR of losses. The default is 0.05.
- beta : float, optional¶
Significance level of RLVaR of gains. If None it duplicates alpha value. The default is None.
- kappa : float, optional¶
Deformation parameter of RLVaR for losses, must be between 0 and 1. The default is 0.3.
- kappa_g : float, optional¶
Deformation parameter of RLVaR for gains, must be between 0 and 1. The default is None.
- solver : str, optional¶
Solver available for CVXPY that supports power cone programming. Used to calculate EVaR, EVRG and EDaR. The default value is ‘CLARABEL’.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Ulcer Index of a cumpounded cumulative returns.
- Return type:¶
-
RiskFunctions.L_Moment(X, k=
2)[source]¶ Calculate the kth l-moment of a returns series.
Where $y_{[i]}$ is the ith-ordered statistic.
-
RiskFunctions.L_Moment_CRM(X, k=
4, method='MSD', g=0.5, max_phi=0.5, solver='CLARABEL')[source]¶ Calculate a custom convex risk measure that is a weighted average of first k-th l-moments.
- Parameters:¶
- X : np.array¶
Returns series, must have Tx1 size.
- k : int¶
Order of the l-moment. Must be an integer higher or equal than 2.
- method : str, optional¶
Method to calculate the weights used to combine the l-moments with order higher than 2. The default value is ‘MSD’. Possible values are:
’CRRA’: Normalized Constant Relative Risk Aversion coefficients.
’ME’: Maximum Entropy.
’MSS’: Minimum Sum Squares.
’MSD’: Minimum Square Distance.
- g : float, optional¶
Risk aversion coefficient of CRRA utility function. The default is 0.5.
- max_phi : float, optional¶
Maximum weight constraint of L-moments. The default is 0.5.
- solver : str, optional¶
Solver available for CVXPY. Used to calculate ‘ME’, ‘MSS’ and ‘MSD’ weights. The default value is None.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Custom convex risk measure that is a weighted average of first k-th l-moments of a returns series.
- Return type:¶
- RiskFunctions.NEA(w)[source]¶
Calculate the number of effective assets (NEA) that is the inverse of the Herfindahl Hirschman index (HHI).
-
RiskFunctions.Sharpe_Risk(returns, w=
None, cov=None, rm='MV', rf=0, alpha=0.05, a_sim=100, beta=None, b_sim=None, kappa=0.3, kappa_g=None, p_em=2, p_esm=2, solver='CLARABEL')[source]¶ Calculate the risk measure available on the Sharpe function.
- Parameters:¶
- w : DataFrame or np.array of shape (n_assets, 1)¶
Weights matrix, where n_assets is the number of assets.
- cov : DataFrame of shape (n_assets, n_assets)¶
Covariance matrix, where n_assets is the number of assets.
- returns : DataFrame or nd-array of shape (n_samples, n_features)¶
Features matrix, where n_samples is the number of samples and n_features is the number of features.
- rm : str, optional¶
Risk measure used in the denominator of the ratio. The default is ‘MV’. Possible values are:
’MV’: Standard Deviation.
’KT’: Square Root Kurtosis.
’EM’: Even Moment of order 2 * p_em.
’MAD’: Mean Absolute Deviation.
’GMD’: Gini Mean Difference.
’MSV’: Semi Standard Deviation.
’SKT’: Square Root Semi Kurtosis.
’ESM’: Even Semi Moment of order 2 * p_em.
’FLPM’: First Lower Partial Moment (Omega Ratio).
’SLPM’: Second Lower Partial Moment (Sortino Ratio).
’VaR’: Value at Risk.
’CVaR’: Conditional Value at Risk.
’TG’: Tail Gini.
’EVaR’: Entropic Value at Risk.
’RLVaR’: Relativistic Value at Risk. I recommend only use this function with MOSEK solver.
’WR’: Worst Realization (Minimax).
’RG’: Range of returns.
’VRG’ VaR range of returns.
’CVRG’: CVaR range of returns.
’TGRG’: Tail Gini range of returns.
’EVRG’: EVaR range of returns.
’RVRG’: RLVaR range of returns. I recommend only use this function with MOSEK solver.
’MDD’: Maximum Drawdown of uncompounded cumulative returns (Calmar Ratio).
’ADD’: Average Drawdown of uncompounded cumulative returns.
’DaR’: Drawdown at Risk of uncompounded cumulative returns.
’CDaR’: Conditional Drawdown at Risk of uncompounded cumulative returns.
’EDaR’: Entropic Drawdown at Risk of uncompounded cumulative returns.
’RLDaR’: Relativistic Drawdown at Risk of uncompounded cumulative returns. I recommend only use this risk measure with MOSEK solver.
’UCI’: Ulcer Index of uncompounded cumulative returns.
’MDD_Rel’: Maximum Drawdown of compounded cumulative returns (Calmar Ratio).
’ADD_Rel’: Average Drawdown of compounded cumulative returns.
’DaR_Rel’: Drawdown at Risk of compounded cumulative returns.
’CDaR_Rel’: Conditional Drawdown at Risk of compounded cumulative returns.
’EDaR_Rel’: Entropic Drawdown at Risk of compounded cumulative returns.
’RLDaR_Rel’: Relativistic Drawdown at Risk of compounded cumulative returns. I recommend only use this risk measure with MOSEK solver.
’UCI_Rel’: Ulcer Index of compounded cumulative returns.
- rf : float, optional¶
Risk free rate. The default is 0.
- alpha : float, optional¶
Significance level of VaR, CVaR, EVaR, RLVaR, DaR, CDaR, EDaR, RLDaR and Tail Gini of losses. The default is 0.05.
- a_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of losses. The default is 100.
- beta : float, optional¶
Significance level of CVaR and Tail Gini of gains. If None it duplicates alpha value. The default is None.
- b_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of gains. If None it duplicates a_sim value. The default is None.
- kappa : float, optional¶
Deformation parameter of RLVaR and RLDaR for losses, must be between 0 and 1. The default is 0.3.
- kappa_g : float, optional¶
Deformation parameter of RLVaR and RLDaR for gains, must be between 0 and 1. The default is None.
- p_em : int, optional¶
Order of the Even Moment of order 2 * p_em. It must be an integer higher equal than 2. The default value is 2.
- p_esm : int, optional¶
Order of the Even Semi Moment of order 2 * p_esm. It must be an integer higher equal than 2. The default value is 2.
- solver : str, optional¶
Solver available for CVXPY that supports exponential and power cone programming. Used to calculate RLVaR and RLDaR. The default value is ‘CLARABEL’.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Risk measure of the portfolio.
- Return type:¶
-
RiskFunctions.Sharpe(returns, w=
None, mu=None, cov=None, rm='MV', rf=0, alpha=0.05, a_sim=100, beta=None, b_sim=None, kappa=0.3, kappa_g=None, p_em=2, p_esm=2, solver='CLARABEL')[source]¶ Calculate the Risk Adjusted Return Ratio from a portfolio returns series.
\[\text{Sharpe}(X) = \frac{\mathbb{E}(X) - r_{f}}{\phi(X)}\]Where:
\(X\) is the vector of portfolio returns.
\(r_{f}\) is the risk free rate, when the risk measure is
\(\text{LPM}\) uses instead of \(r_{f}\) the \(\text{MAR}\).
\(\phi(X)\) is a convex risk measure. The risk measures availabe are:
- Parameters:¶
- returns : DataFrame or nd-array of shape (n_samples, n_features)¶
Features matrix, where n_samples is the number of samples and n_features is the number of features.
- w : DataFrame or np.array of shape (n_assets, 1)¶
Weights matrix, where n_assets is the number of assets.
- mu : DataFrame or nd-array of shape (1, n_assets)¶
Vector of expected returns, where n_assets is the number of assets.
- cov : DataFrame of shape (n_assets, n_assets)¶
Covariance matrix, where n_assets is the number of assets.
- rm : str, optional¶
Risk measure used in the denominator of the ratio. The default is ‘MV’. Possible values are:
’MV’: Standard Deviation.
’KT’: Square Root Kurtosis.
’EM’: Even Moment of order 2 * p_em.
’MAD’: Mean Absolute Deviation.
’GMD’: Gini Mean Difference.
’MSV’: Semi Standard Deviation.
’SKT’: Square Root Semi Kurtosis.
’ESM’: Even Semi Moment of order 2 * p_em.
’FLPM’: First Lower Partial Moment (Omega Ratio).
’SLPM’: Second Lower Partial Moment (Sortino Ratio).
’VaR’: Value at Risk.
’CVaR’: Conditional Value at Risk.
’TG’: Tail Gini.
’EVaR’: Entropic Value at Risk.
’RLVaR’: Relativistic Value at Risk. I recommend only use this function with MOSEK solver.
’WR’: Worst Realization (Minimax).
’RG’: Range of returns.
’VRG’ VaR range of returns.
’CVRG’: CVaR range of returns.
’TGRG’: Tail Gini range of returns.
’EVRG’: EVaR range of returns.
’RVRG’: RLVaR range of returns. I recommend only use this function with MOSEK solver.
’MDD’: Maximum Drawdown of uncompounded cumulative returns (Calmar Ratio).
’ADD’: Average Drawdown of uncompounded cumulative returns.
’DaR’: Drawdown at Risk of uncompounded cumulative returns.
’CDaR’: Conditional Drawdown at Risk of uncompounded cumulative returns.
’EDaR’: Entropic Drawdown at Risk of uncompounded cumulative returns.
’RLDaR’: Relativistic Drawdown at Risk of uncompounded cumulative returns. I recommend only use this function with MOSEK solver.
’UCI’: Ulcer Index of uncompounded cumulative returns.
’MDD_Rel’: Maximum Drawdown of compounded cumulative returns (Calmar Ratio).
’ADD_Rel’: Average Drawdown of compounded cumulative returns.
’DaR_Rel’: Drawdown at Risk of compounded cumulative returns.
’CDaR_Rel’: Conditional Drawdown at Risk of compounded cumulative returns.
’EDaR_Rel’: Entropic Drawdown at Risk of compounded cumulative returns.
’RLDaR_Rel’: Relativistic Drawdown at Risk of compounded cumulative returns. I recommend only use this function with MOSEK solver.
’UCI_Rel’: Ulcer Index of compounded cumulative returns.
- rf : float, optional¶
Risk free rate. The default is 0.
- alpha : float, optional¶
Significance level of VaR, CVaR, EVaR, RLVaR, DaR, CDaR, EDaR, RLDaR and Tail Gini of losses. The default is 0.05.
- a_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of losses. The default is 100.
- beta : float, optional¶
Significance level of CVaR and Tail Gini of gains. If None it duplicates alpha value. The default is None.
- b_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of gains. If None it duplicates a_sim value. The default is None.
- kappa : float, optional¶
Deformation parameter of RLVaR and RLDaR for losses, must be between 0 and 1. The default is 0.3.
- kappa_g : float, optional¶
Deformation parameter of RLVaR and RLDaR for gains, must be between 0 and 1. The default is None.
- p_em : int, optional¶
Order of the Even Moment of order 2 * p_em. It must be an integer higher equal than 2. The default value is 2.
- p_esm : int, optional¶
Order of the Even Semi Moment of order 2 * p_esm. It must be an integer higher equal than 2. The default value is 2.
- solver : str, optional¶
Solver available for CVXPY that supports power cone programming. Used to calculate RLVaR and RLDaR. The default value is None.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Risk adjusted return ratio of \(X\).
- Return type:¶
-
RiskFunctions.Risk_Contribution(w, returns, cov=
None, rm='MV', rf=0, alpha=0.05, a_sim=100, beta=None, b_sim=None, kappa=0.3, kappa_g=None, p_em=2, p_esm=2, solver='CLARABEL')[source]¶ Calculate the risk contribution for each asset based on the selected risk measure.
- Parameters:¶
- w : DataFrame or Series of shape (n_assets, 1)¶
Portfolio weights, where n_assets is the number of assets.
- returns : DataFrame or nd-array of shape (n_samples, n_features)¶
Features matrix, where n_samples is the number of samples and n_features is the number of features.
- cov : DataFrame of shape (n_assets, n_assets)¶
Covariance matrix, where n_assets is the number of assets.
- rm : str, optional¶
Risk measure used in the denominator of the ratio. The default is ‘MV’. Possible values are:
’MV’: Standard Deviation.
’KT’: Square Root Kurtosis.
’EM’: Even Moment of order 2 * p_em.
’MAD’: Mean Absolute Deviation.
’GMD’: Gini Mean Difference.
’MSV’: Semi Standard Deviation.
’SKT’: Square Root Semi Kurtosis.
’ESM’: Even Semi Moment of order 2 * p_esm.
’FLPM’: First Lower Partial Moment (Omega Ratio).
’SLPM’: Second Lower Partial Moment (Sortino Ratio).
’VaR’: Value at Risk.
’CVaR’: Conditional Value at Risk.
’TG’: Tail Gini.
’EVaR’: Entropic Value at Risk.
’RLVaR’: Relativistic Value at Risk. I recommend only use this function with MOSEK solver.
’WR’: Worst Realization (Minimax).
’RG’: Range of returns.
’VRG’ VaR range of returns.
’CVRG’: CVaR range of returns.
’TGRG’: Tail Gini range of returns.
’EVRG’: EVaR range of returns.
’RVRG’: RLVaR range of returns. I recommend only use this function with MOSEK solver.
’MDD’: Maximum Drawdown of uncompounded cumulative returns (Calmar Ratio).
’ADD’: Average Drawdown of uncompounded cumulative returns.
’DaR’: Drawdown at Risk of uncompounded cumulative returns.
’CDaR’: Conditional Drawdown at Risk of uncompounded cumulative returns.
’EDaR’: Entropic Drawdown at Risk of uncompounded cumulative returns.
’RLDaR’: Relativistic Drawdown at Risk of uncompounded cumulative returns. I recommend only use this function with MOSEK solver.
’UCI’: Ulcer Index of uncompounded cumulative returns.
’MDD_Rel’: Maximum Drawdown of compounded cumulative returns (Calmar Ratio).
’ADD_Rel’: Average Drawdown of compounded cumulative returns.
’CDaR_Rel’: Conditional Drawdown at Risk of compounded cumulative returns.
’EDaR_Rel’: Entropic Drawdown at Risk of compounded cumulative returns.
’RLDaR_Rel’: Relativistic Drawdown at Risk of compounded cumulative returns. I recommend only use this function with MOSEK solver.
’UCI_Rel’: Ulcer Index of compounded cumulative returns.
- rf : float, optional¶
Risk free rate. The default is 0.
- alpha : float, optional¶
Significance level of VaR, CVaR, EVaR, RLVaR, DaR, CDaR, EDaR, RLDaR and Tail Gini of losses. The default is 0.05.
- a_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of losses. The default is 100.
- beta : float, optional¶
Significance level of CVaR and Tail Gini of gains. If None it duplicates alpha value. The default is None.
- b_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of gains. If None it duplicates a_sim value. The default is None.
- kappa : float, optional¶
Deformation parameter of RLVaR and RLDaR for losses, must be between 0 and 1. The default is 0.3.
- kappa_g : float, optional¶
Deformation parameter of RLVaR and RLDaR for gains, must be between 0 and 1. The default is None.
- p_em : int, optional¶
Order of the Even Moment of order 2 * p_em. It must be an integer higher equal than 2. The default value is 2.
- p_esm : int, optional¶
Order of the Even Semi Moment of order 2 * p_esm. It must be an integer higher equal than 2. The default value is 2.
- solver : str, optional¶
Solver available for CVXPY that supports power cone programming. Used to calculate RLVaR and RLDaR. The default value is None.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Risk measure of the portfolio.
- Return type:¶
-
RiskFunctions.Risk_Margin(w, returns, cov=
None, rm='MV', rf=0, alpha=0.05, a_sim=100, beta=None, b_sim=None, kappa=0.3, kappa_g=None, p_em=2, p_esm=2, solver='CLARABEL')[source]¶ Calculate the risk margin for each asset based on the risk measure selected.
- Parameters:¶
- w : DataFrame or Series of shape (n_assets, 1)¶
Portfolio weights, where n_assets is the number of assets.
- returns : DataFrame or nd-array of shape (n_samples, n_features)¶
Features matrix, where n_samples is the number of samples and n_features is the number of features.
- cov : DataFrame of shape (n_assets, n_assets)¶
Covariance matrix, where n_assets is the number of assets.
- rm : str, optional¶
Risk measure used in the denominator of the ratio. The default is ‘MV’. Possible values are:
’MV’: Standard Deviation.
’KT’: Square Root Kurtosis.
’EM’: Even Moment of order 2 * p_em.
’MAD’: Mean Absolute Deviation.
’GMD’: Gini Mean Difference.
’MSV’: Semi Standard Deviation.
’SKT’: Square Root Semi Kurtosis.
’ESM’: Even Semi Moment of order 2 * p_esm.
’FLPM’: First Lower Partial Moment (Omega Ratio).
’SLPM’: Second Lower Partial Moment (Sortino Ratio).
’VaR’: Value at Risk.
’CVaR’: Conditional Value at Risk.
’TG’: Tail Gini.
’EVaR’: Entropic Value at Risk.
’RLVaR’: Relativistic Value at Risk. I recommend only use this function with MOSEK solver.
’WR’: Worst Realization (Minimax).
’RG’: Range of returns.
’VRG’ VaR range of returns.
’CVRG’: CVaR range of returns.
’TGRG’: Tail Gini range of returns.
’EVRG’: EVaR range of returns.
’RVRG’: RLVaR range of returns.
’MDD’: Maximum Drawdown of uncompounded cumulative returns (Calmar Ratio).
’ADD’: Average Drawdown of uncompounded cumulative returns.
’DaR’: Drawdown at Risk of uncompounded cumulative returns.
’CDaR’: Conditional Drawdown at Risk of uncompounded cumulative returns.
’EDaR’: Entropic Drawdown at Risk of uncompounded cumulative returns.
’RLDaR’: Relativistic Drawdown at Risk of uncompounded cumulative returns. I recommend only use this function with MOSEK solver.
’UCI’: Ulcer Index of uncompounded cumulative returns.
’MDD_Rel’: Maximum Drawdown of compounded cumulative returns (Calmar Ratio).
’ADD_Rel’: Average Drawdown of compounded cumulative returns.
’CDaR_Rel’: Conditional Drawdown at Risk of compounded cumulative returns.
’EDaR_Rel’: Entropic Drawdown at Risk of compounded cumulative returns.
’RLDaR_Rel’: Relativistic Drawdown at Risk of compounded cumulative returns. I recommend only use this function with MOSEK solver.
’UCI_Rel’: Ulcer Index of compounded cumulative returns.
- rf : float, optional¶
Risk free rate. The default is 0.
- alpha : float, optional¶
Significance level of VaR, CVaR, EVaR, RLVaR, DaR, CDaR, EDaR, RLDaR and Tail Gini of losses. The default is 0.05.
- a_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of losses. The default is 100.
- beta : float, optional¶
Significance level of VaR, CVaR, Tail Gini, EVaR and RLVaR of gains. If None it duplicates alpha value. The default is None.
- b_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of gains. If None it duplicates a_sim value. The default is None.
- kappa : float, optional¶
Deformation parameter of RLVaR and RLDaR for losses, must be between 0 and 1. The default is 0.3.
- kappa_g : float, optional¶
Deformation parameter of RLVaR and RLDaR for gains, must be between 0 and 1. The default is None.
- p_em : int, optional¶
Order of the Even Moment of order 2 * p_em. It must be an integer higher equal than 2. The default value is 2.
- p_esm : int, optional¶
Order of the Even Semi Moment of order 2 * p_esm. It must be an integer higher equal than 2. The default value is 2.
- solver : str, optional¶
Solver available for CVXPY that supports exponential and power cone programming. Used to calculate EVaR, EVRG, EDaR, RLVaR, RVRG and RLDaR. The default value is None.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Risk margin of the portfolio.
- Return type:¶
-
RiskFunctions.Factors_Risk_Contribution(w, returns, factors, cov=
None, B=None, const=False, rm='MV', rf=0, alpha=0.05, a_sim=100, beta=None, b_sim=None, kappa=0.3, kappa_g=None, p_em=2, p_esm=2, solver='CLARABEL', feature_selection='stepwise', stepwise='Forward', criterion='pvalue', threshold=0.05, n_components=0.95)[source]¶ Calculate the risk contribution for each factor based on the selected risk measure.
- Parameters:¶
- w : DataFrame or Series of shape (n_assets, 1)¶
Portfolio weights, where n_assets is the number of assets.
- returns : DataFrame or nd-array of shape (n_samples, n_features)¶
Features matrix, where n_samples is the number of samples and n_features is the number of features.
- factors : DataFrame or nd-array of shape (n_samples, n_factors)¶
Factors matrix, where n_samples is the number of samples and n_factors is the number of factors.
- cov : DataFrame of shape (n_assets, n_assets)¶
Covariance matrix, where n_assets is the number of assets.
- B : DataFrame of shape (n_assets, n_factors), optional¶
Loadings matrix, where n_assets is the number assets and n_factors is the number of risk factors. If is not specified, is estimated using stepwise regression. The default is None.
- const : bool, optional¶
Indicate if the loadings matrix has a constant. The default is False.
- rm : str, optional¶
Risk measure used in the denominator of the ratio. The default is ‘MV’. Possible values are:
’MV’: Standard Deviation.
’KT’: Square Root Kurtosis.
’EM’: Even Moment of order 2 * p_em.
’MAD’: Mean Absolute Deviation.
’GMD’: Gini Mean Difference.
’MSV’: Semi Standard Deviation.
’SKT’: Square Root Semi Kurtosis.
’ESM’: Even Semi Moment of order 2 * p_esm.
’FLPM’: First Lower Partial Moment (Omega Ratio).
’SLPM’: Second Lower Partial Moment (Sortino Ratio).
’VaR’: Value at Risk.
’CVaR’: Conditional Value at Risk.
’TG’: Tail Gini.
’EVaR’: Entropic Value at Risk.
’RLVaR’: Relativistic Value at Risk. I recommend only use this function with MOSEK solver.
’WR’: Worst Realization (Minimax).
’RG’: Range of returns.
’VRG’ VaR range of returns.
’CVRG’: CVaR range of returns.
’TGRG’: Tail Gini range of returns.
’EVRG’: EVaR range of returns.
’RVRG’: RLVaR range of returns. I recommend only use this function with MOSEK solver.
’MDD’: Maximum Drawdown of uncompounded cumulative returns (Calmar Ratio).
’ADD’: Average Drawdown of uncompounded cumulative returns.
’DaR’: Drawdown at Risk of uncompounded cumulative returns.
’CDaR’: Conditional Drawdown at Risk of uncompounded cumulative returns.
’EDaR’: Entropic Drawdown at Risk of uncompounded cumulative returns.
’RLDaR’: Relativistic Drawdown at Risk of uncompounded cumulative returns. I recommend only use this function with MOSEK solver.
’UCI’: Ulcer Index of uncompounded cumulative returns.
’MDD_Rel’: Maximum Drawdown of compounded cumulative returns (Calmar Ratio).
’ADD_Rel’: Average Drawdown of compounded cumulative returns.
’CDaR_Rel’: Conditional Drawdown at Risk of compounded cumulative returns.
’EDaR_Rel’: Entropic Drawdown at Risk of compounded cumulative returns.
’RLDaR_Rel’: Relativistic Drawdown at Risk of compounded cumulative returns. I recommend only use this function with MOSEK solver.
’UCI_Rel’: Ulcer Index of compounded cumulative returns.
- rf : float, optional¶
Risk free rate. The default is 0.
- alpha : float, optional¶
Significance level of VaR, CVaR, EVaR, RLVaR, DaR, CDaR, EDaR, RLDaR and Tail Gini of losses. The default is 0.05.
- a_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of losses. The default is 100.
- beta : float, optional¶
Significance level of CVaR and Tail Gini of gains. If None it duplicates alpha value. The default is None.
- b_sim : float, optional¶
Number of CVaRs used to approximate Tail Gini of gains. If None it duplicates a_sim value. The default is None.
- kappa : float, optional¶
Deformation parameter of RLVaR and RLDaR for losses, must be between 0 and 1. The default is 0.3.
- kappa_g : float, optional¶
Deformation parameter of RLVaR and RLDaR for gains, must be between 0 and 1. The default is None.
- p_em : int, optional¶
Order of the Even Moment of order 2 * p_em. It must be an integer higher equal than 2. The default value is 2.
- p_esm : int, optional¶
Order of the Even Semi Moment of order 2 * p_esm. It must be an integer higher equal than 2. The default value is 2.
- solver : str, optional¶
Solver available for CVXPY that supports power cone programming. Used to calculate RLVaR and RLDaR. The default value is None.
- feature_selection : str 'stepwise' or 'PCR', optional¶
Indicate the method used to estimate the loadings matrix. The default is ‘stepwise’.
- stepwise : str 'Forward' or 'Backward', optional¶
Indicate the method used for stepwise regression. The default is ‘Forward’.
- criterion : str, optional¶
The default is ‘pvalue’. Possible values of the criterion used to select the best features are:
’pvalue’: select the features based on p-values.
’AIC’: select the features based on lowest Akaike Information Criterion.
’SIC’: select the features based on lowest Schwarz Information Criterion.
’R2’: select the features based on highest R Squared.
’R2_A’: select the features based on highest Adjusted R Squared.
- threshold : scalar, optional¶
Is the maximum p-value for each variable that will be accepted in the model. The default is 0.05.
- n_components : int, float, None or str, optional¶
if 1 < n_components (int), it represents the number of components that will be keep. if 0 < n_components < 1 (float), it represents the percentage of variance that the is explained by the components kept. See PCA for more details. The default is 0.95.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
value – Risk measure of the portfolio.
- Return type:¶
-
RiskFunctions.BrinsonAttribution(prices, w, wb, start, end, asset_classes, classes_col, method=
'nearest')[source]¶ Creates a DataFrame with the Brinson Performance Attribution per class and aggregate based on [F1].
- Parameters:¶
- prices : DataFrame of shape (n_samples, n_assets)¶
Assets prices DataFrame, where n_samples is the number of observations and n_assets is the number of assets.
- w : DataFrame or Series of shape (n_assets, 1)¶
A portfolio specified by the user.
- wb : DataFrame or Series of shape (n_assets, 1)¶
A benchmark specified by the user.
- start : str¶
Start date in format ‘YYYY-MM-DD’ specified by the user.
- end : str¶
End date in format ‘YYYY-MM-DD’ specified by the user.
- asset_classes : DataFrame of shape (n_assets, n_cols)¶
Asset’s classes DataFrame, where n_assets is the number of assets and n_cols is the number of columns of the DataFrame where the first column is the asset list and the next columns are the different asset’s classes sets. It is only used when kind value is ‘classes’. The default value is None.
- classes_col : str or int¶
If value is str, it is the column name of the set of classes from asset_classes dataframe. If value is int, it is the column number of the set of classes from asset_classes dataframe. The default value is None.
- method : str¶
Method used to calculate the nearest start or end dates in case one of them is not in prices DataFrame. The default value is ‘nearest’. See get_indexer for more details.
- Raises:¶
ValueError – When the value cannot be calculated.
- Returns:¶
BrinAttr (DataFrame) – A DataFrame with the Brinson Performance Attribution per class and aggregate.
(start_, end_) (tuple) – Start and end dates calculated using get_indexer method in string format.
Example
BrinAttr, (start, end) = BrinsonAttribution( prices=data, w=w, wb=wb, start='2019-01-07', end='2019-12-06', asset_classes=asset_classes, classes_col='Industry', )
Bibliography
Gary P Brinson and Nimrod Fachler. Measuring non-US. equity portfolio performance. J. Portf. Manag., 11(3):73–76, 04 1985.