Financial and Risk Management Toolbox: Methods guide

Mathematical formulations of portfolio optimization, covariance PSD projection, Value-at-Risk, Student-t tail modeling, Kupiec backtesting, and Euler risk attribution.

← Overview · Getting started · Modeling guide · Methods guide · Examples · Reference

Mathematical Formulations

This guide provides the complete mathematical specifications for all optimization routines, matrix conditioning algorithms, down-tail risk metrics, backtesting hypothesis tests, and risk attribution methods implemented in the Financial and Risk Management Toolbox.


1. Mean-Variance Portfolio Optimization

Let \(n\) denote the number of assets in the investment universe. Define: - Decision weight vector: \(\mathbf{w} = (w_1, w_2, \dots, w_n)^T \in \mathbb{R}^n\). - Expected return vector: \(\boldsymbol{\mu} = (\mu_1, \mu_2, \dots, \mu_n)^T \in \mathbb{R}^n\). - Covariance matrix: \(\boldsymbol{\Sigma} \in \mathbb{R}^{n \times n}\), symmetric and positive semi-definite (\(\boldsymbol{\Sigma} \succeq 0\)). - Risk-free rate: \(r_f \in \mathbb{R}\). - Box constraints: \(\mathbf{l} \le \mathbf{w} \le \mathbf{u}\), where \(\mathbf{l}, \mathbf{u} \in \mathbb{R}^n\).

The portfolio expected return \(\mu_p\) and portfolio variance \(\sigma_p^2\) are given by: \[\mu_p(\mathbf{w}) = \mathbf{w}^T \boldsymbol{\mu} = \sum_{i=1}^n w_i \mu_i\] \[\sigma_p(\mathbf{w}) = \sqrt{\mathbf{w}^T \boldsymbol{\Sigma} \mathbf{w}} = \sqrt{\sum_{i=1}^n \sum_{j=1}^n w_i w_j \Sigma_{ij}}\]

Task 1: Maximum Sharpe Ratio Allocation (portfolio_opt_sharpe)

The Sharpe ratio measures excess return per unit of portfolio standard deviation: \[\text{SR}(\mathbf{w}) = \frac{\mathbf{w}^T \boldsymbol{\mu} - r_f}{\sqrt{\mathbf{w}^T \boldsymbol{\Sigma} \mathbf{w}}}\]

The optimization problem solves: \[\begin{aligned} \max_{\mathbf{w}} \quad & \frac{\mathbf{w}^T \boldsymbol{\mu} - r_f}{\sqrt{\mathbf{w}^T \boldsymbol{\Sigma} \mathbf{w}}} \\ \text{subject to} \quad & \sum_{i=1}^n w_i = 1 \\ & l_i \le w_i \le u_i, \quad \forall i = 1, \dots, n \end{aligned}\]

In the numerical implementation, SciPy’s Sequential Least Squares Programming (SLSQP) minimizes the negative Sharpe ratio objective: \[f_{\text{obj}}(\mathbf{w}) = -\frac{\mathbf{w}^T \boldsymbol{\mu} - r_f}{\max\left(\sqrt{\mathbf{w}^T \boldsymbol{\Sigma} \mathbf{w}}, 10^{-6}\right)}\]

Task 2: Global Minimum Variance Portfolio (portfolio_min_variance)

The global minimum variance (GMV) portfolio minimizes total portfolio risk irrespective of expected return: \[\begin{aligned} \min_{\mathbf{w}} \quad & \frac{1}{2} \mathbf{w}^T \boldsymbol{\Sigma} \mathbf{w} \quad \left(\text{or equivalently } \sqrt{\mathbf{w}^T \boldsymbol{\Sigma} \mathbf{w}}\right) \\ \text{subject to} \quad & \sum_{i=1}^n w_i = 1 \\ & l_i \le w_i \le u_i, \quad \forall i = 1, \dots, n \end{aligned}\]

Because \(\boldsymbol{\Sigma} \succeq 0\), this is a strictly convex quadratic programming problem with a unique global minimizer under non-degenerate constraints.

Task 3: Target Return Efficient Portfolio (portfolio_target_return)

Target return optimization constructs the lowest-volatility portfolio that achieves or exceeds a specified hurdle rate \(r_{\text{target}}\): \[\begin{aligned} \min_{\mathbf{w}} \quad & \sqrt{\mathbf{w}^T \boldsymbol{\Sigma} \mathbf{w}} \\ \text{subject to} \quad & \mathbf{w}^T \boldsymbol{\mu} \ge r_{\text{target}} \\ & \sum_{i=1}^n w_i = 1 \\ & l_i \le w_i \le u_i, \quad \forall i = 1, \dots, n \end{aligned}\]


2. Covariance Matrix Construction & Positive Semi-Definite Projection

The covariance matrix \(\boldsymbol{\Sigma}\) is synthesized from user-specified asset volatilities \(\boldsymbol{\sigma} = (\sigma_1, \dots, \sigma_n)^T\) and the correlation matrix \(\mathbf{C}\): \[\Sigma_{ij} = \sigma_i \sigma_j C_{ij} \iff \boldsymbol{\Sigma} = \mathbf{D} \mathbf{C} \mathbf{D}, \quad \text{where } \mathbf{D} = \text{diag}(\boldsymbol{\sigma})\]

Symmetry and PSD Preservation

User-edited correlation matrices can fail to be positive semi-definite due to manual edits or inconsistent pairwise correlations. The toolbox applies an automated spectral decomposition and clipping procedure:

  1. Symmetrization: \[\mathbf{C}_{\text{sym}} = \frac{1}{2} \left( \mathbf{C} + \mathbf{C}^T \right)\]
  2. Spectral Eigendecomposition: \[\mathbf{C}_{\text{sym}} = \mathbf{V} \boldsymbol{\Lambda} \mathbf{V}^T, \quad \boldsymbol{\Lambda} = \text{diag}(\lambda_1, \lambda_2, \dots, \lambda_n)\] where \(\mathbf{V}\) is an orthogonal matrix of eigenvectors and \(\lambda_i\) are the eigenvalues.
  3. Eigenvalue Clipping: If \(\min_i(\lambda_i) < 10^{-7}\), the eigenvalues are projected onto the positive semi-definite cone: \[\lambda_i^+ = \max(\lambda_i, 10^{-7}), \quad \boldsymbol{\Lambda}^+ = \text{diag}(\lambda_1^+, \dots, \lambda_n^+)\] \[\mathbf{C}^* = \mathbf{V} \boldsymbol{\Lambda}^+ \mathbf{V}^T\]
  4. Diagonal Normalization: To restore unit diagonal elements (\(C_{ii} = 1\)): \[\widetilde{C}_{ij} = \frac{C^*_{ij}}{\sqrt{C^*_{ii} C^*_{jj}}}, \quad \text{with } \widetilde{C}_{ii} = 1.0\]
  5. Covariance Assembly: \[\boldsymbol{\Sigma} = \mathbf{D} \widetilde{\mathbf{C}} \mathbf{D}\]

The diagnostics engine monitors the matrix condition number \(\kappa(\boldsymbol{\Sigma}) = \lambda_{\max} / \lambda_{\min}\) and flags a warning if eigenvalue clipping was required.


3. Down-Tail Risk Metrics: Value-at-Risk and Expected Shortfall

Let \(\Delta t = h / 252\) denote the evaluation time horizon scaled in annual units (where \(h\) is horizon in trading days). Let \(\alpha \in (0, 1)\) denote the confidence level (e.g., \(0.95\) or \(0.99\)).

Parametric Gaussian Value-at-Risk (VaR)

Assuming portfolio returns are normally distributed \(r_p \sim \mathcal{N}(\mu_p \Delta t, \sigma_p^2 \Delta t)\), the parametric Value-at-Risk represents the maximum expected loss at probability \(1 - \alpha\): \[\text{VaR}_\alpha^{\text{Gauss}} = \sigma_p \sqrt{\Delta t} \, \Phi^{-1}(\alpha) - \mu_p \Delta t\] where \(\Phi^{-1}(\cdot)\) is the standard normal quantile function (e.g., \(\Phi^{-1}(0.95) \approx 1.6449\), \(\Phi^{-1}(0.99) \approx 2.3263\)).

Expected Shortfall (Conditional Value-at-Risk, CVaR)

Expected Shortfall measures the expected loss conditional on the loss exceeding the VaR threshold: \[\text{ES}_\alpha = \mathbb{E}\left[ L \mid L \ge \text{VaR}_\alpha \right]\] Under the Gaussian assumption: \[\text{ES}_\alpha^{\text{Gauss}} = \sigma_p \sqrt{\Delta t} \left[ \frac{\phi(\Phi^{-1}(\alpha))}{1 - \alpha} \right] - \mu_p \Delta t\] where \(\phi(z) = \frac{1}{\sqrt{2\pi}} e^{-z^2/2}\) is the standard normal probability density function.

Student-t Distribution Tail Risk Modeling

Financial asset returns exhibit fat tails and excess kurtosis. The Student-t distribution with \(\nu\) degrees of freedom has variance \(\text{Var}(X) = \frac{\nu}{\nu - 2}\) (for \(\nu > 2\)).

To match the empirical portfolio volatility \(\sigma_p\), the standardized quantile \(t_\nu^{-1}(\alpha)\) is scaled by the variance adjustment factor: \[s_\nu = \sqrt{\frac{\nu - 2}{\nu}}\] With default degrees of freedom \(\nu = 5\) (\(s_5 = \sqrt{3/5} \approx 0.7746\)): \[z_t = t_{\nu}^{-1}(\alpha) \cdot \sqrt{\frac{\nu - 2}{\nu}}\] \[\text{VaR}_\alpha^{\text{Student-}t} = \sigma_p \sqrt{\Delta t} \, z_t - \mu_p \Delta t\]

The gap between Student-t VaR and Gaussian VaR, \(\Delta \text{VaR} = \text{VaR}^{\text{Student-}t} - \text{VaR}^{\text{Gauss}}\), quantifies the fat-tail premium and is published directly in Diagnostics!C8.


4. Empirical Historical Simulation

Given \(T = 36\) historical monthly return vectors \(\mathbf{r}_t = (r_{1,t}, \dots, r_{n,t})^T\), the historical portfolio return series is: \[r_{p,t} = \mathbf{w}^T \mathbf{r}_t = \sum_{i=1}^n w_i r_{i,t}, \quad t = 1, \dots, T\]

Historical VaR

Let \(q_{1-\alpha}\) denote the empirical sample quantile of \(\{r_{p,t}\}_{t=1}^T\) at probability \(1 - \alpha\). Scaled to the desired horizon (\(h\) days, where 1 month \(\approx 21\) trading days): \[\text{VaR}_\alpha^{\text{Hist}} = -q_{1-\alpha}\left(\{r_{p,t}\}\right) \times \sqrt{\frac{h}{21}}\]

Historical Expected Shortfall

Historical CVaR is the arithmetic mean of all historical losses that exceed the empirical VaR: \[\text{ES}_\alpha^{\text{Hist}} = \frac{1}{|\mathcal{T}_{\text{tail}}|} \sum_{t \in \mathcal{T}_{\text{tail}}} (-r_{p,t}) \times \sqrt{\frac{h}{21}}\] where \(\mathcal{T}_{\text{tail}} = \{ t \mid -r_{p,t} \sqrt{h/21} \ge \text{VaR}_\alpha^{\text{Hist}} \}\).


5. Kupiec Proportion of Failures (POF) Likelihood Ratio Backtest

To statistically validate whether the calculated Value-at-Risk accurately captures tail risk, the toolbox executes Kupiec’s Proportion of Failures (POF) test (Kupiec, 1995).

Let: - \(N\) = total number of historical return periods (\(N = 36\) monthly periods). - \(x\) = observed number of VaR exceedances (months where portfolio return \(r_{p,t} < -\text{VaR}_{1\text{m},\alpha}\)). - \(p = 1 - \alpha\) = theoretical probability of exceedance (e.g., \(p = 0.05\) for a 95% confidence level). - \(\hat{p} = x / N\) = observed empirical failure rate.

Under the null hypothesis \(H_0: p_{\text{true}} = p\), the likelihood ratio test statistic is: \[\text{LR}_{\text{POF}} = -2 \ln \left[ \frac{(1 - p)^{N - x} p^x}{(1 - \hat{p})^{N - x} \hat{p}^x} \right] = 2 \left[ (N - x) \ln\left(\frac{1 - \hat{p}}{1 - p}\right) + x \ln\left(\frac{\hat{p}}{p}\right) \right]\]

For the boundary cases: - If \(x = 0\): \(\text{LR}_{\text{POF}} = -2 N \ln(1 - p)\). - If \(x = N\): \(\text{LR}_{\text{POF}} = -2 N \ln(p)\).

Under \(H_0\), \(\text{LR}_{\text{POF}}\) converges asymptotically to a Chi-square distribution with 1 degree of freedom: \[\text{LR}_{\text{POF}} \sim \chi^2(1)\] The p-value is given by the survival function: \[\text{p-value} = 1 - F_{\chi^2(1)}(\text{LR}_{\text{POF}})\]

If \(\text{p-value} \ge 0.05\), the test passes (PASS), confirming that the risk model’s tail coverage is statistically consistent with the nominal confidence level.


6. Euler Marginal Risk Attribution

By Euler’s homogeneous function theorem, since portfolio volatility \(\sigma_p(\mathbf{w})\) is homogeneous of degree 1 with respect to the weight vector \(\mathbf{w}\) (\(\sigma_p(c\mathbf{w}) = c\sigma_p(\mathbf{w})\)): \[\sigma_p(\mathbf{w}) = \sum_{i=1}^n w_i \frac{\partial \sigma_p}{\partial w_i}\]

Marginal Contribution to Risk (MCR)

The gradient of portfolio volatility with respect to asset weight \(w_i\) is: \[\text{MCR}_i = \frac{\partial \sigma_p}{\partial w_i} = \frac{(\boldsymbol{\Sigma} \mathbf{w})_i}{\sigma_p(\mathbf{w})} = \frac{\sum_{j=1}^n \Sigma_{ij} w_j}{\sqrt{\mathbf{w}^T \boldsymbol{\Sigma} \mathbf{w}}}\] \(\text{MCR}_i\) measures how an incremental 1% increase in asset \(i\)’s allocation impacts total portfolio volatility.

Component Risk Contribution (RC)

The absolute risk contribution of asset \(i\) is: \[\text{RC}_i = w_i \cdot \text{MCR}_i = w_i \frac{(\boldsymbol{\Sigma} \mathbf{w})_i}{\sigma_p}\] By Euler’s theorem, the sum of absolute risk contributions equals total portfolio volatility: \[\sum_{i=1}^n \text{RC}_i = \sigma_p\]

Percentage Risk Share (RiskSharePct)

The relative percentage contribution of asset \(i\) to total portfolio risk is: \[\% \text{RC}_i = \frac{\text{RC}_i}{\sigma_p} \times 100\% = \frac{w_i (\boldsymbol{\Sigma} \mathbf{w})_i}{\mathbf{w}^T \boldsymbol{\Sigma} \mathbf{w}} \times 100\%\] \[\sum_{i=1}^n \% \text{RC}_i = 100.0\%\]


7. Benchmark Tracking Error, CAPM Beta, and Active Risk

Let \(\{r_{p,t}\}_{t=1}^T\) and \(\{r_{b,t}\}_{t=1}^T\) denote historical monthly returns of the portfolio and Benchmark.

CAPM Portfolio Beta

\[\beta_p = \frac{\text{Cov}(r_p, r_b)}{\text{Var}(r_b)} = \frac{\sum_{t=1}^T (r_{p,t} - \bar{r}_p)(r_{b,t} - \bar{r}_b)}{\sum_{t=1}^T (r_{b,t} - \bar{r}_b)^2}\]

Active Return

The annualized expected excess return of the portfolio relative to the benchmark is: \[\alpha_{\text{act}} = \mu_p - \mu_b\] where \(\mu_b = 12 \cdot \bar{r}_b\).

Annualized Tracking Error

Tracking error measures the volatility of the active return series \(r_{\text{act},t} = r_{p,t} - r_{b,t}\): \[\text{TE} = \sqrt{12} \times \sqrt{\frac{1}{T - 1} \sum_{t=1}^T \left( r_{\text{act},t} - \bar{r}_{\text{act}} \right)^2}\]

Information Ratio

\[\text{IR} = \frac{\alpha_{\text{act}}}{\text{TE}} = \frac{\mu_p - \mu_b}{\text{TE}}\]


8. Efficient Frontier Construction

The parametric efficient frontier is generated by solving a sequence of 12 constrained optimization problems spanning the minimum-variance return \(r_{\min}\) to the maximum achievable return \(r_{\max}\):

  1. Minimum Variance Endpoint: \[r_{\min} = \mathbf{w}_{\text{minvar}}^T \boldsymbol{\mu}\]
  2. Maximum Return Endpoint: \[r_{\max} = \mathbf{w}_{\text{maxret}}^T \boldsymbol{\mu}\] where \(\mathbf{w}_{\text{maxret}} = \arg\max_{\mathbf{w} \in \mathcal{F}} \mathbf{w}^T \boldsymbol{\mu}\).
  3. Parametric Grid: \[r_k = r_{\min} + \frac{k - 1}{11} (r_{\max} - r_{\min}), \quad k = 1, \dots, 12\]
  4. Frontier Solves: For each target return \(r_k\), the optimizer solves: \[\mathbf{w}_k = \arg\min_{\mathbf{w} \in \mathcal{F}} \sqrt{\mathbf{w}^T \boldsymbol{\Sigma} \mathbf{w}} \quad \text{subject to } \mathbf{w}^T \boldsymbol{\mu} = r_k\]
  5. Diversification Ratio: At each frontier point, the portfolio diversification ratio (Choueifaty & Coignard, 2008) is computed: \[\text{DR}(\mathbf{w}_k) = \frac{\sum_{i=1}^n w_{k,i} \sigma_i}{\sqrt{\mathbf{w}_k^T \boldsymbol{\Sigma} \mathbf{w}_k}}\]

9. MATLAB Toolboxes Capability Mapping

Analytical Capability MATLAB Financial Toolbox MATLAB Risk Management Toolbox Boardflare Financial & Risk Toolbox
Mean-Variance Optimization Portfolio, estimateFrontier N/A SciPy SLSQP constrained optimization directly in Excel
Max Sharpe Ratio estimateMaxSharpeRatio N/A Automated negative Sharpe minimization
Target Return QP estimateFrontierByReturn N/A Inequality-constrained quadratic programming (portfolio_target_return)
Covariance Nearest PSD nearcorr, cov2corr N/A Automated eigenvalue clipping (\(\lambda \ge 10^{-7}\)) & diagonal normalization
Parametric Gaussian VaR portvrisk var Analytical \(\sigma_p \sqrt{\Delta t} \Phi^{-1}(\alpha) - \mu_p \Delta t\)
Expected Shortfall (CVaR) PortfolioCVaR es Gaussian conditional expectation + Historical tail averaging
Fat-Tail Modeling fitdist (t location-scale) tinv with degrees of freedom Student-t VaR with \(\nu = 5\) variance scaling
Empirical Historical VaR N/A historical VaR engine Sample percentile of 36 monthly historical return series
Kupiec POF Backtest N/A backtest (pof, bin) Analytical \(\text{LR}_{\text{POF}}\) test statistic & \(\chi^2(1)\) p-value
Euler Risk Attribution N/A riskcontribution Euler MCR \(\partial \sigma_p / \partial w_i\) & percentage risk shares
Active Risk & Beta trackingerror, beta N/A Sample covariance beta, tracking error, and Information Ratio