Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_rand_quasi_init_scrambled (g05yn)

## Purpose

nag_rand_quasi_init_scrambled (g05yn) initializes a scrambled quasi-random generator prior to calling nag_rand_quasi_normal (g05yj), nag_rand_quasi_lognormal (g05yk) or nag_rand_quasi_uniform (g05ym). It must be preceded by a call to one of the pseudorandom initialization functions nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).

## Syntax

[iref, state, ifail] = g05yn(genid, stype, idim, iskip, nsdigi, state)
[iref, state, ifail] = nag_rand_quasi_init_scrambled(genid, stype, idim, iskip, nsdigi, state)

## Description

nag_rand_quasi_init_scrambled (g05yn) selects a quasi-random number generator through the input value of genid, a method of scrambling through the input value of stype and initializes the iref communication array for use in the functions nag_rand_quasi_normal (g05yj), nag_rand_quasi_lognormal (g05yk) or nag_rand_quasi_uniform (g05ym).
Scrambled quasi-random sequences are an extension of standard quasi-random sequences that attempt to eliminate the bias inherent in a quasi-random sequence whilst retaining the low-discrepancy properties. The use of a scrambled sequence allows error estimation of Monte–Carlo results by performing a number of iterates and computing the variance of the results.
This implementation of scrambled quasi-random sequences is based on TOMS Algorithm 823 and details can be found in the accompanying paper, Hong and Hickernell (2003). Three methods of scrambling are supplied; the first a restricted form of Owen's scrambling (Owen (1995)), the second based on the method of Faure and Tezuka (2000) and the last method combines the first two.
Scrambled versions of the Niederreiter sequence and two sets of Sobol sequences are provided. The first Sobol sequence is obtained using genid = 1${\mathbf{genid}}=1$. The first 10000 direction numbers for this sequence are based on the work of Joe and Kuo (2008). For dimensions greater than 10000 the direction numbers are randomly generated using the pseudorandom generator specified in state (see Jäckel (2002) for details). The second Sobol sequence is obtained using genid = 2${\mathbf{genid}}=2$ and referred to in the documentation as ‘Sobol (A659)’. The first 1111 direction numbers for this sequence are based on Algorithm 659 of Bratley and Fox (1988) with the extension proposed by Joe and Kuo (2003). For dimensions greater than 1111 the direction numbers are once again randomly generated. The Niederreiter sequence is obtained by setting genid = 3${\mathbf{genid}}=3$.

## References

Bratley P and Fox B L (1988) Algorithm 659: implementing Sobol's quasirandom sequence generator ACM Trans. Math. Software 14(1) 88–100
Faure H and Tezuka S (2000) Another random scrambling of digital (t,s)-sequences Monte Carlo and Quasi-Monte Carlo Methods Springer-Verlag, Berlin, Germany (eds K T Fang, F J Hickernell and H Niederreiter)
Hong H S and Hickernell F J (2003) Algorithm 823: implementing scrambled digital sequences ACM Trans. Math. Software 29:2 95–109
Jäckel P (2002) Monte Carlo Methods in Finance Wiley Finance Series, John Wiley and Sons, England
Joe S and Kuo F Y (2003) Remark on Algorithm 659: implementing Sobol's quasirandom sequence generator ACM Trans. Math. Software (TOMS) 29 49–57
Joe S and Kuo F Y (2008) Constructing Sobol sequences with better two-dimensional projections SIAM J. Sci. Comput. 30 2635–2654
Niederreiter H (1988) Low-discrepancy and low dispersion sequences Journal of Number Theory 30 51–70
Owen A B (1995) Randomly permuted (t,m,s)-nets and (t,s)-sequences Monte Carlo and Quasi-Monte Carlo Methods in Scientific Computing, Lecture Notes in Statistics 106 Springer-Verlag, New York, NY 299–317 (eds H Niederreiter and P J-S Shiue)

## Parameters

### Compulsory Input Parameters

1:     genid – int64int32nag_int scalar
Must identify the quasi-random generator to use.
genid = 1${\mathbf{genid}}=1$
Sobol generator.
genid = 2${\mathbf{genid}}=2$
Sobol (A659) generator.
genid = 3${\mathbf{genid}}=3$
Niederreiter generator.
Constraint: genid = 1${\mathbf{genid}}=1$, 2$2$ or 3$3$.
2:     stype – int64int32nag_int scalar
Must identify the scrambling method to use.
stype = 0${\mathbf{stype}}=0$
No scrambling. This is equivalent to calling nag_rand_quasi_init (g05yl).
stype = 1${\mathbf{stype}}=1$
Owen like scrambling.
stype = 2${\mathbf{stype}}=2$
Faure–Tezuka scrambling.
stype = 3${\mathbf{stype}}=3$
Owen and Faure–Tezuka scrambling.
Constraint: stype = 0${\mathbf{stype}}=0$, 1$1$, 2$2$ or 3$3$.
3:     idim – int64int32nag_int scalar
The number of dimensions required.
Constraints:
• if genid = 1${\mathbf{genid}}=1$, 1idim50000$1\le {\mathbf{idim}}\le 50000$;
• if genid = 2${\mathbf{genid}}=2$, 1idim50000$1\le {\mathbf{idim}}\le 50000$;
• if genid = 3${\mathbf{genid}}=3$, 1idim318$1\le {\mathbf{idim}}\le 318$.
4:     iskip – int64int32nag_int scalar
The number of terms of the sequence to skip on initialization for the Sobol and Niederreiter generators.
Constraint: 0iskip230$0\le {\mathbf{iskip}}\le {2}^{30}$.
5:     nsdigi – int64int32nag_int scalar
Controls the number of digits (bits) to scramble when genid = 1${\mathbf{genid}}=1$ or 2$2$, otherwise nsdigi is ignored. If nsdigi < 1${\mathbf{nsdigi}}<1$ or nsdigi > 30${\mathbf{nsdigi}}>30$ then all the digits are scrambled.
6:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains information on the selected base generator and its current state.

None.

liref

### Output Parameters

1:     iref(liref) – int64int32nag_int array
liref32 × idim + 7$\mathit{liref}\ge 32×{\mathbf{idim}}+7$.
Contains initialization information for use by the generator functions nag_rand_quasi_normal (g05yj), nag_rand_quasi_lognormal (g05yk) and nag_rand_quasi_uniform (g05ym). iref must not be altered in any way between initialization and calls of the generator functions.
2:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains updated information on the state of the generator.
3:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

ifail = 1${\mathbf{ifail}}=1$
On entry, genid1${\mathbf{genid}}\ne 1$, 2$2$ or 3$3$.
ifail = 2${\mathbf{ifail}}=2$
stype0${\mathbf{stype}}\ne 0$, 1$1$, 2$2$ or 3$3$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, idim < 1${\mathbf{idim}}<1$, or idim is too large.
ifail = 5${\mathbf{ifail}}=5$
On entry, liref is too small.
ifail = 6${\mathbf{ifail}}=6$
The value of iskip < 0${\mathbf{iskip}}<0$ or iskip is too large.
ifail = 8${\mathbf{ifail}}=8$
 On entry, state vector was not initialized or has been corrupted.

## Accuracy

Not applicable.

The additional computational cost in using a scrambled quasi-random sequence over a non-scrambled one comes entirely during the initialization. Once nag_rand_quasi_init_scrambled (g05yn) has been called the computational cost of generating a scrambled sequence and a non-scrambled one is identical.

## Example

```function nag_rand_quasi_init_scrambled_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

stype = int64(1);
idim = 8;
iskip = int64(1000);
nsdigi = int64(0);
n = 200;

% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
% Call the initializer for the quasi-random sequence
[iref, state, ifail] = ...
nag_rand_quasi_init_scrambled(genid, stype, int64(idim), iskip, nsdigi, state);
% Generate N quasi-random variates
[quas, iref, ifail] = ...
nag_rand_quasi_uniform(int64(n), iref);

% Evaluate the function, and sum
sum = 0;
for i=1:n
tmp = 1;
for d=1:idim
tmp = tmp*abs(4*quas(d,i)-2);
end
sum = sum + tmp;
end

% Convert sum to mean value
vsbl = sum/double(n);
fprintf('\nValue of integral = %8.4f\n', vsbl);
fprintf('\nFirst 10 variates\n');
for i = 1:10
fprintf(' %3d', i);
for j=1:idim
fprintf(' %8.4f', quas(j,i));
end
fprintf('\n');
end
```
```

Value of integral =   1.0169

First 10 variates
1   0.8688   0.9719   0.5375   0.0876   0.4721   0.3800   0.2977   0.1010
2   0.6287   0.3611   0.4963   0.8648   0.0753   0.0174   0.7011   0.2532
3   0.1244   0.5349   0.8645   0.2621   0.7523   0.7212   0.0538   0.6231
4   0.1353   0.4013   0.6656   0.4691   0.9096   0.9272   0.5481   0.4164
5   0.6154   0.6962   0.0321   0.9000   0.2307   0.3186   0.1989   0.7102
6   0.8870   0.0880   0.9947   0.1775   0.3148   0.2059   0.8033   0.9249
7   0.3603   0.7579   0.3633   0.6995   0.5127   0.5328   0.4496   0.2013
8   0.3304   0.1096   0.5034   0.3596   0.0137   0.3643   0.1719   0.8774
9   0.9207   0.7834   0.1357   0.7596   0.8138   0.8825   0.5831   0.2493
10   0.5828   0.4226   0.8287   0.0370   0.7336   0.5189   0.4143   0.4015

```
```function g05yn_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

stype = int64(1);
idim = 8;
iskip = int64(1000);
nsdigi = int64(0);
n = 200;

% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
% Call the initializer for the quasi-random sequence
[iref, state, ifail] = g05yn(genid, stype, int64(idim), iskip, nsdigi, state);
% Generate N quasi-random variates
[quas, iref, ifail] = g05ym(int64(n), iref);

% Evaluate the function, and sum
sum = 0;
for i=1:n
tmp = 1;
for d=1:idim
tmp = tmp*abs(4*quas(d,i)-2);
end
sum = sum + tmp;
end

% Convert sum to mean value
vsbl = sum/double(n);
fprintf('\nValue of integral = %8.4f\n', vsbl);
fprintf('\nFirst 10 variates\n');
for i = 1:10
fprintf(' %3d', i);
for j=1:idim
fprintf(' %8.4f', quas(j,i));
end
fprintf('\n');
end
```
```

Value of integral =   1.0169

First 10 variates
1   0.8688   0.9719   0.5375   0.0876   0.4721   0.3800   0.2977   0.1010
2   0.6287   0.3611   0.4963   0.8648   0.0753   0.0174   0.7011   0.2532
3   0.1244   0.5349   0.8645   0.2621   0.7523   0.7212   0.0538   0.6231
4   0.1353   0.4013   0.6656   0.4691   0.9096   0.9272   0.5481   0.4164
5   0.6154   0.6962   0.0321   0.9000   0.2307   0.3186   0.1989   0.7102
6   0.8870   0.0880   0.9947   0.1775   0.3148   0.2059   0.8033   0.9249
7   0.3603   0.7579   0.3633   0.6995   0.5127   0.5328   0.4496   0.2013
8   0.3304   0.1096   0.5034   0.3596   0.0137   0.3643   0.1719   0.8774
9   0.9207   0.7834   0.1357   0.7596   0.8138   0.8825   0.5831   0.2493
10   0.5828   0.4226   0.8287   0.0370   0.7336   0.5189   0.4143   0.4015

```