Final commit
[GMM_FEL.git] / crossvalidation.m
blob362198f7b370c6f3b11fd55aeb45de3e026338c5
1 % Crossvalidation to estimate the best parameters
3 clear all;
5 %% common part
7 FEATURE_EXTRACTION_METHOD = 2;
9 disp('Reading the data...');
10 read_data; % has to be configured first - see the code
12 % Set the range of potential boundary height values
13 min_height = 1;
14 max_height = size(Visuals{1}, 1);
16 disp('Extracting features...');
17 Features = cell(size(Visuals));
18 for i=1:length(Features)
19         Features{i} = extract_features(Visuals{i}, FEATURE_EXTRACTION_METHOD);
20 end
22 disp('Generating the hashtable for all possible feature vectors... (this may take a while)');
23 X_ht = get_all_possible_x(FEATURE_EXTRACTION_METHOD, min_height:max_height);
27 %% <--
29 %% the crossvalidation part
30 kernel_types = {'gaussian', 'flat'};
31 kernel_widths = [0.02 0.1 0.5 1 2 4 7];
32 hmm_fb_methods = {'mean', 'map'};
34 for kt = 1:length(kernel_types)
35         for kw = 1:length(kernel_widths)
36                 for fbm = 1:length(hmm_fb_methods)
37                         fprintf(1, '====== Iteration for parameters (%s, %i, %s) ======\n', kernel_types{kt}, kernel_widths(kw), hmm_fb_methods{fbm});
38                         disp('Computing model parameters...');
39                         clear s S X;
40                         [s, S, X] = get_probabilities(Features, Boundary1, X_ht, min_height, max_height, FEATURE_EXTRACTION_METHOD, kernel_types{kt}, kernel_widths(kw));
42                         % Inference:
43                         fim = Features{1};
44                         true_sequence = Boundary1{1};
47                         % create emission probabilities matrix as requested by
48                         % hmm_viterbi()
49                         X_viterbi = zeros(size(fim, 2), length(s));
50                         for i=1:size(X_viterbi, 1)
51                                 fid = num2str(fim(:, i)'); % convert the feature to string -> feature id
52                                 X_viterbi(i, :) = X(X_ht.get(fid), :); % get this features' P(feature|state) for all states 
53                         end
54                         % <--
55                         disp('Inferring boundary 1...');
56                         inferred_sequence = hmm_forward_backward(s, S', X_viterbi, hmm_fb_methods{fbm});
57                         
58                         true_sequence_cleaned = true_sequence(true_sequence > 1); % remove missing data
59                         inferred_sequence_cleaned = inferred_sequence(true_sequence > 1);
61                         dist = sum(norm(inferred_sequence_cleaned - true_sequence_cleaned'));
62                         fprintf(1, '====== Total euclidean distance between the true and the predicted sequence is: %f\n', dist);
63                 end
64         end
65 end