de.jstacs.models.discrete.homogeneous
Class HomogeneousMM

java.lang.Object
  extended by de.jstacs.models.AbstractModel
      extended by de.jstacs.models.discrete.DiscreteGraphicalModel
          extended by de.jstacs.models.discrete.homogeneous.HomogeneousModel
              extended by de.jstacs.models.discrete.homogeneous.HomogeneousMM
All Implemented Interfaces:
InstantiableFromParameterSet, Model, Storable, Cloneable

public class HomogeneousMM
extends HomogeneousModel

This class implements homogeneous Markov models (hMM) of arbitrary order.

Author:
Jens Keilwagen
See Also:
HomMMParameterSet

Nested Class Summary
 
Nested classes/interfaces inherited from class de.jstacs.models.discrete.homogeneous.HomogeneousModel
HomogeneousModel.HomCondProb
 
Field Summary
 
Fields inherited from class de.jstacs.models.discrete.homogeneous.HomogeneousModel
order, powers
 
Fields inherited from class de.jstacs.models.discrete.DiscreteGraphicalModel
params, trained
 
Fields inherited from class de.jstacs.models.AbstractModel
alphabets, length
 
Constructor Summary
HomogeneousMM(HomMMParameterSet params)
          Creates a homogeneous Markov model from a parameter set.
HomogeneousMM(StringBuffer stringBuff)
          Creates a homogeneous Markov model from a StringBuffer.
 
Method Summary
 HomogeneousMM clone()
          Follows the conventions of Object's clone-method.
protected  StringBuffer getFurtherModelInfos()
           
 String getInstanceName()
          Should return a short instance name such as iMM(0), BN(2), ...
 double getLogPriorTerm()
          Returns a value that is proportional to the log of the prior.
protected  Sequence getRandomSequence(Random r, int length)
          This method creates a sequence from a trained model.
protected  String getXMLTag()
           
protected  double logProbFor(Sequence sequence, int startpos, int endpos)
          This method computes the logarithm of the probability of the given sequence in the given interval.
protected  double probFor(Sequence sequence, int startpos, int endpos)
          This method computes the probability of the given sequence in the given interval.
protected  void set(DGMParameterSet params, boolean trained)
          Sets the parameters as internal parameters and does some essential computations.
protected  void setFurtherModelInfos(StringBuffer xml)
          This method replaces the internal model infos with those from the StringBuffer.
 String toString()
          Should give a simple representation (text) of the model as String.
 void train(Sample[] data, double[][] weights)
          Trains the model using an array of weighted samples.
 void train(Sample data, double[] weights)
          Trains the Model object given the data as Sample using the specified weights.
 
Methods inherited from class de.jstacs.models.discrete.homogeneous.HomogeneousModel
check, chooseFromDistr, cloneHomProb, emitSample, getLogProbFor, getMaximalMarkovOrder, getNumericalCharacteristics, getProbFor, train
 
Methods inherited from class de.jstacs.models.discrete.DiscreteGraphicalModel
fromXML, getCurrentParameterSet, getDescription, getESS, isTrained, toXML
 
Methods inherited from class de.jstacs.models.AbstractModel
getAlphabetContainer, getCharacteristics, getLength, getLogProbFor, getLogProbFor, getLogProbFor, getLogProbFor, getPriorTerm, getProbFor, getProbFor, set, setNewAlphabetContainerInstance, train
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HomogeneousMM

public HomogeneousMM(HomMMParameterSet params)
              throws CloneNotSupportedException,
                     IllegalArgumentException,
                     NonParsableException
Creates a homogeneous Markov model from a parameter set.

Parameters:
params - the parameter set
Throws:
CloneNotSupportedException - if the parameter set could not be cloned
IllegalArgumentException - if the parameter set is not instantiated
NonParsableException - if the parameter set is not parsable

HomogeneousMM

public HomogeneousMM(StringBuffer stringBuff)
              throws NonParsableException
Creates a homogeneous Markov model from a StringBuffer.

Parameters:
stringBuff - the StringBuffer
Throws:
NonParsableException - if the buffer is not parsable
Method Detail

clone

public HomogeneousMM clone()
                    throws CloneNotSupportedException
Description copied from class: AbstractModel
Follows the conventions of Object's clone-method.

Specified by:
clone in interface Model
Overrides:
clone in class DiscreteGraphicalModel
Returns:
an object, that is a copy of the current AbstractModel (the member-AlphabetContainer isn't deeply cloned since it is assumed to be immutable). The type of the returned object is defined by the class X directly inherited from AbstractModel. Hence X's clone-method should work as:
1. Object o = (X)super.clone(); 2. all additional member variables of o defined by X that are not of simple data-types like int, double, ... , have to be deeply copied 3. return o
Throws:
CloneNotSupportedException

getRandomSequence

protected Sequence getRandomSequence(Random r,
                                     int length)
                              throws WrongAlphabetException,
                                     WrongSequenceTypeException
Description copied from class: HomogeneousModel
This method creates a sequence from a trained model.

Specified by:
getRandomSequence in class HomogeneousModel
Parameters:
r - the random generator
length - the length of the sequence
Returns:
the sequence
Throws:
WrongAlphabetException
WrongSequenceTypeException

getInstanceName

public String getInstanceName()
Description copied from interface: Model
Should return a short instance name such as iMM(0), BN(2), ...

Returns:
a short instance name

getLogPriorTerm

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

Returns:
a value that is proportional to the log of the prior
Throws:
Exception - if something went wrong
See Also:
Model.getPriorTerm()

logProbFor

protected double logProbFor(Sequence sequence,
                            int startpos,
                            int endpos)
Description copied from class: HomogeneousModel
This method computes the logarithm of the probability of the given sequence in the given interval. The method is only used in Model.getLogProbFor(Sequence, int, int) after the method HomogeneousModel.check(Sequence, int, int) has been invoked.

Specified by:
logProbFor in class HomogeneousModel
Parameters:
sequence - the sequence
startpos - the start position
endpos - the end position
Returns:
the logarithm of the probability for the given subsequence
See Also:
HomogeneousModel.check(Sequence, int, int), Model.getLogProbFor(Sequence, int, int)

probFor

protected double probFor(Sequence sequence,
                         int startpos,
                         int endpos)
Description copied from class: HomogeneousModel
This method computes the probability of the given sequence in the given interval. The method is only used in Model.getProbFor(Sequence, int, int) after the method HomogeneousModel.check(Sequence, int, int) has been invoked.

Specified by:
probFor in class HomogeneousModel
Parameters:
sequence - the sequence
startpos - the start position
endpos - the end position
Returns:
the probability for the given subsequence
See Also:
HomogeneousModel.check(Sequence, int, int), Model.getProbFor(Sequence, int, int)

toString

public String toString()
Description copied from interface: Model
Should give a simple representation (text) of the model as String.

Specified by:
toString in interface Model
Overrides:
toString in class DiscreteGraphicalModel
Returns:
the representation as String

train

public void train(Sample data,
                  double[] weights)
           throws Exception
Description copied from interface: Model
Trains the Model object given the data as Sample using the specified weights. The weight at position i belongs to the element at position i. So the array weight should have the number of sequences in the sample as dimension. (Optionally it is possible to use weight == null if all weights have the value one.)
This method should work non-incrementally. That means the result of the following series: train(data1); train(data2) should be a fully trained model over data2 and not over data1+data2. All parameters of the model were given by the call of the constructor.

Parameters:
data - the given sequences
weights - the weights of the elements, each weight should be non-negative
Throws:
Exception - an Exception should be thrown if the training did not succeed (e.g. the weights dimension of weights and number of samples does not match).
See Also:
Sample.getElementAt(int), Sample.ElementEnumerator

train

public void train(Sample[] data,
                  double[][] weights)
           throws Exception
Description copied from class: HomogeneousModel
Trains the model using an array of weighted samples. The weights[i] are for data[i].

Specified by:
train in class HomogeneousModel
Parameters:
data - the samples
weights - the weights
Throws:
Exception - if something went wrong, furthermore data.length has to be weights.length

getFurtherModelInfos

protected StringBuffer getFurtherModelInfos()
Specified by:
getFurtherModelInfos in class DiscreteGraphicalModel
Returns:
the model infos like parameters of the distribution ... in xml format
See Also:
DiscreteGraphicalModel.toXML()

getXMLTag

protected String getXMLTag()
Specified by:
getXMLTag in class DiscreteGraphicalModel
Returns:
the XML tag that is used in fromXML() and toXML()
See Also:
DiscreteGraphicalModel.fromXML(StringBuffer), DiscreteGraphicalModel.toXML()

set

protected void set(DGMParameterSet params,
                   boolean trained)
            throws CloneNotSupportedException,
                   NonParsableException
Description copied from class: DiscreteGraphicalModel
Sets the parameters as internal parameters and does some essential computations. Used in fromParameterSet-methods.

Overrides:
set in class HomogeneousModel
Parameters:
params - the new ParameterSet
trained - the indicator for the model
Throws:
CloneNotSupportedException - if the parmeterSet could not be cloned
NonParsableException - if the parameters of the model could not be parsed

setFurtherModelInfos

protected void setFurtherModelInfos(StringBuffer xml)
                             throws NonParsableException
Description copied from class: DiscreteGraphicalModel
This method replaces the internal model infos with those from the StringBuffer.

Specified by:
setFurtherModelInfos in class DiscreteGraphicalModel
Parameters:
xml - contains the model infos like parameters of the distribution ... in xml format
Throws:
NonParsableException - if the StringBuffer could not be parsed
See Also:
DiscreteGraphicalModel.fromXML(StringBuffer)