de.jstacs.sequenceScores.statisticalModels.trainable.hmm.transitions
Class BasicHigherOrderTransition

java.lang.Object
  extended by de.jstacs.sequenceScores.statisticalModels.trainable.hmm.transitions.BasicHigherOrderTransition
All Implemented Interfaces:
TrainableTransition, Transition, TransitionWithSufficientStatistic, Storable, Cloneable
Direct Known Subclasses:
HigherOrderTransition

public class BasicHigherOrderTransition
extends Object
implements TrainableTransition

This class implements the basic transition that allows to be trained using the viterbi or the Baum-Welch algorithm. Layer 0, context 0 encodes the start state.

Author:
Jan Grau, Jens Keilwagen

Nested Class Summary
static class BasicHigherOrderTransition.AbstractTransitionElement
          This class declares the probability distribution for a given context, i.e. it contains all possible transition and the corresponding probabilities for a given set offset previously visited states.
 
Field Summary
protected  boolean[] isSilent
          A vector indicating for each state whether it is silent or not.
protected  int[][][] lookup
          The lookup table for spare context en- and decoding.
protected  int maximalMarkovOrder
          The maximal Markov order of the transition.
protected  int maxInDegree
          The maximal in-degree of any state.
protected  BasicHigherOrderTransition.AbstractTransitionElement[] transitions
          The internally used BasicHigherOrderTransition.AbstractTransitionElements.
 
Constructor Summary
BasicHigherOrderTransition(boolean[] isSilent, BasicHigherOrderTransition.AbstractTransitionElement... transitions)
          The main constructor.
BasicHigherOrderTransition(StringBuffer xml)
          The standard constructor for the interface Storable.
 
Method Summary
 void addToStatistic(int layer, int index, int childIdx, double weight, Sequence sequence, int sequencePosition)
          This method allows to add a certain weight to the sufficient statistic of a specific transition.
protected  void appendFurtherInformation(StringBuffer xml)
          This method appends further information to the XML representation.
 BasicHigherOrderTransition clone()
          This method returns a deep clone of the current instance.
 void drawParametersFromStatistic()
          This method allows to draw parameters from the sufficient statistic, i.e., to draw from the posterior.
 void estimateFromStatistic()
          This method estimates the parameter of the transition using the internal sufficient statistic.
protected  void extractFurtherInformation(StringBuffer xml)
          This method extracts further information from the XML representation.
 void fillTransitionInformation(int layer, int index, int childIdx, int[] container)
          This method fills all relevant information for a specific edge in a given container.
 int getChildIdx(int layer, int index, int state)
          This method returns the child index of the state, if this state is no child of the context -1 is returned
 String getGraphizNetworkRepresentation(NumberFormat nf, String arrowOption, boolean graphical)
          This method returns a String representation of the structure that can be used in Graphviz to create an image.
 int getLastContextState(int layer, int index)
          The method returns the index of the state of the context, if there is no context -1 is returned.
 double getLogGammaScoreFromStatistic()
          This method calculates a score for the current statistics, which is independent from the current parameters In general the gamma-score is a product of gamma-functions parameterized with the current statistics
 double getLogPriorTerm()
          Returns a value that is proportional to the log of the prior.
 double getLogScoreFor(int layer, int index, int childIdx, Sequence sequence, int sequencePosition)
          This method returns the logarithm of the score for the transition.
 int getMaximalInDegree()
          This method returns the maximal out degree of any context used in this transition instance.
 int getMaximalMarkovOrder()
          This method returns the maximal used Markov order.
 int getMaximalNumberOfChildren()
          This method returns the maximal number of children for any context used in this transition instance.
 int getNumberOfChildren(int layer, int index)
          This method returns the number of children states for given index, i.e. context, and a given layer of the matrix.
 int getNumberOfIndexes(int layer)
          This method computes the number of different indexes for a given layer of the matrix.
 int getNumberOfStates()
          This method returns the number of states underlying this transition instance.
protected  int getTransitionElementIndex(int layer, int index)
          This method return the index of the BasicHigherOrderTransition.AbstractTransitionElement using the lookup table.
protected  String getXMLTag()
          The method returns the XML tag used during saving and loading the transition.
 boolean hasAnySelfTransitions()
          This method answers the question whether the instance models any self transitions.
 void initializeRandomly()
          This method randomly initializes the parameters of the transition.
 boolean[] isAbsoring()
          This method returns for each state whether it is absorbing or not.
 void joinStatistics(Transition... transitions)
          This method joins the statistics of different instances and sets this joined statistic as statistic of each instance.
 void resetStatistic()
          This method reset the internal sufficient statistics that can be used for estimating the parameters.
 void setParameters(Transition t)
          Set values of parameters of the instance to the value of the parameters of the given instance.
 String toString()
           
 String toString(String[] stateNames)
          This method returns a String representation of the Transition using the given names of the states.
 StringBuffer toXML()
          This method returns an XML representation as StringBuffer of an instance of the implementing class.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

transitions

protected BasicHigherOrderTransition.AbstractTransitionElement[] transitions
The internally used BasicHigherOrderTransition.AbstractTransitionElements.


isSilent

protected boolean[] isSilent
A vector indicating for each state whether it is silent or not.


maximalMarkovOrder

protected int maximalMarkovOrder
The maximal Markov order of the transition.


maxInDegree

protected int maxInDegree
The maximal in-degree of any state.


lookup

protected int[][][] lookup
The lookup table for spare context en- and decoding.

Constructor Detail

BasicHigherOrderTransition

public BasicHigherOrderTransition(boolean[] isSilent,
                                  BasicHigherOrderTransition.AbstractTransitionElement... transitions)
                           throws Exception
The main constructor.

Parameters:
transitions - the BasicHigherOrderTransition.AbstractTransitionElements for the internal use
isSilent - an array indicating for each state whether it is silent or not
Throws:
Exception - if an error occurs during checking the BasicHigherOrderTransition.AbstractTransitionElements and creating internal fields

BasicHigherOrderTransition

public BasicHigherOrderTransition(StringBuffer xml)
                           throws NonParsableException
The standard constructor for the interface Storable. Constructs a BasicHigherOrderTransition out of an XML representation.

Parameters:
xml - the XML representation as StringBuffer
Throws:
NonParsableException - if the BasicHigherOrderTransition could not be reconstructed out of the StringBuffer xml
Method Detail

toXML

public StringBuffer toXML()
Description copied from interface: Storable
This method returns an XML representation as StringBuffer of an instance of the implementing class.

Specified by:
toXML in interface Storable
Returns:
the XML representation

getXMLTag

protected String getXMLTag()
The method returns the XML tag used during saving and loading the transition.

Returns:
he XML tag used during saving and loading the transition.

appendFurtherInformation

protected void appendFurtherInformation(StringBuffer xml)
This method appends further information to the XML representation. It allows subclasses to save further parameters that are not defined in the superclass.

Parameters:
xml - the XML representation

extractFurtherInformation

protected void extractFurtherInformation(StringBuffer xml)
                                  throws NonParsableException
This method extracts further information from the XML representation. It allows subclasses to cast further parameters that are not defined in the superclass.

Parameters:
xml - the XML representation
Throws:
NonParsableException - if the information could not be reconstructed out of the StringBuffer xml

clone

public BasicHigherOrderTransition clone()
                                 throws CloneNotSupportedException
Description copied from interface: Transition
This method returns a deep clone of the current instance.

Specified by:
clone in interface Transition
Overrides:
clone in class Object
Returns:
a deep clone of the current instance.
Throws:
CloneNotSupportedException - if the instance could not be cloned

resetStatistic

public void resetStatistic()
Description copied from interface: TransitionWithSufficientStatistic
This method reset the internal sufficient statistics that can be used for estimating the parameters.

Specified by:
resetStatistic in interface TransitionWithSufficientStatistic

joinStatistics

public void joinStatistics(Transition... transitions)
Description copied from interface: TransitionWithSufficientStatistic
This method joins the statistics of different instances and sets this joined statistic as statistic of each instance. This method might be used for instance in a multi-threaded optimization to join partial statistics.

Specified by:
joinStatistics in interface TransitionWithSufficientStatistic
Parameters:
transitions - the transitions to be joined

addToStatistic

public void addToStatistic(int layer,
                           int index,
                           int childIdx,
                           double weight,
                           Sequence sequence,
                           int sequencePosition)
Description copied from interface: TransitionWithSufficientStatistic
This method allows to add a certain weight to the sufficient statistic of a specific transition.

Specified by:
addToStatistic in interface TransitionWithSufficientStatistic
Parameters:
layer - the layer of the matrix
index - the index encoding the context
childIdx - the index of the child
weight - the weight added to the sufficient statistic
sequence - the sequence
sequencePosition - the position within the sequence

estimateFromStatistic

public void estimateFromStatistic()
Description copied from interface: TrainableTransition
This method estimates the parameter of the transition using the internal sufficient statistic.

Specified by:
estimateFromStatistic in interface TrainableTransition

drawParametersFromStatistic

public void drawParametersFromStatistic()
                                 throws Exception
This method allows to draw parameters from the sufficient statistic, i.e., to draw from the posterior. If no data has been added using addToStatistic(int, int, int, double, Sequence, int), the method draws from the prior.

Throws:
Exception - if for instance the prior has some illegal hyper-parameters (e.g. 0)
See Also:
BasicHigherOrderTransition.AbstractTransitionElement.drawParametersFromStatistic()

getMaximalMarkovOrder

public int getMaximalMarkovOrder()
Description copied from interface: Transition
This method returns the maximal used Markov order.

Specified by:
getMaximalMarkovOrder in interface Transition
Returns:
maximal used Markov order

getLogPriorTerm

public double getLogPriorTerm()
Description copied from interface: Transition
Returns a value that is proportional to the log of the prior. For maximum likelihood (ML) 0 should be returned.

Specified by:
getLogPriorTerm in interface Transition
Returns:
a value that is proportional to the log of the prior
See Also:
StatisticalModel.getLogPriorTerm()

getNumberOfStates

public int getNumberOfStates()
Description copied from interface: Transition
This method returns the number of states underlying this transition instance.

Specified by:
getNumberOfStates in interface Transition
Returns:
the number of states underlying this transition instance

initializeRandomly

public void initializeRandomly()
Description copied from interface: Transition
This method randomly initializes the parameters of the transition.

Specified by:
initializeRandomly in interface Transition

getGraphizNetworkRepresentation

public String getGraphizNetworkRepresentation(NumberFormat nf,
                                              String arrowOption,
                                              boolean graphical)
Description copied from interface: Transition
This method returns a String representation of the structure that can be used in Graphviz to create an image.

Specified by:
getGraphizNetworkRepresentation in interface Transition
Parameters:
nf - the NumberFormat used for the probabilities, if null no probabilities will we written
arrowOption - this parameter gives the possibility to set some arrow option
graphical - represent transition probabilities as thickness of edges instead of textual output
Returns:
String representation of the state

fillTransitionInformation

public void fillTransitionInformation(int layer,
                                      int index,
                                      int childIdx,
                                      int[] container)
Description copied from interface: Transition
This method fills all relevant information for a specific edge in a given container.

Specified by:
fillTransitionInformation in interface Transition
Parameters:
layer - the layer in the matrix
index - the index encoding the context
childIdx - the index of the child that will be visited
container - container[0] the index of the state; container[1] the index encoding the new context; container[2] the distance for the new layer, i.e. 0 for the same layer (=silent state), and 1 for the next layer (=non-silent state)

getTransitionElementIndex

protected final int getTransitionElementIndex(int layer,
                                              int index)
This method return the index of the BasicHigherOrderTransition.AbstractTransitionElement using the lookup table.

Parameters:
layer - the layer of the matrix
index - the index encoding the context
Returns:
the index of the BasicHigherOrderTransition.AbstractTransitionElement with respect to transitions

getNumberOfChildren

public int getNumberOfChildren(int layer,
                               int index)
Description copied from interface: Transition
This method returns the number of children states for given index, i.e. context, and a given layer of the matrix.

Specified by:
getNumberOfChildren in interface Transition
Parameters:
layer - the layer of the matrix
index - the index encoding the context
Returns:
the number of children states

getLogScoreFor

public double getLogScoreFor(int layer,
                             int index,
                             int childIdx,
                             Sequence sequence,
                             int sequencePosition)
Description copied from interface: Transition
This method returns the logarithm of the score for the transition.

Specified by:
getLogScoreFor in interface Transition
Parameters:
layer - the layer in the matrix
index - the index encoding the context
childIdx - the index of the child that will be visited
sequence - the sequence
sequencePosition - the position within the sequence
Returns:
the logarithm of the score for the transition

getNumberOfIndexes

public int getNumberOfIndexes(int layer)
Description copied from interface: Transition
This method computes the number of different indexes for a given layer of the matrix.

Specified by:
getNumberOfIndexes in interface Transition
Parameters:
layer - the layer of the matrix
Returns:
the number of different indexes for a given position

hasAnySelfTransitions

public boolean hasAnySelfTransitions()
Description copied from interface: Transition
This method answers the question whether the instance models any self transitions. It returns true if the current instance has any self transitions, otherwise false

Specified by:
hasAnySelfTransitions in interface Transition
Returns:
true if the current instance has any self transitions, otherwise false

getMaximalInDegree

public int getMaximalInDegree()
Description copied from interface: Transition
This method returns the maximal out degree of any context used in this transition instance.

Specified by:
getMaximalInDegree in interface Transition
Returns:
maximal out degree of any context

getMaximalNumberOfChildren

public int getMaximalNumberOfChildren()
Description copied from interface: Transition
This method returns the maximal number of children for any context used in this transition instance.

Specified by:
getMaximalNumberOfChildren in interface Transition
Returns:
maximal number of children for any context

getLastContextState

public int getLastContextState(int layer,
                               int index)
Description copied from interface: Transition
The method returns the index of the state of the context, if there is no context -1 is returned.

Specified by:
getLastContextState in interface Transition
Parameters:
layer - the layer in the matrix
index - the index encoding the context
Returns:
the index of the last state of the context, if there is no context -1 is returned

getChildIdx

public int getChildIdx(int layer,
                       int index,
                       int state)
Description copied from interface: Transition
This method returns the child index of the state, if this state is no child of the context -1 is returned

Specified by:
getChildIdx in interface Transition
Parameters:
layer - the layer in the matrix
index - the index encoding the context
state - the index of the state
Returns:
the child index of the state, if this state is no child of the context -1 is returned

getLogGammaScoreFromStatistic

public double getLogGammaScoreFromStatistic()
Description copied from interface: TransitionWithSufficientStatistic
This method calculates a score for the current statistics, which is independent from the current parameters In general the gamma-score is a product of gamma-functions parameterized with the current statistics

Specified by:
getLogGammaScoreFromStatistic in interface TransitionWithSufficientStatistic
Returns:
the logarithm of the gamma-score for the current statistics

toString

public String toString()
Overrides:
toString in class Object

toString

public String toString(String[] stateNames)
Description copied from interface: Transition
This method returns a String representation of the Transition using the given names of the states.

Specified by:
toString in interface Transition
Parameters:
stateNames - the names of the states, can be null
Returns:
a String representation of the Transition using the given names of the states

isAbsoring

public boolean[] isAbsoring()
Description copied from interface: Transition
This method returns for each state whether it is absorbing or not.

Specified by:
isAbsoring in interface Transition
Returns:
for each state whether it is absorbing or not

setParameters

public void setParameters(Transition t)
                   throws IllegalArgumentException
Description copied from interface: Transition
Set values of parameters of the instance to the value of the parameters of the given instance. It can be assumed that the given instance and the current instance are from the same class. This method might be used for instance in a multi-threaded optimization to broadcast the parameters.

Specified by:
setParameters in interface Transition
Parameters:
t - the transition with the parameters to be set
Throws:
IllegalArgumentException - if the assumption about the same class for given and current instance is wrong