A linear memory algorithm for Baum-Welch training

Background: Baum-Welch training is an expectation-maximisation algorithm for training the emission and transition probabilities of hidden Markov models in a fully automated way. It can be employed as long as a training set of annotated sequences is known, and provides a rigorous way to derive parameter values which are guaranteed to be at least locally optimal. For complex hidden Markov models such as pair hidden Markov models and very long training sequences, even the most efficient algorithms for Baum-Welch training are currently too memory-consuming. This has so far effectively prevented the automatic parameter training of hidden Markov models that are currently used for biological sequence analyses. Results: We introduce the first linear space algorithm for Baum-Welch training. For a hidden Markov model with M states, T free transition and E free emission parameters, and an input sequence of length L, our new algorithm requires O(M) memory and O(LMTmax (T + E)) time for one Baum-Welch iteration, where Tmax is the maximum number of states that any state is connected to. The most memory efficient algorithm until now was the checkpointing algorithm with O(log(L)M) memory and O(log(L)LMTmax) time requirement. Our novel algorithm thus renders the memory requirement completely independent of the length of the training sequences. More generally, for an n-hidden Markov model and n input sequences of length L, the memory requirement of O(log(L)Ln-1 M) is reduced to O(Ln-1 M) memory while the running time is changed from O(log(L)Ln MTmax + Ln(T + E)) to O(Ln MTmax (T + E)). An added advantage of our new algorithm is that a reduced time requirement can be traded for an increased memory requirement and vice versa, such that for any c ∈ {1, ..., (T + E)}, a time requirement of Ln MTmax c incurs a memory requirement of Ln-1 M(T + E - c). Conclusion For the large class of hidden Markov models used for example in gene prediction, whose number of states does not scale with the length of the input sequence, our novel algorithm can thus be both faster and more memory-efficient than any of the existing algorithms.

When an HMM consisting of M states is used to annotate an input sequence, its predictions crucially depend on its set of emission probabilities E and transition probabilities T .This is for example the case for the state path with the highest overall probability, the so-called optimal state path or Viterbi path [6], which is often reported as the predicted annotation of the input sequence.
When a new HMM is designed, it is usually quite easy to define its states and the transitions between them as these typically closely reflect the underlying problem.However, it can be quite difficult to assign values to its emission probabilities E and transition probabilities T .Ideally, they should be set up such that the model's predictions would perfectly reproduce the known annotation of a large and diverse set of input sequences.
The question is thus how to derive the best set of transition and emission probabilities from a given training set of annotated sequences.Two main scenarios have to be distinguished [1]: (1) If we know the optimal state paths that correspond to the known annotation of the training sequences, the transition and emission probabilities can simply be set to the respective count frequencies within these optimal state paths, i.e. to their maximum likelihood estimators.If the training set is small or not diverse enough, pseudo-counts have to be added to avoid over-fitting.
(2) If we do not know the optimal state paths of the training sequences, either because their annotation is unknown or because their annotation does not unambiguously define a state path in the HMM, we can employ an expectation maximisation (EM) algorithm [7] such as the Baum-Welch algorithm [8] to derive the emission and transition probabilities in an iterative procedure which increases the overall log likelihood of the model in each iteration and which is guaranteed to converge at least to a local maximum.As in case (1), pseudo-counts or Dirichlet priors can be added to avoid over-fitting when the training set is small or not diverse enough.

Baum-Welch training
The Baum-Welch algorithm defines an iterative procedure in which the emission and transition probabilities in iteration n + 1 are set to the number of times each transition and emission is expected to be used when analysing the training sequences with the set of emission and transition probabilities derived in the previous iteration n.
Let T n i,j denote the transition probability for going from state i to state j in iteration n, E n i (y) the emission probability for emitting letter y in state i in iteration n, P (X) the probability of sequence X, and x k the kth letter in input sequence X which has length L. We also define X k as the sequence of letters from the beginning of sequence X up to sequence position k, (x 1 , ...x k ).X k is defined as the sequence of letters from sequence position k + 1 to the end of the sequence, (x k+1 , ...x L ).
For a given set of training sequences, S, the expectation maximisation update for transition probability T n i,j , T n+1 i,j , can then be written as where The superfix n on the quantities on the right hand side indicates that they are based on the transition probabilities T n i,j and emission probabilities the so-called forward probability of the sequence up to and including sequence position k, requiring that sequence letter x k is read by state i.It is equal to the sum of probabilities of all state paths that finish in state i at sequence position k.The probability of sequence X, P (X), is therefore equal to f (X L , End).b(X k , i) := P (x k+1 , ...x L |s(x k ) = i) is the so-called backward probability of the sequence from sequence position k + 1 to the end, given that the letter at sequence position k, x k , is read by state i.It is equal to the sum of probabilities of all state paths that start in state i at sequence position k.
For a given set of training sequences, S, the expectation maximisation update for emission probability where e n i (y, X) := As before, the superfix n on the quantities on the right hand side indicates that they are calculated using the transition probabilities The forward and backward probabilities f n (X k , i) and b n (X k , i) can be calculated using the forward and backward algorithms [1] which are introduced in the following section.

Baum-Welch training using the forward and backward algorithm
The forward algorithm proposes a procedure for calculating the forward probabilities f (X k , i) in an iterative way.f (X k , i) is the sum of probabilities of all state paths that finish in state i at sequence position k.
The recursion starts with the initialisation where Start is the number of the start state in the HMM.The recursion proceeds towards higher sequence positions and terminates with where End is the number of the end state in the HMM.The recursion can be implemented as a dynamic programming procedure which works its way through a two-dimensional matrix, starting at the start of the sequence in the Start state and finishing at the end of the sequence in the End state of the HMM.
The backward algorithm calculates the backward probabilities b(X k , i) in a similar iterative way.b(X k , i) is the sum of probabilities of all state paths that start in state i at sequence position k.Opposed to the forward algorithm the backward algorithm starts at the end of the sequence in the End state and finishes at the start of the sequence in the Start state of the HMM.
The backward algorithm starts with the initialisation and continues towards lower sequence positions with the recursion and terminates with As can be seen in the recursion steps of the forward and backward algorithms described above, the calculation of f (X k+1 , i) requires at most T max previously calculated elements f (X k , j) for j ∈ {1, ..M }.
T max is the maximum number of states that any state of the model is connected to.Likewise, the calculation of b(X k , i) refers to at most T max elements b(X k+1 , j) for j ∈ {1, ..M }.
In order to continue the calculation of the forward and backward values f (X k , i) and b(X k , i) for all states i ∈ {1, ..M } along the entire sequence, we thus only have to memorise M elements.

Baum-Welch training using the checkpointing algorithm
Unit now, the checkpointing algorithm [11][12][13] was the most efficient way to perform Baum-Welch training.
The basic idea of the checkpointing algorithm is to perform the forward and backward algorithm by

Baum-Welch training using the new algorithm
It is not trivial to see that the quantities T n+1 i,j and E n+1 i (y) of Equations 1 and 2 can be calculated in an even more memory-sparse way as both, the forward and the corresponding backward probabilities are needed at the same time in order to calculate the terms f n (X k , i)T n i,j E n i (x k+1 )b n (X k+1 , j) in t n i,j (X) and ) in e n i (y, X) of Equations 1 and 2. A calculation of these quantities for each sequence position using a memory-sparse implementation (that would memorise only M values at a time) both for the forward and backward algorithm would require L-times more time, i.e. significantly more time.
Also, an algorithm along the lines of the Hirschberg algorithm [9,10] cannot be applied as we cannot halve the dynamic programming table after the first recursion.
We here propose a new algorithm to calculate the quantities T n+1 i,j The trick for coming up with a memory efficient algorithm is to realise that • t n i,j (X) and e n i (y, X) in Equations 1 and 2 can be interpreted as a weighted sum of probabilities of state paths that satisfy certain constraints and that • the weight of each state path is equal to the number of times that the constraint is fulfilled.
For example, t n i,j (X) in the numerator in Equation 1is the weighted sum of probabilities of state paths for sequence X that contain at least one i → j transition, and the weight of each such state path in the sum is the number of times this transition occurs in the state path.
We now show how t n i,j (X) in Equation 1 can be calculated in O(M ) memory and O(LM T max ) time.As the superfix n is only there to remind us that the calculation of t n i,j (X) is based on the transition and emission probabilities of iteration n and as this index does not change in the calculation of t n i,j , we discard it for simplicity sake in the following.
Let t i,j (X k , l) denote the weighted sum of probabilities of state paths that finish in state l at sequence position k of sequence X and that contain at least one i → j transition, where the weight for each state path is equal to its number of i → j transitions.
Theorem 1: The following algorithm calculates t i,j (X) in O(M ) memory and is the weighted sum of probabilities of all state paths for sequence X that have at least one i → j transition, where the weight for each state path is equal to its number of i → j transitions.
The algorithm starts with the initialisation and proceeds via the following recursion and finishes with Proof: The expression on the right hand side is the weighted sum of probabilities of state paths that finish in sequence position k + 1 and contain at least one i → j transition.
We have therefore shown that if Equation 3 is true for sequence position k, it is also true for sequence position k + 1.This concludes the proof of theorem 1.
It is easy to show that e i (y, X) in Equation 2can also be calculated in O(M ) memory and O(LM T max ) time in a similar way as t i,j (X).Let e i (y, X k , l) denote the weighted sum of probabilities of state paths that finish at sequence position k in state l and for which state i reads letter y at least once, the weight of each state path being equal to the number of times state i reads letter y.As in the calculation of t i,j (X), we again omit the superfix n as the calculation of e i (y, X) is again entirely based on the transition and emission probabilities of iteration n.
Theorem 2: e i (y, X) can be calculated in O(M ) memory and O(LM T max ) time using the following algorithm.e i (y, X) is the weighted sum of probabilities of state paths for sequence X that read letter y in state i at least once, the weight of each state path being equal to the number of times letter y is read by state i.
Initialisation step: For HMMs, our novel algorithm renders the memory requirement completely independent of the sequence length.Generally, for n-HMMs and all T + E parameters being estimated consecutively, our novel algorithm reduces the memory requirement by a factor log(L) and the time requirement by a factor log(L)/(T + E) + 1/(M T max ).For all hidden Markov models whose number of states does not depend on the length of the input sequence, this thus amounts to a significantly reduced memory requirement and -in cases where the number of free parameters and states of the model (i.e. T + E) is smaller than the logarithm of sequence lengths -even to a reduced time requirement.
For example, for an HMM that is used to predict human genes, the training sequences have a mean length of at least 2.7 • 10 4 bp which is the average length of a human gene [14].Using our new algorithm, the memory requirement for Baum-Welch training is reduced by a factor of about 28 ≈ e * ln (2.7 • 10 4 ) with respect to the most memory-sparse version of the checkpointing algorithm.
Our new algorithm makes use of the fact that the numerators and denominators of Equations 1 and 2 can be decomposed in a smart way that allows a very memory-sparse calculation.This calculation requires only one uni-directional scan along the sequence rather than one or more bi-directional scans, see Figure 1.This property gives our algorithm the added advantage that it is easier to implement as it does not require programming techniques like recursive functions or checkpoints.
Baum-Welch training is only guaranteed to converge to a local optimum.Other optimisation techniques have been developed in order to find better optima.One of the most successful methods is simulated annealing (SA) [1,15].SA is essentially a Markov chain Monte Carlo (MCMC) in which the target distribution is sequentially changed such that the distribution gets eventually trapped in a local optimum.
One can give proposal steps a higher probability as they are approaching locally better points.This can increase the performance of the MCMC method, especially in higher dimensional spaces [16].One could base the candidate distribution on the expectations such that proposals are more likely to be made near the EM updates (calculated with our algorithm).There is no need to update all the parameters in one MCMC step, modifying a random subset of parameters yields also an irreducible chain.The last feature makes SA significantly faster than Baum-Welch updates as we need to calculate expectations only for a few  1is calculated at the pair of sequence positions indicated by the black square using the standard forward and backward algorithm.(b) shows how our algorithm simultaneously calculates a strip of forward values f (X k , Y q , m) and a strip of t i,j (X k Y q , m) values at sequence position k in sequence X in order to estimate t i,j in Equation 1.

1 k 1 k ) = O(L k− 1 k 1 k
memorising the forward and backward values only in O( √ L) columns along the sequence dimension of the dynamic programming table.The checkpointing algorithm starts with the forward algorithm, retaining only the forward values in O( √ L) columns.These columns partition the dynamic programming table into O( √ L) separate fields.The checkpointing algorithm then invokes the backward algorithm which memorises the backward values in a strip of length O( √ L) as it moves along the sequence.When the backward calculation reaches the boundary of one field, the pre-calculated forward values of the neighbouring checkpointing column are used to calculate the corresponding forward values for that field.The forward and backward values of that field are then available at the same time and are used to calculate the corresponding values for the EM update.The checkpointing algorithm can be further refined by using embedded checkpoints.With an embedding level of k, the forward values in O(L ) columns of the initial calculation are memorised, thus defining O(L/L ) long fields.When the memory-sparse calculation of the backward values reaches the field in question, the forward algorithm is invoked again to calculate the forward values for O(L 1 k ) additional columns within that field.This procedure is iterated k times within the thus emerging fields.In the end, for each of the O(L )-long k-sub-fields, the forward and backward values are simultaneously available and are used to calculate the corresponding values for the EM update.The time complexity of this algorithm for one Baum-Welch iteration and a given training sequence of length L is O(kLM T max + L(T + E)), since k forward and 1 backward algorithms have to be invoked, and the memory complexity is O(kL 1 k M ).For k = log(L), this amounts to a time requirement of O(log(L)LM T max + L(T + E)) and a memory requirement of O(log(L)M ), since L 1 log(L) = e.
which are required for Baum-Welch training.Our algorithm requires O(M ) memory and O(LM T max (T + E)) time rather than O(log(L)M ) memory and O(log(L)LM T max + L(T + E)) time.
at a time in a straightforward way.In the n-HMM case, our algorithm reduces the memory and timerequirements from O(log(L)L n−1 M ) memory and O(log(L)L n M T max + L n (T + E)) time to O(L n−1 M ) memory and O(L n M T max (T + E)))time.An added advantage of our new algorithm is that a reduced time requirement can be traded for an increased memory requirement and vice versa, such that for any c ∈ {1, . . ., (T + E)}, a time requirement of L n M T max c incurs a memory requirement of

FigureFigure 1 -
Figure Figure 1 -Pictorial description of the new algorithm for pair-HMMs This figure shows a pictorial description of the differences between the forward-backward algorithm (a) and our new algorithm (b) for the Baum-Welch training of a pair-HMM.Each large rectangle corresponds to the projection of the three-dimensional dynamic programming matrix (spanned by the two input sequences X and Y and the states of the HMM) onto the sequence plane.(a) shows how the numerator in Equation 1