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_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 $m$ row totals ${R}_{i}$ and $n$ column totals ${C}_{j}$ (with $\sum _{i=1}^{m}{R}_{i}=\sum _{j=1}^{n}{C}_{j}=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:     $\mathrm{mode}$int64int32nag_int scalar
A code for selecting the operation to be performed by the function.
${\mathbf{mode}}=0$
Set up reference vector only.
${\mathbf{mode}}=1$
Generate two-way table using reference vector set up in a prior call to nag_rand_matrix_2waytable (g05pz).
${\mathbf{mode}}=2$
Set up reference vector and generate two-way table.
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
2:     $\mathrm{totr}\left({\mathbf{nrow}}\right)$int64int32nag_int array
The $m$ row totals, ${R}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,m$.
Constraints:
• ${\mathbf{totr}}\left(\mathit{i}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,m$;
• $\sum _{i=1}^{m}{\mathbf{totr}}\left(i\right)=\sum _{j=1}^{n}{\mathbf{totc}}\left(j\right)$;
• ${\sum }_{\mathit{i}}{\mathbf{totr}}\left(\mathit{i}\right)>0$, for $\mathit{i}=1,2,\dots ,m$.
3:     $\mathrm{totc}\left({\mathbf{ncol}}\right)$int64int32nag_int array
The $n$ column totals, ${C}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,n$.
Constraints:
• ${\mathbf{totc}}\left(\mathit{j}\right)\ge 0$, for $\mathit{j}=1,2,\dots ,n$;
• $\sum _{j=1}^{n}{\mathbf{totc}}\left(j\right)=\sum _{i=1}^{m}{\mathbf{totr}}\left(i\right)$.
4:     $\mathrm{r}\left({\mathbf{lr}}\right)$ – double array
If ${\mathbf{mode}}=1$, the reference vector from the previous call to nag_rand_matrix_2waytable (g05pz).
5:     $\mathrm{state}\left(:\right)$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:     $\mathrm{nrow}$int64int32nag_int scalar
Default: the dimension of the array totr.
$m$, the number of rows in the table.
Constraint: ${\mathbf{nrow}}\ge 2$.
2:     $\mathrm{ncol}$int64int32nag_int scalar
Default: the dimension of the array totc.
$n$, the number of columns in the table.
Constraint: ${\mathbf{ncol}}\ge 2$.
3:     $\mathrm{lr}$int64int32nag_int scalar
Default: the dimension of the array r.
The dimension of the array r.
Constraint: ${\mathbf{lr}}\ge \sum _{i=1}^{m}{\mathbf{totr}}\left(i\right)+5$.

### Output Parameters

1:     $\mathrm{r}\left({\mathbf{lr}}\right)$ – double array
The reference vector.
2:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Contains updated information on the state of the generator.
3:     $\mathrm{x}\left(\mathit{ldx},{\mathbf{ncol}}\right)$int64int32nag_int array
If ${\mathbf{mode}}=1$ or $2$, a pseudorandom two-way $m$ by $n$ table, $X$, with element ${\mathbf{x}}\left(i,j\right)$ containing the $\left(i,j\right)$th entry in the table such that $\sum _{\mathit{i}=1}^{m}{\mathbf{x}}\left(i,j\right)={\mathbf{totc}}\left(j\right)$ and $\sum _{\mathit{j}=1}^{n}{\mathbf{x}}\left(i,j\right)={\mathbf{totr}}\left(i\right)$
4:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{nrow}}\ge 2$.
${\mathbf{ifail}}=3$
Constraint: ${\mathbf{ncol}}\ge 2$.
${\mathbf{ifail}}=4$
On entry, at least one element of totr is negative or totr sums to zero.
${\mathbf{ifail}}=5$
On entry, totc has at least one negative element.
${\mathbf{ifail}}=6$
nrow or ncol is not the same as when r was set up in a previous call.
${\mathbf{ifail}}=7$
On entry, lr is not large enough, ${\mathbf{lr}}=_$: minimum length required .
${\mathbf{ifail}}=8$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=10$
Constraint: $\mathit{ldx}\ge {\mathbf{nrow}}$.
${\mathbf{ifail}}=15$
On entry, the arrays totr and totc do not sum to the same total.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

None.

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

```