Value at Risk (VaR)
The Value at Risk (VaR) module provides functionality to calculate VaR, a widely used measure of financial risk.
Functions
- torchquantlib.core.risk.market_risk.var.calculate_var(returns: Tensor, confidence_level: float) Tensor[source]
Calculate Value at Risk (VaR) using the historical simulation method.
Value at Risk is a measure of the potential loss in value of a risky asset or portfolio over a defined period for a given confidence interval.
- Parameters:
returns (Tensor) – A tensor of historical returns for the asset or portfolio.
confidence_level (float) – The confidence level for VaR calculation, typically 0.95 or 0.99.
- Returns:
The calculated Value at Risk.
- Return type:
Tensor
Note
This function assumes that the input returns are properly preprocessed and represent a relevant historical period for the asset or portfolio.
The calculated VaR represents the loss that is expected to be exceeded only (1 - confidence_level) * 100% of the time.
A higher confidence level results in a more conservative (higher) VaR estimate.
Usage Example
Here’s an example of how to use the calculate_var function:
import torch
from torchquantlib.core.risk.market_risk.var import calculate_var
# Generate sample returns data
returns = torch.randn(1000) # 1000 random returns
# Calculate VaR at 95% confidence level
var_95 = calculate_var(returns, confidence_level=0.95)
print(f"Value at Risk (95% confidence): {var_95.item():.4f}")
# Calculate VaR at 99% confidence level
var_99 = calculate_var(returns, confidence_level=0.99)
print(f"Value at Risk (99% confidence): {var_99.item():.4f}")
Notes
VaR represents the maximum potential loss at a given confidence level over a specific time horizon.
A higher confidence level results in a more conservative (higher) VaR estimate.
VaR is widely used but has limitations, especially in capturing tail risks.
Consider using Expected Shortfall (ES) alongside VaR for a more comprehensive risk assessment.
See Also
expected_shortfall for information on Expected Shortfall calculation, which addresses some limitations of VaR.