risk-metrics-calculation
Calculate portfolio risk metrics including VaR, CVaR, Sharpe, Sortino, and drawdown analysis. Use when measuring portfolio risk, implementing risk limits, or building risk monitoring systems.
Profile →npx skills add majiayu000/claude-skill-registry --skill risk-metrics-calculation-codewithbehnam-cc-docs --agent claude-code
Same command for any agent — swap --agent for codex, cursor, copilot.
Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.
What it does
Calculates a broad set of risk metrics for portfolios, covering volatility, tail risk (VaR and CVaR), drawdowns, risk-adjusted returns (Sharpe, Sortino, Calmar, Omega), and information ratios. Includes both a core metric calculator for a single return series and portfolio-level tools for aggregations, rolling calculations, and stress testing.
How it works
- Core metrics (Pattern 1) operate on a series of returns: compute volatility (annualized), downside deviation, beta against market, VaR variants (historical, parametric, Cornish-Fisher), CVaR, drawdowns and max/average drawdown, and risk-adjusted measures (Sharpe, Sortino, Calmar, Omega). It also provides a summary dictionary with many of these fields plus distribution stats (skewness, kurtosis).
- Portfolio risk (Pattern 2) handles a returns DataFrame and optional weights. It computes portfolio return (annualized), portfolio volatility, marginal risk contribution, component risk, risk-parity weights optimization, correlation matrix, diversification ratio, tracking error, and a conditional correlation during stress.
- Rolling risk metrics (Pattern 3) provide rolling volatility, rolling Sharpe, rolling VaR, rolling max drawdown, rolling beta, and volatility regime classification over a specified window.
- Stress testing (Pattern 4) defines historical crisis scenarios and can run a historical stress test using crisis period data, returning total return, max drawdown, and worst day for the portfolio within the crisis window.
When to use it
- Measuring portfolio risk
- Implementing risk limits
- Building risk dashboards
- Calculating risk-adjusted returns
- Setting position sizes
- Regulatory reporting
What it can touch
- Core metrics operate on a pandas Series of returns (Pattern 1) and use NumPy, SciPy, and pandas.
- Portfolio risk expects a DataFrame of asset returns and an optional weights Series.
- Rolling metrics rely on rolling windows and standard pandas operations.
- Stress testing uses provided historical data DataFrames and predefined scenarios.
Caveats
- License: MIT
- Implementations assume daily returns and an annual factor of 252 unless specified otherwise (ann_factor).
- Some methods return 0 or inf in degenerate cases (e.g., zero volatility or zero downside deviation).
- Optimizer in risk_parity_weights uses SciPy minimize with SLSQP and specific bounds/constraints; results may depend on initialization and data quality.
# Risk Metrics Calculation Comprehensive risk measurement toolkit for portfolio management, including Value at Risk, Expected Shortfall, and drawdown analysis. ## When to Use This Skill - Measuring portfolio risk - Implementing risk limits - Building risk dashboards - Calculating risk-adjusted returns - Setting position sizes - Regulatory reporting ## Core Concepts ### 1. Risk Metric Categories | Category | Metrics | Use Case | | ----------------- | --------------- | -------------------- | | **Volatility** | Std Dev, Beta | General risk | | **Tail Risk** | VaR, CVaR | Extreme losses | | **Drawdown** | Max DD, Calmar | Capital preservation | | **Risk-Adjusted** | Sharpe, Sortino | Performance | ### 2. Time Horizons ``` Intraday: Minute/hourly VaR for day traders Daily: Standard risk reporting Weekly: Rebalancing decisions Monthly: Performance attribution Annual: Strategic allocation ``` ## Implementation ### Pattern 1: Core Risk Metrics ```python import numpy as np import pandas as pd from scipy import stats from typing import Dict, Optional, Tuple class RiskMetrics: """Core risk metric calculations.""" def __init__(self, returns: pd.Series, rf_rate: float = 0.02): """ Args: returns
- When to Use This Skill
- Core Concepts
- 1. Risk Metric Categories
- 2. Time Horizons
- Implementation
- Pattern 1: Core Risk Metrics
- Pattern 2: Portfolio Risk
- Pattern 3: Rolling Risk Metrics
- Pattern 4: Stress Testing
- Quick Reference
- Best Practices
- Do's
- Don'ts
What does the risk-metrics-calculation skill do?
Calculate portfolio risk metrics including VaR, CVaR, Sharpe, Sortino, and drawdown analysis. Use when measuring portfolio risk, implementing risk limits, or building risk monitoring systems.
How do I install it?
Run `npx skills add majiayu000/claude-skill-registry --skill risk-metrics-calculation-codewithbehnam-cc-docs --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.
Where does this skill come from?
From majiayu000/claude-skill-registry, a repository with 534 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.
Is a popular skill a good skill?
Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.