cc.mallet.grmm.types
Class CPT

java.lang.Object
  extended by cc.mallet.grmm.types.CPT
All Implemented Interfaces:
DiscreteFactor, Factor, java.io.Serializable, java.lang.Cloneable

public class CPT
extends java.lang.Object
implements DiscreteFactor

$Id: CPT.java,v 1.1 2007/10/22 21:37:44 mccallum Exp $

See Also:
Serialized Form

Constructor Summary
CPT(DiscreteFactor subFactor, Variable child)
           
 
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()
           
 boolean containsVar(Variable var)
          Returns whether the potential is over the given variable.
 void divideBy(Factor pot)
          Computes this /= pot
 java.lang.String dumpToString()
           
 Factor duplicate()
           
 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.
 void exponentiate(double power)
           
 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 getChild()
           
 VarSet getParents()
           
 Variable getVariable(int i)
           
 int indexAtLocation(int loc)
           
 boolean isNaN()
           
 double logValue(Assignment assn)
           
 double logValue(AssignmentIterator it)
           
 double logValue(int loc)
           
 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.
 Factor marginalizeOut(Variable var)
          Returns the marginal distribution attained by summing out the given variable.
 Factor marginalizeOut(VarSet varset)
          Returns the marginal distribution attained by summing out the given set of variables.
 Factor multiply(Factor dist)
          Returns the elementwise product of this factor with another.
 void multiplyBy(Factor pot)
          Does this *= pot.
 Factor normalize()
          Multiplies this potential by a constant such that it sums to 1.
 int numLocations()
           
 java.lang.String prettyOutputString()
           
 Assignment sample(Randoms r)
          Return an assignment sampled from this factor, interpreting it as an unnormalized probability distribution.
 int sampleLocation(Randoms r)
           
 void setSubFactor(DiscreteFactor subFactor)
           
 int singleIndex(int[] smallDims)
           
 Factor slice(Assignment assn)
           
 double sum()
          Returns the sum of this potential over all cases.
 java.lang.String toString()
           
 double[] toValueArray()
           
 double value(Assignment assn)
          Returns the value of the local function for a given assignment.
 double value(AssignmentIterator it)
          Returns the probability of an assignment to these variables.
 double value(int index)
           
 double valueAtLocation(int loc)
           
 VarSet varSet()
          Returns set of variables in this potential.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CPT

public CPT(DiscreteFactor subFactor,
           Variable child)
Method Detail

getParents

public VarSet getParents()

getChild

public Variable getChild()

setSubFactor

public void setSubFactor(DiscreteFactor subFactor)

toString

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

prettyOutputString

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

value

public 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

value

public double value(AssignmentIterator it)
Description copied from interface: Factor
Returns the probability of an assignment to these variables. The assignment used is the curret assignment from the given AssignmentIterator.

This can be used to do things like

         DiscretePotential phi = createMyPtl ();
         for (AssignmentIterator it = phi.assignmentIterator; it.hasNext(); it.advance()) {
           double val = ptl.phi (it);
                 // do something with val
   }
  

This is equivalent to creating an assignment object explicitly using (Assignment) it.next(), but can be much faster.

Specified by:
value in interface Factor

normalize

public 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

marginalize

public Factor marginalize(Variable[] 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(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

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

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

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

sum

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

Specified by:
sum in interface Factor

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

multiply

public Factor multiply(Factor dist)
Description copied from interface: Factor
Returns the elementwise product of this factor with another.

Specified by:
multiply in interface Factor

multiplyBy

public void multiplyBy(Factor pot)
Description copied from interface: Factor
Does this *= pot.

If both potentials are currently in log space, then does addition instead.

Specified by:
multiplyBy in interface Factor

exponentiate

public void exponentiate(double power)
Specified by:
exponentiate in interface Factor

divideBy

public void divideBy(Factor pot)
Description copied from interface: Factor
Computes this /= pot

If both potentials are currently in log space, then does subtraction instead.

Specified by:
divideBy in interface Factor

containsVar

public boolean containsVar(Variable var)
Description copied from interface: Factor
Returns whether the potential is over the given variable.

Specified by:
containsVar in interface Factor

varSet

public VarSet varSet()
Description copied from interface: Factor
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

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

duplicate

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

isNaN

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

logValue

public double logValue(AssignmentIterator it)
Specified by:
logValue in interface Factor

logValue

public double logValue(Assignment assn)
Specified by:
logValue in interface Factor

logValue

public double logValue(int loc)
Specified by:
logValue in interface Factor

getVariable

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

sampleLocation

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

value

public double value(int index)
Specified by:
value in interface DiscreteFactor

numLocations

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

valueAtLocation

public double valueAtLocation(int loc)
Specified by:
valueAtLocation in interface DiscreteFactor

indexAtLocation

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

toValueArray

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

singleIndex

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

dumpToString

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

slice

public Factor slice(Assignment assn)
Specified by:
slice in interface Factor

asTable

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

marginalizeOut

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

Specified by:
marginalizeOut in interface Factor