forward memoryless markov chain in matlab

Assuming that you have already constructed or defined your memoryless Markov chain, you can use the forward algorithm to calculate the probability of observing a sequence of states up to a specific time point. Here's how to implement it in MATLAB using an example 3-state MC:

main.m
% Define the transition probability matrix
P = [0.4, 0.2, 0.4;
     0.3, 0.3, 0.4;
     0.1, 0.5, 0.4];

% Define the initial state distribution
pi0 = [0.5, 0.3, 0.2];

% Define a sequence of observations
obs = [1, 2, 3, 1];

% Define the time horizon
T = length(obs);

% Initialize the forward messages
alpha = zeros(T, size(P, 1));

% Calculate the forward messages
for t=1:T
    % Obtain the observation at time t
    y = obs(t);
    
    % Calculate the first message
    if t == 1
        alpha(1,:) = pi0 .* P(:,y)';
    else
        alpha(t,:) = alpha(t-1,:) * P .* repmat(P(:,y)', size(P,1), 1);
    end
    
    % Normalize the message to avoid numerical issues
    alpha(t,:) = alpha(t,:) ./ sum(alpha(t,:));
end

% The final forward message is the product of all intermediate messages
alpha_T = alpha(T,:);
818 chars
36 lines

The final result alpha_T is a vector containing the probability of observing the sequence of states up to time T, given the memoryless Markov chain.

gistlibby LogSnag