C Vectors 1734274106
C Vectors 1734274106
Introduction
This document provides useful unexplored C++ techniques related to vectors that can enhance the productivity
of quants working on stochastic models, derivative pricing, and risk management. These techniques are designed
to optimize computation and handle high-dimensional data efficiently.
This method is crucial for normalizing large vectors, often required in risk factor models or PCA.
This is useful for creating covariance matrices for risk factor aggregation.
1
1 // Parallelized asset price simulation
2 # include < vector >
3 # include < cmath >
4 # include < execution >
5
6 void p a r a l l e l S i m u l a t e A s s e t P r i c e s ( std :: vector < double >& assetPrices ,
7 double S0 , double r , double sigma ,
8 double T , size_t N ) {
9 std :: vector < double > randomNumbers (N , 0.0) ;
10 std :: random_device rd ;
11 std :: mt19937 gen ( rd () ) ;
12 std :: normal_distribution < > dist (0.0 , 1.0) ;
13
14 for ( size_t i = 0; i < N ; ++ i ) randomNumbers [ i ] = dist ( gen ) ;
15
16 std :: transform ( std :: execution :: par , randomNumbers . begin () , randomNumbers . end ()
,
17 assetPrices . begin () ,
18 [=]( double z ) {
19 return S0 * exp (( r - 0.5 * sigma * sigma ) * T + sigma *
sqrt ( T ) * z ) ;
20 }) ;
21 }
This is ideal for handling high-dimensional risk factors with sparse data.
2
5. Rolling Statistics for Volatility Estimation
6 std :: vector < double > rollingMean ( const std :: vector < double >& data , size_t windowSize
) {
7 std :: deque < double > window ;
8 std :: vector < double > result ;
9 double sum = 0.0;
10
Rolling statistics are crucial for estimating moving averages or rolling volatility.
8 class GreeksVector {
9 public :
10 std :: vector < Greeks > values ;
11
12 void add ( double delta , double gamma , double vega ) {
13 values . push_back ({ delta , gamma , vega }) ;
14 }
15
16 Greeks aggregate () const {
17 double totalDelta = 0.0 , totalGamma = 0.0 , totalVega = 0.0;
18 for ( const auto & g : values ) {
19 totalDelta += g . delta ;
20 totalGamma += g . gamma ;
21 totalVega += g . vega ;
22 }
23 return { totalDelta , totalGamma , totalVega };
24 }
25 };
3
This helps in efficiently aggregating sensitivities across portfolios.
5 std :: vector < size_t > s ortByC orrela tion ( const std :: vector < double >& correlations ) {
6 std :: vector < size_t > indices ( correlations . size () ) ;
7 iota ( indices . begin () , indices . end () , 0) ;
8
9 std :: sort ( indices . begin () , indices . end () , [&]( size_t i , size_t j ) {
10 return correlations [ i ] > correlations [ j ];
11 }) ;
12
13 return indices ;
14 }