001    package calhoun.analysis.crf.solver;
002    
003    import java.util.Arrays;
004    
005    /**
006     * This object holds information about previous positions during the computation of betas and expectations. This
007     * allows us to quickly access data about previous positions. These objects are kept in a recycling buffer that
008     * keeps one buffer for each possible lookback.
009     * <p>
010     * One tricky aspect of this is that the details change slightly between the forward and backwards pass.  On the forward
011     * pass, the lookback contains the information in the normal way.  In the backwards pass, stable states and transitions are 
012     * shifted back one base compared to the betas.
013     */
014    public final class LookbackBuffer {
015            public int pos;
016            
017            /// In the beta pass, the mi matrix for transitioning from pos+l to pos
018            public double[] mi;
019    
020            /// For each transition to a semi-markov state, stores the probability of all segments including that transition.
021            public double[] transitionProb;
022    
023            /// The weighted sum of feature values for staying in this position from the end of the sequence to this position
024            public double[] stableState;
025            
026            /// Beta values at this position
027            public double[] beta;
028    
029            /// Norm of beta values
030            public int betaNorm;
031            
032            public LookbackBuffer(int states, int transitions) {
033                    mi = new double[states+transitions];
034                    transitionProb = new double[transitions];
035                    stableState = new double[states];
036                    beta = new double[states];
037            }
038            
039            /** mi and stableStates are cleared as new values are entered. This fixes the others */
040            public void clear()
041            {
042                    pos = -1;
043                    Arrays.fill(beta, 0.0);
044                    betaNorm = Integer.MIN_VALUE;
045                    Arrays.fill(transitionProb, 0.0);
046            }
047    }