NAG Library Routine Document
G05PZF
1 Purpose
G05PZF generates a random twoway table.
2 Specification
SUBROUTINE G05PZF ( 
MODE, NROW, NCOL, TOTR, TOTC, R, LR, STATE, X, LDX, IFAIL) 
INTEGER 
MODE, NROW, NCOL, TOTR(NROW), TOTC(NCOL), LR, STATE(*), X(LDX,NCOL), LDX, IFAIL 
REAL (KIND=nag_wp) 
R(LR) 

3 Description
Given $m$ row totals ${R}_{i}$ and $n$ column totals ${C}_{j}$ (with $\sum _{i=1}^{m}}{R}_{i}={\displaystyle \sum _{j=1}^{n}}{C}_{j}=T$, say),
G05PZF will generate a pseudorandom twoway 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\times 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. G05PZF can be called to simply set up the reference vector, or to generate a twoway 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 routines
G05KFF (for a repeatable sequence if computed sequentially) or
G05KGF (for a nonrepeatable sequence) must be called prior to the first call to G05PZF.
4 References
Patefield W M (1981) An efficient method of generating $R\times C$ tables with given row and column totals Appl. Stats. 30 91–97
5 Arguments
 1: $\mathrm{MODE}$ – INTEGERInput

On entry: a code for selecting the operation to be performed by the routine.
 ${\mathbf{MODE}}=0$
 Set up reference vector only.
 ${\mathbf{MODE}}=1$
 Generate twoway table using reference vector set up in a prior call to G05PZF.
 ${\mathbf{MODE}}=2$
 Set up reference vector and generate twoway table.
Constraint:
${\mathbf{MODE}}=0$, $1$ or $2$.
 2: $\mathrm{NROW}$ – INTEGERInput

On entry: $m$, the number of rows in the table.
Constraint:
${\mathbf{NROW}}\ge 2$.
 3: $\mathrm{NCOL}$ – INTEGERInput

On entry: $n$, the number of columns in the table.
Constraint:
${\mathbf{NCOL}}\ge 2$.
 4: $\mathrm{TOTR}\left({\mathbf{NROW}}\right)$ – INTEGER arrayInput

On entry: 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)={\displaystyle \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$.
 5: $\mathrm{TOTC}\left({\mathbf{NCOL}}\right)$ – INTEGER arrayInput

On entry: 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)={\displaystyle \sum _{i=1}^{m}}{\mathbf{TOTR}}\left(i\right)$.
 6: $\mathrm{R}\left({\mathbf{LR}}\right)$ – REAL (KIND=nag_wp) arrayCommunication Array

On entry: if ${\mathbf{MODE}}=1$, the reference vector from the previous call to G05PZF.
On exit: the reference vector.
 7: $\mathrm{LR}$ – INTEGERInput

On entry: the dimension of the array
R as declared in the (sub)program from which G05PZF is called.
Constraint:
${\mathbf{LR}}\ge {\displaystyle \sum _{i=1}^{m}}{\mathbf{TOTR}}\left(i\right)+5$.
 8: $\mathrm{STATE}\left(*\right)$ – INTEGER arrayCommunication Array

Note: the actual argument supplied
must be the array
STATE supplied to the initialization routines
G05KFF or
G05KGF.
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
 9: $\mathrm{X}\left({\mathbf{LDX}},{\mathbf{NCOL}}\right)$ – INTEGER arrayOutput

On exit: if ${\mathbf{MODE}}=1$ or $2$, a pseudorandom twoway $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)$
 10: $\mathrm{LDX}$ – INTEGERInput

On entry: the first dimension of the array
X as declared in the (sub)program from which G05PZF is called.
Constraint:
${\mathbf{LDX}}\ge {\mathbf{NROW}}$.
 11: $\mathrm{IFAIL}$ – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
$1\text{ or}1$ is recommended. If the output of error messages is undesirable, then the value
$1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is
$0$.
When the value $\mathbf{1}\text{ or}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit:
${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see
Section 6).
6 Error Indicators and Warnings
If on entry
${\mathbf{IFAIL}}=0$ or
$1$, explanatory error messages are output on the current error message unit (as defined by
X04AAF).
Errors or warnings detected by the routine:
 ${\mathbf{IFAIL}}=1$

On entry, ${\mathbf{MODE}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{MODE}}=0$, $1$ or $2$.
 ${\mathbf{IFAIL}}=2$

On entry, ${\mathbf{NROW}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{NROW}}\ge 2$.
 ${\mathbf{IFAIL}}=3$

On entry, ${\mathbf{NCOL}}=\u2329\mathit{\text{value}}\u232a$.
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.
Previous value of
${\mathbf{NROW}}=\u2329\mathit{\text{value}}\u232a$ and
${\mathbf{NROW}}=\u2329\mathit{\text{value}}\u232a$.
Previous value of
${\mathbf{NCOL}}=\u2329\mathit{\text{value}}\u232a$ and
${\mathbf{NCOL}}=\u2329\mathit{\text{value}}\u232a$.
 ${\mathbf{IFAIL}}=7$

On entry,
LR is not large enough,
${\mathbf{LR}}=\u2329\mathit{\text{value}}\u232a$: minimum length required
$\text{}=\u2329\mathit{\text{value}}\u232a$.
 ${\mathbf{IFAIL}}=8$

On entry,
STATE vector has been corrupted or not initialized.
 ${\mathbf{IFAIL}}=10$

On entry, ${\mathbf{LDX}}=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{NROW}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{NROW}}$.
 ${\mathbf{IFAIL}}=15$

On entry, the arrays
TOTR and
TOTC do not sum to the same total:
TOTR array total is
$\u2329\mathit{\text{value}}\u232a$,
TOTC array total is
$\u2329\mathit{\text{value}}\u232a$.
 ${\mathbf{IFAIL}}=99$
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{IFAIL}}=399$
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{IFAIL}}=999$
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
7 Accuracy
None.
8 Parallelism and Performance
G05PZF is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the
X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the
Users' Note for your implementation for any additional implementationspecific information.
None.
10 Example
Following initialization of the pseudorandom number generator by a call to
G05KFF, this example generates and prints a
$4$ by
$3$ twoway table, with row totals of
$9$,
$11$,
$7$ and
$23$ respectively, and column totals of
$16$,
$17$ and
$17$ respectively.
10.1 Program Text
Program Text (g05pzfe.f90)
10.2 Program Data
Program Data (g05pzfe.d)
10.3 Program Results
Program Results (g05pzfe.r)