亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? myhmm.c

?? 一個馬爾可夫模型的源碼
?? C
?? 第 1 頁 / 共 3 頁
字號:
    /* basic condition in forward algorithm */
    t=0;
    iObservation = getObservation( line[t] );
    for(i=0; i < N; i++)
    {
        alpha[t][i] = pi[i]*emissions[i][ iObservation ];
    }

    /* the induction step in forward algorithm */
    for(t=1; t < length; t++)
    {
        iObservation = getObservation( line[t] );
        for(i=0; i < N; i++)
        {
            sumTransProb = 0;
            for(j=0; j < N; j++)
            {
                sumTransProb = sumTransProb + alpha[t-1][j] * transitions[j][i];
            }

            alpha[t][i] = sumTransProb * emissions[i][iObservation];
        }
    }

    /* the termination step */
    t = length-1;
    prob = 0;
    for(i=0; i < N; i++)
    {
        prob = prob + alpha[t][i];
    }

    return prob;
}


/************************************************************************
NAME
     backward - backward algorithm

DESCRIPTION
     This function calculates the probability of the observation sequence
	 with backward algorithm given the HMM model.

     Input:
	   HMM model & the observation sequence
	 Output:
	   Probability
	 Global variables list:
	   N, beta, pi, transitions, emissions.
*************************************************************************/
double backward(char *line)
{
    int i, j, t;
    int length = strlen( line );
    /* the order of the real observation in the observation set */
    int iObservation;
    /* probability to state i given the sequence and the observation */
    double sumProbi;
    double prob;

    /* basic condition in backward algorithm */
    t = length-1;
    for(i=0; i < N; i++)
    {
        beta[t][i] = 1;
    }

    /* the induction step in backward algorithm */
    for(t=length-2; t >= 0; t--)
    {
        iObservation = getObservation( line[t+1] );
        for(i=0; i < N; i++)
        {
            sumProbi = 0;
            for(j=0; j < N; j++)
            {
                sumProbi = sumProbi + transitions[i][j]*emissions[j][iObservation] * beta[t+1][j];
            }

            beta[t][i] = sumProbi;
        }
    }

    /* the termination step */
    t = 0;
    iObservation = getObservation( line[t] );
    prob = 0;
    for(i=0; i < N; i++)
    {
        prob = prob + pi[i]*emissions[i][iObservation]*beta[t][i];
    }

    return prob;
}

/************************************************************************
NAME
     viterbi - viterbi algorithm

DESCRIPTION
     This function calculates the most probable state path of the
	 observation sequence with viterbi algorithm given the HMM model.

     Input:
	   HMM model & the observation sequence
	 Output:
	   the most probable state path
	 Global variables list:
	   N, beta, pi, transitions, emissions.
*************************************************************************/
void viterbi(char *line, char *buffer)
{
    /* delta_t0[ N ] */
    double* delta_t0;
    /* delta_t1[ N ] */
    double* delta_t1;
    /* the matrix to store the optimal states, used for backtracking
       size: N * length */
    int *phi;
    /* position in the matrix phi */
    int pos;
    int length = strlen( line );
    int i, j, t;
    int fromState, inState;
    int iObservation;
    double sum_for_scale;
    double maxProb, currentProb;

    /* allocate space for buffer */
    delta_t0 = (double*)malloc(N*sizeof(double));
    delta_t1 = (double*)malloc(N*sizeof(double));
    phi = (int*)malloc(length * N * sizeof( int ));

    /* the initial step */
    iObservation = getObservation( line[0] );
    for(i=0; i < N; i++)
    {
        delta_t0[i] = pi[i] * emissions[i][iObservation];
        phi[i] = 0;
    }

    /* recursion step */
    pos = N;
    for(t=1; t < length; t++)
    {
        iObservation = getObservation( line[t] );
        sum_for_scale = 0;
        for(j=0; j < N; j++)
        {
            /* find the optimal state from which transfered to state j */
            maxProb = delta_t0[0] * transitions[0][j];
            fromState = 0;
            for(i=1; i < N; i++)
            {
                currentProb = delta_t0[i] * transitions[i][j];
                if(maxProb < currentProb)
                {
                    maxProb = currentProb;
                    fromState = i;
                }
            }
            /* calculate the transition probability */
            delta_t1[j] = emissions[j][iObservation] * maxProb;
            sum_for_scale = sum_for_scale + delta_t1[j];
            phi[ pos++ ] = fromState;
        }

        /* update current probabilities */
        for(j=0; j < N; j++)
        {
            if(sum_for_scale < 0.1)
                delta_t0[j] = delta_t1[j] * 10;
            else
                delta_t0[j] = delta_t1[j];
        }
    }

    /* termination */
    maxProb = delta_t0[0];
    inState = 0;
    for(i=1; i < N; i++)
    {
        currentProb = delta_t0[i];
        if(maxProb < currentProb)
        {
            maxProb = currentProb;
            inState = i;
        }
    }

    /* path backtracking */
    i = inState;
    pos = length * N;
    for(t=length-1; t >= 0 ; t--)
    {
        buffer[ t ] = getSymbol( i );
        pos = pos - N;
        if(pos+i < 0)
        {
            printf("Error in the subscribe 1: pos = %d, statei = %d\n", pos, i);
            exit(1);
        }
        if(pos+i >= length * N)
        {
            printf("Error in the subscribe 2\n");
            exit(1);
        }
        i = phi[pos + i];
    }

    buffer[length] = '\0';
    free(delta_t0);
    free(delta_t1);
    free( phi );

    return;
}

/************************************************************************
NAME
     baumWelch - baumWelch algorithm

DESCRIPTION
     This function estimates the parameters of HMM with specified topology
	 given the training data and initial parameters of HMM

     Input:
	   HMM topology, HMM initial parameters, and training data
	 Output:
	   the estimated parameters
	 Global variables list:
	   iTrain, trainData, T, M, N, alpha, beta, pi, transitions, emissions.
*************************************************************************/
void baumWelch()
{
    int loops;
    int i, j, k, t;
    int iObservation;
    int length;
    double** _transitions; /* _transitions[ N ][ N ] estimated transition matrix T */
    double** _emissions;   /* _emissions[ N ][ M ]   estimated emission matrix E */
    double* _pi;           /* _pi[ N ]               estimated initial distribution */
    double oldLikelihood, Likelihood;
    double error;
    double sumGamma1;
    double* gamma1;  // gamma1[N]: for estimating initial distribution of the states
    double** Eij;  // Eij[N][N]: expected numbers transited from state i to state j
    double** Eia;  // Eia[N][M]: expected numbers of emitting a in state i
	/* X_d_t[N][N]: expected numbers transited from state i to state j
         when it is with observation O in the position t of d-th sequence */
    double** X_d_t;
    double* ei;    // ei[N]: expected numbers in state i
    double* ej;    // ej[N]: expected numbers of emissions in state j
    double Prob;
    double temp, temp2, tempM;

    /* allocate space */
    AllocateDataSpace( &_transitions, N, N );
    AllocateDataSpace( &_emissions, N, M );
    _pi = (double*)malloc(N*sizeof(double));
    gamma1 = (double*)malloc(N*sizeof(double));
    AllocateDataSpace( &Eij, N, N );
    AllocateDataSpace( &Eia, N, M );
    AllocateDataSpace( &X_d_t, N, N );
    ei = (double*)malloc(N*sizeof(double));
    ej = (double*)malloc(N*sizeof(double));

    /* initialization */
    error = 1;
    oldLikelihood = forward( trainData[0] );
    Likelihood = backward( trainData[0] );
    for(k=1; k < iTrain; k++)
    {
        oldLikelihood = oldLikelihood + forward(trainData[k]);
        Likelihood = Likelihood + backward(trainData[k]);
    }
    /* check whether forward and backward algorithms work well */
    if(fabs((Likelihood-oldLikelihood)/Likelihood) > 0.01)
    {
        printf("there are errors in forward or backward algorithm\n");
        exit(1);
    }
    Likelihood = oldLikelihood;
    printf("Likelihood from forward: %e\n", Likelihood);
    loops = 0;
    while((error > threshold && loops < 1000) && Likelihood > threshold)
    {
        printf("loops: %d\n", loops);
        loops++;
        /* initialize the expected matrix */
        for(i=0; i < N; i++)
        {
            gamma1[i] = 0;
            for(j=0; j < N; j++)
            {
                Eij[i][j] = 0;
            }
            for(j=0; j < M; j++)
            {
                Eia[i][j] = 0;
            }
        }

        // loop for all the sequences
        for(k=0; k < iTrain; k++)
        {
            // initialize the matrix alpha and beta
            Prob = backward(trainData[k]);
            Prob = forward(trainData[k]);
            length = strlen(trainData[k]);
            // loop for one sequence
            for(t=0; t < length-1; t++)
            {
                iObservation = getObservation(trainData[k][t+1]);
                for(i=0; i < N; i++)
                {
                    for(j=0; j < N; j++)
                    {
                        X_d_t[i][j] = (alpha[t][i] * transitions[i][j] * emissions[j][iObservation] * beta[t+1][j]) / Prob;
                        Eij[i][j] = Eij[i][j] + X_d_t[i][j];
                        Eia[j][iObservation] = Eia[j][iObservation] + X_d_t[i][j];
                        if(t == 0)
                        {
                            gamma1[i] = gamma1[i] + X_d_t[i][j];
                        }
                    }
                }
            }
        }
        sumGamma1 = 0;
        for(i=0; i < N; i++)
        {
            sumGamma1 = sumGamma1 + gamma1[i];
            ei[i] = Eij[i][0];
            for(j=1; j < N; j++)
            {
                ei[i] = ei[i] + Eij[i][j];
            }
            ej[i] = Eia[i][0];
            for(j=1; j < M; j++)
            {
                ej[i] = ej[i] + Eia[i][j];
            }
        }

        temp = 1.0 / (double)N;
        temp2= temp/ (double)N;
        tempM= 1.0 / (double)M;
        for(i=0; i < N; i++)
        {
            // estimate initial distribution
            if(sumGamma1 < threshold2)
                _pi[i] = temp; /* uniform distribution */
            else
                _pi[i] = gamma1[i] / sumGamma1;
            // estimate the transition matrix
            for(j=0; j < N; j++)
            {
                if(ei[i] < threshold2)
                {
                    if(i == j)
                        _transitions[i][j] = 1 - temp + temp2;
                    else
                    _transitions[i][j] = temp2;
                }
                else
                {
                    _transitions[i][j] = Eij[i][j] / ei[i];
                }
            }
            // estimate the emission matrix
            for(j=0; j < M; j++)
            {
                if(ej[i] < threshold2)
                    _emissions[i][j] = tempM;
                else
                    _emissions[i][j] = Eia[i][j] / ej[i];
            }
        }

        /* assign the estimated new values to the HMM model */
        for(i=0; i < N; i++)
        {
            /* do not need to update the initial state distribution */
            /*pi[i] = _pi[i];*/

            /* transition matrix */
            for(j=0; j < N; j++)
            {
                transitions[i][j] = _transitions[i][j];
            }

            /* emission matrix */
            for(j=0; j < M; j++)
            {
                emissions[i][j] = _emissions[i][j];
            }
        }
        /* compute the error */
        Likelihood = forward( trainData[0] );
        for(k=1; k < iTrain; k++)
        {
            Likelihood = Likelihood + forward(trainData[k]);
        }
        error = fabs(Likelihood - oldLikelihood);
        printf("oldLikelihood = %e, Likelihood = %e, error = %e\n", oldLikelihood, Likelihood, error);
        oldLikelihood = Likelihood;
    }
}

/************************************************************************
NAME
     loadHMM - load hidden Markov model from the specified file

DESCRIPTION
     This function ...

     Input:
	   the specified HMM file name
	 Output:
	   N, M, transitions, emissions, pi
	 Global variables list:
	   M, N, pi, transitions, emissions.
*************************************************************************/
void loadHMM(char* hmmFile)
{
    FILE *in_fp;
    char* token;
    int i, j, m;
    int end;
    int nLines = cal_lines(hmmFile);
    int nBuffer = getLengthOfLongestLine(hmmFile)+extraSpace;
    char *line, *buffer;
    /* Open for read (will fail if inputfile does not exist) */
    if( (in_fp  = fopen( hmmFile, "r" )) == NULL )
    {
        printf( "The file '%s' was not opened\n", hmmFile);
        exit(1);
    }
    line = (char *) malloc(nBuffer * sizeof(char));
    buffer = (char *) malloc(nBuffer * sizeof(char));

    // Part 1
    // read the first line
    fgets(line, nBuffer, in_fp);

    // number of states
    token = strtok( line, seps );
    N = atoi(token);
    
    /* exceptions for number of states */
    if(N < 1)
    {
        printf("the number of states is too small!\n");
        exit(1);
    }
    if(N > 100)
    {
        printf("the program can't deal with states more than 100!\n");
        exit(1);
    }
    /* number of observations */
    token = strtok( NULL, seps );
    M = atoi(token);
    observationsDefined = TRUE;
    if(M == noDefObservations)
    {
        observationsDefined = FALSE;
    }
    /* exceptions for number of observations */
    if((M <= 0 && M != noDefObservations) || M > 100)
    {
        printf("the number of the observations is not correct!\n");
        exit(1);
    }
    /* the possible length of the longest line */
    token = strtok( NULL, seps );
    T = atoi(token);

    // skip one line
    fgets(line, nBuffer, in_fp);

    // Part 2
    fgets(line, nBuffer, in_fp);
    /* whether to read the observations */
    if(observationsDefined == TRUE)
    {
        strcpy(buffer, line);
        m = 0;
        token = strtok( buffer, seps );
        // count how many observations there are in the line
        while(token != NULL)
        {
            token = strtok( NULL, seps );
            m++;
        }
        m = m - 1; // because there is a prefix in the sequence

        /* check whether the nunber of the observations is consistent */

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
一区二区三区成人在线视频| 国产欧美一区视频| 99精品视频一区| 国精品**一区二区三区在线蜜桃| 日韩av网站免费在线| 天涯成人国产亚洲精品一区av| 午夜久久久久久| 日韩av在线发布| 精品在线视频一区| 国产露脸91国语对白| 国产馆精品极品| 99精品热视频| 91成人免费网站| 欧美精选在线播放| 色婷婷综合久久久中文字幕| 欧美色图一区二区三区| 欧美理论在线播放| 日韩美女视频在线| 国产婷婷色一区二区三区四区| 久久综合给合久久狠狠狠97色69| 久久这里只精品最新地址| 欧美精品一区二区三区蜜桃| 国产精品久线在线观看| 一区二区欧美国产| 久久精品国产一区二区三区免费看| 精品一区二区久久久| 风间由美一区二区av101| 91在线丨porny丨国产| 欧美午夜精品久久久久久孕妇 | 精品一区二区三区免费观看| 国产一区不卡视频| 99国产精品视频免费观看| 91成人看片片| 2欧美一区二区三区在线观看视频 337p粉嫩大胆噜噜噜噜噜91av | 精品国产乱码久久久久久久久| 国产欧美精品一区二区色综合 | 精品在线一区二区| 99久久国产综合精品色伊| 666欧美在线视频| 久久精品人人做| 亚洲高清免费观看| 豆国产96在线|亚洲| 欧美色中文字幕| 国产欧美日本一区视频| 亚洲成人午夜电影| 国产a久久麻豆| 欧美日韩五月天| 国产精品乱人伦中文| 日本aⅴ亚洲精品中文乱码| 成人午夜碰碰视频| 欧美一区二区免费视频| **网站欧美大片在线观看| 久久国产剧场电影| 欧美日韩一区二区三区视频| 国产午夜精品理论片a级大结局 | 欧美日韩国产高清一区二区 | av电影在线不卡| 欧美一二三四在线| 亚洲欧美日韩久久精品| 国产精品99久久久久久有的能看 | 天天操天天色综合| 99国产欧美另类久久久精品| 精品国产91久久久久久久妲己| 亚洲一二三专区| eeuss影院一区二区三区| 亚洲精品一线二线三线无人区| 亚洲在线视频免费观看| 粗大黑人巨茎大战欧美成人| 久久欧美一区二区| 激情六月婷婷综合| 精品少妇一区二区三区| 三级一区在线视频先锋| 在线观看一区日韩| 亚洲视频综合在线| 成人午夜视频免费看| 国产目拍亚洲精品99久久精品| 久久se精品一区二区| 日韩美女视频在线| 免费高清视频精品| 精品久久一二三区| 韩国av一区二区三区| 精品免费一区二区三区| 捆绑调教一区二区三区| 日韩区在线观看| 黄色资源网久久资源365| 欧美xxx久久| 精品一区二区三区在线播放 | 老汉av免费一区二区三区| 欧美一级国产精品| 麻豆成人久久精品二区三区小说| 欧美一区二区三区播放老司机| 五月开心婷婷久久| 欧美精品第一页| 免费看欧美女人艹b| 欧美r级在线观看| 国产成人在线观看| 亚洲人妖av一区二区| 在线视频你懂得一区| 亚洲va欧美va人人爽午夜| 欧美一区二区视频在线观看2020| 日本亚洲天堂网| 精品国产亚洲一区二区三区在线观看| 国产精品一区在线观看乱码| 国产精品美女一区二区三区 | 国产精品18久久久久| 综合电影一区二区三区| 欧美视频一区在线| 美女一区二区久久| 中文字幕亚洲精品在线观看| 欧美日韩午夜在线| 九九精品视频在线看| 国产欧美日韩不卡免费| 在线观看91视频| 麻豆精品一区二区av白丝在线| 久久久电影一区二区三区| 91免费视频网址| 久久精品国产一区二区三区免费看| 国产婷婷色一区二区三区 | 亚洲精品一卡二卡| 精品久久久久久久久久久久包黑料 | 亚洲一区二区三区三| 欧美成人精品福利| 色哟哟精品一区| 紧缚奴在线一区二区三区| 亚洲人精品午夜| 亚洲精品在线电影| 欧美中文字幕一区二区三区亚洲| 激情五月激情综合网| 亚洲综合清纯丝袜自拍| 久久久久亚洲综合| 欧美男男青年gay1069videost| 国产.欧美.日韩| 秋霞国产午夜精品免费视频| 综合久久久久综合| 久久综合久久综合九色| 欧美精品123区| 91在线国产福利| 国产精品一二三区在线| 亚洲v中文字幕| 亚洲日本在线观看| 久久精品日产第一区二区三区高清版| 欧美影院一区二区三区| 成人手机电影网| 久久不见久久见免费视频1| 亚洲综合一二区| 中文字幕一区二区5566日韩| 久久女同精品一区二区| 日韩欧美国产1| 51精品国自产在线| 在线区一区二视频| 成人高清视频在线| 国产福利一区在线| 久久 天天综合| 久久国产精品露脸对白| 日韩二区在线观看| 亚洲一区二区三区影院| 亚洲人成人一区二区在线观看| 国产日韩欧美不卡在线| www久久精品| 久久这里只有精品视频网| 精品国产露脸精彩对白| 欧美α欧美αv大片| 日韩一区和二区| 91精品国产一区二区人妖| 欧洲精品在线观看| 在线看不卡av| 精品视频1区2区| 欧美性xxxxx极品少妇| 91福利区一区二区三区| 在线亚洲免费视频| 91福利在线看| 在线观看免费成人| 欧美日韩国产在线观看| 欧美二区三区的天堂| 欧美久久久久久久久| 91精品婷婷国产综合久久竹菊| 欧美美女直播网站| 欧美一区在线视频| 精品国产乱码久久久久久1区2区| 久久久噜噜噜久久中文字幕色伊伊 | 在线播放中文字幕一区| 91麻豆精品国产91久久久使用方法| 91精品国产综合久久香蕉的特点| 91麻豆精品国产91久久久使用方法 | 91麻豆精品国产自产在线| 99久久精品国产导航| 欧美亚一区二区| 91精品在线观看入口| 久久久精品国产99久久精品芒果| 国产精品无遮挡| 一区二区三区欧美日| 亚洲aⅴ怡春院| 精品一区二区三区欧美| 成人网男人的天堂| 欧美日韩中文字幕一区| 日韩欧美一区二区免费| 国产精品区一区二区三区| 亚洲国产一区二区视频| 国产做a爰片久久毛片| 91在线小视频|