<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.jstacs.de/index.php?action=history&amp;feed=atom&amp;title=Recipes</id>
	<title>Recipes - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.jstacs.de/index.php?action=history&amp;feed=atom&amp;title=Recipes"/>
	<link rel="alternate" type="text/html" href="https://www.jstacs.de/index.php?title=Recipes&amp;action=history"/>
	<updated>2026-04-04T12:24:56Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.38.2</generator>
	<entry>
		<id>https://www.jstacs.de/index.php?title=Recipes&amp;diff=563&amp;oldid=prev</id>
		<title>Keilwagen at 07:50, 14 May 2012</title>
		<link rel="alternate" type="text/html" href="https://www.jstacs.de/index.php?title=Recipes&amp;diff=563&amp;oldid=prev"/>
		<updated>2012-05-14T07:50:42Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://www.jstacs.de/index.php?title=Recipes&amp;amp;diff=563&amp;amp;oldid=526&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Keilwagen</name></author>
	</entry>
	<entry>
		<id>https://www.jstacs.de/index.php?title=Recipes&amp;diff=526&amp;oldid=prev</id>
		<title>Grau at 14:28, 2 February 2012</title>
		<link rel="alternate" type="text/html" href="https://www.jstacs.de/index.php?title=Recipes&amp;diff=526&amp;oldid=prev"/>
		<updated>2012-02-02T14:28:17Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://www.jstacs.de/index.php?title=Recipes&amp;amp;diff=526&amp;amp;oldid=500&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Grau</name></author>
	</entry>
	<entry>
		<id>https://www.jstacs.de/index.php?title=Recipes&amp;diff=500&amp;oldid=prev</id>
		<title>Grau: Created page with &quot;In this section, we show some more complex code examples. All these code examples can be downloaded at &lt;font color=red&gt;XXXX&lt;/font&gt; and may serve as a starting points for your own...&quot;</title>
		<link rel="alternate" type="text/html" href="https://www.jstacs.de/index.php?title=Recipes&amp;diff=500&amp;oldid=prev"/>
		<updated>2012-02-01T23:06:42Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;In this section, we show some more complex code examples. All these code examples can be downloaded at &amp;lt;font color=red&amp;gt;XXXX&amp;lt;/font&amp;gt; and may serve as a starting points for your own...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In this section, we show some more complex code examples. All these code examples can be downloaded at &amp;lt;font color=red&amp;gt;XXXX&amp;lt;/font&amp;gt; and may serve as a starting points for your own applications.&lt;br /&gt;
&lt;br /&gt;
== Creation of user-specfic alphabet ==&lt;br /&gt;
In this example, we create a new [http://www.jstacs.de/api-2.0//de/jstacs/data/alphabets/ComplementableDiscreteAlphabet.html ComplementableDiscreteAlphabet] using the generic implementation. We then use this [http://www.jstacs.de/api-2.0//de/jstacs/data/alphabets/Alphabet.html Alphabet] to create a [http://www.jstacs.de/api-2.0//de/jstacs/data/sequences/Sequence.html Sequence] and compute its reverse complement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		String[] symbols = {&amp;quot;A&amp;quot;, &amp;quot;C&amp;quot;, &amp;quot;G&amp;quot;, &amp;quot;T&amp;quot;, &amp;quot;-&amp;quot;};&lt;br /&gt;
		//new alphabet&lt;br /&gt;
		DiscreteAlphabet abc = new DiscreteAlphabet( true, symbols );&lt;br /&gt;
		&lt;br /&gt;
		//new alphabet that allows to build the reverse complement of a sequence&lt;br /&gt;
		int[] revComp = new int[symbols.length];&lt;br /&gt;
		revComp[0] = 3; //symbols[0]^rc = symbols[3]&lt;br /&gt;
		revComp[1] = 2; //symbols[1]^rc = symbols[2]&lt;br /&gt;
		revComp[2] = 1; //symbols[2]^rc = symbols[1]&lt;br /&gt;
		revComp[3] = 0; //symbols[3]^rc = symbols[0]&lt;br /&gt;
		revComp[4] = 4; //symbols[4]^rc = symbols[4]&lt;br /&gt;
		GenericComplementableDiscreteAlphabet abc2 = new GenericComplementableDiscreteAlphabet( true, symbols, revComp );&lt;br /&gt;
		&lt;br /&gt;
		Sequence seq = Sequence.create( new AlphabetContainer( abc2 ), &amp;quot;ACGT-&amp;quot; );&lt;br /&gt;
		Sequence rc = seq.reverseComplement();&lt;br /&gt;
		System.out.println( seq );&lt;br /&gt;
		System.out.println( rc );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Creating Data sets ==&lt;br /&gt;
In this example, we show different ways of creating a [http://www.jstacs.de/api-2.0//de/jstacs/data/DataSet.html DataSet] in Jstacs from plain text and FastA files and using the adaptor to BioJava.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		String home = args[0];&lt;br /&gt;
		&lt;br /&gt;
		//load DNA sequences in FastA-format&lt;br /&gt;
		DataSet data = new DNADataSet( home+&amp;quot;myfile.fa&amp;quot; ); &lt;br /&gt;
		&lt;br /&gt;
		//create a DNA-alphabet&lt;br /&gt;
		AlphabetContainer container = DNAAlphabetContainer.SINGLETON;&lt;br /&gt;
		&lt;br /&gt;
		//create a DataSet using the alphabet from above in FastA-format&lt;br /&gt;
		data = new DataSet( container, new SparseStringExtractor( home+&amp;quot;myfile.fa&amp;quot;, StringExtractor.FASTA ));&lt;br /&gt;
		&lt;br /&gt;
		//create a DataSet using the alphabet from above&lt;br /&gt;
		data = new DataSet( container, new SparseStringExtractor( home+&amp;quot;myfile.txt&amp;quot; ));&lt;br /&gt;
		&lt;br /&gt;
		//defining the ids, we want to obtain from NCBI Genbank:&lt;br /&gt;
		GenbankRichSequenceDB db = new GenbankRichSequenceDB();&lt;br /&gt;
		&lt;br /&gt;
		SimpleSequenceIterator it = new SimpleSequenceIterator(&lt;br /&gt;
				db.getRichSequence( &amp;quot;NC_001284.2&amp;quot; ),&lt;br /&gt;
				db.getRichSequence( &amp;quot;NC_000932.1&amp;quot; )&lt;br /&gt;
				);&lt;br /&gt;
		 &lt;br /&gt;
		//conversion to Jstacs DataSet&lt;br /&gt;
		data = BioJavaAdapter.sequenceIteratorToDataSet( it, null );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Using TrainSMBasedClassifier ==&lt;br /&gt;
In this example, we show how to create a [http://www.jstacs.de/api-2.0//de/jstacs/classifiers/trainSMBased/TrainSMBasedClassifier.html TrainSMBasedClassifier] using to position weight matrices, train this classifier, classify previously unlabeled data, store the classifier to its XML representation, and load it back into Jstacs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		String home = args[0];&lt;br /&gt;
		&lt;br /&gt;
		//create a DataSet for each class from the input data, using the DNA alphabet&lt;br /&gt;
		DataSet[] data = new DataSet[2];&lt;br /&gt;
		data[0] = new DNADataSet( args[1] );&lt;br /&gt;
		&lt;br /&gt;
		//the length of our input sequences&lt;br /&gt;
		int length = data[0].getElementLength();&lt;br /&gt;
&lt;br /&gt;
		data[1] = new DataSet( new DNADataSet( args[2] ), length );&lt;br /&gt;
		 &lt;br /&gt;
		//create a new PWM&lt;br /&gt;
		BayesianNetworkTrainSM pwm = new BayesianNetworkTrainSM( new BayesianNetworkTrainSMParameterSet(&lt;br /&gt;
				//the alphabet and the length of the model:&lt;br /&gt;
				data[0].getAlphabetContainer(), length, &lt;br /&gt;
				//the equivalent sample size to compute hyper-parameters&lt;br /&gt;
				4, &lt;br /&gt;
				//some identifier for the model&lt;br /&gt;
				&amp;quot;my PWM&amp;quot;, &lt;br /&gt;
				//we want a PWM, which is an inhomogeneous Markov model (IMM) of order 0&lt;br /&gt;
				ModelType.IMM, (byte) 0, &lt;br /&gt;
				//we want to estimate the MAP-parameters&lt;br /&gt;
				LearningType.ML_OR_MAP ) );&lt;br /&gt;
		 &lt;br /&gt;
		//create a new classifier&lt;br /&gt;
		TrainSMBasedClassifier classifier = new TrainSMBasedClassifier( pwm, pwm );&lt;br /&gt;
		System.out.println(&amp;quot;x&amp;quot;);&lt;br /&gt;
		//train the classifier&lt;br /&gt;
		classifier.train( data );&lt;br /&gt;
		System.out.println(&amp;quot;y&amp;quot;);&lt;br /&gt;
		//sequences that will be classified&lt;br /&gt;
		DataSet toClassify = new DNADataSet( args[3] );&lt;br /&gt;
		 &lt;br /&gt;
		//use the trained classifier to classify new sequences&lt;br /&gt;
		byte[] result = classifier.classify( toClassify ); &lt;br /&gt;
		System.out.println( Arrays.toString( result ) );&lt;br /&gt;
		 &lt;br /&gt;
		//create the XML-representation of the classifier&lt;br /&gt;
		StringBuffer buf = new StringBuffer();&lt;br /&gt;
		XMLParser.appendObjectWithTags( buf, classifier, &amp;quot;classifier&amp;quot; );&lt;br /&gt;
		 &lt;br /&gt;
		//write it to disk&lt;br /&gt;
		FileManager.writeFile( new File(home+&amp;quot;myClassifier.xml&amp;quot;), buf );&lt;br /&gt;
		&lt;br /&gt;
		//read XML-representation from disk&lt;br /&gt;
		StringBuffer buf2 = FileManager.readFile( new File(home+&amp;quot;myClassifier.xml&amp;quot;) );&lt;br /&gt;
		 &lt;br /&gt;
		//create new classifier from read StringBuffer containing XML-code&lt;br /&gt;
		AbstractClassifier trainedClassifier = (AbstractClassifier) XMLParser.extractObjectForTags(buf2, &amp;quot;classifier&amp;quot;);	&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Using GenDisMixClassifier ==&lt;br /&gt;
In this example, we show how to create [http://www.jstacs.de/api-2.0//de/jstacs/classifiers/differentiableSequenceScoreBased/gendismix/GenDisMixClassifier.html GenDisMixClassifier] s using two position weight matrices. We show how [http://www.jstacs.de/api-2.0//de/jstacs/classifiers/differentiableSequenceScoreBased/gendismix/GenDisMixClassifier.html GenDisMixClassifier] s can be created for all basic learning principles (ML, MAP, MCL, MSP), and how these classifiers can be trained and assessed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		//read FastA-files&lt;br /&gt;
		DataSet[] data = {&lt;br /&gt;
		         new DNADataSet( args[0] ),&lt;br /&gt;
		         new DNADataSet( args[1] )&lt;br /&gt;
		};&lt;br /&gt;
		AlphabetContainer container = data[0].getAlphabetContainer();&lt;br /&gt;
		int length = data[0].getElementLength();&lt;br /&gt;
		&lt;br /&gt;
		//equivalent sample size =^= ESS&lt;br /&gt;
		double essFg = 4, essBg = 4;&lt;br /&gt;
		//create DifferentiableSequenceScore, here PWM&lt;br /&gt;
		DifferentiableStatisticalModel pwmFg = new BayesianNetworkDiffSM( container, length, essFg, true, new InhomogeneousMarkov(0) );&lt;br /&gt;
		DifferentiableStatisticalModel pwmBg = new BayesianNetworkDiffSM( container, length, essBg, true, new InhomogeneousMarkov(0) );&lt;br /&gt;
		&lt;br /&gt;
		//create parameters of the classifier&lt;br /&gt;
		GenDisMixClassifierParameterSet cps = new GenDisMixClassifierParameterSet(&lt;br /&gt;
				container,//the used alphabets&lt;br /&gt;
				length,//sequence length that can be modeled/classified&lt;br /&gt;
				Optimizer.QUASI_NEWTON_BFGS, 1E-1, 1E-1, 1,//optimization parameter&lt;br /&gt;
				false,//use free parameters or all&lt;br /&gt;
				KindOfParameter.PLUGIN,//how to start the numerical optimization&lt;br /&gt;
				true,//use a normalized objective function&lt;br /&gt;
				AbstractMultiThreadedOptimizableFunction.getNumberOfAvailableProcessors()//number of compute threads		&lt;br /&gt;
		);&lt;br /&gt;
		&lt;br /&gt;
		//create classifiers&lt;br /&gt;
		LearningPrinciple[] lp = LearningPrinciple.values();&lt;br /&gt;
		GenDisMixClassifier[] cl = new GenDisMixClassifier[lp.length+1];&lt;br /&gt;
		//elementary learning principles&lt;br /&gt;
		int i = 0;&lt;br /&gt;
		for( ; i &amp;lt; cl.length-1; i++ ){&lt;br /&gt;
			System.out.println( &amp;quot;classifier &amp;quot; + i + &amp;quot; uses &amp;quot; + lp[i] );&lt;br /&gt;
			cl[i] = new GenDisMixClassifier( cps, new CompositeLogPrior(), lp[i], pwmFg, pwmBg );&lt;br /&gt;
		}&lt;br /&gt;
		&lt;br /&gt;
		//use some weighted version of log conditional likelihood, log likelihood, and log prior&lt;br /&gt;
		double[] beta = {0.3,0.3,0.4};&lt;br /&gt;
		System.out.println( &amp;quot;classifier &amp;quot; + i + &amp;quot; uses the weights &amp;quot; + Arrays.toString( beta ) );&lt;br /&gt;
		cl[i] = new GenDisMixClassifier( cps, new CompositeLogPrior(), beta, pwmFg, pwmBg );&lt;br /&gt;
		&lt;br /&gt;
		//do what ever you like&lt;br /&gt;
		&lt;br /&gt;
		//e.g., train&lt;br /&gt;
		for( i = 0; i &amp;lt; cl.length; i++ ){&lt;br /&gt;
			cl[i].train( data );&lt;br /&gt;
		}&lt;br /&gt;
		&lt;br /&gt;
		//e.g., evaluate (normally done on a test data set)&lt;br /&gt;
		PerformanceMeasureParameterSet mp = PerformanceMeasureParameterSet.createFilledParameters();&lt;br /&gt;
		for( i = 0; i &amp;lt; cl.length; i++ ){&lt;br /&gt;
			System.out.println( cl[i].evaluate( mp, true, data ) );&lt;br /&gt;
		}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Accessing R from Jstacs ==&lt;br /&gt;
Here, we show a number of examples how R can be used from within Jstacs using RServe.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		REnvironment e = null;&lt;br /&gt;
		try {&lt;br /&gt;
			//create a connection to R with YOUR server name, login and password&lt;br /&gt;
			e = new REnvironment();//might be adjusted&lt;br /&gt;
		 &lt;br /&gt;
			System.out.println( &amp;quot;java: &amp;quot; + System.getProperty( &amp;quot;java.version&amp;quot; ) );&lt;br /&gt;
			System.out.println();&lt;br /&gt;
			System.out.println( e.getVersionInformation() );&lt;br /&gt;
		 &lt;br /&gt;
			// compute something in R&lt;br /&gt;
			REXP erg = e.eval( &amp;quot;sin(10)&amp;quot; );&lt;br /&gt;
			System.out.println( erg.asDouble() );&lt;br /&gt;
		 &lt;br /&gt;
			//create a histrgram in R in 3 steps&lt;br /&gt;
			//1) create the data&lt;br /&gt;
			e.voidEval( &amp;quot;a = 100;&amp;quot; );&lt;br /&gt;
			e.voidEval( &amp;quot;n = rnorm(a)&amp;quot; );&lt;br /&gt;
			//2) create the plot command&lt;br /&gt;
			String plotCmd = &amp;quot;hist(n,breaks=a/5)&amp;quot;;&lt;br /&gt;
			//3a) plot as pdf&lt;br /&gt;
			e.plotToPDF( plotCmd, args[0]+&amp;quot;/test.pdf&amp;quot;, true );&lt;br /&gt;
			//or&lt;br /&gt;
			//3b) create an image and show it&lt;br /&gt;
			BufferedImage i = e.plot( plotCmd, 640, 480 );&lt;br /&gt;
			REnvironment.showImage( &amp;quot;histogramm&amp;quot;, i, JFrame.EXIT_ON_CLOSE );&lt;br /&gt;
		 &lt;br /&gt;
		} catch ( Exception ex ) {&lt;br /&gt;
			ex.printStackTrace();&lt;br /&gt;
		} finally {&lt;br /&gt;
			if( e != null ) {&lt;br /&gt;
				try {&lt;br /&gt;
					//close REnvironment correctly&lt;br /&gt;
					e.close();&lt;br /&gt;
				} catch ( Exception e1 ) {&lt;br /&gt;
					System.err.println( &amp;quot;could not close REnvironment.&amp;quot; );&lt;br /&gt;
					e1.printStackTrace();&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Getting ROC and PR curve from a classifier ==&lt;br /&gt;
In this example, we show how a classifier (loaded from disk) can be assessed on test data, and how we can plot ROC and PR curves of this classifier and test data set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
	public static void main(String[] args) throws Exception {&lt;br /&gt;
		//read XML-representation from disk&lt;br /&gt;
		StringBuffer buf2 = FileManager.readFile( new File(args[0]+&amp;quot;myClassifier.xml&amp;quot;) );&lt;br /&gt;
		 &lt;br /&gt;
		//create new classifier from read StringBuffer containing XML-code&lt;br /&gt;
		AbstractClassifier trainedClassifier = (AbstractClassifier) XMLParser.extractObjectForTags(buf2, &amp;quot;classifier&amp;quot;);	&lt;br /&gt;
&lt;br /&gt;
		//create a DataSet for each class from the input data, using the DNA alphabet&lt;br /&gt;
		DataSet[] test = new DataSet[2];&lt;br /&gt;
		test[0] = new DNADataSet( args[1] );&lt;br /&gt;
		&lt;br /&gt;
		//the length of our input sequences&lt;br /&gt;
		int length = test[0].getElementLength();&lt;br /&gt;
&lt;br /&gt;
		test[1] = new DataSet( new DNADataSet( args[2] ), length );&lt;br /&gt;
		&lt;br /&gt;
		 &lt;br /&gt;
		AbstractPerformanceMeasure[] m = { new PRCurve(), new ROCCurve() };&lt;br /&gt;
		PerformanceMeasureParameterSet mp = new PerformanceMeasureParameterSet( m );&lt;br /&gt;
		ResultSet rs = trainedClassifier.evaluate( mp, true, test );&lt;br /&gt;
		 &lt;br /&gt;
		REnvironment r = null;&lt;br /&gt;
		try {&lt;br /&gt;
			r = new REnvironment();//might be adjusted&lt;br /&gt;
			for( int i = 0; i &amp;lt; rs.getNumberOfResults(); i++ )  {&lt;br /&gt;
				Result res = rs.getResultAt(i);&lt;br /&gt;
				if( res instanceof DoubleTableResult ) {&lt;br /&gt;
					DoubleTableResult dtr = (DoubleTableResult) res;&lt;br /&gt;
					ImageResult ir = DoubleTableResult.plot( r, dtr );&lt;br /&gt;
					REnvironment.showImage( dtr.getName(), ir.getValue() );&lt;br /&gt;
				} else {&lt;br /&gt;
					System.out.println( res );&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
		} catch( Exception e ) {&lt;br /&gt;
			e.printStackTrace();&lt;br /&gt;
		} finally {&lt;br /&gt;
			if( r != null ) {&lt;br /&gt;
				r.close();&lt;br /&gt;
			}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Performing crossvalidation ==&lt;br /&gt;
In this example, we show how we can compare classifiers built on different types of models and using different learning principles in a cross validation. Specifically, we create a position weight matrix, use that matrix to create a mixture model, and we create an inhomogeneous Markov model of order &amp;lt;math&amp;gt;3&amp;lt;/math&amp;gt;. We do so in the world of [http://www.jstacs.de/api-2.0//de/jstacs/sequenceScores/statisticalModels/trainable/TrainableStatisticalModel.html TrainableStatisticalModel] s and in the world of [http://www.jstacs.de/api-2.0//de/jstacs/sequenceScores/statisticalModels/differentiable/DifferentiableStatisticalModel.html DifferentiableStatisticalModel] s. We then use the mixture model as foreground model and the inhomogeneous Markov model as the background model when building classifiers. The classifiers are learned by the generative MAP principle and the discriminative MSP principle, respectively. &lt;br /&gt;
We then assess these classifiers in a &amp;lt;math&amp;gt;10&amp;lt;/math&amp;gt;-fold cross validation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		//create a DataSet for each class from the input data, using the DNA alphabet&lt;br /&gt;
		DataSet[] data = new DataSet[2];&lt;br /&gt;
		data[0] = new DNADataSet( args[0] );&lt;br /&gt;
		&lt;br /&gt;
		//the length of our input sequences&lt;br /&gt;
		int length = data[0].getElementLength();&lt;br /&gt;
&lt;br /&gt;
		data[1] = new DataSet( new DNADataSet( args[1] ), length );&lt;br /&gt;
		 &lt;br /&gt;
		AlphabetContainer container = data[0].getAlphabetContainer();&lt;br /&gt;
		&lt;br /&gt;
		//create a new PWM&lt;br /&gt;
		BayesianNetworkTrainSM pwm = new BayesianNetworkTrainSM( new BayesianNetworkTrainSMParameterSet(&lt;br /&gt;
				//the alphabet and the length of the model:&lt;br /&gt;
				container, length, &lt;br /&gt;
				//the equivalent sample size to compute hyper-parameters&lt;br /&gt;
				4, &lt;br /&gt;
				//some identifier for the model&lt;br /&gt;
				&amp;quot;my PWM&amp;quot;, &lt;br /&gt;
				//we want a PWM, which is an inhomogeneous Markov model (IMM) of order 0&lt;br /&gt;
				ModelType.IMM, (byte) 0, &lt;br /&gt;
				//we want to estimate the MAP-parameters&lt;br /&gt;
				LearningType.ML_OR_MAP ) );&lt;br /&gt;
		 &lt;br /&gt;
		//create a new mixture model using 2 PWMs&lt;br /&gt;
		MixtureTrainSM mixPwms = new MixtureTrainSM(&lt;br /&gt;
				//the length of the mixture model&lt;br /&gt;
				length, &lt;br /&gt;
				//the two components, which are PWMs&lt;br /&gt;
				new TrainableStatisticalModel[]{pwm,pwm},&lt;br /&gt;
				//the number of starts of the EM&lt;br /&gt;
				10,&lt;br /&gt;
				//the equivalent sample sizes&lt;br /&gt;
				new double[]{pwm.getESS(),pwm.getESS()},&lt;br /&gt;
				//the hyper-parameters to draw the initial sequence-specific component weights (hidden variables)&lt;br /&gt;
				1,&lt;br /&gt;
				//stopping criterion&lt;br /&gt;
				new SmallDifferenceOfFunctionEvaluationsCondition(1E-6),&lt;br /&gt;
				//parameterization of the model, LAMBDA complies with the&lt;br /&gt;
				//parameterization by log-probabilities&lt;br /&gt;
				Parameterization.LAMBDA);&lt;br /&gt;
		 &lt;br /&gt;
		//create a new inhomogeneous Markov model of order 3&lt;br /&gt;
		BayesianNetworkTrainSM mm = new BayesianNetworkTrainSM( &lt;br /&gt;
				new BayesianNetworkTrainSMParameterSet( container, length, 256, &amp;quot;my iMM(3)&amp;quot;, ModelType.IMM, (byte) 3, LearningType.ML_OR_MAP ) );&lt;br /&gt;
		 &lt;br /&gt;
		//create a new PWM scoring function&lt;br /&gt;
		BayesianNetworkDiffSM dPwm = new BayesianNetworkDiffSM(&lt;br /&gt;
				//the alphabet and the length of the scoring function&lt;br /&gt;
				container, length, &lt;br /&gt;
				//the equivalent sample size for the plug-in parameters&lt;br /&gt;
				4, &lt;br /&gt;
				//we use plug-in parameters&lt;br /&gt;
				true, &lt;br /&gt;
				//a PWM is an inhomogeneous Markov model of order 0&lt;br /&gt;
				new InhomogeneousMarkov(0));&lt;br /&gt;
		 &lt;br /&gt;
		//create a new mixture scoring function&lt;br /&gt;
		MixtureDiffSM dMixPwms = new MixtureDiffSM(&lt;br /&gt;
				//the number of starts&lt;br /&gt;
				2,&lt;br /&gt;
				//we use plug-in parameters&lt;br /&gt;
				true,&lt;br /&gt;
				//the two components, which are PWMs&lt;br /&gt;
				dPwm,dPwm);&lt;br /&gt;
		 &lt;br /&gt;
		//create a new scoring function that is an inhomogeneous Markov model of order 3&lt;br /&gt;
		BayesianNetworkDiffSM dMm = new BayesianNetworkDiffSM(container, length, 4, true, new InhomogeneousMarkov(3));&lt;br /&gt;
		 &lt;br /&gt;
		//create the classifiers&lt;br /&gt;
		int threads = AbstractMultiThreadedOptimizableFunction.getNumberOfAvailableProcessors();&lt;br /&gt;
		AbstractScoreBasedClassifier[] classifiers = new AbstractScoreBasedClassifier[]{&lt;br /&gt;
									   //model based with mixture model and Markov model&lt;br /&gt;
									   new TrainSMBasedClassifier( mixPwms, mm ),&lt;br /&gt;
									   //conditional likelihood based classifier&lt;br /&gt;
									   new MSPClassifier( new GenDisMixClassifierParameterSet(container, length, &lt;br /&gt;
											   //method for optimizing the conditional likelihood and &lt;br /&gt;
											   //other parameters of the numerical optimization&lt;br /&gt;
											   Optimizer.QUASI_NEWTON_BFGS, 1E-2, 1E-2, 1, true, KindOfParameter.PLUGIN, false, threads),&lt;br /&gt;
											   //mixture scoring function and Markov model scoring function&lt;br /&gt;
											   dMixPwms,dMm )&lt;br /&gt;
		};&lt;br /&gt;
		 &lt;br /&gt;
		//create an new k-fold cross validation using above classifiers&lt;br /&gt;
		KFoldCrossValidation cv = new KFoldCrossValidation( classifiers );&lt;br /&gt;
		 &lt;br /&gt;
		//we use a specificity of 0.999 to compute the sensitivity and a sensitivity of 0.95 to compute FPR and PPV&lt;br /&gt;
		NumericalPerformanceMeasureParameterSet mp = PerformanceMeasureParameterSet.createFilledParameters();&lt;br /&gt;
		//we do a 10-fold cross validation and partition the data by means of the number of symbols&lt;br /&gt;
		KFoldCrossValidationAssessParameterSet cvpars = new KFoldCrossValidationAssessParameterSet(PartitionMethod.PARTITION_BY_NUMBER_OF_SYMBOLS, length, true, 10);&lt;br /&gt;
		 &lt;br /&gt;
		//compute the result of the cross validation and print them to System.out&lt;br /&gt;
		System.out.println( cv.assess( mp, cvpars, data ) );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Implementing a TrainableStatisticalModel ==&lt;br /&gt;
In this example, we show how to implement a new [http://www.jstacs.de/api-2.0//de/jstacs/sequenceScores/statisticalModels/trainable/TrainableStatisticalModel.html TrainableStatisticalModel]. Here, we implement a simple homogeneous Markov models of order &amp;lt;math&amp;gt;0&amp;lt;/math&amp;gt; to focus on the technical side of the implementation. A homogeneous Markov model of order &amp;lt;math&amp;gt;0&amp;lt;/math&amp;gt; has parameters &amp;lt;math&amp;gt;\theta_a&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; is a symbol of the alphabet &amp;lt;math&amp;gt;\Sigma&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\sum_{a \in \Sigma} \theta_a = 1&amp;lt;/math&amp;gt;. For an input sequence &amp;lt;math&amp;gt;\mathbf{x} = x_1,\ldots,x_L&amp;lt;/math&amp;gt; it models the likelihood&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
&lt;br /&gt;
P(\mathbf{x}|\boldsymbol{\theta}) &amp;amp;= \prod_{l=1}^{L} \theta_{x_l}.&lt;br /&gt;
&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the implementation, we use log-parameters &amp;lt;math&amp;gt;\log \theta_a&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
public class HomogeneousMarkovModel extends AbstractTrainableStatisticalModel {&lt;br /&gt;
 &lt;br /&gt;
	private double[] logProbs;//array for the parameters, i.e. the probabilities for each symbol&lt;br /&gt;
 &lt;br /&gt;
	public HomogeneousMarkovModel( AlphabetContainer alphabets ) throws Exception {&lt;br /&gt;
		super( alphabets, 0 ); //we have a homogeneous TrainableStatisticalModel, hence the length is set to 0&lt;br /&gt;
		//a homogeneous TrainableStatisticalModel can only handle simple alphabets&lt;br /&gt;
		if(! (alphabets.isSimple() &amp;amp;&amp;amp; alphabets.isDiscrete()) ){&lt;br /&gt;
			throw new Exception(&amp;quot;Only simple and discrete alphabets allowed&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
		//initialize parameter array&lt;br /&gt;
		this.logProbs = new double[(int) alphabets.getAlphabetLengthAt( 0 )];&lt;br /&gt;
		Arrays.fill( logProbs, -Math.log(logProbs.length) );&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	public HomogeneousMarkovModel( StringBuffer stringBuff ) throws NonParsableException { &lt;br /&gt;
        super( stringBuff ); &lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
	protected void fromXML( StringBuffer xml ) throws NonParsableException {&lt;br /&gt;
		//extract our XML-code&lt;br /&gt;
		xml = XMLParser.extractForTag( xml, &amp;quot;homogeneousMarkovModel&amp;quot; );&lt;br /&gt;
		//extract all the variables using XMLParser&lt;br /&gt;
		alphabets = (AlphabetContainer) XMLParser.extractObjectForTags( xml, &amp;quot;alphabets&amp;quot; );&lt;br /&gt;
		length = XMLParser.extractObjectForTags( xml, &amp;quot;length&amp;quot;, int.class );&lt;br /&gt;
		logProbs = XMLParser.extractObjectForTags( xml, &amp;quot;logProbs&amp;quot;, double[].class );&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	public StringBuffer toXML() {&lt;br /&gt;
		StringBuffer buf = new StringBuffer();&lt;br /&gt;
		//pack all the variables using XMLParser&lt;br /&gt;
		XMLParser.appendObjectWithTags( buf, alphabets, &amp;quot;alphabets&amp;quot; );&lt;br /&gt;
		XMLParser.appendObjectWithTags( buf, length, &amp;quot;length&amp;quot; );&lt;br /&gt;
		XMLParser.appendObjectWithTags( buf, logProbs, &amp;quot;logProbs&amp;quot; );&lt;br /&gt;
		//add our own tag&lt;br /&gt;
		XMLParser.addTags( buf, &amp;quot;homogeneousMarkovModel&amp;quot; );&lt;br /&gt;
		return buf;&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	public String getInstanceName() { &lt;br /&gt;
            return &amp;quot;Homogeneous Markov model of order 0&amp;quot;; &lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
	public double getLogPriorTerm() throws Exception { &lt;br /&gt;
            //we use ML-estimation, hence no prior term&lt;br /&gt;
            return 0; &lt;br /&gt;
        } &lt;br /&gt;
 &lt;br /&gt;
	public NumericalResultSet getNumericalCharacteristics() throws Exception {&lt;br /&gt;
		//we do not have much to tell here&lt;br /&gt;
		return new NumericalResultSet(new NumericalResult(&amp;quot;Number of parameters&amp;quot;,&amp;quot;The number of parameters this model uses&amp;quot;,logProbs.length));&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	public double getLogProbFor( Sequence sequence, int startpos, int endpos ) throws NotTrainedException, Exception {&lt;br /&gt;
		double seqLogProb = 0.0;&lt;br /&gt;
		//compute the log-probability of the sequence between startpos and endpos (inclusive)&lt;br /&gt;
		//as sum of the single symbol log-probabilities&lt;br /&gt;
		for(int i=startpos;i&amp;lt;=endpos;i++){&lt;br /&gt;
			//directly access the array by the numerical representation of the symbols&lt;br /&gt;
			seqLogProb += logProbs[sequence.discreteVal( i )];&lt;br /&gt;
		}&lt;br /&gt;
		return seqLogProb;&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	public boolean isInitialized() {&lt;br /&gt;
        return true; &lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
	public void train( DataSet data, double[] weights ) throws Exception {&lt;br /&gt;
		//reset the parameter array&lt;br /&gt;
		Arrays.fill( logProbs, 0.0 );&lt;br /&gt;
		//default sequence weight&lt;br /&gt;
		double w = 1;&lt;br /&gt;
		//for each sequence in the data set&lt;br /&gt;
		for(int i=0;i&amp;lt;data.getNumberOfElements();i++){&lt;br /&gt;
			//retrieve sequence&lt;br /&gt;
			Sequence seq = data.getElementAt( i );&lt;br /&gt;
			//if we do have any weights, use them&lt;br /&gt;
			if(weights != null){&lt;br /&gt;
				w = weights[i];&lt;br /&gt;
			}&lt;br /&gt;
			//for each position in the sequence&lt;br /&gt;
			for(int j=0;j&amp;lt;seq.getLength();j++){&lt;br /&gt;
				//count symbols, weighted by weights&lt;br /&gt;
				logProbs[ seq.discreteVal( j ) ] += w;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		//compute normalization&lt;br /&gt;
		double norm = 0.0;&lt;br /&gt;
		for(int i=0;i&amp;lt;logProbs.length;i++){ norm += logProbs[i]; }&lt;br /&gt;
		//normalize probs to obtain proper probabilities&lt;br /&gt;
		for(int i=0;i&amp;lt;logProbs.length;i++){ logProbs[i] = Math.log( logProbs[i]/norm ); }&lt;br /&gt;
	} &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Implementing a DifferentiableStatisticalModel ==&lt;br /&gt;
In this example, we show how to implement a new [http://www.jstacs.de/api-2.0//de/jstacs/sequenceScores/statisticalModels/differentiable/DifferentiableStatisticalModel.html DifferentiableStatisticalModel]. Here, we implement a simple position weight matrix, i.e., an inhomogeneous Markov model of order &amp;lt;math&amp;gt;0&amp;lt;/math&amp;gt;. Since we want to use this position weight matrix in numerical optimization, we parameterize it in the so called &amp;amp;quot;natural parameterization&amp;amp;quot;, where the probability of symbol &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; at position &amp;lt;math&amp;gt;l&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;P(X_l=a | \boldsymbol{\lambda}) = \frac{\exp(\lambda_{l,a})}{ \sum_{\tilde{a}} \exp(\lambda_{l,\tilde{a}}) }&amp;lt;/math&amp;gt;. Since we use a product-Dirichlet prior on the parameters, we transformed this prior to the parameterization we use.&lt;br /&gt;
&lt;br /&gt;
Here, the method &amp;lt;code&amp;gt;getLogScore&amp;lt;/code&amp;gt; returns a log-score that can be normalized to a proper log-likelihood by subtracting a log-normalization constant.&lt;br /&gt;
The log-score for an input sequence &amp;lt;math&amp;gt;\mathbf{x} = x_1,\ldots,x_L&amp;lt;/math&amp;gt; essentially is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
&lt;br /&gt;
S(\mathbf{x}|\boldsymbol{\lambda}) &amp;amp;= \sum_{l=1}^{L} \lambda_{l,x_l}.&lt;br /&gt;
&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The normalization constant is a partition function, i.e., the sum of the scores over all possible input sequences:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
&lt;br /&gt;
Z(\boldsymbol{\lambda}) &amp;amp;= \sum_{\mathbf{x} \in \Sigma^L} \exp( S(\mathbf{x}|\boldsymbol{\lambda}) )&lt;br /&gt;
&lt;br /&gt;
&amp;amp;= \sum_{\mathbf{x} \in \Sigma^L} \prod_{l=1}^{L} \exp(\lambda_{l,x_l})&lt;br /&gt;
&lt;br /&gt;
&amp;amp;= \prod_{l=1}^{L} \sum_{a \in \Sigma} \exp(\lambda_{l,a})&lt;br /&gt;
&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus, the likelihood is defined as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
&lt;br /&gt;
P(\mathbf{x}|\lambda) &amp;amp;= \frac{\exp(S(\mathbf{x}|\boldsymbol{\lambda}))}{Z(\boldsymbol{\lambda})}&lt;br /&gt;
&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
&lt;br /&gt;
\log P(\mathbf{x}|\lambda) &amp;amp;= S(\mathbf{x}|\boldsymbol{\lambda})) - \log Z(\boldsymbol{\lambda}).&lt;br /&gt;
&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
public class PositionWeightMatrixDiffSM extends AbstractDifferentiableStatisticalModel {&lt;br /&gt;
&lt;br /&gt;
	private double[][] parameters;// array for the parameters of the PWM in natural parameterization&lt;br /&gt;
	private double ess;// the equivalent sample size&lt;br /&gt;
	private boolean isInitialized;// if the parameters of this PWM are initialized&lt;br /&gt;
	private Double norm;// normalization constant, must be reset for new parameter values&lt;br /&gt;
	&lt;br /&gt;
	public PositionWeightMatrixDiffSM( AlphabetContainer alphabets, int length, double ess ) throws IllegalArgumentException {&lt;br /&gt;
		super( alphabets, length );&lt;br /&gt;
		//we allow only discrete alphabets with the same symbols at all positions&lt;br /&gt;
		if(!alphabets.isSimple() || !alphabets.isDiscrete()){&lt;br /&gt;
			throw new IllegalArgumentException( &amp;quot;This PWM can handle only discrete alphabets with the same alphabet at each position.&amp;quot; );&lt;br /&gt;
		}&lt;br /&gt;
		//create parameter-array&lt;br /&gt;
		this.parameters = new double[length][(int)alphabets.getAlphabetLengthAt( 0 )];&lt;br /&gt;
		//set fields&lt;br /&gt;
		this.ess = ess;&lt;br /&gt;
		this.isInitialized = false;&lt;br /&gt;
		this.norm = null;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * @param xml&lt;br /&gt;
	 * @throws NonParsableException&lt;br /&gt;
	 */&lt;br /&gt;
	public PositionWeightMatrixDiffSM( StringBuffer xml ) throws NonParsableException {&lt;br /&gt;
		//super-constructor in the end calls fromXML(StringBuffer)&lt;br /&gt;
		//and checks that alphabet and length are set&lt;br /&gt;
		super( xml );&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public int getSizeOfEventSpaceForRandomVariablesOfParameter( int index ) {&lt;br /&gt;
		//the event space are the symbols of the alphabet&lt;br /&gt;
		return parameters[0].length;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public double getLogNormalizationConstant() {&lt;br /&gt;
		//only depends on current parameters&lt;br /&gt;
		//-&amp;gt; compute only once&lt;br /&gt;
		if(this.norm == null){&lt;br /&gt;
			norm = 0.0;&lt;br /&gt;
			//sum over all sequences of product over all positions&lt;br /&gt;
			//can be re-ordered for a PWM to the product over all positions&lt;br /&gt;
			//of the sum over the symbols. In log-space the outer&lt;br /&gt;
			//product becomes a sum, the inner sum must be computed&lt;br /&gt;
			//by getLogSum(double[])&lt;br /&gt;
			for(int i=0;i&amp;lt;parameters.length;i++){&lt;br /&gt;
				norm += Normalisation.getLogSum( parameters[i] );&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		return norm;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public double getLogPartialNormalizationConstant( int parameterIndex ) throws Exception {&lt;br /&gt;
		//norm computed?&lt;br /&gt;
		if(norm == null){&lt;br /&gt;
			getLogNormalizationConstant();&lt;br /&gt;
		}&lt;br /&gt;
		//row and column of the parameter&lt;br /&gt;
		//in the PWM&lt;br /&gt;
		int symbol = parameterIndex%(int)alphabets.getAlphabetLengthAt( 0 );&lt;br /&gt;
		int position = parameterIndex/(int)alphabets.getAlphabetLengthAt( 0 );&lt;br /&gt;
		//partial derivation only at current position, rest is factor&lt;br /&gt;
		return norm - Normalisation.getLogSum( parameters[position] ) + parameters[position][symbol];&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public double getLogPriorTerm() {&lt;br /&gt;
		double logPrior = 0;&lt;br /&gt;
		for(int i=0;i&amp;lt;parameters.length;i++){&lt;br /&gt;
			for(int j=0;j&amp;lt;parameters[i].length;j++){&lt;br /&gt;
				//prior without gamma-normalization (only depends on hyper-parameters),&lt;br /&gt;
				//uniform hyper-parameters (BDeu), tranformed prior density,&lt;br /&gt;
				//without normalization constant (getLogNormalizationConstant()*ess subtracted later)&lt;br /&gt;
				logPrior += ess/alphabets.getAlphabetLengthAt( 0 ) * parameters[i][j];&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		return logPrior;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public void addGradientOfLogPriorTerm( double[] grad, int start ) throws Exception {&lt;br /&gt;
		for(int i=0;i&amp;lt;parameters.length;i++){&lt;br /&gt;
			for(int j=0;j&amp;lt;parameters[i].length;j++,start++){&lt;br /&gt;
				//partial derivations of the logPriorTerm above&lt;br /&gt;
				grad[start] = ess/alphabets.getAlphabetLengthAt( 0 );&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public double getESS() {&lt;br /&gt;
		return ess;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public void initializeFunction( int index, boolean freeParams, DataSet[] data, double[][] weights ) throws Exception {&lt;br /&gt;
		if(!data[index].getAlphabetContainer().checkConsistency( alphabets ) || &lt;br /&gt;
				data[index].getElementLength() != length){&lt;br /&gt;
			throw new IllegalArgumentException( &amp;quot;Alphabet or length to not match.&amp;quot; );&lt;br /&gt;
		}&lt;br /&gt;
		//initially set pseudo-counts&lt;br /&gt;
		for(int i=0;i&amp;lt;parameters.length;i++){&lt;br /&gt;
			Arrays.fill( parameters[i], ess/alphabets.getAlphabetLengthAt( 0 ) );&lt;br /&gt;
		}&lt;br /&gt;
		//counts in data&lt;br /&gt;
		for(int i=0;i&amp;lt;data[index].getNumberOfElements();i++){&lt;br /&gt;
			Sequence seq = data[index].getElementAt( i );&lt;br /&gt;
			for(int j=0;j&amp;lt;seq.getLength();j++){&lt;br /&gt;
				parameters[j][ seq.discreteVal( j ) ] += weights[index][i];&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		for(int i=0;i&amp;lt;parameters.length;i++){&lt;br /&gt;
			//normalize -&amp;gt; MAP estimation&lt;br /&gt;
			Normalisation.sumNormalisation( parameters[i] );&lt;br /&gt;
			//parameters are log-probabilities from MAP estimation&lt;br /&gt;
			for(int j=0;j&amp;lt;parameters[i].length;j++){&lt;br /&gt;
				parameters[i][j] = Math.log( parameters[i][j] );&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		norm = null;&lt;br /&gt;
		isInitialized = true;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public void initializeFunctionRandomly( boolean freeParams ) throws Exception {&lt;br /&gt;
		int al = (int)alphabets.getAlphabetLengthAt( 0 );&lt;br /&gt;
		//draw parameters from prior density -&amp;gt; Dirichlet&lt;br /&gt;
		DirichletMRGParams pars = new DirichletMRGParams( ess/al, al );&lt;br /&gt;
		for(int i=0;i&amp;lt;parameters.length;i++){&lt;br /&gt;
			parameters[i] = DirichletMRG.DEFAULT_INSTANCE.generate( al, pars );&lt;br /&gt;
			//parameters are log-probabilities&lt;br /&gt;
			for(int j=0;j&amp;lt;parameters[i].length;j++){&lt;br /&gt;
				parameters[i][j] = Math.log( parameters[i][j] );&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		norm = null;&lt;br /&gt;
		isInitialized = true;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public double getLogScoreFor( Sequence seq, int start ) {&lt;br /&gt;
		double score = 0.0;&lt;br /&gt;
		//log-score is sum of parameter values used&lt;br /&gt;
		//normalization to likelihood can be achieved&lt;br /&gt;
		//by subtracting getLogNormalizationConstant&lt;br /&gt;
		for(int i=0;i&amp;lt;parameters.length;i++){&lt;br /&gt;
			score += parameters[i][ seq.discreteVal( i+start ) ];&lt;br /&gt;
		}&lt;br /&gt;
		return score;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public double getLogScoreAndPartialDerivation( Sequence seq, int start, IntList indices, DoubleList partialDer ) {&lt;br /&gt;
		double score = 0.0;&lt;br /&gt;
		int off = 0;&lt;br /&gt;
		for(int i=0;i&amp;lt;parameters.length;i++){&lt;br /&gt;
			int v = seq.discreteVal( i+start );&lt;br /&gt;
			score += parameters[i][ v ];&lt;br /&gt;
			//add index of parameter used to indices&lt;br /&gt;
			indices.add( off + v );&lt;br /&gt;
			//derivations are just one&lt;br /&gt;
			partialDer.add( 1 );&lt;br /&gt;
			off += parameters[i].length;&lt;br /&gt;
		}&lt;br /&gt;
		return score;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public int getNumberOfParameters() {&lt;br /&gt;
		int num = 0;&lt;br /&gt;
		for(int i=0;i&amp;lt;parameters.length;i++){&lt;br /&gt;
			num += parameters[i].length;&lt;br /&gt;
		}&lt;br /&gt;
		return num;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public double[] getCurrentParameterValues() throws Exception {&lt;br /&gt;
		double[] pars = new double[getNumberOfParameters()];&lt;br /&gt;
		for(int i=0,k=0;i&amp;lt;parameters.length;i++){&lt;br /&gt;
			for(int j=0;j&amp;lt;parameters[i].length;j++,k++){&lt;br /&gt;
				pars[k] = parameters[i][j];&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		return pars;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public void setParameters( double[] params, int start ) {&lt;br /&gt;
		for(int i=0;i&amp;lt;parameters.length;i++){&lt;br /&gt;
			for(int j=0;j&amp;lt;parameters[i].length;j++,start++){&lt;br /&gt;
				parameters[i][j] = params[start];&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		norm = null;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public String getInstanceName() {&lt;br /&gt;
		return &amp;quot;Position weight matrix&amp;quot;;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public boolean isInitialized() {&lt;br /&gt;
		return isInitialized;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public StringBuffer toXML() {&lt;br /&gt;
		StringBuffer xml = new StringBuffer();&lt;br /&gt;
		//store all fields with XML parser&lt;br /&gt;
		//including alphabet and length of the super-class&lt;br /&gt;
		XMLParser.appendObjectWithTags( xml, alphabets, &amp;quot;alphabets&amp;quot; );&lt;br /&gt;
		XMLParser.appendObjectWithTags( xml, length, &amp;quot;length&amp;quot; );&lt;br /&gt;
		XMLParser.appendObjectWithTags( xml, parameters, &amp;quot;parameters&amp;quot; );&lt;br /&gt;
		XMLParser.appendObjectWithTags( xml, isInitialized, &amp;quot;isInitialized&amp;quot; );&lt;br /&gt;
		XMLParser.appendObjectWithTags( xml, ess, &amp;quot;ess&amp;quot; );&lt;br /&gt;
		XMLParser.addTags( xml, &amp;quot;PWM&amp;quot; );&lt;br /&gt;
		return xml;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	protected void fromXML( StringBuffer xml ) throws NonParsableException {&lt;br /&gt;
		xml = XMLParser.extractForTag( xml, &amp;quot;PWM&amp;quot; );&lt;br /&gt;
		//extract all fields&lt;br /&gt;
		alphabets = (AlphabetContainer)XMLParser.extractObjectForTags( xml, &amp;quot;alphabets&amp;quot; );&lt;br /&gt;
		length = XMLParser.extractObjectForTags( xml, &amp;quot;length&amp;quot;, int.class );&lt;br /&gt;
		parameters = (double[][])XMLParser.extractObjectForTags( xml, &amp;quot;parameters&amp;quot; );&lt;br /&gt;
		isInitialized = XMLParser.extractObjectForTags( xml, &amp;quot;isInitialized&amp;quot;, boolean.class );&lt;br /&gt;
		ess = XMLParser.extractObjectForTags( xml, &amp;quot;ess&amp;quot;, double.class );&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Grau</name></author>
	</entry>
</feed>