mark.core.math
Class Table

java.lang.Object
  |
  +--mark.core.math.Table

public class Table
extends java.lang.Object

Assume a sampling process, where each sample determines two discrete random variables, X1 and X2. The range of Xi is the integers in [0, l(Xi) - 1], where l(Xi) is the number of values Xi can assume. A Table maintains joint counts of X1 and X2. Counts may be doubles, meaning that a count of 0.5, for example, is legal.


Constructor Summary
Table(double[][] a)
          Initializes all counts with the values in the given array.
Table(int X1Length, int X2Length)
          Initializes all counts to zero.
Table(int X1Length, int X2Length, boolean laplace)
          Initializes all counts to zero if not laplace and to one if laplace.
Table(int X1Length, int X2Length, double lidstoneLambda)
          Initializes all counts to zero if not laplace and to one if laplace.
 
Method Summary
 int[] argmax()
          Returns the (X1, X2) value of the largest count in the table.
 int[] argmin()
          Returns the (X1, X2) value of the smallest count in the table.
 double chiSquared()
          Returns the chi-squared value of the given table.
 java.lang.Object clone()
          Clones the Table.
 boolean equals(java.lang.Object rhs)
          Returns whether the Table is equivalent to the given Table.
 void inc(int x1, int x2)
          Increments #(X1 = x1, X2 = x2).
 void inc(int x1, int x2, double c)
          Increments #(X1 = x1, X2 = x2) by the given count.
static void main(java.lang.String[] a)
          Tests the class.
 double max()
          Returns the largest count in the table.
 double min()
          Returns the smallest count in the table.
 double mutualInformation()
          Returns the mutual information of (X1, X2): I(X1;X2).
 void normalizeCounts()
          Divides each count by the sum of the counts.
 double num()
          Returns the total number of samples.
 double numX1(int x1)
          Returns #(X1 = x1).
 double numX1X2(int x1, int x2)
          Returns #(X1 = x1, X2 = x2).
 double numX2(int x2)
          Returns #(X2 = x2).
 Table pointwiseChiSquared()
          Returns a table that contains the pointwise chi squared values of each each (X1, X2) value.
 double pointwiseChiSquared(int x1, int x2)
          Returns the pointwise chi-squared value of (X1, X2).
 double prob()
          Returns 1.0.
 double probX1(int x1)
          Returns p(X1 = x1).
 double probX1X2(int x1, int x2)
          Returns p(X1 = x1, X2 = x2).
 double probX2(int x2)
          Returns p(X2 = x2).
 void reset(boolean laplace)
          Resets all counts to zero if not laplace and to one if laplace.
 void reset(double lidstoneLambda)
          Resets all counts to lidStoneLambda, and that times the vector lengths.
 void set(int x1, int x2, double c)
          Sets #(X1 = x1, X2 = x2).
 double[][] toDoubleArray()
          Returns a representation as a double[][].
 java.lang.String toSSString()
          Returns a string representation of the table, suitable for entry into a spreadsheet.
 java.lang.String toSSStringWithSums()
          Returns a string representation of the table and its sums, suitable for entry into a spreadsheet.
 java.lang.String toSSStringWithSums(java.lang.String[] X1Names, java.lang.String[] X2Names)
          Returns a string representation of the table and its sums, along with a header row and column, suitable for entry into a spreadsheet.
 java.lang.String toString()
          Returns a string representation of the table.
 Table weightedPointwiseMutualInformation()
          Returns a table that contains the weighted pointwise mutual information of each (X1, X2) value.
 double weightedPointwiseMutualInformation(int x1, int x2)
          Returns the weighted pointwise mutual information of (X1, X2).
 double[] X1Counts(int x1)
          Returns a double array that indicates, for the given x1, and for each x2 in the range of X2, #(X1 = x1, X2 = x2).
 int X1Length()
          Returns the number of values X1 can assume.
 double[] X2Counts(int x2)
          Returns a double array that indicates, for the given x2, and for each x1 in the range of X1, #(X1, X2).
 int X2Length()
          Returns the number of values X2 can assume.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Table

public Table(int X1Length,
             int X2Length)
Initializes all counts to zero.

Parameters:
X1Length - l(X1), the number of values X1 can assume.
X2Length - l(X2), the number of values X2 can assume.

Table

public Table(int X1Length,
             int X2Length,
             boolean laplace)
Initializes all counts to zero if not laplace and to one if laplace.

Parameters:
X1Length - the number values X1 can assume.
X2Length - the number values X2 can assume.
laplace - whether to use laplace smoothing.

Table

public Table(int X1Length,
             int X2Length,
             double lidstoneLambda)
Initializes all counts to zero if not laplace and to one if laplace.

Parameters:
X1Length - the number values X1 can assume.
X2Length - the number values X2 can assume.
lidstoneLambda - the quantity of phantom counts to add in smoothing

Table

public Table(double[][] a)
Initializes all counts with the values in the given array.

Parameters:
a - the array.
Method Detail

clone

public java.lang.Object clone()
Clones the Table.

Overrides:
clone in class java.lang.Object
Returns:
the copy.

equals

public boolean equals(java.lang.Object rhs)
Returns whether the Table is equivalent to the given Table.

Overrides:
equals in class java.lang.Object
Parameters:
rhs - the given Table.
Returns:
whether the two Tables are equivalent.

toString

public java.lang.String toString()
Returns a string representation of the table.

Overrides:
toString in class java.lang.Object
Returns:
the representation.

toDoubleArray

public double[][] toDoubleArray()
Returns a representation as a double[][].

Returns:
the double[].

reset

public void reset(boolean laplace)
Resets all counts to zero if not laplace and to one if laplace.

Parameters:
laplace - whether to use laplace smoothing.

reset

public void reset(double lidstoneLambda)
Resets all counts to lidStoneLambda, and that times the vector lengths. This represents placing this many phantom counts in each data cell, and avoids zero counts in a simple way. Good values are commonly between 0.1 and 0.5 (but it depends on the data set).

Parameters:
lidstoneLambda - the number of phantom counts per cell to add

X1Length

public int X1Length()
Returns the number of values X1 can assume.

Returns:
l(X1).

X2Length

public int X2Length()
Returns the number of values X2 can assume.

Returns:
l(X2).

numX1X2

public double numX1X2(int x1,
                      int x2)
Returns #(X1 = x1, X2 = x2).

Parameters:
x1 - the X1 value.
x2 - the X2 value.
Returns:
the count.

numX1

public double numX1(int x1)
Returns #(X1 = x1).

Parameters:
x1 - the X1 value.
Returns:
the count.

numX2

public double numX2(int x2)
Returns #(X2 = x2).

Parameters:
x2 - the X2 value.
Returns:
the count.

num

public double num()
Returns the total number of samples.

Returns:
the count.

probX1X2

public double probX1X2(int x1,
                       int x2)
Returns p(X1 = x1, X2 = x2).

Parameters:
x1 - the X1 value.
x2 - the X2 value.
Returns:
the probability.

probX1

public double probX1(int x1)
Returns p(X1 = x1).

Parameters:
x1 - the X1 value.
Returns:
the probability.

probX2

public double probX2(int x2)
Returns p(X2 = x2).

Parameters:
x2 - the X2 value.
Returns:
the probability.

prob

public double prob()
Returns 1.0.

Returns:
the probability.

set

public void set(int x1,
                int x2,
                double c)
Sets #(X1 = x1, X2 = x2).

Parameters:
x1 - the X1 value.
x2 - the X2 value.
c - the count.

inc

public void inc(int x1,
                int x2)
Increments #(X1 = x1, X2 = x2).

Parameters:
x1 - the X1 value.
x2 - the X2 value.

inc

public void inc(int x1,
                int x2,
                double c)
Increments #(X1 = x1, X2 = x2) by the given count.

Parameters:
x1 - the X1 value.
x2 - the X2 value.
c - the count.

normalizeCounts

public void normalizeCounts()
Divides each count by the sum of the counts.


main

public static void main(java.lang.String[] a)
                 throws java.lang.Exception
Tests the class. This code assumes that toDoubleArray works.

Parameters:
a - ignored.
java.lang.Exception

max

public double max()
Returns the largest count in the table.

Returns:
th largest count in the table.

min

public double min()
Returns the smallest count in the table.

Returns:
the smallest count in the table.

argmax

public int[] argmax()
Returns the (X1, X2) value of the largest count in the table.

Returns:
a two-element array representing the (X1, X2) value of the largest count in the table.

argmin

public int[] argmin()
Returns the (X1, X2) value of the smallest count in the table.

Returns:
a two-element array representing the (X1, X2) value of the smallest count in the table.

X1Counts

public double[] X1Counts(int x1)
Returns a double array that indicates, for the given x1, and for each x2 in the range of X2, #(X1 = x1, X2 = x2).

Parameters:
x1 - the X1 value.
Returns:
the double array.

X2Counts

public double[] X2Counts(int x2)
Returns a double array that indicates, for the given x2, and for each x1 in the range of X1, #(X1, X2).

Parameters:
x2 - the X2 value.
Returns:
the double array.

chiSquared

public double chiSquared()
Returns the chi-squared value of the given table.

Returns:
the chi squared value.

pointwiseChiSquared

public Table pointwiseChiSquared()
Returns a table that contains the pointwise chi squared values of each each (X1, X2) value.

Returns:
the table containing the pointwise chi squared information values.

pointwiseChiSquared

public double pointwiseChiSquared(int x1,
                                  int x2)
Returns the pointwise chi-squared value of (X1, X2).

Parameters:
x1 - the X1 value.
x2 - the X2 value.
Returns:
the chi-squared value.

mutualInformation

public double mutualInformation()
Returns the mutual information of (X1, X2): I(X1;X2).

Returns:
the mutual information.

weightedPointwiseMutualInformation

public Table weightedPointwiseMutualInformation()
Returns a table that contains the weighted pointwise mutual information of each (X1, X2) value.

Returns:
the table containing the weighted pointwise mutual information values.

weightedPointwiseMutualInformation

public double weightedPointwiseMutualInformation(int x1,
                                                 int x2)
Returns the weighted pointwise mutual information of (X1, X2).

Parameters:
x1 - the X1 value.
x2 - the X2 value.
Returns:
the weighted pointwise mutual information.

toSSString

public java.lang.String toSSString()
Returns a string representation of the table, suitable for entry into a spreadsheet.

Returns:
the string representation.

toSSStringWithSums

public java.lang.String toSSStringWithSums()
Returns a string representation of the table and its sums, suitable for entry into a spreadsheet.

Returns:
the string representation.

toSSStringWithSums

public java.lang.String toSSStringWithSums(java.lang.String[] X1Names,
                                           java.lang.String[] X2Names)
                                    throws java.lang.Exception
Returns a string representation of the table and its sums, along with a header row and column, suitable for entry into a spreadsheet.

Parameters:
X1Names - defines the X1 titles.
X2Names - defines the X2 titles.
Returns:
the string representation.
Throws:
java.lang.Exception - if something goes wrong.