hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_rand_matrix_2waytable (g05pz)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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 m row totals Ri and n column totals Cj (with i=1m Ri=j=1n Cj=T, 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 T is large relative to the number of table entries m×n (i.e., T>2mn). 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 R×C tables with given row and column totals Appl. Stats. 30 91–97

Parameters

Compulsory Input Parameters

1:     mode int64int32nag_int scalar
A code for selecting the operation to be performed by the function.
mode=0
Set up reference vector only.
mode=1
Generate two-way table using reference vector set up in a prior call to nag_rand_matrix_2waytable (g05pz).
mode=2
Set up reference vector and generate two-way table.
Constraint: mode=0, 1 or 2.
2:     totrnrow int64int32nag_int array
The m row totals, Ri, for i=1,2,,m.
Constraints:
  • totri0, for i=1,2,,m;
  • i=1mtotri=j=1ntotcj;
  • i totri>0, for i=1,2,,m.
3:     totcncol int64int32nag_int array
The n column totals, Cj, for j=1,2,,n.
Constraints:
  • totcj0, for j=1,2,,n;
  • j=1ntotcj=i=1mtotri.
4:     rlr – double array
If mode=1, the reference vector from the previous call to nag_rand_matrix_2waytable (g05pz).
5:     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.

Optional Input Parameters

1:     nrow int64int32nag_int scalar
Default: the dimension of the array totr.
m, the number of rows in the table.
Constraint: nrow2.
2:     ncol int64int32nag_int scalar
Default: the dimension of the array totc.
n, the number of columns in the table.
Constraint: ncol2.
3:     lr int64int32nag_int scalar
Default: the dimension of the array r.
The dimension of the array r.
Constraint: lri=1mtotri+5.

Output Parameters

1:     rlr – double array
The reference vector.
2:     state: int64int32nag_int array
Contains updated information on the state of the generator.
3:     xldxncol int64int32nag_int array
If mode=1 or 2, a pseudorandom two-way m by n table, X, with element xij containing the i,jth entry in the table such that i=1 m xij=totcj and j=1 n xij=totri
4:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
   ifail=1
Constraint: mode=0, 1 or 2.
   ifail=2
Constraint: nrow2.
   ifail=3
Constraint: ncol2.
   ifail=4
On entry, at least one element of totr is negative or totr sums to zero.
   ifail=5
On entry, totc has at least one negative element.
   ifail=6
nrow or ncol is not the same as when r was set up in a previous call.
   ifail=7
On entry, lr is not large enough, lr=_: minimum length required .
   ifail=8
On entry, state vector has been corrupted or not initialized.
   ifail=10
Constraint: ldxnrow.
   ifail=15
On entry, the arrays totr and totc do not sum to the same total.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
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 4 by 3 two-way table, with row totals of 9, 11, 7 and 23 respectively, and column totals of 16, 17 and 17 respectively.
function g05pz_example


fprintf('g05pz example results\n\n');

% Initialize the base generator to a repeatable sequence
seed  = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
                        genid, subid, seed);

% row and column totals
totr = [int64( 9); 11;  7; 23];
totc = [int64(16); 17; 17];

% Set up and generate in one go
mode = int64(2);

% Generate the random table with given totals
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)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015