PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_rand_matrix_2waytable (g05pz)
Purpose
nag_rand_matrix_2waytable (g05pz) generates a random two-way table.
Syntax
[
r,
state,
x,
ifail] = g05pz(
mode,
totr,
totc,
r,
state, 'nrow',
nrow, 'ncol',
ncol, 'lr',
lr)
[
r,
state,
x,
ifail] = nag_rand_matrix_2waytable(
mode,
totr,
totc,
r,
state, 'nrow',
nrow, 'ncol',
ncol, 'lr',
lr)
Description
Given row totals and column totals (with , say),
nag_rand_matrix_2waytable (g05pz) will generate a pseudorandom two-way table of integers such that the row and column totals are satisfied.
The method used is based on that described by
Patefield (1981) which is most efficient when
is large relative to the number of table entries
(i.e.,
). Entries are generated one row at a time and one entry at a time within a row. Each entry is generated using the conditional probability distribution for that entry given the entries in the previous rows and the previous entries in the same row.
A reference vector is used to store computed values that can be reused in the generation of new tables with the same row and column totals. nag_rand_matrix_2waytable (g05pz) can be called to simply set up the reference vector, or to generate a two-way table using a reference vector set up in a previous call, or it can combine both functions in a single call.
One of the initialization functions
nag_rand_init_repeat (g05kf) (for a repeatable sequence if computed sequentially) or
nag_rand_init_nonrepeat (g05kg) (for a non-repeatable sequence) must be called prior to the first call to
nag_rand_matrix_2waytable (g05pz).
References
Patefield W M (1981) An efficient method of generating tables with given row and column totals Appl. Stats. 30 91–97
Parameters
Compulsory Input Parameters
- 1:
– int64int32nag_int scalar
-
A code for selecting the operation to be performed by the function.
- Set up reference vector only.
- Generate two-way table using reference vector set up in a prior call to nag_rand_matrix_2waytable (g05pz).
- Set up reference vector and generate two-way table.
Constraint:
, or .
- 2:
– int64int32nag_int array
-
The row totals,
, for .
Constraints:
- , for ;
- ;
- , for .
- 3:
– int64int32nag_int array
-
The column totals,
, for .
Constraints:
- , for ;
- .
- 4:
– double array
-
If , the reference vector from the previous call to nag_rand_matrix_2waytable (g05pz).
- 5:
– 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.
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the array
totr.
, the number of rows in the table.
Constraint:
.
- 2:
– int64int32nag_int scalar
-
Default:
the dimension of the array
totc.
, the number of columns in the table.
Constraint:
.
- 3:
– int64int32nag_int scalar
-
Default:
the dimension of the array
r.
The dimension of the array
r.
Constraint:
.
Output Parameters
- 1:
– double array
-
The reference vector.
- 2:
– int64int32nag_int array
-
Contains updated information on the state of the generator.
- 3:
– int64int32nag_int array
-
If or , a pseudorandom two-way by table, , with element containing the th entry in the table such that and
- 4:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
Constraint: , or .
-
-
Constraint: .
-
-
Constraint: .
-
-
On entry, at least one element of
totr is negative or
totr sums to zero.
-
-
On entry,
totc has at least one negative element.
-
-
nrow or
ncol is not the same as when
r was set up in a previous call.
-
-
On entry,
lr is not large enough,
: minimum length required .
-
-
On entry,
state vector has been corrupted or not initialized.
-
-
Constraint: .
-
-
On entry, the arrays
totr and
totc do not sum to the same total.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
None.
Further Comments
None.
Example
Following initialization of the pseudorandom number generator by a call to
nag_rand_init_repeat (g05kf), this example generates and prints a
by
two-way table, with row totals of
,
,
and
respectively, and column totals of
,
and
respectively.
Open in the MATLAB editor:
g05pz_example
function g05pz_example
fprintf('g05pz example results\n\n');
seed = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);
totr = [int64( 9); 11; 7; 23];
totc = [int64(16); 17; 17];
mode = int64(2);
lr = sum(totr) + 5;
r = zeros(lr, 1);
[r, state, x, ifail] = g05pz( ...
mode, totr, totc, r, state);
disp('Random Table')
disp(double(x));
disp('Supplied row totals:');
disp(double(totr)');
disp('Supplied column totals:')
disp(double(totc)');
g05pz example results
Random Table
2 4 3
6 1 4
2 4 1
6 8 9
Supplied row totals:
9 11 7 23
Supplied column totals:
16 17 17
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015