cc.mallet.grmm.types
Class AbstractTableFactor

java.lang.Object
  extended by cc.mallet.grmm.types.AbstractTableFactor
All Implemented Interfaces:
DiscreteFactor, Factor, java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
LogTableFactor, TableFactor

public abstract class AbstractTableFactor
extends java.lang.Object
implements DiscreteFactor

Class for a multivariate multinomial distribution.

Created: Mon Sep 15 17:19:24 2003

Version:
$Id: AbstractTableFactor.java,v 1.1 2007/10/22 21:37:44 mccallum Exp $
Author:
Charles Sutton
See Also:
Serialized Form

Field Summary
protected  Matrix probs
           
 
Constructor Summary
  AbstractTableFactor()
          Creates an identity potential over NO variables.
  AbstractTableFactor(AbstractTableFactor in)
          Copy constructor.
  AbstractTableFactor(AbstractTableFactor ptl, double[] probs)
          Creates a potential with the same variables as another, but different probabilites.
protected AbstractTableFactor(BidirectionalIntObjectMap varMap)
           
  AbstractTableFactor(java.util.Collection allVars)
          Creates an identity potential with the given variables.
  AbstractTableFactor(Variable var)
          Creates an identity potential over the given variable.
  AbstractTableFactor(Variable[] allVars)
          Creates an identity potential with the given variables.
  AbstractTableFactor(Variable[] allVars, double[] probs)
          Creates a potential with the given variables and the given probabilities.
  AbstractTableFactor(Variable[] allVars, Matrix probsIn)
          Creates a potential with the given variables and the given probabilities.
  AbstractTableFactor(Variable var, double[] values)
           
  AbstractTableFactor(VarSet allVars, double[] probs)
          Creates a potential with the given variables and the given probabilities.
  AbstractTableFactor(VarSet allVars, Matrix probsIn)
          Creates a potential with the given variables and the given probabilities.
 
Method Summary
 boolean almostEquals(Factor p)
          Returns whether this is almost equal to another potential.
 boolean almostEquals(Factor p, double epsilon)
           
 int argmax()
          Returns the assignment that maximizes this potential.
 AssignmentIterator assignmentIterator()
          Returns an iterator over all Assignmentss to this potential.
 AbstractTableFactor asTable()
           
 java.lang.Object clone()
           
 boolean containsVar(Variable var)
          Returns true iff this potential is over the given variable
protected abstract  AbstractTableFactor createBlankSubset(Variable[] vars)
           
 void divideBy(double v)
           
 void divideBy(Factor pot)
          Does the conceptual equivalent of this /= pot.
protected abstract  void divideByInternal(DiscreteFactor ptl)
           
 java.lang.String dumpToString()
           
abstract  Factor duplicate()
           
protected  DiscreteFactor ensureOperandCompatible(DiscreteFactor ptl)
          Ensures that this.inLogSpace == ptl.inLogSpace.
 double entropy()
          Returns the expected log factor value, i.e., sum_x factor.value(x) * Math.log (factor.value (x)) where the summation is taken over all passible assignments.
 Factor extractMax(java.util.Collection vars)
          Returns a potential phi over the given variables obtained by taking phi (x) = max_[all v that contain x] this.prob (x)
 Factor extractMax(Variable var)
          Returns a potential phi over the given variables obtained by taking phi (x) = max_[all v that contain x] this.prob (x)
 Factor extractMax(Variable[] vars)
          Returns a potential phi over the given variables obtained by taking phi (x) = max_[all v that contain x] this.prob (x)
 Variable findVariable(java.lang.String name)
           
abstract  Matrix getLogValueMatrix()
           
protected  int getNumVars()
           
abstract  Matrix getValueMatrix()
           
 double[] getValues()
           
 Variable getVariable(int i)
           
 int indexAtLocation(int loc)
           
 boolean isNaN()
           
 double logsum()
           
static Factor makeIdentityFactor(AbstractTableFactor copy)
          STATIC FACTORY METHODS
 Factor marginalize(java.util.Collection vars)
          Returns the marginal of this distribution over the given variables.
 Factor marginalize(Variable var)
          Returns the marginal of this distribution over one variable.
 Factor marginalize(Variable[] vars)
          Returns the marginal of this distribution over the given variables.
protected abstract  Factor marginalizeInternal(AbstractTableFactor result)
           
 Factor marginalizeOut(Variable var)
          Returns the marginal distribution attained by summing out the given variable.
 Factor marginalizeOut(VarSet badVars)
          Returns the marginal distribution attained by summing out the given set of variables.
 Factor multiply(Factor dist)
          Returns the elementwise product of this potential and another one.
 void multiplyBy(Factor pot)
          Does the conceptual equivalent of this *= pot.
protected abstract  void multiplyByInternal(DiscreteFactor ptl)
           
abstract  Factor normalize()
          Multiplies this potential by a constant such that it sums to 1.
 int numLocations()
           
 void plusEquals(double v)
          Adds a constant to all values in the table.
 void plusEquals(Factor f)
           
protected abstract  void plusEqualsAtLocation(int loc, double v)
           
protected abstract  void plusEqualsInternal(DiscreteFactor ptl)
           
 java.lang.String prettyOutputString()
           
 void printSizes()
           
 void printValues()
           
protected abstract  double rawValue(int singleIdx)
           
abstract  AbstractTableFactor recenter()
          Multiplies this factor by the constant 1/max().
 Assignment sample(Randoms r)
          Return an assignment sampled from this factor, interpreting it as an unnormalized probability distribution.
 int sampleLocation(Randoms r)
           
abstract  void setLogValue(Assignment assn, double logValue)
          Use of this method is discouraged.
abstract  void setLogValue(AssignmentIterator assnIt, double logValue)
          Use of this method is discouraged.
abstract  void setLogValues(double[] vals)
           
 void setRawValue(Assignment assn, double value)
           
 void setRawValue(AssignmentIterator it, double value)
           
protected  void setRawValue(int loc, double value)
           
abstract  void setValue(AssignmentIterator assnIt, double logValue)
          Use of this method is discouraged.
abstract  void setValues(double[] vals)
           
 void setValues(Matrix probs)
           
 int singleIndex(int[] smallDims)
           
protected abstract  Factor slice_general(Variable[] vars, Assignment observed)
           
protected abstract  Factor slice_onevar(Variable var, Assignment observed)
           
protected abstract  Factor slice_twovar(Variable v1, Variable v2, Assignment observed)
           
 Factor slice(Assignment assn)
          Creates a new potential that is equal to this one, restricted to a given assignment.
abstract  double sum()
          Returns the sum of this potential over all cases.
abstract  void timesEquals(double v)
          Multiplies a constant by all values in the table.
 double[] toLogValueArray()
           
 java.lang.String toString()
           
 double[] toValueArray()
           
abstract  double value(Assignment assn)
          Returns the value of the local function for a given assignment.
 VarSet varSet()
          Returns set of variables in this potential.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface cc.mallet.grmm.types.DiscreteFactor
value, valueAtLocation
 
Methods inherited from interface cc.mallet.grmm.types.Factor
exponentiate, logValue, logValue, logValue, value
 

Field Detail

probs

protected Matrix probs
Constructor Detail

AbstractTableFactor

protected AbstractTableFactor(BidirectionalIntObjectMap varMap)

AbstractTableFactor

public AbstractTableFactor(Variable var)
Creates an identity potential over the given variable.


AbstractTableFactor

public AbstractTableFactor(Variable var,
                           double[] values)

AbstractTableFactor

public AbstractTableFactor()
Creates an identity potential over NO variables.


AbstractTableFactor

public AbstractTableFactor(Variable[] allVars)
Creates an identity potential with the given variables.


AbstractTableFactor

public AbstractTableFactor(java.util.Collection allVars)
Creates an identity potential with the given variables.

Parameters:
allVars - A collection containing the Variables of this distribution.

AbstractTableFactor

public AbstractTableFactor(Variable[] allVars,
                           double[] probs)
Creates a potential with the given variables and the given probabilities.

Parameters:
allVars - Variables of the potential
probs - All phi values of the potential, in row-major order.

AbstractTableFactor

public AbstractTableFactor(VarSet allVars,
                           double[] probs)
Creates a potential with the given variables and the given probabilities.

Parameters:
allVars - Variables of the potential
probs - All phi values of the potential, in row-major order.

AbstractTableFactor

public AbstractTableFactor(Variable[] allVars,
                           Matrix probsIn)
Creates a potential with the given variables and the given probabilities.

Parameters:
allVars - Variables of the potential
probsIn - All the phi values of the potential.

AbstractTableFactor

public AbstractTableFactor(AbstractTableFactor in)
Copy constructor.


AbstractTableFactor

public AbstractTableFactor(VarSet allVars,
                           Matrix probsIn)
Creates a potential with the given variables and the given probabilities.

Parameters:
allVars - Variables of the potential
probsIn - All the phi values of the potential.

AbstractTableFactor

public AbstractTableFactor(AbstractTableFactor ptl,
                           double[] probs)
Creates a potential with the same variables as another, but different probabilites.

Parameters:
ptl -
probs -
Method Detail

makeIdentityFactor

public static Factor makeIdentityFactor(AbstractTableFactor copy)
STATIC FACTORY METHODS


duplicate

public abstract Factor duplicate()
Specified by:
duplicate in interface Factor

normalize

public abstract Factor normalize()
Description copied from interface: Factor
Multiplies this potential by a constant such that it sums to 1. Destructive; returns this factor.

Specified by:
normalize in interface Factor

sum

public abstract double sum()
Description copied from interface: Factor
Returns the sum of this potential over all cases.

Specified by:
sum in interface Factor

createBlankSubset

protected abstract AbstractTableFactor createBlankSubset(Variable[] vars)

getNumVars

protected int getNumVars()

setValues

public void setValues(Matrix probs)

containsVar

public boolean containsVar(Variable var)
Returns true iff this potential is over the given variable

Specified by:
containsVar in interface Factor

varSet

public VarSet varSet()
Returns set of variables in this potential.

Specified by:
varSet in interface Factor

assignmentIterator

public AssignmentIterator assignmentIterator()
Description copied from interface: Factor
Returns an iterator over all Assignmentss to this potential.

Specified by:
assignmentIterator in interface Factor

setRawValue

public void setRawValue(Assignment assn,
                        double value)

setRawValue

public void setRawValue(AssignmentIterator it,
                        double value)

setRawValue

protected void setRawValue(int loc,
                           double value)

value

public abstract double value(Assignment assn)
Description copied from interface: Factor
Returns the value of the local function for a given assignment. All variables in the potential must be included, but it's okay if the assignment uses variables not in the potential.

Specified by:
value in interface Factor

logsum

public double logsum()

entropy

public double entropy()
Description copied from interface: Factor
Returns the expected log factor value, i.e., sum_x factor.value(x) * Math.log (factor.value (x)) where the summation is taken over all passible assignments.

Specified by:
entropy in interface Factor

marginalize

public Factor marginalize(Variable[] vars)
Returns the marginal of this distribution over the given variables.

Specified by:
marginalize in interface Factor

marginalize

public Factor marginalize(java.util.Collection vars)
Description copied from interface: Factor
Returns the marginal of this distribution over the given variables.

Specified by:
marginalize in interface Factor

marginalize

public Factor marginalize(Variable var)
Description copied from interface: Factor
Returns the marginal of this distribution over one variable.

Specified by:
marginalize in interface Factor

marginalizeOut

public Factor marginalizeOut(Variable var)
Description copied from interface: Factor
Returns the marginal distribution attained by summing out the given variable.

Specified by:
marginalizeOut in interface Factor

marginalizeOut

public Factor marginalizeOut(VarSet badVars)
Description copied from interface: Factor
Returns the marginal distribution attained by summing out the given set of variables.

Specified by:
marginalizeOut in interface Factor

marginalizeInternal

protected abstract Factor marginalizeInternal(AbstractTableFactor result)

extractMax

public Factor extractMax(Variable var)
Description copied from interface: Factor
Returns a potential phi over the given variables obtained by taking phi (x) = max_[all v that contain x] this.prob (x)

Specified by:
extractMax in interface Factor

extractMax

public Factor extractMax(Variable[] vars)
Description copied from interface: Factor
Returns a potential phi over the given variables obtained by taking phi (x) = max_[all v that contain x] this.prob (x)

Specified by:
extractMax in interface Factor

extractMax

public Factor extractMax(java.util.Collection vars)
Description copied from interface: Factor
Returns a potential phi over the given variables obtained by taking phi (x) = max_[all v that contain x] this.prob (x)

Specified by:
extractMax in interface Factor

multiplyBy

public void multiplyBy(Factor pot)
Does the conceptual equivalent of this *= pot. Assumes that pot's variables are a subset of this potential's.

Specified by:
multiplyBy in interface Factor

ensureOperandCompatible

protected DiscreteFactor ensureOperandCompatible(DiscreteFactor ptl)
Ensures that this.inLogSpace == ptl.inLogSpace. If this is not the case, return a copy of ptl logified or delogified as appropriate.

Parameters:
ptl -
Returns:
A potential equivalent to ptl, possibly logified or delogified. ptl itself could be returned.

multiplyByInternal

protected abstract void multiplyByInternal(DiscreteFactor ptl)

plusEqualsInternal

protected abstract void plusEqualsInternal(DiscreteFactor ptl)

multiply

public Factor multiply(Factor dist)
Returns the elementwise product of this potential and another one.

Specified by:
multiply in interface Factor

divideBy

public void divideBy(Factor pot)
Does the conceptual equivalent of this /= pot. Assumes that pot's variables are a subset of this potential's.

Specified by:
divideBy in interface Factor

divideByInternal

protected abstract void divideByInternal(DiscreteFactor ptl)

argmax

public int argmax()
Description copied from interface: Factor
Returns the assignment that maximizes this potential.

Specified by:
argmax in interface Factor

sample

public Assignment sample(Randoms r)
Description copied from interface: Factor
Return an assignment sampled from this factor, interpreting it as an unnormalized probability distribution.

Specified by:
sample in interface Factor

sampleLocation

public int sampleLocation(Randoms r)
Specified by:
sampleLocation in interface DiscreteFactor

almostEquals

public boolean almostEquals(Factor p)
Description copied from interface: Factor
Returns whether this is almost equal to another potential.

Specified by:
almostEquals in interface Factor

almostEquals

public boolean almostEquals(Factor p,
                            double epsilon)
Specified by:
almostEquals in interface Factor

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

dumpToString

public java.lang.String dumpToString()
Specified by:
dumpToString in interface Factor

isNaN

public boolean isNaN()
Specified by:
isNaN in interface Factor

printValues

public void printValues()

printSizes

public void printSizes()

findVariable

public Variable findVariable(java.lang.String name)

numLocations

public int numLocations()
Specified by:
numLocations in interface DiscreteFactor

indexAtLocation

public int indexAtLocation(int loc)
Specified by:
indexAtLocation in interface DiscreteFactor

getVariable

public Variable getVariable(int i)
Specified by:
getVariable in interface Factor

divideBy

public void divideBy(double v)

setLogValue

public abstract void setLogValue(Assignment assn,
                                 double logValue)
Use of this method is discouraged.


setLogValue

public abstract void setLogValue(AssignmentIterator assnIt,
                                 double logValue)
Use of this method is discouraged.


setValue

public abstract void setValue(AssignmentIterator assnIt,
                              double logValue)
Use of this method is discouraged.


rawValue

protected abstract double rawValue(int singleIdx)

toValueArray

public double[] toValueArray()
Specified by:
toValueArray in interface DiscreteFactor

singleIndex

public int singleIndex(int[] smallDims)
Specified by:
singleIndex in interface DiscreteFactor

getValueMatrix

public abstract Matrix getValueMatrix()

getLogValueMatrix

public abstract Matrix getLogValueMatrix()

setLogValues

public abstract void setLogValues(double[] vals)

setValues

public abstract void setValues(double[] vals)

toLogValueArray

public double[] toLogValueArray()

getValues

public double[] getValues()

plusEquals

public void plusEquals(double v)
Adds a constant to all values in the table. This is most useful to add a small constant to avoid zeros.


plusEquals

public void plusEquals(Factor f)

timesEquals

public abstract void timesEquals(double v)
Multiplies a constant by all values in the table.


plusEqualsAtLocation

protected abstract void plusEqualsAtLocation(int loc,
                                             double v)

recenter

public abstract AbstractTableFactor recenter()
Multiplies this factor by the constant 1/max(). This ensures that the maximum value of this factor is 1.0


asTable

public AbstractTableFactor asTable()
Specified by:
asTable in interface Factor

slice

public Factor slice(Assignment assn)
Creates a new potential that is equal to this one, restricted to a given assignment.

Specified by:
slice in interface Factor
Parameters:
assn - Variables to hold as fixed
Returns:
A new factor over VARS(factor)\VARS(assn)

slice_onevar

protected abstract Factor slice_onevar(Variable var,
                                       Assignment observed)

slice_twovar

protected abstract Factor slice_twovar(Variable v1,
                                       Variable v2,
                                       Assignment observed)

slice_general

protected abstract Factor slice_general(Variable[] vars,
                                        Assignment observed)

prettyOutputString

public java.lang.String prettyOutputString()
Specified by:
prettyOutputString in interface Factor