Skip to content
All library documents

Choosing Sobol Dimension and Sample Counts in QuantLib

Article Quant Q&A · Author: Yoshiro

Summary

The document explains why rows generated from a Sobol sequence repeat when the generator is reinitialized inside each asset loop. The generator should persist across draws, and its dimension should match the number of random coordinates needed for one sample. For a trial containing five asset values, one sequence draw should provide five coordinates; a simulated path across multiple assets and time steps requires a dimension matching all those coordinates.

It also cautions that Sobol sequences are low-discrepancy designs, not ordinary independent random samples. Their coverage properties depend on using appropriate sample counts, typically powers of two or counts aligned with the sequence construction. The answer warns that taking an arbitrary count such as 5,000 may not cover the domain fairly, while giving nearby supported counts as examples. The discussion does not provide a bulk-generation implementation or explain alternative sequence variants, so users should check their library's conventions and simulation design.

Key ideas

  • Reinitialize the Sobol generator outside the loop over samples to avoid repeating its sequence.
  • Set the generator dimension to the number of random coordinates in one complete trial.
  • A multidimensional path requires coordinates for each asset and each time step.
  • Sobol coverage depends on sample count, so arbitrary counts can weaken its low-discrepancy properties.
  • Match sequence dimensions and sample counts to the needs of the simulation.

Tags

Full text
# Generating normally distributed random numbers using Sobol generator in QuantLib


# Generating normally distributed random numbers using Sobol generator in QuantLib












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.

## Answer by Luigi Ballabio (score 2)

https://quant.stackexchange.com/a/76519

As Dimitri said, the initialization of the RNG should be outside the loop.

The dimension parameter is, roughly speaking, how many random numbers you need for one sample.

If one of your trials consists of the prices for your 5 assets, you'll have to initialize the RNG with dimension 5 (because you need one random number for each price) and then you'll get a list of 5 prices for the 5 assets each time you call `sobolEngine.nextSequence().value`.

(If, instead, you were generating random paths for an asset over $N$ steps, a full sample would be one path and the dimension would be $N$. If you were generating random paths for $M$ assets over $N$ steps, a full sample would be one path for each asset and the dimension would be $M \times N$.)

However, there's an additional, important consideration: low-discrepancy sequences such as Sobol have constraints that the usual RNG don't have. For instance, you can't extract just any number of samples. Sobol and other low-discrepancy RNGs are designed to cover fairly the domain using $2^N-1$ samples for any given $N$. If you extract 4095 samples ($2^{12}-1$) or 8191 samples ($2^{13}-1$), you'll get correct results from your simulation. If you extract 5000 samples, you won't, because you won't cover the domain fairly. If you need to work with that specific number of samples, you'll have to go for a classic quasi-random generator.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.