about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/HMM/transmat_train_observed.m
blob: 0691842963632da96914d671e6d263789e317f0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function [transmat, initState] = transmat_train_observed(labels,  nstates, varargin)
% transmat_train_observed ML estimation from fully observed data
% function [transmat, initState] = transmat_train_observed(labels,  nstates, varargin)
%
% If all sequences have the same length
% labels(ex,t)
% If sequences have different lengths, we use cell arrays
% labels{ex}(t)

[dirichletPriorWeight, mkSymmetric, other] = process_options(...
    varargin, 'dirichletPriorWeight', 0, 'mkSymmetric', 0);

if ~iscell(labels)
  [numex T] = size(labels);
  if T==1
    labels = labels';
  end
  %fprintf('T=%d, numex=%d\n', T, numex);
  labels = num2cell(labels,2); % each row gets its own cell
end
numex = length(labels);

counts = zeros(nstates, nstates);
counts1 = zeros(nstates,1);
for s=1:numex
  labs = labels{s}; labs = labs(:)';
  dat = [labs(1:end-1); labs(2:end)];
  counts = counts + compute_counts(dat, [nstates nstates]);
  q = labs(1);
  counts1(q) = counts1(q) + 1;
end
pseudo_counts = dirichletPriorWeight*ones(nstates, nstates);
if mkSymmetric
  counts = counts + counts';
end
transmat = mk_stochastic(counts + pseudo_counts);
initState = normalize(counts1 + dirichletPriorWeight*ones(nstates,1));