quant-pricer-cpp
Loading...
Searching...
No Matches
risk.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <vector>
5
6namespace quant::risk {
7
8// Compute historical VaR and CVaR (Expected Shortfall) from a vector of P&L samples
9// alpha in (0,1), e.g., 0.99 for 99% VaR
10struct VarEs {
11 double var;
12 double cvar;
13};
14
15VarEs var_cvar_from_pnl(const std::vector<double>& pnl, double alpha);
16
17// Monte Carlo VaR under GBM for a single asset position
18// Returns VaR/CVaR of horizon P&L for position size (positive long)
19VarEs var_cvar_gbm(double spot, double mu, double sigma, double horizon_years, double position,
20 unsigned long num_sims, unsigned long seed, double alpha);
21
22// Portfolio VaR/CVaR under joint normal/Gaussian copula (Cholesky)
23// mu, sigma, weights have size N; corr is N x N row-major correlation matrix
24VarEs var_cvar_portfolio(const std::vector<double>& mu, const std::vector<double>& sigma,
25 const std::vector<double>& corr, const std::vector<double>& weights,
26 double horizon_years, unsigned long num_sims, unsigned long seed, double alpha);
27
28// Backtest statistics for VaR exceptions
30 double alpha; // VaR confidence
31 unsigned long T; // total trials
32 unsigned long N; // number of exceptions
33 double lr_pof; // Kupiec POF LR statistic (df=1)
34 double p_pof; // p-value for POF
35 double lr_ind; // Christoffersen independence LR (df=1)
36 double p_ind; // p-value for independence
37 double lr_cc; // Combined conditional coverage LR (df=2)
38 double p_cc; // p-value for combined
39};
40
41// Compute Kupiec (POF) and Christoffersen (independence) tests from exception sequence (0/1)
42BacktestStats kupiec_christoffersen(const std::vector<int>& exceptions, double alpha);
43
44// t-Student VaR/CVaR for a single asset P&L over a horizon
45VarEs var_cvar_t(double mu, double sigma, double nu, double horizon_years, double position,
46 unsigned long num_sims, unsigned long seed, double alpha);
47
48} // namespace quant::risk
double sigma
Definition bs_barrier_rr.cpp:19
double mu
Definition bs_barrier_rr.cpp:24
double spot
Definition mc.cpp:69
Simple VaR/CVaR computation utilities.
Definition risk.hpp:6
BacktestStats kupiec_christoffersen(const std::vector< int > &exceptions, double alpha)
Definition risk.cpp:114
VarEs var_cvar_t(double mu, double sigma, double nu, double horizon_years, double position, unsigned long num_sims, unsigned long seed, double alpha)
Definition risk.cpp:161
VarEs var_cvar_gbm(double spot, double mu, double sigma, double horizon_years, double position, unsigned long num_sims, unsigned long seed, double alpha)
Definition risk.cpp:27
VarEs var_cvar_portfolio(const std::vector< double > &mu, const std::vector< double > &sigma, const std::vector< double > &corr, const std::vector< double > &weights, double horizon_years, unsigned long num_sims, unsigned long seed, double alpha)
Definition risk.cpp:44
VarEs var_cvar_from_pnl(const std::vector< double > &pnl, double alpha)
Definition risk.cpp:13
Definition risk.hpp:29
double p_ind
Definition risk.hpp:36
double p_pof
Definition risk.hpp:34
double p_cc
Definition risk.hpp:38
unsigned long N
Definition risk.hpp:32
double lr_ind
Definition risk.hpp:35
double alpha
Definition risk.hpp:30
double lr_pof
Definition risk.hpp:33
double lr_cc
Definition risk.hpp:37
unsigned long T
Definition risk.hpp:31
Definition risk.hpp:10
double cvar
Definition risk.hpp:12
double var
Definition risk.hpp:11