Daniel@0: function [LL, prior, transmat, obsmat, nrIterations] = ... Daniel@0: dhmm_em(data, prior, transmat, obsmat, varargin) Daniel@0: % LEARN_DHMM Find the ML/MAP parameters of an HMM with discrete outputs using EM. Daniel@0: % [ll_trace, prior, transmat, obsmat, iterNr] = learn_dhmm(data, prior0, transmat0, obsmat0, ...) Daniel@0: % Daniel@0: % Notation: Q(t) = hidden state, Y(t) = observation Daniel@0: % Daniel@0: % INPUTS: Daniel@0: % data{ex} or data(ex,:) if all sequences have the same length Daniel@0: % prior(i) Daniel@0: % transmat(i,j) Daniel@0: % obsmat(i,o) Daniel@0: % Daniel@0: % Optional parameters may be passed as 'param_name', param_value pairs. Daniel@0: % Parameter names are shown below; default values in [] - if none, argument is mandatory. Daniel@0: % Daniel@0: % 'max_iter' - max number of EM iterations [10] Daniel@0: % 'thresh' - convergence threshold [1e-4] Daniel@0: % 'verbose' - if 1, print out loglik at every iteration [1] Daniel@0: % 'obs_prior_weight' - weight to apply to uniform dirichlet prior on observation matrix [0] Daniel@0: % Daniel@0: % To clamp some of the parameters, so learning does not change them: Daniel@0: % 'adj_prior' - if 0, do not change prior [1] Daniel@0: % 'adj_trans' - if 0, do not change transmat [1] Daniel@0: % 'adj_obs' - if 0, do not change obsmat [1] Daniel@0: % Daniel@0: % Modified by Herbert Jaeger so xi are not computed individually Daniel@0: % but only their sum (over time) as xi_summed; this is the only way how they are used Daniel@0: % and it saves a lot of memory. Daniel@0: Daniel@0: [max_iter, thresh, verbose, obs_prior_weight, adj_prior, adj_trans, adj_obs] = ... Daniel@0: process_options(varargin, 'max_iter', 10, 'thresh', 1e-4, 'verbose', 1, ... Daniel@0: 'obs_prior_weight', 0, 'adj_prior', 1, 'adj_trans', 1, 'adj_obs', 1); Daniel@0: Daniel@0: previous_loglik = -inf; Daniel@0: loglik = 0; Daniel@0: converged = 0; Daniel@0: num_iter = 1; Daniel@0: LL = []; Daniel@0: Daniel@0: if ~iscell(data) Daniel@0: data = num2cell(data, 2); % each row gets its own cell Daniel@0: end Daniel@0: Daniel@0: while (num_iter <= max_iter) & ~converged Daniel@0: % E step Daniel@0: [loglik, exp_num_trans, exp_num_visits1, exp_num_emit] = ... Daniel@0: compute_ess_dhmm(prior, transmat, obsmat, data, obs_prior_weight); Daniel@0: Daniel@0: % M step Daniel@0: if adj_prior Daniel@0: prior = normalise(exp_num_visits1); Daniel@0: end Daniel@0: if adj_trans & ~isempty(exp_num_trans) Daniel@0: transmat = mk_stochastic(exp_num_trans); Daniel@0: end Daniel@0: if adj_obs Daniel@0: obsmat = mk_stochastic(exp_num_emit); Daniel@0: end Daniel@0: Daniel@0: if verbose, fprintf(1, 'iteration %d, loglik = %f\n', num_iter, loglik); end Daniel@0: num_iter = num_iter + 1; Daniel@0: converged = em_converged(loglik, previous_loglik, thresh); Daniel@0: previous_loglik = loglik; Daniel@0: LL = [LL loglik]; Daniel@0: end Daniel@0: nrIterations = num_iter - 1; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: function [loglik, exp_num_trans, exp_num_visits1, exp_num_emit, exp_num_visitsT] = ... Daniel@0: compute_ess_dhmm(startprob, transmat, obsmat, data, dirichlet) Daniel@0: % COMPUTE_ESS_DHMM Compute the Expected Sufficient Statistics for an HMM with discrete outputs Daniel@0: % function [loglik, exp_num_trans, exp_num_visits1, exp_num_emit, exp_num_visitsT] = ... Daniel@0: % compute_ess_dhmm(startprob, transmat, obsmat, data, dirichlet) Daniel@0: % Daniel@0: % INPUTS: Daniel@0: % startprob(i) Daniel@0: % transmat(i,j) Daniel@0: % obsmat(i,o) Daniel@0: % data{seq}(t) Daniel@0: % dirichlet - weighting term for uniform dirichlet prior on expected emissions Daniel@0: % Daniel@0: % OUTPUTS: Daniel@0: % exp_num_trans(i,j) = sum_l sum_{t=2}^T Pr(X(t-1) = i, X(t) = j| Obs(l)) Daniel@0: % exp_num_visits1(i) = sum_l Pr(X(1)=i | Obs(l)) Daniel@0: % exp_num_visitsT(i) = sum_l Pr(X(T)=i | Obs(l)) Daniel@0: % exp_num_emit(i,o) = sum_l sum_{t=1}^T Pr(X(t) = i, O(t)=o| Obs(l)) Daniel@0: % where Obs(l) = O_1 .. O_T for sequence l. Daniel@0: Daniel@0: numex = length(data); Daniel@0: [S O] = size(obsmat); Daniel@0: exp_num_trans = zeros(S,S); Daniel@0: exp_num_visits1 = zeros(S,1); Daniel@0: exp_num_visitsT = zeros(S,1); Daniel@0: exp_num_emit = dirichlet*ones(S,O); Daniel@0: loglik = 0; Daniel@0: Daniel@0: for ex=1:numex Daniel@0: obs = data{ex}; Daniel@0: T = length(obs); Daniel@0: %obslik = eval_pdf_cond_multinomial(obs, obsmat); Daniel@0: obslik = multinomial_prob(obs, obsmat); Daniel@0: [alpha, beta, gamma, current_ll, xi_summed] = fwdback(startprob, transmat, obslik); Daniel@0: Daniel@0: loglik = loglik + current_ll; Daniel@0: exp_num_trans = exp_num_trans + xi_summed; Daniel@0: exp_num_visits1 = exp_num_visits1 + gamma(:,1); Daniel@0: exp_num_visitsT = exp_num_visitsT + gamma(:,T); Daniel@0: % loop over whichever is shorter Daniel@0: if T < O Daniel@0: for t=1:T Daniel@0: o = obs(t); Daniel@0: exp_num_emit(:,o) = exp_num_emit(:,o) + gamma(:,t); Daniel@0: end Daniel@0: else Daniel@0: for o=1:O Daniel@0: ndx = find(obs==o); Daniel@0: if ~isempty(ndx) Daniel@0: exp_num_emit(:,o) = exp_num_emit(:,o) + sum(gamma(:, ndx), 2); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end