1 % To compute log likelihood of the model for the given observation
\r
2 % To prevent underflow, we need to do some tricks
\r
4 function likelihood = model_log_likelihood(observation, model)
\r
6 likelihood_vect = zeros(model.num_mcs, 1);
\r
7 for i=1:model.num_mcs
\r
8 likelihood_vect(i) = log(model.priors(i)) + chain_log_likelihood(observation, model.chain(i));
\r
12 % Get the max element in this vector
\r
13 max_chain_log_likelihood = max(likelihood_vect);
\r
15 % Now subtract this max likelihood from each log_likelihood
\r
16 % Now take each the exp of each element. Sum over the vector, take the
\r
17 % log, and add the max_chain_log_likelihood.
\r
18 likelihood_vect = exp(likelihood_vect - max_chain_log_likelihood);
\r
19 likelihood = max_chain_log_likelihood + log(sum(likelihood_vect));
\r
21 % The likelihood of the observation for a single Markov Chain
\r
22 %function likelihood = chain_likelihood(observation, mc)
\r
23 %ikelihood = mc.t(observation(1));
\r
24 %for i=2:size(observation, 2)
\r
25 % likelihood = likelihood * mc.T(observation(i-1), observation(i));
\r