@Test public void testPredict() { System.out.println("predict"); HMM hmm = new HMM(pi, new Matrix(a), new Matrix(b)); int[] o = {0, 0, 1, 1, 0, 1, 1, 0}; int[] s = {0, 0, 0, 0, 0, 0, 0, 0}; int[] result = hmm.predict(o); assertEquals(o.length, result.length); for (int i = 0; i < s.length; i++) { assertEquals(s[i], result[i]); } }
public int[] predict(int[] o) { int N = a.nrows(); double[][] trellis = new double[o.length][N]; int[][] psy = new int[o.length][N]; int[] s = new int[o.length]; for (int i = 0; i < N; i++) { trellis[0][i] = MathEx.log(pi[i]) + MathEx.log(b.get(i, o[0])); psy[0][i] = 0; } for (int t = 1; t < o.length; t++) { for (int j = 0; j < N; j++) { double maxDelta = Double.NEGATIVE_INFINITY; int maxPsy = 0; for (int i = 0; i < N; i++) { double delta = trellis[t - 1][i] + MathEx.log(a.get(i, j)); if (maxDelta < delta) { maxDelta = delta; maxPsy = i; } } trellis[t][j] = maxDelta + MathEx.log(b.get(j, o[t])); psy[t][j] = maxPsy; } } int n = o.length - 1; double maxDelta = Double.NEGATIVE_INFINITY; for (int i = 0; i < N; i++) { if (maxDelta < trellis[n][i]) { maxDelta = trellis[n][i]; s[n] = i; } } for (int t = n; t-- > 0;) { s[t] = psy[t + 1][s[t + 1]]; } return s; }
HMM implements Serializable { public int[] predict(int[] o) { int N = a.nrows(); double[][] trellis = new double[o.length][N]; int[][] psy = new int[o.length][N]; int[] s = new int[o.length]; for (int i = 0; i < N; i++) { trellis[0][i] = MathEx.log(pi[i]) + MathEx.log(b.get(i, o[0])); psy[0][i] = 0; } for (int t = 1; t < o.length; t++) { for (int j = 0; j < N; j++) { double maxDelta = Double.NEGATIVE_INFINITY; int maxPsy = 0; for (int i = 0; i < N; i++) { double delta = trellis[t - 1][i] + MathEx.log(a.get(i, j)); if (maxDelta < delta) { maxDelta = delta; maxPsy = i; } } trellis[t][j] = maxDelta + MathEx.log(b.get(j, o[t])); psy[t][j] = maxPsy; } } int n = o.length - 1; double maxDelta = Double.NEGATIVE_INFINITY; for (int i = 0; i < N; i++) { if (maxDelta < trellis[n][i]) { maxDelta = trellis[n][i]; s[n] = i; } } for (int t = n; t-- > 0;) { s[t] = psy[t + 1][s[t + 1]]; } return s; } }
HMM implements Serializable { public int[] predict(int[] o) { int N = a.nrows(); double[][] trellis = new double[o.length][N]; int[][] psy = new int[o.length][N]; int[] s = new int[o.length]; for (int i = 0; i < N; i++) { trellis[0][i] = MathEx.log(pi[i]) + MathEx.log(b.get(i, o[0])); psy[0][i] = 0; } for (int t = 1; t < o.length; t++) { for (int j = 0; j < N; j++) { double maxDelta = Double.NEGATIVE_INFINITY; int maxPsy = 0; for (int i = 0; i < N; i++) { double delta = trellis[t - 1][i] + MathEx.log(a.get(i, j)); if (maxDelta < delta) { maxDelta = delta; maxPsy = i; } } trellis[t][j] = maxDelta + MathEx.log(b.get(j, o[t])); psy[t][j] = maxPsy; } } int n = o.length - 1; double maxDelta = Double.NEGATIVE_INFINITY; for (int i = 0; i < N; i++) { if (maxDelta < trellis[n][i]) { maxDelta = trellis[n][i]; s[n] = i; } } for (int t = n; t-- > 0;) { s[t] = psy[t + 1][s[t + 1]]; } return s; } HMM(double[] pi, Matrix a, Matrix b); }
HMM implements Serializable { public int[] predict(int[] o) { int N = a.nrows(); double[][] trellis = new double[o.length][N]; int[][] psy = new int[o.length][N]; int[] s = new int[o.length]; for (int i = 0; i < N; i++) { trellis[0][i] = MathEx.log(pi[i]) + MathEx.log(b.get(i, o[0])); psy[0][i] = 0; } for (int t = 1; t < o.length; t++) { for (int j = 0; j < N; j++) { double maxDelta = Double.NEGATIVE_INFINITY; int maxPsy = 0; for (int i = 0; i < N; i++) { double delta = trellis[t - 1][i] + MathEx.log(a.get(i, j)); if (maxDelta < delta) { maxDelta = delta; maxPsy = i; } } trellis[t][j] = maxDelta + MathEx.log(b.get(j, o[t])); psy[t][j] = maxPsy; } } int n = o.length - 1; double maxDelta = Double.NEGATIVE_INFINITY; for (int i = 0; i < N; i++) { if (maxDelta < trellis[n][i]) { maxDelta = trellis[n][i]; s[n] = i; } } for (int t = n; t-- > 0;) { s[t] = psy[t + 1][s[t + 1]]; } return s; } HMM(double[] pi, Matrix a, Matrix b); double[] getInitialStateProbabilities(); Matrix getStateTransitionProbabilities(); Matrix getSymbolEmissionProbabilities(); double p(int[] o, int[] s); double logp(int[] o, int[] s); double p(int[] o); double logp(int[] o); int[] predict(int[] o); static HMM fit(int[][] observations, int[][] labels); static HMM fit(T[][] observations, int[][] labels, ToIntFunction<T> ordinal); void update(T[][] observations, int iterations, ToIntFunction<T> ordinal); void update(int[][] observations, int iterations); @Override String toString(); }
HMM implements Serializable { public int[] predict(int[] o) { int N = a.nrows(); double[][] trellis = new double[o.length][N]; int[][] psy = new int[o.length][N]; int[] s = new int[o.length]; for (int i = 0; i < N; i++) { trellis[0][i] = MathEx.log(pi[i]) + MathEx.log(b.get(i, o[0])); psy[0][i] = 0; } for (int t = 1; t < o.length; t++) { for (int j = 0; j < N; j++) { double maxDelta = Double.NEGATIVE_INFINITY; int maxPsy = 0; for (int i = 0; i < N; i++) { double delta = trellis[t - 1][i] + MathEx.log(a.get(i, j)); if (maxDelta < delta) { maxDelta = delta; maxPsy = i; } } trellis[t][j] = maxDelta + MathEx.log(b.get(j, o[t])); psy[t][j] = maxPsy; } } int n = o.length - 1; double maxDelta = Double.NEGATIVE_INFINITY; for (int i = 0; i < N; i++) { if (maxDelta < trellis[n][i]) { maxDelta = trellis[n][i]; s[n] = i; } } for (int t = n; t-- > 0;) { s[t] = psy[t + 1][s[t + 1]]; } return s; } HMM(double[] pi, Matrix a, Matrix b); double[] getInitialStateProbabilities(); Matrix getStateTransitionProbabilities(); Matrix getSymbolEmissionProbabilities(); double p(int[] o, int[] s); double logp(int[] o, int[] s); double p(int[] o); double logp(int[] o); int[] predict(int[] o); static HMM fit(int[][] observations, int[][] labels); static HMM fit(T[][] observations, int[][] labels, ToIntFunction<T> ordinal); void update(T[][] observations, int iterations, ToIntFunction<T> ordinal); void update(int[][] observations, int iterations); @Override String toString();  }