HmmAlgorithms { public static Matrix backwardAlgorithm(HmmModel model, int[] observations, boolean scaled) { Matrix beta = new DenseMatrix(observations.length, model.getNrOfHiddenStates()); backwardAlgorithm(beta, model, observations, scaled); return beta; } 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 testBackwardAlgorithm() { double[][] betaExpectedA = { {0.0015730559, 0.003543656, 0.00738264, 0.040692, 0.0848, 0.17, 1}, {0.0017191865, 0.002386795, 0.00923652, 0.052232, 0.1018, 0.17, 1}, {0.0003825772, 0.001238558, 0.00259464, 0.012096, 0.0664, 0.66, 1}, {0.0004390858, 0.007076994, 0.01063512, 0.013556, 0.0304, 0.17, 1}}; Matrix beta = HmmAlgorithms.backwardAlgorithm(getModel(), getSequence(), false); assertNotNull(beta); assertEquals(4, beta.numCols()); assertEquals(7, beta.numRows()); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 7; ++j) { assertEquals(betaExpectedA[i][j], beta.get(j, i), EPSILON); } } }
@Test public void testLogScaledBackwardAlgorithm() { double[][] betaExpectedA = { {0.0015730559, 0.003543656, 0.00738264, 0.040692, 0.0848, 0.17, 1}, {0.0017191865, 0.002386795, 0.00923652, 0.052232, 0.1018, 0.17, 1}, {0.0003825772, 0.001238558, 0.00259464, 0.012096, 0.0664, 0.66, 1}, {0.0004390858, 0.007076994, 0.01063512, 0.013556, 0.0304, 0.17, 1}}; Matrix beta = HmmAlgorithms.backwardAlgorithm(getModel(), getSequence(), true); assertNotNull(beta); assertEquals(4, beta.numCols()); assertEquals(7, beta.numRows()); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 7; ++j) { assertEquals(Math.log(betaExpectedA[i][j]), beta.get(j, i), EPSILON); } } }