HmmUtils { public static void validate(HmmModel model) { if (model == null) { return; } Preconditions.checkArgument(model.getNrOfHiddenStates() > 0, "Error: The number of hidden states has to be greater than 0"); Preconditions.checkArgument(model.getNrOfOutputStates() > 0, "Error: The number of output states has to be greater than 0!"); Preconditions.checkArgument(model.getInitialProbabilities() != null && model.getInitialProbabilities().size() == model.getNrOfHiddenStates(), "Error: The vector of initial probabilities is not initialized!"); double sum = 0; for (int i = 0; i < model.getInitialProbabilities().size(); i++) { Preconditions.checkArgument(model.getInitialProbabilities().get(i) >= 0, "Error: Initial probability of state %d is negative", i); sum += model.getInitialProbabilities().get(i); } Preconditions.checkArgument(Math.abs(sum - 1) <= 0.00001, "Error: Initial probabilities do not add up to 1"); Preconditions.checkNotNull(model.getEmissionMatrix(), "Error: The output state matrix is not initialized!"); Preconditions.checkArgument(model.getEmissionMatrix().numRows() == model.getNrOfHiddenStates() && model.getEmissionMatrix().numCols() == model.getNrOfOutputStates(), "Error: The output state matrix is not of the form nrOfHiddenStates x nrOfOutputStates"); for (int i = 0; i < model.getEmissionMatrix().numRows(); i++) { sum = 0; for (int j = 0; j < model.getEmissionMatrix().numCols(); j++) { Preconditions.checkArgument(model.getEmissionMatrix().get(i, j) >= 0, "The output state probability from hidden state " + i + " to output state " + j + " is negative"); sum += model.getEmissionMatrix().get(i, j); } Preconditions.checkArgument(Math.abs(sum - 1) <= 0.00001, "Error: The output state probabilities for hidden state %d don't add up to 1", i); } Preconditions.checkArgument(model.getTransitionMatrix() != null, "Error: The hidden state matrix is not initialized!"); Preconditions.checkArgument(model.getTransitionMatrix().numRows() == model.getNrOfHiddenStates() && model.getTransitionMatrix().numCols() == model.getNrOfHiddenStates(), "Error: The output state matrix is not of the form nrOfHiddenStates x nrOfHiddenStates"); for (int i = 0; i < model.getTransitionMatrix().numRows(); i++) { sum = 0; for (int j = 0; j < model.getTransitionMatrix().numCols(); j++) { Preconditions.checkArgument(model.getTransitionMatrix().get(i, j) >= 0, "Error: The transition probability from hidden state %d to hidden state %d is negative", i, j); sum += model.getTransitionMatrix().get(i, j); } Preconditions.checkArgument(Math.abs(sum - 1) <= 0.00001, "Error: The transition probabilities for hidden state " + i + " don't add up to 1."); } } 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 testValidatorLegal() { HmmUtils.validate(new HmmModel(legal22, legal23, legal2)); }
@Test public void testValidatorDimensionError() { try { HmmUtils.validate(new HmmModel(legal33, legal23, legal2)); } catch (IllegalArgumentException e) { return; } fail(); }
@Test public void testValidatorIllegelMatrixError() { try { HmmUtils.validate(new HmmModel(illegal22, legal23, legal2)); } catch (IllegalArgumentException e) { return; } fail(); }