I am trying use low discrepancy Sobol RNG to generate normally distributed random numbers and fill an Eigen matrix with those random numbers. The matrix represents a basket of 5 assets (rows) each having 5000 trials (columns). After going thru QuantLib documentation, I have come up with following code. However, the random numbers generated are all the same for each column in the matrix.
MoroInverseCumulativeNormal invGauss;
MatrixXd quasi = MatrixXd::Zero(num_credits,num_trials);
double current_sobol_num{}, current_normal_number{};
for (int c{0}; c< num_credits;++c){
SobolRsg sobolEngine(1);
for (int t{0}; t < num_trials;++t){
current_sobol_num = (sobolEngine.nextSequence().value)[0];
current_normal_number = invGauss(current_sobol_num);
quasi(c,t) = current_normal_number;
};
}
The matrix after the above code is run :
Asset | t0 | t1 | t2 | t3 | t4 |
---|---|---|---|---|---|
First | 0 | 0.6745 | -0.6745 | -0.3186 | 1.1503 |
Second | 0 | 0.6745 | -0.6745 | -0.3186 | 1.1503 |
Third | 0 | 0.6745 | -0.6745 | -0.3186 | 1.1503 |
Fourth | 0 | 0.6745 | -0.6745 | -0.3186 | 1.1503 |
Fifth | 0 | 0.6745 | -0.6745 | -0.3186 | 1.1503 |
Any idea what I am doing wrong? Is there a way to get all 5000 randoms in one shot instead of pulling one at a time?
I am using QuantLib just for SobolRng and Inverse Gaussian functions. I am open to using any other open source library if suggested.
Thank you very much for your time.
c
, you re-initialize the the rsg with seed defaulting to 0. have you tried moving the instantiation of SobolRsg sobolEngine outside of the for loop? $\endgroup$