wolffd@0: function [transmat, initState] = transmat_train_observed(labels, nstates, varargin) wolffd@0: % transmat_train_observed ML estimation from fully observed data wolffd@0: % function [transmat, initState] = transmat_train_observed(labels, nstates, varargin) wolffd@0: % wolffd@0: % If all sequences have the same length wolffd@0: % labels(ex,t) wolffd@0: % If sequences have different lengths, we use cell arrays wolffd@0: % labels{ex}(t) wolffd@0: wolffd@0: [dirichletPriorWeight, mkSymmetric, other] = process_options(... wolffd@0: varargin, 'dirichletPriorWeight', 0, 'mkSymmetric', 0); wolffd@0: wolffd@0: if ~iscell(labels) wolffd@0: [numex T] = size(labels); wolffd@0: if T==1 wolffd@0: labels = labels'; wolffd@0: end wolffd@0: %fprintf('T=%d, numex=%d\n', T, numex); wolffd@0: labels = num2cell(labels,2); % each row gets its own cell wolffd@0: end wolffd@0: numex = length(labels); wolffd@0: wolffd@0: counts = zeros(nstates, nstates); wolffd@0: counts1 = zeros(nstates,1); wolffd@0: for s=1:numex wolffd@0: labs = labels{s}; labs = labs(:)'; wolffd@0: dat = [labs(1:end-1); labs(2:end)]; wolffd@0: counts = counts + compute_counts(dat, [nstates nstates]); wolffd@0: q = labs(1); wolffd@0: counts1(q) = counts1(q) + 1; wolffd@0: end wolffd@0: pseudo_counts = dirichletPriorWeight*ones(nstates, nstates); wolffd@0: if mkSymmetric wolffd@0: counts = counts + counts'; wolffd@0: end wolffd@0: transmat = mk_stochastic(counts + pseudo_counts); wolffd@0: initState = normalize(counts1 + dirichletPriorWeight*ones(nstates,1)); wolffd@0: wolffd@0: