首页 > 代码库 > 隐马尔可夫模型(六)——隐马尔可夫模型的评估问题(前向后向相结合算法)(转载)

隐马尔可夫模型(六)——隐马尔可夫模型的评估问题(前向后向相结合算法)(转载)

 

重新回顾:

    前向变量αt(i):在时刻t,在已知模型μ=(A,B,π)的条件下,状态处于si,输出序列为O102...Ot,前向变量为αt(i)

    后向变量βt(i):在时刻t,在已知模型μ=(A,B,π)和状态处于si的条件下,输出序列为Ot+1Ot+2...OT,后向变量为βt(i)

公式推导:

    P(O,qt=si|μ) = P(O1O2...OT, qt=si|μ)

                         =P(O1O2...Ot, qt=si,Ot+1Ot+2...OT|μ)

                         =P(O1O2...Ot, qt=si|μ) * P(Ot+1Ot+2...OT|O1O2...Ot, qt=si,μ)

                         =P(O1O2...Ot, qt=si|μ) * P(Ot+1Ot+2...OT|qt=si,μ)

                         =αt(i) *  βt(i)

     P(O|μ)=技术分享

案例分析:

    技术分享

      分析:

        P(q4=s3|O,M) =  P(q4=s3, O|M)/P(O|M)

                            = P(O,q4=s3|M)/P(O|M)

                            = α4(3) *  β4(3) 技术分享

     程序:

 

技术分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
        float a[3][3] = {{0.5,0.2,0.3},{0.3,0.5,0.2},{0.2,0.3,0.5}};
        float b[3][2] = {{0.5,0.5},{0.4,0.6},{0.7,0.3}};
        float result_b[8][3];
        float result_f[8][3];
        float result, result_t;
        int list[8] = {0,1,0,0,1,0,1,1};
        result_b[7][0] = 1;
        result_b[7][1] = 1;
        result_b[7][2] = 1;
        result_f[0][0] = 0.2 * 0.5;
        result_f[0][1] = 0.4 * 0.4;
        result_f[0][2] = 0.4 * 0.7;
        //Backward
        int i,j,k, count = 7;
        for (i=6; i>=0; i--)
        {
            for(j=0; j<=2; j++)
            {
                result_b[i][j] = 0;
                for(k=0; k<=2; k++)
                {
                   result_b[i][j] += result_b[i+1][k] * a[j][k] * b[k][list[count]];
                }
            }
            count -= 1;
        }
       for (i=0; i<=7; i++)
        {
            for(j=0; j<=2; j++)
            {
                printf("b[%d][%d]= %f\n",i+1,j+1, result_b[i][j]);

            }
        }
        printf("Backward:%f\n", result_b[0][0]*0.2*0.5+result_b[0][1]*0.4*0.4+result_b[0][2]*0.4*0.7);
        //Forward
        count = 1;
        for (i=1; i<=7; i++)
        {
            for(j=0; j<=2; j++)
            {
                result_f[i][j] = 0;
                for(k=0; k<=2; k++)
                {
                    result_f[i][j] += result_f[i-1][k] * a[k][j] * b[j][list[count]];
                }
            }
            count += 1;
        }
        for (i=0; i<=7; i++)
        {
            for(j=0; j<=2; j++)
            {
                printf("a[%d][%d]= %f\n", i+1, j+1, result_f[i][j]);
            }
        }
        result = result_f[7][0] + result_f[7][1] + result_f[7][2];
        printf("Forward: %f\n", result);
        
        result_t = 0;
        for (i=0; i<=2; i++)
        {
            result_t += result_f[3][i] * result_b[3][i];
        }
        printf("Result:%f\n", result_f[3][2]*result_b[3][2]/result_t);

        return 0;
}
技术分享

        运行结果 

                  技术分享                技术分享

 

隐马尔可夫模型(六)——隐马尔可夫模型的评估问题(前向后向相结合算法)(转载)