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

java.lang.Object
  extended by de.jstacs.sequenceScores.statisticalModels.trainable.hmm.transitions.BasicHigherOrderTransition
      extended by de.jstacs.sequenceScores.statisticalModels.trainable.hmm.transitions.HigherOrderTransition
All Implemented Interfaces:
SamplingComponent, SamplingFromStatistic, DifferentiableTransition, SamplingTransition, TrainableTransition, Transition, TransitionWithSufficientStatistic, Storable, Cloneable

public class HigherOrderTransition
extends BasicHigherOrderTransition
implements DifferentiableTransition, SamplingTransition

This class can be used in any AbstractHMM allowing to use gradient based or sampling training algorithm.

Author:
Jens Keilwagen

Nested Class Summary
 
Nested classes/interfaces inherited from class de.jstacs.sequenceScores.statisticalModels.trainable.hmm.transitions.BasicHigherOrderTransition
BasicHigherOrderTransition.AbstractTransitionElement
 
Field Summary
protected  int[] counter
          The counter for the sampling steps of each sampling.
protected  File[] paramsFile
          The files for saving the parameters during the sampling.
protected  BufferedReader reader
          The reader for the paramsFile after a sampling.
protected  int samplingIndex
          The index of the current sampling.
protected  BufferedWriter writer
          The writer for the paramsFile in a sampling.
 
Fields inherited from class de.jstacs.sequenceScores.statisticalModels.trainable.hmm.transitions.BasicHigherOrderTransition
isSilent, lookup, maximalMarkovOrder, maxInDegree, transitions
 
Constructor Summary
HigherOrderTransition(boolean[] isSilent, TransitionElement... transitions)
          The main constructor.
HigherOrderTransition(StringBuffer xml)
          The standard constructor for the interface Storable.
 
Method Summary
 void acceptParameters()
          This methods accepts the drawn parameters.
 void addGradientForLogPriorTerm(double[] gradient, int start)
          This method computes the gradient of Transition.getLogPriorTerm() for each parameter of this transition.
protected  void appendFurtherInformation(StringBuffer xml)
          This method appends further information to the XML representation.
 HigherOrderTransition clone()
          This method returns a deep clone of the current instance.
 void extendSampling(int sampling, boolean append)
          This method allows to extend a sampling.
protected  void extractFurtherInformation(StringBuffer xml)
          This method extracts further information from the XML representation.
 void fillParameters(double[] params)
          This method allows to fill the parameters of the transition in a given array.
protected  void fillParameters(double[] params, int offset)
          This method allows to fill the current parameters using a specific offset.
 void fillSamplingGroups(int parameterOffset, LinkedList<int[]> list)
          Adds the groups of indexes of those parameters of this transition that should be sampled together in one step of a grouped sampling procedure, each as an int[], into list.
protected  void finalize()
           
 double getLogPosteriorFromStatistic()
          This method calculates the a-posteriori probability for the current statistics
 double getLogScoreAndPartialDerivation(int layer, int index, int childIdx, IntList indices, DoubleList partDer, Sequence sequence, int sequencePosition)
          This method allows to compute the logarithm of the score and the gradient for a specific transition.
 int getSizeOfEventSpace(int index)
          Returns the size of the event space, i.e., the number of possible outcomes, for the random variable of parameter index
protected  String getXMLTag()
          The method returns the XML tag used during saving and loading the transition.
 void initForSampling(int starts)
          This method initializes the instance for the sampling.
 boolean isInSamplingMode()
          This method returns true if the object is currently used in a sampling, otherwise false.
 boolean parseNextParameterSet()
          This method allows the user to parse the next set of parameters (from a file).
 boolean parseParameterSet(int sampling, int n)
          This method allows the user to parse the set of parameters with index n of a certain sampling (from a file).
 void samplingStopped()
          This method is the opposite of the method SamplingComponent.extendSampling(int, boolean).
protected  int setParameterOffset()
          This method allows to set the parameter offset in each internally used TransitionElement.
 int setParameterOffset(int offset)
          This method sets the internal offset of the parameter index.
 void setParameters(double[] params, int start)
          This method allows to set the parameters of the transition.
protected  void setParams(double[] params, int start)
          This method allows to set the new parameters using a specific offset.
 
Methods inherited from class de.jstacs.sequenceScores.statisticalModels.trainable.hmm.transitions.BasicHigherOrderTransition
addToStatistic, drawParametersFromStatistic, estimateFromStatistic, fillTransitionInformation, getChildIdx, getGraphizNetworkRepresentation, getLastContextState, getLogGammaScoreFromStatistic, getLogPriorTerm, getLogScoreFor, getMaximalInDegree, getMaximalMarkovOrder, getMaximalNumberOfChildren, getNumberOfChildren, getNumberOfIndexes, getNumberOfStates, getTransitionElementIndex, hasAnySelfTransitions, initializeRandomly, isAbsoring, joinStatistics, resetStatistic, setParameters, toString, toString, toXML
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface de.jstacs.sequenceScores.statisticalModels.trainable.hmm.transitions.TransitionWithSufficientStatistic
addToStatistic, getLogGammaScoreFromStatistic, joinStatistics, resetStatistic
 
Methods inherited from interface de.jstacs.sequenceScores.statisticalModels.trainable.hmm.transitions.Transition
fillTransitionInformation, getChildIdx, getGraphizNetworkRepresentation, getLastContextState, getLogPriorTerm, getLogScoreFor, getMaximalInDegree, getMaximalMarkovOrder, getMaximalNumberOfChildren, getNumberOfChildren, getNumberOfIndexes, getNumberOfStates, hasAnySelfTransitions, initializeRandomly, isAbsoring, setParameters, toString
 
Methods inherited from interface de.jstacs.Storable
toXML
 
Methods inherited from interface de.jstacs.sampling.SamplingFromStatistic
drawParametersFromStatistic
 

Field Detail

paramsFile

protected File[] paramsFile
The files for saving the parameters during the sampling.


counter

protected int[] counter
The counter for the sampling steps of each sampling.


samplingIndex

protected int samplingIndex
The index of the current sampling.


writer

protected BufferedWriter writer
The writer for the paramsFile in a sampling.


reader

protected BufferedReader reader
The reader for the paramsFile after a sampling.

Constructor Detail

HigherOrderTransition

public HigherOrderTransition(boolean[] isSilent,
                             TransitionElement... transitions)
                      throws Exception
The main constructor.

Parameters:
transitions - the TransitionElements 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 TransitionElements and creating internal fields

HigherOrderTransition

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

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

getXMLTag

protected String getXMLTag()
Description copied from class: BasicHigherOrderTransition
The method returns the XML tag used during saving and loading the transition.

Overrides:
getXMLTag in class BasicHigherOrderTransition
Returns:
he XML tag used during saving and loading the transition.

appendFurtherInformation

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

Overrides:
appendFurtherInformation in class BasicHigherOrderTransition
Parameters:
xml - the XML representation

extractFurtherInformation

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

Overrides:
extractFurtherInformation in class BasicHigherOrderTransition
Parameters:
xml - the XML representation
Throws:
NonParsableException - if the information could not be reconstructed out of the StringBuffer xml

clone

public HigherOrderTransition 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 BasicHigherOrderTransition
Returns:
a deep clone of the current instance.
Throws:
CloneNotSupportedException - if the instance could not be cloned

fillParameters

public void fillParameters(double[] params)
Description copied from interface: DifferentiableTransition
This method allows to fill the parameters of the transition in a given array.

Specified by:
fillParameters in interface DifferentiableTransition
Parameters:
params - the parameters

fillParameters

protected void fillParameters(double[] params,
                              int offset)
This method allows to fill the current parameters using a specific offset.

Parameters:
params - the parameters
offset - the offset indicating the start position
See Also:
fillParameters(double[]), TransitionElement.fillParameters(double[], int)

setParameterOffset

public int setParameterOffset(int offset)
Description copied from interface: DifferentiableTransition
This method sets the internal offset of the parameter index.

Specified by:
setParameterOffset in interface DifferentiableTransition
Parameters:
offset - the offset
Returns:
the offset for the next parameters

setParameterOffset

protected int setParameterOffset()
This method allows to set the parameter offset in each internally used TransitionElement.

Returns:
the new parameter offset
See Also:
setParameterOffset(int), TransitionElement.setParameterOffset(int)

setParameters

public void setParameters(double[] params,
                          int start)
Description copied from interface: DifferentiableTransition
This method allows to set the parameters of the transition.

Specified by:
setParameters in interface DifferentiableTransition
Parameters:
params - the parameters
start - the (global) start position

setParams

protected void setParams(double[] params,
                         int start)
This method allows to set the new parameters using a specific offset.

Parameters:
params - the parameters
start - the offset indicating the start position
See Also:
setParameters(double[], int), TransitionElement.setParameters(double[], int)

addGradientForLogPriorTerm

public void addGradientForLogPriorTerm(double[] gradient,
                                       int start)
Description copied from interface: DifferentiableTransition
This method computes the gradient of Transition.getLogPriorTerm() for each parameter of this transition. The results are added to the array gradient beginning at index start.

Specified by:
addGradientForLogPriorTerm in interface DifferentiableTransition
Parameters:
gradient - the array of gradients
start - the start index in the gradient array, where the partial derivations for the parameters of this Transition shall be entered

getLogScoreAndPartialDerivation

public double getLogScoreAndPartialDerivation(int layer,
                                              int index,
                                              int childIdx,
                                              IntList indices,
                                              DoubleList partDer,
                                              Sequence sequence,
                                              int sequencePosition)
Description copied from interface: DifferentiableTransition
This method allows to compute the logarithm of the score and the gradient for a specific transition.

Specified by:
getLogScoreAndPartialDerivation in interface DifferentiableTransition
Parameters:
layer - the layer of the matrix
index - the index encoding the context
childIdx - the index of the child
indices - a list for the parameter indices
partDer - a list for the partial derivations
sequence - the sequence
sequencePosition - the position within the sequence
Returns:
the logarithm of the score
See Also:
Transition.getLogScoreFor(int, int, int, Sequence, int)

initForSampling

public void initForSampling(int starts)
                     throws IOException
Description copied from interface: SamplingComponent
This method initializes the instance for the sampling. For instance this method can be used to create new files where all parameter sets are stored.

Specified by:
initForSampling in interface SamplingComponent
Parameters:
starts - the number of different sampling starts that will be done
Throws:
IOException - if something went wrong
See Also:
File.createTempFile(String, String, java.io.File )

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable

extendSampling

public void extendSampling(int sampling,
                           boolean append)
                    throws IOException
Description copied from interface: SamplingComponent
This method allows to extend a sampling.

Specified by:
extendSampling in interface SamplingComponent
Parameters:
sampling - the index of the sampling
append - whether to append the sampled parameters to an existing file or to overwrite the file
Throws:
IOException - if the file could not be handled correctly

isInSamplingMode

public boolean isInSamplingMode()
Description copied from interface: SamplingComponent
This method returns true if the object is currently used in a sampling, otherwise false.

Specified by:
isInSamplingMode in interface SamplingComponent
Returns:
true if the object is currently used in a sampling, otherwise false

parseNextParameterSet

public boolean parseNextParameterSet()
Description copied from interface: SamplingComponent
This method allows the user to parse the next set of parameters (from a file).

Specified by:
parseNextParameterSet in interface SamplingComponent
Returns:
true if the parameters could be parsed, otherwise false
See Also:
SamplingComponent.parseParameterSet(int, int)

parseParameterSet

public boolean parseParameterSet(int sampling,
                                 int n)
                          throws IOException
Description copied from interface: SamplingComponent
This method allows the user to parse the set of parameters with index n of a certain sampling (from a file). The internal numbering should start with 0. The parameter set with index 0 is the initial (random) parameter set. It is recommended that a series of parameter sets is accessed by the following lines:

for( sampling = 0; sampling < numSampling; sampling++ )
{

boolean b = parseParameterSet( sampling, n );
while( b )
{
//do something
b = parseNextParameterSet();
}
}

Specified by:
parseParameterSet in interface SamplingComponent
Parameters:
sampling - the index of the sampling
n - the index of the parameter set
Returns:
true if the parameter set could be parsed
Throws:
IOException
See Also:
SamplingComponent.parseNextParameterSet()

samplingStopped

public void samplingStopped()
                     throws IOException
Description copied from interface: SamplingComponent
This method is the opposite of the method SamplingComponent.extendSampling(int, boolean). It can be used for closing any streams of writer, ...

Specified by:
samplingStopped in interface SamplingComponent
Throws:
IOException - if something went wrong
See Also:
SamplingComponent.extendSampling(int, boolean)

acceptParameters

public void acceptParameters()
                      throws IOException
Description copied from interface: SamplingComponent
This methods accepts the drawn parameters. Internally the drawn parameters should be saved (to a file).

Specified by:
acceptParameters in interface SamplingComponent
Throws:
IOException - if the file could not be handled correctly

getLogPosteriorFromStatistic

public double getLogPosteriorFromStatistic()
Description copied from interface: SamplingFromStatistic
This method calculates the a-posteriori probability for the current statistics

Specified by:
getLogPosteriorFromStatistic in interface SamplingFromStatistic
Returns:
the logarithm of the a-posteriori probability

getSizeOfEventSpace

public int getSizeOfEventSpace(int index)
Description copied from interface: DifferentiableTransition
Returns the size of the event space, i.e., the number of possible outcomes, for the random variable of parameter index

Specified by:
getSizeOfEventSpace in interface DifferentiableTransition
Parameters:
index - the index of the parameter
Returns:
the size of the event space

fillSamplingGroups

public void fillSamplingGroups(int parameterOffset,
                               LinkedList<int[]> list)
Description copied from interface: DifferentiableTransition
Adds the groups of indexes of those parameters of this transition that should be sampled together in one step of a grouped sampling procedure, each as an int[], into list. In most cases, one group should contain the parameters that are living on a common simplex, e.g. the parameters of one TransitionElement of this transition. The internal indexes of the parameters are incremeneted by an external parameterOffset

Specified by:
fillSamplingGroups in interface DifferentiableTransition
Parameters:
parameterOffset - the external parameter offset
list - the list of sampling groups