de.jstacs.scoringFunctions.directedGraphicalModels
Class ParameterTree

java.lang.Object
  extended by de.jstacs.scoringFunctions.directedGraphicalModels.ParameterTree
All Implemented Interfaces:
Cloneable

public class ParameterTree
extends Object
implements Cloneable

Class for the tree that represents the context of a Parameter in a BayesianNetworkScoringFunction.

Author:
Jan Grau

Constructor Summary
ParameterTree(int pos, int[] contextPoss, AlphabetContainer alphabet, int firstParent, int[] firstChildren)
          Creates a new ParameterTree for the parameters at position pos using the parent positions in contextPoss.
ParameterTree(StringBuffer source, AlphabetContainer alphabet)
          Re-creates a ParameterTree from its XML-representation as returned by toXML().
 
Method Summary
 void addCount(Sequence seq, int start, double count)
          Adds count to the parameter as returned by getParameterFor(Sequence, int).
 void backward(ParameterTree[] trees, int[][] order)
          Starts the computation of the backward-part of the normalization constant starting from this ParameterTree.
 ParameterTree clone()
           
 Double computeGammaNorm()
          Computes the Gamma-normalization for the prior
 void copy(ParameterTree parameterTree)
          Copies the values of the parameters from another ParameterTree
 void divideByUnfree()
          Divides each of the normalized parameters on a simplex by the last parameter, which is defined not to be free.
 void fill(double[] fillEmptyWith)
          Fills all parameters with the probabilities given in fillEmptyWith.
 double forward(ParameterTree[] trees)
          Computes the forward-part of the normalization constant starting from this ParameterTree.
static ParameterTree[] fromStringArray(String[] strs, AlphabetContainer alphabet)
          Re-creates an array of ParameterTrees from their XML-representation as given in strs.
 int getFirstParent()
          Returns the first parent of the random variable of this ParameterTree in the topological ordering of the network structure of the enclosing BayesianNetworkScoringFunction.
 double getKLDivergence(double[] q)
          Returns the KL-divergence of the parameters of a PWM to the reference distribution q.
 int getNumberOfParents()
          Returns the number of parents for the random variable of this ParameterTree in the network structure of the enclosing BayesianNetworkScoringFunction.
 Parameter getParameterFor(Sequence seq, int start)
          Returns the Parameter that is responsible for the suffix of sequence seq starting at position start.
 void initializeRandomly(double ess)
          Initializes the parameters of this ParameterTree randomly
 void insertProbs(double[] probs)
          Computes the probabilities for a PWM, i.e. the parameters in the tree have an empty context, and inserts them into probs.
 void invalidateNormalizers()
          Resets all pre-computed normalization constants.
 boolean isLeaf()
          Returns true if the random variable of this ParameterTree is a leaf, i.e. it has no children in the network structure of the enclosing BayesianNetworkScoringFunction.
 LinkedList<Parameter> linearizeParameters()
          Extracts the Parameters from the leaves of this tree in left-to-right order (as specified by the order of the alphabet) and returns them as a LinkedList.
 void normalizePlugInParameters()
          Starts the normalization of the plug in parameters to the log of the MAP-estimates.
 void print()
          Prints the structure of this tree.
 void setParameterFor(int symbol, int[][] context, Parameter par)
          Sets the instance of the parameter for symbol symbol and context context to parameter par.
 String toString()
           
static String[] toStringArray(ParameterTree[] trees)
          Creates a String array from the XML-representation of all ParameterTrees in trees as returned by the corresponding toXML() methods.
 StringBuffer toXML()
          Works as defined in Storable.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ParameterTree

public ParameterTree(int pos,
                     int[] contextPoss,
                     AlphabetContainer alphabet,
                     int firstParent,
                     int[] firstChildren)
Creates a new ParameterTree for the parameters at position pos using the parent positions in contextPoss. These are used the extract the correct alphabet out of alphabet for every context position. The first parent is the first parent of the random variable at pos as given by the topological ordering of the network structure of the enclosing BayesianNetworkScoringFunction. The first children are the children the random variable at pos is the first parent for.

Parameters:
pos - the position of the random variable of the parameters in the tree
contextPoss - the positions of the context
alphabet - the alphabet of the enclosing BayesianNetworkScoringFunction
firstParent - the first parent of this random variable, or -1 if the random variable has no parent
firstChildren - the first children of this random variable

ParameterTree

public ParameterTree(StringBuffer source,
                     AlphabetContainer alphabet)
              throws NonParsableException
Re-creates a ParameterTree from its XML-representation as returned by toXML(). ParameterTree does not implement the Storable interface to recycle the AlphabetContainer of the enclosing BayesianNetworkScoringFunction, but besides the different constructor works like any implementation of Storable.

Parameters:
source - the XML-representation
alphabet - the alphabet
Throws:
NonParsableException - is thrown if the XML-code could not be parsed
Method Detail

clone

public ParameterTree clone()
                    throws CloneNotSupportedException
Overrides:
clone in class Object
Throws:
CloneNotSupportedException

toString

public String toString()
Overrides:
toString in class Object

insertProbs

public void insertProbs(double[] probs)
                 throws Exception
Computes the probabilities for a PWM, i.e. the parameters in the tree have an empty context, and inserts them into probs. Used by BayesianNetworkScoringFunction.getPWM().

Parameters:
probs - the array to store the probabilities
Throws:
Exception - is thrown if the tree structure does have a non-empty context

linearizeParameters

public LinkedList<Parameter> linearizeParameters()
Extracts the Parameters from the leaves of this tree in left-to-right order (as specified by the order of the alphabet) and returns them as a LinkedList.

Returns:
the Parameters in linear order

isLeaf

public boolean isLeaf()
Returns true if the random variable of this ParameterTree is a leaf, i.e. it has no children in the network structure of the enclosing BayesianNetworkScoringFunction.

Returns:
if this tree is a leaf

getNumberOfParents

public int getNumberOfParents()
Returns the number of parents for the random variable of this ParameterTree in the network structure of the enclosing BayesianNetworkScoringFunction. This corresponds to the length of the context or the depth of the tree.

Returns:
the number of parents.

print

public void print()
Prints the structure of this tree.


getParameterFor

public Parameter getParameterFor(Sequence seq,
                                 int start)
Returns the Parameter that is responsible for the suffix of sequence seq starting at position start.

Parameters:
seq - the sequence
start - the first position in the suffix
Returns:
the parameter

setParameterFor

public void setParameterFor(int symbol,
                            int[][] context,
                            Parameter par)
Sets the instance of the parameter for symbol symbol and context context to parameter par.

Parameters:
symbol - the symbol
context - the context
par - the new Parameter instance

invalidateNormalizers

public void invalidateNormalizers()
Resets all pre-computed normalization constants.


forward

public double forward(ParameterTree[] trees)
               throws RuntimeException
Computes the forward-part of the normalization constant starting from this ParameterTree. This is only possible for roots, i.e. ParameterTrees that do not have parents in the network structure of the enclosing BayesianNetworkScoringFunction.

Parameters:
trees - the array of all trees as from the enclosing BayesianNetworkScoringFunction
Returns:
the forward-part of the normalization constant
Throws:
RuntimeException - is thrown if this ParameterTree is not a root

backward

public void backward(ParameterTree[] trees,
                     int[][] order)
              throws RuntimeException
Starts the computation of the backward-part of the normalization constant starting from this ParameterTree. This is only possible for leaves, i.e. ParameterTrees that do not have children in the network structure of the enclosing BayesianNetworkScoringFunction.

Parameters:
trees - the array of all trees as from the enclosing BayesianNetworkScoringFunction
order - the topological ordering as returned by TopSort.getTopologicalOrder(int[][])
Throws:
RuntimeException - is thrown if this ParameterTree is not a leaf

addCount

public void addCount(Sequence seq,
                     int start,
                     double count)
Adds count to the parameter as returned by getParameterFor(Sequence, int).

Parameters:
seq - the sequence
start - the first position of the suffix of seqcount - the added count

normalizePlugInParameters

public void normalizePlugInParameters()
Starts the normalization of the plug in parameters to the log of the MAP-estimates.


divideByUnfree

public void divideByUnfree()
Divides each of the normalized parameters on a simplex by the last parameter, which is defined not to be free. In the log-space this amounts to subtracting the value of the last parameter from all parameters on the simplex.


toXML

public StringBuffer toXML()
Works as defined in Storable.

Returns:
the XML-representation

toStringArray

public static String[] toStringArray(ParameterTree[] trees)
Creates a String array from the XML-representation of all ParameterTrees in trees as returned by the corresponding toXML() methods.

Parameters:
trees - the trees
Returns:
the XML-representations as String array

fromStringArray

public static ParameterTree[] fromStringArray(String[] strs,
                                              AlphabetContainer alphabet)
                                       throws NonParsableException
Re-creates an array of ParameterTrees from their XML-representation as given in strs.

Parameters:
strs - the XML-representations as returned by toStringArray(ParameterTree[])
alphabet - the alphabet of the enclosing BayesianNetworkScoringFunction
Returns:
the ParameterTrees
Throws:
NonParsableException - is thrown if one of the XML-representations could not be parsed

getFirstParent

public int getFirstParent()
Returns the first parent of the random variable of this ParameterTree in the topological ordering of the network structure of the enclosing BayesianNetworkScoringFunction.

Returns:
the first parent

getKLDivergence

public double getKLDivergence(double[] q)
Returns the KL-divergence of the parameters of a PWM to the reference distribution q.

Parameters:
q - the reference
Returns:
the KL-divergence

fill

public void fill(double[] fillEmptyWith)
Fills all parameters with the probabilities given in fillEmptyWith.

Parameters:
fillEmptyWith - the replacement

copy

public void copy(ParameterTree parameterTree)
Copies the values of the parameters from another ParameterTree

Parameters:
parameterTree - the template

initializeRandomly

public void initializeRandomly(double ess)
Initializes the parameters of this ParameterTree randomly

Parameters:
ess - the equivalent sample size

computeGammaNorm

public Double computeGammaNorm()
Computes the Gamma-normalization for the prior

Returns:
the Gamma-normalization