marginal_riskΒΆ

Defined in fynance.portfolio.attribution

marginal_risk(w, sigma)[source]

Marginal contribution of each asset to portfolio volatility.

Compute the gradient of portfolio volatility with respect to weights: \(\partial \sigma_p / \partial w = (\Sigma w) / \sigma_p\), where \(\sigma_p = \sqrt{w^\top \Sigma w}\) is the portfolio volatility.

Parameters:
warray_like

Portfolio weights, shape (N,) or (N, 1).

sigmaarray_like

Symmetric (N, N) covariance matrix.

Returns:
np.ndarray

Marginal risk per asset, shape (N,). If portfolio volatility is zero, returns zeros.

Examples

>>> import numpy as np
>>> w = np.array([0.6, 0.4])
>>> sigma = np.array([[0.04, 0.0], [0.0, 0.01]])
>>> mr = marginal_risk(w, sigma)
>>> mr.shape
(2,)
>>> bool(np.all(mr > 0))
True