de.jstacs.models
Class VariableLengthWrapperModel

java.lang.Object
  extended by de.jstacs.models.AbstractModel
      extended by de.jstacs.models.VariableLengthWrapperModel
All Implemented Interfaces:
Model, Storable, Cloneable

public class VariableLengthWrapperModel
extends AbstractModel

This class allows to train any Model on Samples of Sequences with variable length if each individual length is at least Model.getLength(). All other methods are piped to the internally used Model.

This class might be useful in any ClassifierAssessment.

Author:
Jens Keilwagen
See Also:
Sample.WeightedSampleFactory.Sample.WeightedSampleFactory(SortOperation, Sample, double[], int), ClassifierAssessment

Field Summary
 
Fields inherited from class de.jstacs.models.AbstractModel
alphabets, length
 
Constructor Summary
VariableLengthWrapperModel(Model m)
          This is the main constructor that creates an instance from any Model.
VariableLengthWrapperModel(StringBuffer stringBuff)
          The standard constructor for the interface Storable.
 
Method Summary
 VariableLengthWrapperModel clone()
          Follows the conventions of Object's clone()-method.
protected  void fromXML(StringBuffer xml)
          This method should only be used by the constructor that works on a StringBuffer.
 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.
 NumericalResultSet getNumericalCharacteristics()
          Returns the subset of numerical values that are also returned by Model.getCharacteristics().
 double getProbFor(Sequence sequence, int startpos, int endpos)
          Returns the probability of (a part of) the given sequence given the model.
 boolean isTrained()
          Returns true if the model has been trained successfully, false otherwise.
 StringBuffer toXML()
          This method returns an XML representation as StringBuffer of an instance of the implementing class.
 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.AbstractModel
emitSample, getAlphabetContainer, getCharacteristics, getLength, getLogProbFor, getLogProbFor, getLogProbFor, getLogProbFor, getLogProbFor, getMaximalMarkovOrder, getPriorTerm, getProbFor, getProbFor, set, setNewAlphabetContainerInstance, train
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface de.jstacs.models.Model
toString
 

Constructor Detail

VariableLengthWrapperModel

public VariableLengthWrapperModel(Model m)
                           throws CloneNotSupportedException
This is the main constructor that creates an instance from any Model.

Parameters:
m - the model
Throws:
CloneNotSupportedException - if the mode m could not be cloned

VariableLengthWrapperModel

public VariableLengthWrapperModel(StringBuffer stringBuff)
                           throws NonParsableException
The standard constructor for the interface Storable. Creates a new VariableLengthWrapperModel out of a StringBuffer.

Parameters:
stringBuff - the StringBuffer to be parsed
Throws:
NonParsableException - is thrown if the StringBuffer could not be parsed
Method Detail

clone

public VariableLengthWrapperModel 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 AbstractModel
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 - if something went wrong while cloning

fromXML

protected void fromXML(StringBuffer xml)
                throws NonParsableException
Description copied from class: AbstractModel
This method should only be used by the constructor that works on a StringBuffer. It is the counter part of Storable.toXML().

Specified by:
fromXML in class AbstractModel
Parameters:
xml - the XML representation of the model
Throws:
NonParsableException - if the StringBuffer is not parsable or the representation is conflicting
See Also:
AbstractModel.AbstractModel(StringBuffer)

toXML

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

Returns:
the XML representation

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 maximum likelihood (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()

getNumericalCharacteristics

public NumericalResultSet getNumericalCharacteristics()
                                               throws Exception
Description copied from interface: Model
Returns the subset of numerical values that are also returned by Model.getCharacteristics().

Returns:
the numerical characteristics of the current instance of the model
Throws:
Exception - if some of the characteristics could not be defined

getProbFor

public double getProbFor(Sequence sequence,
                         int startpos,
                         int endpos)
                  throws NotTrainedException,
                         Exception
Description copied from interface: Model
Returns the probability of (a part of) the given sequence given the model. If at least one random variable is continuous the value of density function is returned.

It extends the possibility given by the method Model.getProbFor(Sequence, int) by the fact, that the model could be e.g. homogeneous and therefore the length of the sequences, whose probability should be returned, is not fixed. Additionally the end position of the part of the given sequence is given and the probability of the part from position startpos to endpos (inclusive) should be returned.
The length and the alphabets define the type of data that can be modeled and therefore both has to be checked.

Parameters:
sequence - the given sequence
startpos - the start position within the given sequence
endpos - the last position to be taken into account
Returns:
the probability or the value of the density function of (the part of) the given sequence given the model
Throws:
NotTrainedException - if the model is not trained yet
Exception - if the sequence could not be handled (e.g. startpos > endpos, endpos > sequence.length, ...) by the model

isTrained

public boolean isTrained()
Description copied from interface: Model
Returns true if the model has been trained successfully, false otherwise.

Returns:
true if the model has been trained successfully, false otherwise

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 as Sample
weights - the weights of the elements, each weight should be non-negative
Throws:
Exception - if the training did not succeed (e.g. the dimension of weights and the number of sequences in the sample do not match)
See Also:
Sample.getElementAt(int), Sample.ElementEnumerator