HmmAlgorithms { public static Matrix forwardAlgorithm(HmmModel model, int[] observations, boolean scaled) { Matrix alpha = new DenseMatrix(observations.length, model.getNrOfHiddenStates()); forwardAlgorithm(alpha, model, observations, scaled); return alpha; } private  HmmAlgorithms(); static Matrix forwardAlgorithm(HmmModel model, int[] observations, boolean scaled); static Matrix backwardAlgorithm(HmmModel model, int[] observations, boolean scaled); static int[] viterbiAlgorithm(HmmModel model, int[] observations, boolean scaled);  }
@Test public void testForwardAlgorithm() { double[][] alphaExpectedA = { {0.02, 0.0392, 0.002438, 0.00035456, 0.0011554672, 7.158497e-04, 4.614927e-05}, {0.01, 0.0054, 0.001824, 0.00069486, 0.0007586904, 2.514137e-04, 1.721505e-05}, {0.32, 0.0262, 0.002542, 0.00038026, 0.0001360234, 3.002345e-05, 9.659608e-05}, {0.03, 0.0000, 0.013428, 0.00951084, 0.0000000000, 0.000000e+00, 2.428986e-05},}; Matrix alpha = HmmAlgorithms.forwardAlgorithm(getModel(), getSequence(), false); assertNotNull(alpha); assertEquals(4, alpha.numCols()); assertEquals(7, alpha.numRows()); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 7; ++j) { assertEquals(alphaExpectedA[i][j], alpha.get(j, i), EPSILON); } } }
@Test public void testLogScaledForwardAlgorithm() { double[][] alphaExpectedA = { {0.02, 0.0392, 0.002438, 0.00035456, 0.0011554672, 7.158497e-04, 4.614927e-05}, {0.01, 0.0054, 0.001824, 0.00069486, 0.0007586904, 2.514137e-04, 1.721505e-05}, {0.32, 0.0262, 0.002542, 0.00038026, 0.0001360234, 3.002345e-05, 9.659608e-05}, {0.03, 0.0000, 0.013428, 0.00951084, 0.0000000000, 0.000000e+00, 2.428986e-05},}; Matrix alpha = HmmAlgorithms.forwardAlgorithm(getModel(), getSequence(), true); assertNotNull(alpha); assertEquals(4, alpha.numCols()); assertEquals(7, alpha.numRows()); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 7; ++j) { assertEquals(Math.log(alphaExpectedA[i][j]), alpha.get(j, i), EPSILON); } } }