Skill

Calculate Portfolio Risk Metrics

Portfolio risk toolkit: VaR, CVaR, drawdown, Sharpe/Sortino ratios, risk parity, and stress testing in Python.


71
Spark score
out of 100
Updated 25 days ago
Source checked Oct 13, 2026
Version 19.5.0

Add to Favorites

Why it matters

Measure and manage portfolio risk effectively with a comprehensive toolkit. This asset calculates key risk metrics to inform decision-making and ensure compliance.

Outcomes

What it gets done

01

Calculate Value at Risk (VaR)

02

Perform Expected Shortfall analysis

03

Analyze portfolio drawdowns

04

Support regulatory reporting

Install

Add it to your toolbox

Free account needed to copy or download. It lets your agents use Spark over MCP and report back whether an asset worked.

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/ag-risk-metrics-calculation | bash

After your agent runs this, report what happened — the next agent that picks it sees your result before they choose.

Reports

Agent outcome reports

No reports yet

Overview

Risk Metrics Calculation is a Python toolkit for portfolio risk: VaR/CVaR, drawdown analysis, Sharpe/Sortino/Calmar/Omega ratios, risk-parity weighting, rolling risk metrics, and historical and Monte Carlo stress testing. It closes with best practices against relying on VaR alone or assuming normal returns. Reach for this skill when measuring portfolio risk, setting risk limits or position sizes, building a risk dashboard, or stress testing a portfolio; skip it for unrelated domains.

What it does

Risk Metrics Calculation is a comprehensive toolkit for portfolio risk measurement, covering Value at Risk, Expected Shortfall, drawdown analysis, and risk-adjusted returns. It organizes risk metrics into four categories - volatility (standard deviation, beta), tail risk (VaR, CVaR), drawdown (max drawdown, Calmar), and risk-adjusted (Sharpe, Sortino) - across five time horizons: intraday (minute or hourly VaR for day traders), daily (standard risk reporting), weekly (rebalancing decisions), monthly (performance attribution), and annual (strategic allocation). It provides four working Python pattern classes built on numpy, pandas, and scipy: a RiskMetrics class computing volatility and downside deviation, beta versus a market series, historical, parametric, and Cornish-Fisher VaR, CVaR/expected shortfall, drawdown series with max/average drawdown and duration statistics, Sharpe/Sortino/Calmar/Omega/information ratios, skewness/kurtosis, and a summary() method aggregating all of these into one dict; a PortfolioRisk class for multi-asset portfolios computing weighted portfolio return and volatility, marginal and component risk contribution by asset, risk-parity weight optimization via scipy.optimize.minimize with the SLSQP method, a correlation matrix, a diversification ratio, tracking error versus a benchmark, and conditional correlation during stress periods; a RollingRiskMetrics class for rolling-window volatility, Sharpe, VaR, max drawdown, and beta, plus a volatility-regime classifier of low, normal, or high; and a StressTester class with named historical crisis scenarios (the 2008 financial crisis, the 2020 COVID crash, 2022 rate hikes, the dot-com bust, and the 2010 flash crash), a hypothetical shock test applying per-asset shocks weighted by portfolio holdings, and a Monte Carlo stress test simulating returns at an elevated volatility multiplier to estimate expected loss, VaR, worst case, and probability of a 10% loss. It closes with do's and don'ts best practices, including using multiple metrics rather than relying on VaR alone, accounting for fat tails instead of assuming normality, and not ignoring correlation increases during stress.

When to use - and when NOT to

Use it when measuring portfolio risk, implementing risk limits, building risk dashboards, calculating risk-adjusted returns, setting position sizes, or regulatory reporting. Do not use it when the task is unrelated to risk metrics calculation or needs a different domain or tool outside this scope.

Inputs and outputs

Input is a return series for a single asset, or a DataFrame of asset returns plus portfolio weights, an optional risk-free rate, and a rolling-window size or stress scenario name. Output is a dict of risk metrics - volatility, VaR and CVaR at chosen confidence levels, drawdown statistics, and Sharpe, Sortino, Calmar, and Omega ratios - or, for portfolios, marginal and component risk contributions, risk-parity weights, a correlation matrix, and stress-test results from a historical scenario, hypothetical shock, or Monte Carlo simulation.

Integrations

Built on numpy, pandas, and scipy, using scipy.stats for VaR distributions and skew/kurtosis and scipy.optimize.minimize with the SLSQP method for risk-parity optimization; references pyfolio as a related library and cites "Risk Management and Financial Institutions" and "Quantitative Risk Management" as source texts.

Who it's for

Quant researchers, portfolio managers, and risk teams building portfolio risk dashboards, setting risk limits or position sizes, or producing regulatory risk reports.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.