tableOneway

Tallies observations into a one-way frequency table.

Synopsis

tableOneway (x, nIntervals)

Required Arguments

float x[] (Input)
Array of length nObservations containing the observations.
int nIntervals (Input)
Number of intervals (bins).

Return Value

An array of length nIntervals containing the counts.

Optional Arguments

dataBounds, float minimum, float maximum (Output)
or
knownBounds, float lowerBound, float upperBound (Input)
or
cutpoints, float[] (Input)
or
classMarks, float[] (Input)

None, or exactly one, of these four optional arguments can be specified in order to define the intervals or bins for the one-way table. If none is specified, or if dataBounds is specified, nIntervals, intervals of equal length, are used with the initial interval starting with the minimum value in x and the last interval ending with the maximum value in x. The initial interval is closed on the left and right. The remaining intervals are open on the left and closed on the right. When dataBounds is explicitly specified, the minimum and maximum values in x are output in minimum and maximum. With this option, each interval is of (maximumminimum)∕nIntervals length. If knownBounds is specified, two semi-infinite intervals are used as the initial and last interval. The initial interval is closed on the right and includes lowerBound as its right endpoint. The last interval is open on the left and includes all values greater than upperBound. The remaining nIntervals − 2 intervals are each of length

\[\frac{\mathit{upperBound}-\mathit{lowerBound}}{\mathit{nIntervals}-2}\]

and are open on the left and closed on the right. Argument nIntervals must be greater than or equal to three for this option. If classMarks is specified, equally spaced class marks in ascending order must be provided in the array classMarks of length nIntervals. The class marks are the midpoints of each of the nIntervals, and each interval is taken to have length classMarks[1] − classMarks[0]. The argument nIntervals must be greater than or equal to two for this option. If cutpoints is specified, cutpoints (boundaries) must be provided in the array cutpoints of length nIntervals − 1. This option allows unequal interval lengths. The initial interval is closed on the right and includes the initial cutpoint as its right endpoint. The last interval is open on the left and includes all values greater than the last cutpoint. The remaining nIntervals − 2 intervals are open on the left and closed on the right. The argument nIntervals must be greater than or equal to three for this option.

Examples

Example 1

The data for this example is from Hinkley (1977) and Velleman and Hoaglin (1981). They are the measurements (in inches) of precipitation in Minneapolis/St. Paul during the month of March for 30 consecutive years.

from numpy import *
from pyimsl.math.tableOneway import tableOneway
from pyimsl.math.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])

table = tableOneway(x, n_intervals)
writeMatrix('counts', table)

Output

 
                                   counts
          1            2            3            4            5            6
          4            8            5            5            3            1
 
          7            8            9           10
          3            0            0            1

Example 2

This example selects knownBounds and sets lowerBound = 0.5 and upperBound = 4.5 so that the eight interior intervals each have width \((4.5-0.5)/(10-2)=0.5\). The 10 intervals are \(\left(-\infty,0.5\right],\left(0.5,1.0\right],\ldots,\left(4.0,4.5\right],\text{ and }\left(4.5,\infty\right]\).

from numpy import *
from pyimsl.math.tableOneway import tableOneway
from pyimsl.math.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])
table = tableOneway(x, n_intervals,
                    knownBounds={'lowerBound': 0.5, 'upperBound': 4.5})
writeMatrix('counts', table)

Output

 
                                   counts
          1            2            3            4            5            6
          2            7            6            6            4            2
 
          7            8            9           10
          2            0            0            1

Example 3

This example inputs 10 class marks 0.25, 0.75, 1.25, …, 4.75. This defines the class intervals \(\left(0.0,0.5\right],\left(0.5,1.0\right], \ldots,\left(4.0,4.5\right],\left(4.5,5.0\right]\). Note that unlike the previous example, the initial and last intervals are the same length as the remaining intervals.

from numpy import *
from pyimsl.math.tableOneway import tableOneway
from pyimsl.math.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])
class_marks = [0.25, 0.75, 1.25, 1.75, 2.25, 2.75,
               3.25, 3.75, 4.25, 4.75]
table = tableOneway(x, n_intervals, classMarks=class_marks)
writeMatrix('table', table)

Output

 
                                    table
          1            2            3            4            5            6
          2            7            6            6            4            2
 
          7            8            9           10
          2            0            0            1

Example 4

This example inputs nine cutpoints 0.5, 1.0, 1.5, 2.0, …, 4.5 to define the same 10 intervals as in Example 3. Here again, the initial and last intervals are semi-infinite intervals.

from numpy import *
from pyimsl.math.tableOneway import tableOneway
from pyimsl.math.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])
cutpoints = [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
table = tableOneway(x, n_intervals, cutpoints=cutpoints)
writeMatrix('counts', table)

Output

 
                                   counts
          1            2            3            4            5            6
          2            7            6            6            4            2
 
          7            8            9           10
          2            0            0            1