HmmUtils { public static void normalizeModel(HmmModel model) { Vector ip = model.getInitialProbabilities(); Matrix emission = model.getEmissionMatrix(); Matrix transition = model.getTransitionMatrix(); double isum = 0; for (int i = 0; i < model.getNrOfHiddenStates(); ++i) { isum += ip.getQuick(i); double sum = 0; for (int j = 0; j < model.getNrOfHiddenStates(); ++j) { sum += transition.getQuick(i, j); } if (sum != 1.0) { for (int j = 0; j < model.getNrOfHiddenStates(); ++j) { transition.setQuick(i, j, transition.getQuick(i, j) / sum); } } sum = 0; for (int j = 0; j < model.getNrOfOutputStates(); ++j) { sum += emission.getQuick(i, j); } if (sum != 1.0) { for (int j = 0; j < model.getNrOfOutputStates(); ++j) { emission.setQuick(i, j, emission.getQuick(i, j) / sum); } } } if (isum != 1.0) { for (int i = 0; i < model.getNrOfHiddenStates(); ++i) { ip.setQuick(i, ip.getQuick(i) / isum); } } } private  HmmUtils(); static Matrix getCumulativeTransitionMatrix(HmmModel model); static Matrix getCumulativeOutputMatrix(HmmModel model); static Vector getCumulativeInitialProbabilities(HmmModel model); static void validate(HmmModel model); static int[] encodeStateSequence(HmmModel model,
                                          Collection<String> sequence, boolean observed, int defaultValue); static List<String> decodeStateSequence(HmmModel model,
                                                 int[] sequence,
                                                 boolean observed,
                                                 String defaultValue); static void normalizeModel(HmmModel model); static HmmModel truncateModel(HmmModel model, double threshold);  }
@Test public void testNormalizeModel() { DenseVector ip = new DenseVector(new double[]{10, 20}); DenseMatrix tr = new DenseMatrix(new double[][]{{10, 10}, {20, 25}}); DenseMatrix em = new DenseMatrix(new double[][]{{5, 7}, {10, 15}}); HmmModel model = new HmmModel(tr, em, ip); HmmUtils.normalizeModel(model); HmmUtils.validate(model); }