Risk Functions

https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86
Buy Me a Coffee at ko-fi.com

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}) |\]
Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Returns:

value – MAD of a returns series.

Return type:

float

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}\]
Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Semi Deviation of a returns series.

Return type:

float

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}\]
Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Square Root Kurtosis of a returns series.

Return type:

float

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}\]
Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Semi Square Root Kurtosis of a returns series.

Return type:

float

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 \}\]
Parameters:
  • X (1d-array) – Returns series, must have Tx1 size.

  • alpha (float, optional) – Significance level of VaR. The default is 0.05.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – VaR of a returns series.

Return type:

float

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)\]
Parameters:
  • X (1d-array) – Returns series, must have Tx1 size.

  • alpha (float, optional) – Significance level of CVaR. The default is 0.05.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – CVaR of a returns series.

Return type:

float

RiskFunctions.WR(X)[source]

Calculate the Worst Realization (WR) or Worst Scenario of a returns series.

\[\text{WR}(X) = \max(-X)\]
Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – WR of a returns series.

Return type:

float

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:
  • X (1d-array) – Returns series, must have Tx1 size.

  • MAR (float, optional) – Minimum acceptable return. The default is 0.

  • p (float, optional can be {1,2}) – order of the \(\text{LPM}\). The default is 1.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – p-th Lower Partial Moment of a returns series.

Return type:

float

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:
  • X (1d-array) – Returns series, must have Tx1 size.

  • theta (float, optional) – Risk aversion parameter, must be greater than zero. The default is 1.

  • alpha (float, optional) – Significance level of EVaR. The default is 0.05.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – ERM of a returns series.

Return type:

float

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:
  • X (1d-array) – Returns series, must have Tx1 size.

  • alpha (float, optional) – Significance level of EVaR. The default is 0.05.

  • solver (str, optional) – Solver available for CVXPY that supports exponential cone programming. Used to calculate EVaR and EDaR. The default value is ‘CLARABEL’.

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:

tuple

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{split}\]

Where:

\(\mathcal{P}_3^{\alpha,\, 1-\alpha}\) is the power cone 3D.

\(\kappa\) is the deformation parameter.

Parameters:
  • X (1d-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:

tuple

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 ]\]
Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – MDD of an uncompounded cumulative returns.

Return type:

float

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 ]\]
Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – ADD of an uncompounded cumulative returns.

Return type:

float

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}\]
Parameters:
  • X (1d-array) – Returns series, must have Tx1 size..

  • alpha (float, optional) – Significance level of DaR. The default is 0.05.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – DaR of an uncompounded cumulative returns series.

Return type:

float

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\).

Parameters:
  • X (1d-array) – Returns series, must have Tx1 size..

  • alpha (float, optional) – Significance level of CDaR. The default is 0.05.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – CDaR of an uncompounded cumulative returns series.

Return type:

float

RiskFunctions.EDaR_Abs(X, alpha=0.05)[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:
  • X (1d-array) – Returns series, must have Tx1 size..

  • alpha (float, optional) – Significance level of EDaR. The default is 0.05.

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:

tuple

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}(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 (1d-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 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:

tuple

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}\]
Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Ulcer Index of an uncompounded cumulative returns.

Return type:

float

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]\]
Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – MDD of a cumpounded cumulative returns.

Return type:

float

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 ]\]
Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – ADD of a cumpounded cumulative returns.

Return type:

float

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}\]
Parameters:
  • X (1d-array) – Returns series, must have Tx1 size..

  • alpha (float, optional) – Significance level of DaR. The default is 0.05.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – DaR of a cumpounded cumulative returns series.

Return type:

float

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\).

Parameters:
  • X (1d-array) – Returns series, must have Tx1 size..

  • alpha (float, optional) – Significance level of CDaR. The default is 0.05.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – CDaR of a cumpounded cumulative returns series.

Return type:

float

RiskFunctions.EDaR_Rel(X, alpha=0.05)[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:
  • X (1d-array) – Returns series, must have Tx1 size..

  • alpha (float, optional) – Significance level of EDaR. The default is 0.05.

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:

tuple

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}(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 (1d-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 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:

tuple

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}\]
Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Ulcer Index of a cumpounded cumulative returns.

Return type:

float

RiskFunctions.GMD(X)[source]

Calculate the Gini Mean Difference (GMD) of a returns series.

Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Gini Mean Difference of a returns series.

Return type:

float

RiskFunctions.TG(X, alpha=0.05, a_sim=100)[source]

Calculate the Tail Gini of a returns series.

Parameters:
  • X (1d-array) – Returns series, must have Tx1 size.

  • alpha (float, optional) – Significance level of Tail Gini. The default is 0.05.

  • a_sim (float, optional) – Number of CVaRs used to approximate Tail Gini. The default is 100.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Ulcer Index of a cumpounded cumulative returns.

Return type:

float

RiskFunctions.RG(X)[source]

Calculate the range of a returns series.

Parameters:

X (1d-array) – Returns series, must have Tx1 size.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Ulcer Index of a cumpounded cumulative returns.

Return type:

float

RiskFunctions.CVRG(X, alpha=0.05, beta=None)[source]

Calculate the CVaR range of a returns series.

Parameters:
  • X (1d-array) – Returns series, must have Tx1 size.

  • alpha (float, optional) – Significance level of CVaR of losses. The default is 0.05.

  • beta (float, optional) – Significance level of CVaR of gains. If None it duplicates alpha value. The default is None.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Ulcer Index of a cumpounded cumulative returns.

Return type:

float

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 (1d-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:

float

RiskFunctions.L_Moment(X, k=2)[source]

Calculate the kth l-moment of a returns series.

Where $y_{[i]}$ is the ith-ordered statistic.

Parameters:
  • X (1d-array) – Returns series, must have Tx1 size.

  • k (int) – Order of the l-moment. Must be an integer higher or equal than 1.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Kth l-moment of a returns series.

Return type:

float

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 (1d-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:

float

RiskFunctions.Sharpe_Risk(w, cov=None, returns=None, rm='MV', rf=0, alpha=0.05, a_sim=100, beta=None, b_sim=None, kappa=0.3, solver='CLARABEL')[source]

Calculate the risk measure available on the Sharpe function.

Parameters:
  • w (DataFrame or 1d-array of shape (n_assets, 1)) – Weights matrix, where n_assets is the number of assets.

  • cov (DataFrame or nd-array of shape (n_features, n_features)) – Covariance matrix, where n_features is the number of features.

  • 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.

    • ’MAD’: Mean Absolute Deviation.

    • ’GMD’: Gini Mean Difference.

    • ’MSV’: Semi Standard Deviation.

    • ’SKT’: Square Root Semi Kurtosis.

    • ’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.

    • ’CVRG’: CVaR range of returns.

    • ’TGRG’: Tail Gini 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 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.

    • ’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, 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 – Risk measure of the portfolio.

Return type:

float

RiskFunctions.Sharpe(w, mu, cov=None, returns=None, rm='MV', rf=0, alpha=0.05, a_sim=100, beta=None, b_sim=None, kappa=0.3, 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:
  • w (DataFrame or 1d-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 or nd-array of shape (n_features, n_features)) – Covariance matrix, where n_features is the number of features.

  • 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.

    • ’MAD’: Mean Absolute Deviation.

    • ’GMD’: Gini Mean Difference.

    • ’MSV’: Semi Standard Deviation.

    • ’SKT’: Square Root Semi Kurtosis.

    • ’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.

    • ’CVRG’: CVaR range of returns.

    • ’TGRG’: Tail Gini 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 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, 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 None.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Risk adjusted return ratio of \(X\).

Return type:

float

RiskFunctions.Risk_Contribution(w, cov=None, returns=None, rm='MV', rf=0, alpha=0.05, a_sim=100, beta=None, b_sim=None, kappa=0.3, solver='CLARABEL')[source]

Calculate the risk contribution for each asset based on the risk measure selected.

Parameters:
  • w (DataFrame or 1d-array of shape (n_assets, 1)) – Weights matrix, where n_assets is the number of assets.

  • cov (DataFrame or nd-array of shape (n_features, n_features)) – Covariance matrix, where n_features is the number of features.

  • 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.

    • ’MAD’: Mean Absolute Deviation.

    • ’GMD’: Gini Mean Difference.

    • ’MSV’: Semi Standard Deviation.

    • ’SKT’: Square Root Semi Kurtosis.

    • ’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.

    • ’CVRG’: CVaR range of returns.

    • ’TGRG’: Tail Gini 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 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, 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 None.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Risk measure of the portfolio.

Return type:

float

RiskFunctions.Risk_Margin(w, cov=None, returns=None, rm='MV', rf=0, alpha=0.05, a_sim=100, beta=None, b_sim=None, kappa=0.3, solver='CLARABEL')[source]

Calculate the risk margin for each asset based on the risk measure selected.

Parameters:
  • w (DataFrame or 1d-array of shape (n_assets, 1)) – Weights matrix, where n_assets is the number of assets.

  • cov (DataFrame or nd-array of shape (n_features, n_features)) – Covariance matrix, where n_features is the number of features.

  • 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.

    • ’MAD’: Mean Absolute Deviation.

    • ’GMD’: Gini Mean Difference.

    • ’MSV’: Semi Standard Deviation.

    • ’SKT’: Square Root Semi Kurtosis.

    • ’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.

    • ’CVRG’: CVaR range of returns.

    • ’TGRG’: Tail Gini 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 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, 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 None.

Raises:

ValueError – When the value cannot be calculated.

Returns:

value – Risk margin of the portfolio.

Return type:

float

RiskFunctions.Factors_Risk_Contribution(w, cov=None, returns=None, factors=None, B=None, const=False, rm='MV', rf=0, alpha=0.05, a_sim=100, beta=None, b_sim=None, kappa=0.3, 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 risk measure selected.

Parameters:
  • w (DataFrame or 1d-array of shape (n_assets, 1)) – Weights matrix, where n_assets is the number of assets.

  • cov (DataFrame or nd-array of shape (n_features, n_features)) – Covariance matrix, where n_features is the number of features.

  • 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.

  • B (DataFrame of shape (n_assets, n_features), optional) – Loadings matrix. 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.

    • ’MAD’: Mean Absolute Deviation.

    • ’GMD’: Gini Mean Difference.

    • ’MSV’: Semi Standard Deviation.

    • ’SKT’: Square Root Semi Kurtosis.

    • ’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.

    • ’CVRG’: CVaR range of returns.

    • ’TGRG’: Tail Gini 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 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, 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 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:

float