|
| 1 | +function outputHMM = analyseFactorialHMM(inputHMM) |
| 2 | + |
| 3 | + %%%% outputHMM = runEM(inputHMM); |
| 4 | + O = inputHMM.O; |
| 5 | + libraries = inputHMM.libraries; |
| 6 | + num_components = inputHMM.num_components; |
| 7 | + |
| 8 | + % starting values |
| 9 | + ScalingHybrid = 1; |
| 10 | + Sigma = (max(O(:)) - min(O(:)))/2/(inputHMM.num_components); |
| 11 | + Init = ones(1,inputHMM.num_components)/(inputHMM.num_components); |
| 12 | + A = ones(inputHMM.num_components,inputHMM.num_components)/(inputHMM.num_components); |
| 13 | + |
| 14 | + cyc = 1000; |
| 15 | + tol = 10^(-12); |
| 16 | + |
| 17 | + H_viterbi = []; |
| 18 | + LL_viterbi = 0; |
| 19 | + coordinates_T = inputHMM.coordinates_T; |
| 20 | + outputHMM = outputStatSTEM_HMM(ScalingHybrid,Sigma,Init,A,H_viterbi,LL_viterbi,coordinates_T); %inputHMMect uit die klasse aanmaken eerst |
| 21 | + |
| 22 | + loglikelihood = -Inf; |
| 23 | + |
| 24 | + if isempty(inputHMM.GUI) |
| 25 | + outputHMM.GUI = inputHMM.GUI; |
| 26 | + end |
| 27 | + if ~isempty(inputHMM.GUI) |
| 28 | + inputHMM.waitbar.setValue(0) |
| 29 | + else |
| 30 | + h_bar = waitbar(0,'Fitting... '); |
| 31 | + end |
| 32 | + |
| 33 | + |
| 34 | + for cycle=1:cyc |
| 35 | + oldlikelihood = loglikelihood; |
| 36 | + |
| 37 | + [gamma,xi,loglikelihood] = runEstep(O,libraries,num_components,ScalingHybrid,Sigma,Init,A); |
| 38 | + [A,Init,Sigma,ScalingHybrid] = runMstep(O,libraries,gamma,xi); |
| 39 | + |
| 40 | + check = checkConvergence(oldlikelihood,loglikelihood,tol,cycle); |
| 41 | + if check == 1 || cycle == cyc |
| 42 | + outputHMM.maxLogLikelihood = loglikelihood; |
| 43 | + outputHMM.A = A; |
| 44 | + outputHMM.Sigma = Sigma; |
| 45 | + outputHMM.Init = Init; |
| 46 | + outputHMM.ScalingHybrid = ScalingHybrid; |
| 47 | + break; |
| 48 | + end |
| 49 | + |
| 50 | + % Update waitbar |
| 51 | + if ~isempty(inputHMM.GUI) |
| 52 | + inputHMM.waitbar.setValue(cycle/cyc*100) |
| 53 | + else |
| 54 | + waitbar(cycle/cyc,h_bar,'Fitting ...') |
| 55 | + end |
| 56 | + |
| 57 | + end |
| 58 | + % Update waitbar |
| 59 | + if ~isempty(inputHMM.GUI) |
| 60 | + inputHMM.waitbar.setValue(95) |
| 61 | + else |
| 62 | + delete(h_bar) |
| 63 | + end |
| 64 | + %%%% outputHMM = Viterbi(inputHMM,outputHMM); |
| 65 | + T = size(O,1); |
| 66 | + N = size(O,2); |
| 67 | + |
| 68 | + delta = zeros(T,num_components,N); |
| 69 | + delta_all = zeros(T,num_components,num_components,N); |
| 70 | + arg = zeros(T,num_components,N); |
| 71 | + H = zeros(T,N); |
| 72 | + LL = zeros(1,N); |
| 73 | + |
| 74 | + Gauss = calculateEmissionProbability(O,libraries,num_components,ScalingHybrid,Sigma); |
| 75 | + |
| 76 | + % initialise best score and argument array |
| 77 | + delta(1,:,:) = Init.*reshape(Gauss(1,:,:),N,num_components)'; |
| 78 | + arg(1,:,:) = 0; |
| 79 | + |
| 80 | + % recursion |
| 81 | + for t=2:T |
| 82 | + for n = 1:N |
| 83 | + for i_g = 1:(num_components) |
| 84 | + delta_all(t,:,i_g,n) = delta(t-1,:,n).*A(:,i_g)'; |
| 85 | + [delta(t,i_g,n),arg(t,i_g,n)] = max(reshape(delta_all(t,:,i_g,n),num_components,1)); |
| 86 | + delta(t,i_g,n) = delta(t,i_g,n)*Gauss(t,n,i_g); |
| 87 | + end |
| 88 | + delta(t,:,n) = delta(t,:,n)/sum(delta(t,:,n)); %normalisatie owv computationele redenen! |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + % termination step at time T |
| 93 | + for n = 1:N |
| 94 | + [LL(n), H(T,n)] = max(delta(T,:,n)); %nog steeds LL door normalisatie owv computationele redenen? |
| 95 | + end |
| 96 | + LL = prod(LL); |
| 97 | + |
| 98 | + % path backtracking |
| 99 | + for t = T-1:-1:1 |
| 100 | + for n = 1:N |
| 101 | + H(t,n) = arg(t+1,H(t+1,n),n); |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + if libraries(1) == 0 |
| 106 | + outputHMM.H_viterbi = H - 1; |
| 107 | + else |
| 108 | + outputHMM.H_viterbi = H; |
| 109 | + end |
| 110 | + |
| 111 | + outputHMM.LL_viterbi = LL; |
| 112 | + |
| 113 | +end |
0 commit comments