private void forward(int[] sequence, double[][] fwd, double[] scaling) {
        int T = sequence.length;
        int S = this.getNumberOfStates();
        double[] pi = this.pi;
        double[][] a = this.a;
        double[][] b = this.b;

        // 1. Initialization
        scaling[0x00] = 0.0d;
        for (int i = 0; i < S; i++) {
            double fwdi = pi[i] * b[i][sequence[0]];
            fwd[0][i] = fwdi;
            scaling[0] += fwdi;
        }
        if (scaling[0] > 0.0) { // Scaling
            double sc = 1.0d / scaling[0x00];
            for (int i = 0; i < S; i++) {
                fwd[0][i] *= sc;
            }
        }
        // 2. Induction
        for (int t = 1; t < T; t++) {
            scaling[t] = 0.0d;
            for (int i = 0; i < S; i++) {
                double p = b[i][sequence[t]];
                double sum = 0.0;
                for (int j = 0; j < S; j++) {
                    sum += fwd[t - 1][j] * a[j][i];
                }
                sum *= p;
                fwd[t][i] = sum;
                scaling[t] += sum;// scaling coefficient
            }
            if (scaling[t] > 0.0) { // Scaling
                double sc = 1.0d / scaling[t];
                for (int i = 0; i < S; i++) {
                    fwd[t][i] *= sc;
                }
            }
        }
    }