@Test public void testTruncateModel() { DenseVector ip = new DenseVector(new double[]{0.0001, 0.0001, 0.9998}); DenseMatrix tr = new DenseMatrix(new double[][]{ {0.9998, 0.0001, 0.0001}, {0.0001, 0.9998, 0.0001}, {0.0001, 0.0001, 0.9998}}); DenseMatrix em = new DenseMatrix(new double[][]{ {0.9998, 0.0001, 0.0001}, {0.0001, 0.9998, 0.0001}, {0.0001, 0.0001, 0.9998}}); HmmModel model = new HmmModel(tr, em, ip); HmmModel sparseModel = HmmUtils.truncateModel(model, 0.01); HmmUtils.validate(sparseModel); Vector sparse_ip = sparseModel.getInitialProbabilities(); Matrix sparse_tr = sparseModel.getTransitionMatrix(); Matrix sparse_em = sparseModel.getEmissionMatrix(); for (int i = 0; i < sparseModel.getNrOfHiddenStates(); ++i) { assertEquals(i == 2 ? 1.0 : 0.0, sparse_ip.getQuick(i), EPSILON); for (int j = 0; j < sparseModel.getNrOfHiddenStates(); ++j) { if (i == j) { assertEquals(1.0, sparse_tr.getQuick(i, j), EPSILON); assertEquals(1.0, sparse_em.getQuick(i, j), EPSILON); } else { assertEquals(0.0, sparse_tr.getQuick(i, j), EPSILON); assertEquals(0.0, sparse_em.getQuick(i, j), EPSILON); } } } }
public static HmmModel truncateModel(HmmModel model, double threshold) { Vector ip = model.getInitialProbabilities(); Matrix em = model.getEmissionMatrix(); Matrix tr = model.getTransitionMatrix(); RandomAccessSparseVector sparseIp = new RandomAccessSparseVector(model .getNrOfHiddenStates()); SparseMatrix sparseEm = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfOutputStates()); SparseMatrix sparseTr = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfHiddenStates()); for (int i = 0; i < model.getNrOfHiddenStates(); ++i) { double value = ip.getQuick(i); if (value > threshold) { sparseIp.setQuick(i, value); } for (int j = 0; j < model.getNrOfHiddenStates(); ++j) { value = tr.getQuick(i, j); if (value > threshold) { sparseTr.setQuick(i, j, value); } } for (int j = 0; j < model.getNrOfOutputStates(); ++j) { value = em.getQuick(i, j); if (value > threshold) { sparseEm.setQuick(i, j, value); } } } HmmModel sparseModel = new HmmModel(sparseTr, sparseEm, sparseIp); normalizeModel(sparseModel); sparseModel.registerHiddenStateNames(model.getHiddenStateNames()); sparseModel.registerOutputStateNames(model.getOutputStateNames()); return sparseModel; }
HmmUtils { public static HmmModel truncateModel(HmmModel model, double threshold) { Vector ip = model.getInitialProbabilities(); Matrix em = model.getEmissionMatrix(); Matrix tr = model.getTransitionMatrix(); RandomAccessSparseVector sparseIp = new RandomAccessSparseVector(model .getNrOfHiddenStates()); SparseMatrix sparseEm = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfOutputStates()); SparseMatrix sparseTr = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfHiddenStates()); for (int i = 0; i < model.getNrOfHiddenStates(); ++i) { double value = ip.getQuick(i); if (value > threshold) { sparseIp.setQuick(i, value); } for (int j = 0; j < model.getNrOfHiddenStates(); ++j) { value = tr.getQuick(i, j); if (value > threshold) { sparseTr.setQuick(i, j, value); } } for (int j = 0; j < model.getNrOfOutputStates(); ++j) { value = em.getQuick(i, j); if (value > threshold) { sparseEm.setQuick(i, j, value); } } } HmmModel sparseModel = new HmmModel(sparseTr, sparseEm, sparseIp); normalizeModel(sparseModel); sparseModel.registerHiddenStateNames(model.getHiddenStateNames()); sparseModel.registerOutputStateNames(model.getOutputStateNames()); return sparseModel; } }
HmmUtils { public static HmmModel truncateModel(HmmModel model, double threshold) { Vector ip = model.getInitialProbabilities(); Matrix em = model.getEmissionMatrix(); Matrix tr = model.getTransitionMatrix(); RandomAccessSparseVector sparseIp = new RandomAccessSparseVector(model .getNrOfHiddenStates()); SparseMatrix sparseEm = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfOutputStates()); SparseMatrix sparseTr = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfHiddenStates()); for (int i = 0; i < model.getNrOfHiddenStates(); ++i) { double value = ip.getQuick(i); if (value > threshold) { sparseIp.setQuick(i, value); } for (int j = 0; j < model.getNrOfHiddenStates(); ++j) { value = tr.getQuick(i, j); if (value > threshold) { sparseTr.setQuick(i, j, value); } } for (int j = 0; j < model.getNrOfOutputStates(); ++j) { value = em.getQuick(i, j); if (value > threshold) { sparseEm.setQuick(i, j, value); } } } HmmModel sparseModel = new HmmModel(sparseTr, sparseEm, sparseIp); normalizeModel(sparseModel); sparseModel.registerHiddenStateNames(model.getHiddenStateNames()); sparseModel.registerOutputStateNames(model.getOutputStateNames()); return sparseModel; } private  HmmUtils(); }
HmmUtils { public static HmmModel truncateModel(HmmModel model, double threshold) { Vector ip = model.getInitialProbabilities(); Matrix em = model.getEmissionMatrix(); Matrix tr = model.getTransitionMatrix(); RandomAccessSparseVector sparseIp = new RandomAccessSparseVector(model .getNrOfHiddenStates()); SparseMatrix sparseEm = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfOutputStates()); SparseMatrix sparseTr = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfHiddenStates()); for (int i = 0; i < model.getNrOfHiddenStates(); ++i) { double value = ip.getQuick(i); if (value > threshold) { sparseIp.setQuick(i, value); } for (int j = 0; j < model.getNrOfHiddenStates(); ++j) { value = tr.getQuick(i, j); if (value > threshold) { sparseTr.setQuick(i, j, value); } } for (int j = 0; j < model.getNrOfOutputStates(); ++j) { value = em.getQuick(i, j); if (value > threshold) { sparseEm.setQuick(i, j, value); } } } HmmModel sparseModel = new HmmModel(sparseTr, sparseEm, sparseIp); normalizeModel(sparseModel); sparseModel.registerHiddenStateNames(model.getHiddenStateNames()); sparseModel.registerOutputStateNames(model.getOutputStateNames()); return sparseModel; } 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); }
HmmUtils { public static HmmModel truncateModel(HmmModel model, double threshold) { Vector ip = model.getInitialProbabilities(); Matrix em = model.getEmissionMatrix(); Matrix tr = model.getTransitionMatrix(); RandomAccessSparseVector sparseIp = new RandomAccessSparseVector(model .getNrOfHiddenStates()); SparseMatrix sparseEm = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfOutputStates()); SparseMatrix sparseTr = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfHiddenStates()); for (int i = 0; i < model.getNrOfHiddenStates(); ++i) { double value = ip.getQuick(i); if (value > threshold) { sparseIp.setQuick(i, value); } for (int j = 0; j < model.getNrOfHiddenStates(); ++j) { value = tr.getQuick(i, j); if (value > threshold) { sparseTr.setQuick(i, j, value); } } for (int j = 0; j < model.getNrOfOutputStates(); ++j) { value = em.getQuick(i, j); if (value > threshold) { sparseEm.setQuick(i, j, value); } } } HmmModel sparseModel = new HmmModel(sparseTr, sparseEm, sparseIp); normalizeModel(sparseModel); sparseModel.registerHiddenStateNames(model.getHiddenStateNames()); sparseModel.registerOutputStateNames(model.getOutputStateNames()); return sparseModel; } 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);  }