• Viterbi算法


    clc;
    clear all;
    close all;

    Start_Pi = [-1,-1];
    State_k = ['H','L'];
    % 转移矩阵
    Transition_matrix = [-1,-1.322;-1.322,-0.737];
    % 0 H L
    % H -1 -1
    % L -1.322,-0.373
    % 序列中包含字母ACTG
    sequence = ['A','C','G','T'];
    Emission_matrix = [-2.322,-1.737,-1.737,-2.322;
    -1.737,-2.322,-2.322,-1.737];
    Observation_sequence = ['G','G','C','A','C','T','G','A','A'];
    k = length(State_k);
    l = length(Observation_sequence);
    Result_matrix = zeros(k + 1,l + 1);
    Result_matrix(1,2:l+1) = Observation_sequence;
    Result_matrix(2:k+1,1) = State_k ;
    %% Viterbi algorithm(这里是用权值代替概率计算的)
    Result_matrix(2,2) = Start_Pi(1) + Emission_matrix(1,find(sequence ...
    == Observation_sequence(1),1));
    Result_matrix(3,2) = Start_Pi(2) + Emission_matrix(2,find(sequence ...
    == Observation_sequence(1),1));
    for i = 3 : l + 1
    for j = 2: k+1
    if j ~= k + 1
    Result_matrix(j,i) = Emission_matrix(j - 1,find(sequence == Observation_sequence(i - 1),1)) +...
    max(Result_matrix(j,i - 1) + Transition_matrix(j - 1,1),Result_matrix(j + 1,i - 1) + Transition_matrix(j,1) );
    else
    Result_matrix(j,i) = Emission_matrix(j - 1,find(sequence == Observation_sequence(i - 1),1)) +...
    max(Result_matrix(j -1,i - 1) + Transition_matrix(j - 2,2),Result_matrix(j ,i - 1) + Transition_matrix(j - 1,2) );
    end
    end
    end
    %% back tracing
    Result = max(Result_matrix(2:k+1,l+1));
    [row,~ ]= find(Result_matrix == Result);
    State_sequence = [];
    State_sequence= strcat(State_k(row-1));
    for i = l : -1: 3
    if row == k+ 1
    if Result == Result_matrix(row,i ) + Transition_matrix(row - 1,2) +...
    Emission_matrix(row - 1,find(sequence == Observation_sequence(i),1))
    State_sequence = strcat(State_k(2),State_sequence);
    Result = Result_matrix(row,i );
    else
    State_sequence =strcat(State_k(1),State_sequence);
    row = row -1;
    Result = Result_matrix(row,i );
    end
    elseif row == 2
    if Result == Result_matrix(row,i ) + Transition_matrix(row - 1,1) + ...
    Emission_matrix(row - 1,find(sequence == Observation_sequence(i),1));
    State_sequence=strcat(State_k(1),State_sequence);
    Result = Result_matrix(row,i );
    else
    State_sequence=strcat(State_k(2),State_sequence);
    row = row + 1;
    Result = Result_matrix(row,i );
    end
    else
    break;% 因为例子只有两个状态,其他就没有写
    end
    end
    %% 输出序列头部处理
    tem = max(Result_matrix(2:3,2));
    [r,~] = find(Result_matrix == tem);
    State_sequence= strcat(State_k(r-1),State_sequence);
    disp(State_sequence)







  • 相关阅读:
    JAVA学习前应该了解
    JAVA帝国的诞生
    常用的快捷方式
    MarkDown学习
    运动检测
    图像分割
    感知机
    线性判别函数
    距离
    概率密度估计笔记——非参数估计
  • 原文地址:https://www.cnblogs.com/Kermit-Li/p/4078982.html
Copyright © 2020-2023  润新知