-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindNumberOfGMMComponents.m
182 lines (159 loc) · 4.81 KB
/
findNumberOfGMMComponents.m
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
%%
% Find the optimal number of GMM components for the modelisation of experimental walk
%
% The lowest pointy in the score differential diagram indicates the
% selected number of components
%
% The problem is identified at: https://towardsdatascience.com/gaussian-mixture-model-clusterization-how-to-select-the-number-of-components-clusters-553bef45f6e4
%%%
%% Initialisation
%%
clear;
close all;
clc;
database=load('steps_database').database_passi; % load database
database = clearDb(database); % Clear database
[time,force, x_coord, y_coord] = retrieveAllVariables(database); % Retrieve forces, times and coordinates
[X, ~, ~, ~, ~] = computeAllDesiredVariables(force, time, x_coord, y_coord); % Extract Dt, meanf, len, angle
clear database time force x_coord y_coord;
%% Multivariate GMM
%% Single subject
subj = randi(125); % random subject
maxComp = 15; % maximum components
tries = 20;
Xtemp = X(:,1:3,subj);
Xtemp = Xtemp(any(~isnan(Xtemp), 2), :);
% Plot AIC/BIC for a variety of components and tries
figure;
t = tiledlayout(5,4,'TileSpacing','Compact');
for i=1:tries
for comp=1:maxComp % Components of GMM
GMModel = fitgmdist (Xtemp, comp, 'Options', statset('MaxIter', 1500), 'SharedCovariance', true);
AIC(i, comp)= GMModel.AIC;
BIC(i, comp)= GMModel.BIC;
end
nexttile;
plot(BIC(i,:), '.-', 'DisplayName','BIC')
hold on;
plot(AIC(i,:), 'o-', 'DisplayName','AIC')
hold off;
lg = legend;
end
title(t,'GMMs fitted on a random pedestrian')
xlabel(t, 'Components')
ylabel(t, 'AIC/BIC')
% Plot mean index evaluation diagram
meanBIC = sum(BIC,1)/tries;
meanAIC = sum(AIC,1)/tries;
figure;
plot(meanBIC, '.-', 'DisplayName','BIC')
title('Fitting a GMM on a random pedestrian - Mean histogram')
xlabel('Components')
ylabel('AIC/BIC')
hold on;
plot(meanAIC, 'o-', 'DisplayName','AIC')
hold off;
lgd = legend;
% Plot socre differentials
dBIC = diff(meanBIC);
dAIC = diff(meanAIC);
figure;
plot([dBIC(1) dBIC], '.-', 'DisplayName','BIC')
title('Fitting a GMM on a random pedestrian – Score intervals')
xlabel('Components')
ylabel('AIC/BIC interval (current to previous)')
hold on;
plot([dAIC(1) dAIC], 'o-', 'DisplayName','AIC')
hold off;
lgd = legend;
%% 2nd option: Put all steps into a unified walk table
% Xtotal is a matrix with all steps performed
temp = X(:,1:3,1);
Xtotal = temp(any(~isnan(temp), 2), :);
for i=2:size(X,3)
temp = X(:,1:3,i);
Xtotal = [Xtotal; temp(any(~isnan(temp), 2), :)];
end
maxComp = 15; % maximum components
tries = 12; % number of modelisation tries
% Plot AIC/BIC for a variety of components and tries
figure;
t = tiledlayout(3,4,'TileSpacing','Compact');
for i=1:tries
for comp=1:maxComp % Components of GMM
GMModel = fitgmdist (Xtotal, comp, 'Options', statset('MaxIter', 1500), 'SharedCovariance', true);
AIC(i,comp)= GMModel.AIC;
BIC(i,comp)= GMModel.BIC;
end
nexttile;
plot(BIC(i,:), '.-', 'DisplayName','BIC')
hold on;
plot(AIC(i,:), '.-', 'DisplayName','AIC')
hold off;
lg = legend;
end
title(t,'GMMs fitted on the unified table')
xlabel(t, 'Components')
ylabel(t, 'AIC/BIC')
% Plot mean index evaluation diagram
meanBIC = sum(BIC,1)/tries;
meanAIC = sum(AIC,1)/tries;
figure;
plot(meanBIC, '.-', 'DisplayName','BIC')
title('Fitting a GMM on the unified table - Mean histogram')
xlabel('Components')
ylabel('AIC/BIC')
hold on;
plot(meanAIC, 'o-', 'DisplayName','AIC')
hold off;
lgd = legend;
% Plot socre differentials
dBIC = diff(meanBIC);
dAIC = diff(meanAIC);
figure;
plot([dBIC(1) dBIC], '.-', 'DisplayName','BIC')
title('Fitting a GMM on the unified table – Score intervals')
xlabel('Components')
ylabel('AIC/BIC interval (current to previous)')
hold on;
plot([dAIC(1) dAIC], 'o-', 'DisplayName','AIC')
hold off;
lgd = legend;
%% Angle
% Single subject
subj = randi(125); % subject
maxComp = 15; % maximum components
tries = 20;
% Plot AIC/BIC for a variety of components and tries
Xtemp = X(:,4,subj);
Xtemp = Xtemp(any(~isnan(Xtemp), 2), :);
figure;
t = tiledlayout(5,4,'TileSpacing','Compact');
for i=1:tries
for comp=1:maxComp % Components of GMM
GMModel = fitgmdist (Xtemp, comp, 'Options', statset('MaxIter', 1500), 'SharedCovariance', true);
AIC(i, comp)= GMModel.AIC;
BIC(i, comp)= GMModel.BIC;
end
nexttile;
plot(BIC(i,:), '.-', 'DisplayName','BIC')
hold on;
plot(AIC(i,:), 'o-', 'DisplayName','AIC')
hold off;
lg = legend;
end
title(t,'GMMs fitted on a random pedestrian')
xlabel(t, 'Components')
ylabel(t, 'AIC/BIC')
% Plot mean index evaluation diagram
meanBIC = sum(BIC,1)/tries;
meanAIC = sum(AIC,1)/tries;
figure;
plot(meanBIC, '.-', 'DisplayName','BIC')
title('Fitting a GMM on a random pedestrian - Mean histogram')
xlabel('Components')
ylabel('AIC/BIC')
hold on;
plot(meanAIC, 'o-', 'DisplayName','AIC')
hold off;
lgd = legend;