Spaces:
Sleeping
Sleeping
File size: 11,150 Bytes
72fc0ee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | """
Value at Risk (VaR) and Conditional Value at Risk (CVaR) calculators.
Methods implemented:
- Historical simulation
- Parametric (variance-covariance)
- Monte Carlo simulation
- Cornish-Fisher expansion (semi-parametric)
All methods return both VaR and CVaR (also known as Expected Shortfall).
"""
from __future__ import annotations
import logging
import math
from dataclasses import dataclass
from enum import Enum
from typing import Optional
import numpy as np
import pandas as pd
from scipy.stats import norm, t as t_dist
from quantlib_pro.utils.validation import require_positive, require_probability
log = logging.getLogger(__name__)
class VaRMethod(str, Enum):
HISTORICAL = "historical"
PARAMETRIC = "parametric"
MONTE_CARLO = "monte_carlo"
CORNISH_FISHER = "cornish_fisher"
@dataclass
class VaRResult:
"""
VaR and CVaR results for a single confidence level.
Attributes
----------
var : float
Value at Risk β the maximum expected loss at the given confidence level
(positive number represents loss)
cvar : float
Conditional VaR (Expected Shortfall) β average loss beyond VaR
confidence_level : float
Confidence level (e.g., 0.95 for 95%)
method : VaRMethod
Calculation method used
time_horizon : float
Time horizon in days
portfolio_value : float
Current portfolio value (optional, for dollar VaR)
"""
var: float
cvar: float
confidence_level: float
method: VaRMethod
time_horizon: float = 1.0
portfolio_value: Optional[float] = None
@property
def var_dollars(self) -> Optional[float]:
"""VaR in dollars (if portfolio_value is set)."""
if self.portfolio_value is None:
return None
return self.var * self.portfolio_value
@property
def cvar_dollars(self) -> Optional[float]:
"""CVaR in dollars (if portfolio_value is set)."""
if self.portfolio_value is None:
return None
return self.cvar * self.portfolio_value
# βββ Historical VaR βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def var_historical(
returns: pd.Series | np.ndarray,
confidence_level: float = 0.95,
time_horizon: int = 1,
) -> VaRResult:
"""
Historical simulation VaR/CVaR.
Parameters
----------
returns : array-like
Historical returns series (daily, assumed i.i.d.)
confidence_level : float
Confidence level (e.g., 0.95 for 95%)
time_horizon : int
Number of days to scale VaR (βt scaling)
Returns
-------
VaRResult
VaR and CVaR as fractions of portfolio value
"""
require_probability(confidence_level, "confidence_level")
require_positive(time_horizon, "time_horizon")
if isinstance(returns, pd.Series):
returns = returns.values
returns = returns[~np.isnan(returns)]
if len(returns) < 30:
log.warning("Historical VaR with <30 observations may be unreliable")
# Scale returns by βt if horizon > 1
if time_horizon > 1:
scaled_returns = returns * math.sqrt(time_horizon)
else:
scaled_returns = returns
# VaR is the (1 - confidence_level) percentile of the loss distribution
# Loss = -return, so VaR = -percentile(returns, alpha)
alpha = 1 - confidence_level
var = -np.percentile(scaled_returns, alpha * 100)
# CVaR is the average of all losses beyond VaR
tail_losses = -scaled_returns[scaled_returns < -var]
if len(tail_losses) > 0:
cvar = tail_losses.mean()
else:
cvar = var # fallback if no tail observations
return VaRResult(
var=var,
cvar=cvar,
confidence_level=confidence_level,
method=VaRMethod.HISTORICAL,
time_horizon=time_horizon,
)
# βββ Parametric VaR βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def var_parametric(
returns: pd.Series | np.ndarray,
confidence_level: float = 0.95,
time_horizon: int = 1,
distribution: str = "normal",
) -> VaRResult:
"""
Parametric (variance-covariance) VaR/CVaR.
Assumes returns are normally or t-distributed.
Parameters
----------
distribution : str
"normal" or "t" (Student's t with df estimated from data)
"""
require_probability(confidence_level, "confidence_level")
require_positive(time_horizon, "time_horizon")
if isinstance(returns, pd.Series):
returns = returns.values
returns = returns[~np.isnan(returns)]
mu = returns.mean()
sigma = returns.std(ddof=1)
# Scale for time horizon
mu_scaled = mu * time_horizon
sigma_scaled = sigma * math.sqrt(time_horizon)
alpha = 1 - confidence_level
if distribution == "normal":
z = norm.ppf(alpha)
var = -(mu_scaled + z * sigma_scaled)
# CVaR for normal: VaR + Ο * Ο(z) / Ξ±
cvar = var + sigma_scaled * norm.pdf(z) / alpha
elif distribution == "t":
# Estimate degrees of freedom via method of moments (kurtosis)
kurtosis = pd.Series(returns).kurtosis()
df = max(4, 6 / kurtosis + 4) if kurtosis > 0 else 10
t_val = t_dist.ppf(alpha, df=df)
var = -(mu_scaled + t_val * sigma_scaled)
# CVaR for t-distribution (approximation)
cvar = var * (df + t_val**2) / ((df - 1) * (1 - alpha))
else:
raise ValueError(f"Unknown distribution: {distribution}")
return VaRResult(
var=var,
cvar=cvar,
confidence_level=confidence_level,
method=VaRMethod.PARAMETRIC,
time_horizon=time_horizon,
)
# βββ Monte Carlo VaR ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def var_monte_carlo(
returns: pd.Series | np.ndarray,
confidence_level: float = 0.95,
time_horizon: int = 1,
n_simulations: int = 10_000,
random_seed: Optional[int] = None,
) -> VaRResult:
"""
Monte Carlo VaR/CVaR via bootstrapping historical returns.
Parameters
----------
n_simulations : int
Number of bootstrap samples
random_seed : int, optional
Random seed for reproducibility
"""
require_probability(confidence_level, "confidence_level")
require_positive(time_horizon, "time_horizon")
require_positive(n_simulations, "n_simulations")
if isinstance(returns, pd.Series):
returns = returns.values
returns = returns[~np.isnan(returns)]
rng = np.random.default_rng(random_seed)
# Bootstrap daily returns and compound over the time horizon
simulated_returns = np.zeros(n_simulations)
for i in range(n_simulations):
path = rng.choice(returns, size=time_horizon, replace=True)
# Compound returns: (1+r1)*(1+r2)*...*(1+rT) - 1
simulated_returns[i] = np.prod(1 + path) - 1
alpha = 1 - confidence_level
var = -np.percentile(simulated_returns, alpha * 100)
tail_losses = -simulated_returns[simulated_returns < -var]
if len(tail_losses) > 0:
cvar = tail_losses.mean()
else:
cvar = var
return VaRResult(
var=var,
cvar=cvar,
confidence_level=confidence_level,
method=VaRMethod.MONTE_CARLO,
time_horizon=time_horizon,
)
# βββ Cornish-Fisher VaR βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def var_cornish_fisher(
returns: pd.Series | np.ndarray,
confidence_level: float = 0.95,
time_horizon: int = 1,
) -> VaRResult:
"""
Cornish-Fisher expansion VaR/CVaR.
Adjusts parametric VaR for skewness and kurtosis in the return distribution.
More accurate than plain parametric when returns are non-normal.
References
----------
Favre, L., & Galeano, J. A. (2002). "Mean-Modified Value-at-Risk Optimization
with Hedge Funds." Journal of Alternative Investments, 5(2), 21-25.
"""
require_probability(confidence_level, "confidence_level")
require_positive(time_horizon, "time_horizon")
if isinstance(returns, pd.Series):
returns_series = returns
returns = returns.values
else:
returns_series = pd.Series(returns)
returns = returns[~np.isnan(returns)]
mu = returns.mean()
sigma = returns.std(ddof=1)
skew = returns_series.skew()
kurt = returns_series.kurtosis() # excess kurtosis
# Scale for time horizon
mu_scaled = mu * time_horizon
sigma_scaled = sigma * math.sqrt(time_horizon)
alpha = 1 - confidence_level
z = norm.ppf(alpha)
# Cornish-Fisher adjustment
z_cf = (
z
+ (z**2 - 1) * skew / 6
+ (z**3 - 3 * z) * kurt / 24
- (2 * z**3 - 5 * z) * skew**2 / 36
)
var = -(mu_scaled + z_cf * sigma_scaled)
# CVaR approximation (use parametric formula with adjusted z)
cvar = var + sigma_scaled * norm.pdf(z_cf) / alpha
return VaRResult(
var=var,
cvar=cvar,
confidence_level=confidence_level,
method=VaRMethod.CORNISH_FISHER,
time_horizon=time_horizon,
)
# βββ Unified interface ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def calculate_var(
returns: pd.Series | np.ndarray,
confidence_level: float = 0.95,
time_horizon: int = 1,
method: VaRMethod = VaRMethod.HISTORICAL,
portfolio_value: Optional[float] = None,
**kwargs,
) -> VaRResult:
"""
Calculate VaR and CVaR using the specified method.
Parameters
----------
method : VaRMethod
Calculation method (HISTORICAL, PARAMETRIC, MONTE_CARLO, CORNISH_FISHER)
portfolio_value : float, optional
Current portfolio value in dollars (for dollar VaR reporting)
**kwargs
Method-specific parameters (e.g., n_simulations for Monte Carlo)
Returns
-------
VaRResult
Contains var, cvar, confidence_level, method, and optional dollar values
"""
if method == VaRMethod.HISTORICAL:
result = var_historical(returns, confidence_level, time_horizon)
elif method == VaRMethod.PARAMETRIC:
result = var_parametric(returns, confidence_level, time_horizon, **kwargs)
elif method == VaRMethod.MONTE_CARLO:
result = var_monte_carlo(returns, confidence_level, time_horizon, **kwargs)
elif method == VaRMethod.CORNISH_FISHER:
result = var_cornish_fisher(returns, confidence_level, time_horizon)
else:
raise ValueError(f"Unknown VaR method: {method}")
if portfolio_value is not None:
result.portfolio_value = portfolio_value
return result
|