pyvallocation.utils.functions module

Portfolio risk utilities (VaR, CVaR, variance, volatility).

pyvallocation.utils.functions.portfolio_cvar(w, R, p=None, confidence=None, demean=None)[source]

Computes portfolio Conditional Value-at-Risk (CVaR or Expected Shortfall).

Parameters:
  • w – Portfolio weights matrix (N, M). N=instruments, M=portfolios.

  • R – Instrument P&L or returns matrix (T, N). T=scenarios.

  • p – Scenario probability vector (T, 1). Defaults to uniform.

  • confidence – Confidence level for CVaR (e.g. 0.95 means 5% tail). Defaults to 0.95.

  • demean – If True, uses demeaned P&L. Defaults to False.

Returns:

The portfolio’s CVaR, returned as a positive float or a 1xM array.

Parameters:
  • w (np.ndarray)

  • R (np.ndarray | pd.DataFrame)

  • p (np.ndarray | None)

  • confidence (float | None)

  • demean (bool | None)

Return type:

float | np.ndarray

pyvallocation.utils.functions.portfolio_var(w, R, p=None, confidence=None, demean=None)[source]

Computes portfolio Value-at-Risk (VaR).

Parameters:
  • w – Portfolio weights matrix (N, M). N=instruments, M=portfolios.

  • R – Instrument P&L or returns matrix (T, N). T=scenarios.

  • p – Scenario probability vector (T, 1). Defaults to uniform.

  • confidence – Confidence level for VaR (e.g. 0.95 means 5% tail). Defaults to 0.95.

  • demean – If True, uses demeaned P&L. Defaults to False.

Returns:

The portfolio’s VaR, returned as a positive float or a 1xM array.

Parameters:
  • w (np.ndarray)

  • R (np.ndarray | pd.DataFrame)

  • p (np.ndarray | None)

  • confidence (float | None)

  • demean (bool | None)

Return type:

float | np.ndarray

pyvallocation.utils.functions.portfolio_variance(w, cov)[source]

Compute portfolio variance for one or many portfolios.

Parameters:
  • w – Weights vector (N,) or matrix (N, M).

  • cov – Covariance matrix (N, N).

Returns:

Scalar variance (single portfolio) or (M,) array for multiple portfolios.

Parameters:
  • w (np.ndarray | pd.Series)

  • cov (np.ndarray | pd.DataFrame)

Return type:

float | np.ndarray

pyvallocation.utils.functions.portfolio_volatility(w, cov)[source]

Compute portfolio volatility (standard deviation).

Parameters:
  • w – Weights vector (N,) or matrix (N, M).

  • cov – Covariance matrix (N, N).

Parameters:
  • w (np.ndarray | pd.Series)

  • cov (np.ndarray | pd.DataFrame)

Return type:

float | np.ndarray