tableTwoway

Tallies observations into two-way frequency table.

Synopsis

tableTwoway (x, y, nx, ny)

Required Arguments

float x[] (Input)
Array of length nObservations containing the data for the first variable.
float y[] (Input)
Array of length nObservations containing the data for the second variable.
int nx (Input)
Number of intervals (bins) for variable x.
int ny (Input)
Number of intervals (bins) for variable y.

Return Value

An array of size nx by ny containing the counts.

Optional Arguments

dataBounds, xlo, xhi, ylo, yhi (Output)
If none is specified or if dataBounds is specified, nIntervals intervals of equal length are used. Let xmin and xmax be the minimum and maximum values in x, respectively, with similar meanings for ymin and ymax. Then, table[0] is the tally of observations with the x value less than or equal to xmin + (xmaxxmin)/nx, and the y value less than or equal to ymin + (ymaxymin)/ny. When dataBounds is explicitly specified, the minimum and maximum values in x and y are output in xmin, xmax, ymin, and ymax.

or

knownBounds, float xlo, float xhi, float ylo, float yhi (Input)
Intervals of equal lengths are used just as in the case of dataBounds, except the upper and lower bounds are taken as the user supplied variables xlo, xhi, ylo, and yhi, instead of the actual minima and maxima in the data. Therefore, the first and last intervals for both variables are semi-infinite in length. Arguments nx and ny must be greater than or equal to 3.

or

cutpoints, float cx[], float cy[] (Input)
If cutpoints is specified, cutpoints (boundaries) must be provided in the arrays cx and cy, of length (nx-1) and (ny-1) respectively. The tally in table[0] is the number of observations for which the x value is less than or equal to cx[0], and the y value is less than or equal to cy[0]. This option allows unequal interval lengths. Arguments nx and ny must be greater than or equal to 2.

or

classMarks, float cx[], float cy[] (Input)

If classMarks is specified, equally spaced class marks in ascending order must be provided in the arrays cx and cy. The class marks are the midpoints of each interval. Each interval is taken to have length cx[1]cx[0] in the x direction and cy[1]cy[0] in the y direction. The total number of elements in table may be less than nObservations. Arguments nx and ny must be greater than or equal to 2.

None or exactly one of the four optional arguments described above can be specified in order to define the intervals or bins for the one-way table.

Examples

Example 1

The data for x in this example are the same as those used in the examples for tableOneway. The data for y was created by adding small integers to the data in x. This example uses the default tally method, dataBounds, which may be appropriate when the range of the data is unknown.

from numpy import *
from pyimsl.stat.tableTwoway import tableTwoway
from pyimsl.stat.writeMatrix import writeMatrix

n_intervals = 10
x = array([0.77, 1.74, 0.81, 1.20, 1.95, 1.20, 0.47, 1.43, 3.37,
           2.20, 3.00, 3.09, 1.51, 2.10, 0.52, 1.62, 1.31, 0.32,
           0.59, 0.81, 2.81, 1.87, 1.18, 1.35, 4.75, 2.48, 0.96,
           1.89, 0.90, 2.05])
y = array([1.77, 3.74, 3.81, 2.20, 3.95, 4.20, 1.47, 3.43, 6.37,
           3.20, 5.00, 6.09, 2.51, 4.10, 3.52, 2.62, 3.31, 3.32,
           1.59, 2.81, 5.81, 2.87, 3.18, 4.35, 5.75, 4.48, 3.96,
           2.89, 2.90, 5.05])
table = tableTwoway(x, y, 5, 6)
writeMatrix("Counts", table,
            rowNumberZero=True, colNumberZero=True, writeFormat="%8i")

Output

 
                           Counts
          0         1         2         3         4         5
0         4         2         4         2         0         0
1         0         4         3         2         1         0
2         0         0         1         2         0         1
3         0         0         0         0         1         2
4         0         0         0         0         0         1

Example 2

In this example, xlo, xhi, ylo, and yhi are chosen so that the intervals will be 0 to 1, 1 to 2, and so on for x, and 1 to 2, 2 to 3, and so on for y.

from numpy import *
from pyimsl.stat.tableTwoway import tableTwoway
from pyimsl.stat.writeMatrix import writeMatrix

n_intervals = 10
x = array([0.77, 1.74, 0.81, 1.20, 1.95, 1.20, 0.47, 1.43, 3.37,
           2.20, 3.00, 3.09, 1.51, 2.10, 0.52, 1.62, 1.31, 0.32,
           0.59, 0.81, 2.81, 1.87, 1.18, 1.35, 4.75, 2.48, 0.96,
           1.89, 0.90, 2.05])
y = array([1.77, 3.74, 3.81, 2.20, 3.95, 4.20, 1.47, 3.43, 6.37,
           3.20, 5.00, 6.09, 2.51, 4.10, 3.52, 2.62, 3.31, 3.32,
           1.59, 2.81, 5.81, 2.87, 3.18, 4.35, 5.75, 4.48, 3.96,
           2.89, 2.90, 5.05])
knownBounds = {}
knownBounds["xlo"] = 1.0
knownBounds["xhi"] = 4.0
knownBounds["ylo"] = 2.0
knownBounds["yhi"] = 6.0
table = tableTwoway(x, y, 5, 6, knownBounds=knownBounds)
writeMatrix("Counts", table,
            rowNumberZero=True, colNumberZero=True, writeFormat="%8i")

Output

 
                           Counts
          0         1         2         3         4         5
0         3         2         4         0         0         0
1         0         5         5         2         0         0
2         0         0         1         3         2         0
3         0         0         0         0         0         2
4         0         0         0         0         1         0

Example 3

In this example, the class boundaries are input in cx and cy. The same intervals are chosen as in Example 2, where the first element of cx and cy specify the first cutpoint between classes.

from numpy import *
from pyimsl.stat.tableTwoway import tableTwoway
from pyimsl.stat.writeMatrix import writeMatrix

n_intervals = 10
cmx = [0.5, 1.5, 2.5, 3.5, 4.5]
cmy = [1.5, 2.5, 3.5, 4.5, 5.5, 6.5]
x = array([0.77, 1.74, 0.81, 1.20, 1.95, 1.20, 0.47, 1.43, 3.37,
           2.20, 3.00, 3.09, 1.51, 2.10, 0.52, 1.62, 1.31, 0.32,
           0.59, 0.81, 2.81, 1.87, 1.18, 1.35, 4.75, 2.48, 0.96,
           1.89, 0.90, 2.05])
y = array([1.77, 3.74, 3.81, 2.20, 3.95, 4.20, 1.47, 3.43, 6.37,
           3.20, 5.00, 6.09, 2.51, 4.10, 3.52, 2.62, 3.31, 3.32,
           1.59, 2.81, 5.81, 2.87, 3.18, 4.35, 5.75, 4.48, 3.96,
           2.89, 2.90, 5.05])
classMarks = {}
classMarks["cx"] = cmx
classMarks["cy"] = cmy
table = tableTwoway(x, y, 5, 6, classMarks=classMarks)
writeMatrix("Counts", table,
            rowNumberZero=True, colNumberZero=True, writeFormat="%8i")

Output

 
                           Counts
          0         1         2         3         4         5
0         3         2         4         0         0         0
1         0         5         5         2         0         0
2         0         0         1         3         2         0
3         0         0         0         0         0         2
4         0         0         0         0         1         0

Example 4

This example, uses the cutpoints tally option with cutpoints such that the intervals are specified as in the previous examples.

from numpy import *
from pyimsl.stat.tableTwoway import tableTwoway
from pyimsl.stat.writeMatrix import writeMatrix

n_intervals = 10
cpx = [1, 2, 3, 4]
cpy = [2, 3, 4, 5, 6]
cmx = [0.5, 1.5, 2.5, 3.5, 4.5]
cmy = [1.5, 2.5, 3.5, 4.5, 5.5, 6.5]
x = array([0.77, 1.74, 0.81, 1.20, 1.95, 1.20, 0.47, 1.43, 3.37,
           2.20, 3.00, 3.09, 1.51, 2.10, 0.52, 1.62, 1.31, 0.32,
           0.59, 0.81, 2.81, 1.87, 1.18, 1.35, 4.75, 2.48, 0.96,
           1.89, 0.90, 2.05])
y = array([1.77, 3.74, 3.81, 2.20, 3.95, 4.20, 1.47, 3.43, 6.37,
           3.20, 5.00, 6.09, 2.51, 4.10, 3.52, 2.62, 3.31, 3.32,
           1.59, 2.81, 5.81, 2.87, 3.18, 4.35, 5.75, 4.48, 3.96,
           2.89, 2.90, 5.05])
cutpoints = {}
cutpoints["cx"] = cpx
cutpoints["cy"] = cpy
table = tableTwoway(x, y, 5, 6, cutpoints=cutpoints)
writeMatrix("Counts", table,
            rowNumberZero=True, colNumberZero=True, writeFormat="%8i")

Output

 
                           Counts
          0         1         2         3         4         5
0         3         2         4         0         0         0
1         0         5         5         2         0         0
2         0         0         1         3         2         0
3         0         0         0         0         0         2
4         0         0         0         0         1         0