Utils Functions

Functions on this page are small helpful functions that are not supported by standard Python data science libraries but can be very useful when working with risk modeling and Monte Carlo simulation.

correlation_matrix(R, probability=None)

Function for computing the correlation matrix.

Parameters:
  • R (ndarray) – matrix 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 R. Set to np.ones(S) / S by default.

Return type:

ndarray

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(R, probability=None)

Function for computing the covariance matrix.

Parameters:
  • R (ndarray) – matrix 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 R. Set to np.ones(S) / S by default.

Return type:

ndarray

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.

exp_decay_prob(T, half_life)

Function for computing exponential decay probabilities.

Parameters:
  • T (int) – Number of historical observations.

  • half_life (int) – Half life for the probabilities.

Return type:

ndarray

Returns:

Exponentially decaying probabilities vector with shape (T, 1).

uncorrelated_factors(R, probability=None, method=None)

Function for computing the uncorrelated factors with minimum-torsion or PCA.

Parameters:
  • R (ndarray) – matrix with shape (S, I) containing a P&L / risk factor simulation.

  • probability (ndarray) – vector with shape (S,) containing the probabilites of the S scenarios in R. 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’}.

weighted_percentile(data, percentile, p=None)

Function for computing weighted percentiles.

Parameters:
  • data (ndarray) – Vector of values with shape (N, 1) or (N,).

  • percentile (float) – Desired percentile in (0, 1).

  • p (ndarray) – Vector of value probabilities with shape (N,). Set to np.ones(N) / N by default.

Return type:

float

Returns:

Desired percentile.

Raises:
  • ValueError – If data does not have shape (N, 1) or (N,)

  • ValueError – If probability is not a vector with shape (N,) containing strictly positive elements that sum to 1.

  • ValueError – If percentile is not in (0, 1).