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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/**
* @file test_gensvm_predict.c
* @author G.J.J. van den Burg
* @date 2016-09-01
* @brief Unit tests for gensvm_predict.c functions
*
* @copyright
Copyright 2016, G.J.J. van den Burg.
This file is part of GenSVM.
GenSVM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GenSVM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GenSVM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "minunit.h"
#include "gensvm_predict.h"
/**
* This testcase is designed as follows: 12 evenly spaced points are plotted
* on the unit circle in the simplex space. These points are the ones for
* which we want to predict the class. To get these points, we need a Z and a
* V which map to these points. To get this, Z was equal to [1 Q] and V was
* equal to [0; R] where Q and R are from the reduced QR decomposition of the
* 12x2 matrix S which contains the points in simplex space. Here's the
* Matlab/Octave code to generate this data:
*
* n = 12;
* K = 3;
* S = [cos(1/12*pi+1/6*pi*[0:(n-1)])', sin(1/12*pi+1/6*pi*[0:(n-1)])'];
* [Q, R] = qr(S, '0');
* Z = [ones(n, 1), Q];
* V = [zeros(1, K-1); R];
*
*/
char *test_gensvm_predict_labels_dense()
{
int n = 12;
int m = 2;
int K = 3;
struct GenData *data = gensvm_init_data();
struct GenModel *model = gensvm_init_model();
model->n = n;
model->m = m;
model->K = K;
data->n = n;
data->m = m;
data->r = m;
data->K = K;
data->Z = Calloc(double, n*(m+1));
data->y = Calloc(long, n);
matrix_set(data->Z, m+1, 0, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 0, 1, -0.3943375672974065);
matrix_set(data->Z, m+1, 0, 2, -0.1056624327025935);
matrix_set(data->Z, m+1, 1, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 1, 1, -0.2886751345948129);
matrix_set(data->Z, m+1, 1, 2, -0.2886751345948128);
matrix_set(data->Z, m+1, 2, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 2, 1, -0.1056624327025937);
matrix_set(data->Z, m+1, 2, 2, -0.3943375672974063);
matrix_set(data->Z, m+1, 3, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 3, 1, 0.1056624327025935);
matrix_set(data->Z, m+1, 3, 2, -0.3943375672974064);
matrix_set(data->Z, m+1, 4, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 4, 1, 0.2886751345948129);
matrix_set(data->Z, m+1, 4, 2, -0.2886751345948129);
matrix_set(data->Z, m+1, 5, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 5, 1, 0.3943375672974064);
matrix_set(data->Z, m+1, 5, 2, -0.1056624327025937);
matrix_set(data->Z, m+1, 6, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 6, 1, 0.3943375672974065);
matrix_set(data->Z, m+1, 6, 2, 0.1056624327025935);
matrix_set(data->Z, m+1, 7, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 7, 1, 0.2886751345948130);
matrix_set(data->Z, m+1, 7, 2, 0.2886751345948128);
matrix_set(data->Z, m+1, 8, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 8, 1, 0.1056624327025939);
matrix_set(data->Z, m+1, 8, 2, 0.3943375672974063);
matrix_set(data->Z, m+1, 9, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 9, 1, -0.1056624327025934);
matrix_set(data->Z, m+1, 9, 2, 0.3943375672974064);
matrix_set(data->Z, m+1, 10, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 10, 1, -0.2886751345948126);
matrix_set(data->Z, m+1, 10, 2, 0.2886751345948132);
matrix_set(data->Z, m+1, 11, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 11, 1, -0.3943375672974064);
matrix_set(data->Z, m+1, 11, 2, 0.1056624327025939);
gensvm_allocate_model(model);
matrix_set(model->V, K-1, 0, 0, 0.0000000000000000);
matrix_set(model->V, K-1, 0, 1, 0.0000000000000000);
matrix_set(model->V, K-1, 1, 0, -2.4494897427831779);
matrix_set(model->V, K-1, 1, 1, -0.0000000000000002);
matrix_set(model->V, K-1, 2, 0, 0.0000000000000000);
matrix_set(model->V, K-1, 2, 1, -2.4494897427831783);
// start test code
long *predy = Calloc(long, n);
gensvm_predict_labels(data, model, predy);
mu_assert(predy[0] == 2, "Incorrect label at index 0");
mu_assert(predy[1] == 3, "Incorrect label at index 1");
mu_assert(predy[2] == 3, "Incorrect label at index 2");
mu_assert(predy[3] == 3, "Incorrect label at index 3");
mu_assert(predy[4] == 3, "Incorrect label at index 4");
mu_assert(predy[5] == 1, "Incorrect label at index 5");
mu_assert(predy[6] == 1, "Incorrect label at index 6");
mu_assert(predy[7] == 1, "Incorrect label at index 7");
mu_assert(predy[8] == 1, "Incorrect label at index 8");
mu_assert(predy[9] == 2, "Incorrect label at index 9");
mu_assert(predy[10] == 2, "Incorrect label at index 10");
mu_assert(predy[11] == 2, "Incorrect label at index 11");
// end test code
gensvm_free_data(data);
gensvm_free_model(model);
free(predy);
return NULL;
}
char *test_gensvm_predict_labels_sparse()
{
int n = 12;
int m = 2;
int K = 3;
struct GenData *data = gensvm_init_data();
struct GenModel *model = gensvm_init_model();
model->n = n;
model->m = m;
model->K = K;
data->n = n;
data->m = m;
data->r = m;
data->K = K;
data->Z = Calloc(double, n*(m+1));
data->y = Calloc(long, n);
matrix_set(data->Z, m+1, 0, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 0, 1, -0.3943375672974065);
matrix_set(data->Z, m+1, 0, 2, -0.1056624327025935);
matrix_set(data->Z, m+1, 1, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 1, 1, -0.2886751345948129);
matrix_set(data->Z, m+1, 1, 2, -0.2886751345948128);
matrix_set(data->Z, m+1, 2, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 2, 1, -0.1056624327025937);
matrix_set(data->Z, m+1, 2, 2, -0.3943375672974063);
matrix_set(data->Z, m+1, 3, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 3, 1, 0.1056624327025935);
matrix_set(data->Z, m+1, 3, 2, -0.3943375672974064);
matrix_set(data->Z, m+1, 4, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 4, 1, 0.2886751345948129);
matrix_set(data->Z, m+1, 4, 2, -0.2886751345948129);
matrix_set(data->Z, m+1, 5, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 5, 1, 0.3943375672974064);
matrix_set(data->Z, m+1, 5, 2, -0.1056624327025937);
matrix_set(data->Z, m+1, 6, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 6, 1, 0.3943375672974065);
matrix_set(data->Z, m+1, 6, 2, 0.1056624327025935);
matrix_set(data->Z, m+1, 7, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 7, 1, 0.2886751345948130);
matrix_set(data->Z, m+1, 7, 2, 0.2886751345948128);
matrix_set(data->Z, m+1, 8, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 8, 1, 0.1056624327025939);
matrix_set(data->Z, m+1, 8, 2, 0.3943375672974063);
matrix_set(data->Z, m+1, 9, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 9, 1, -0.1056624327025934);
matrix_set(data->Z, m+1, 9, 2, 0.3943375672974064);
matrix_set(data->Z, m+1, 10, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 10, 1, -0.2886751345948126);
matrix_set(data->Z, m+1, 10, 2, 0.2886751345948132);
matrix_set(data->Z, m+1, 11, 0, 1.0000000000000000);
matrix_set(data->Z, m+1, 11, 1, -0.3943375672974064);
matrix_set(data->Z, m+1, 11, 2, 0.1056624327025939);
// convert Z to a sparse matrix to test the sparse functions
data->spZ = gensvm_dense_to_sparse(data->Z, data->n, data->m+1);
free(data->Z);
data->RAW = NULL;
data->Z = NULL;
gensvm_allocate_model(model);
matrix_set(model->V, K-1, 0, 0, 0.0000000000000000);
matrix_set(model->V, K-1, 0, 1, 0.0000000000000000);
matrix_set(model->V, K-1, 1, 0, -2.4494897427831779);
matrix_set(model->V, K-1, 1, 1, -0.0000000000000002);
matrix_set(model->V, K-1, 2, 0, 0.0000000000000000);
matrix_set(model->V, K-1, 2, 1, -2.4494897427831783);
// start test code
long *predy = Calloc(long, n);
gensvm_predict_labels(data, model, predy);
mu_assert(predy[0] == 2, "Incorrect label at index 0");
mu_assert(predy[1] == 3, "Incorrect label at index 1");
mu_assert(predy[2] == 3, "Incorrect label at index 2");
mu_assert(predy[3] == 3, "Incorrect label at index 3");
mu_assert(predy[4] == 3, "Incorrect label at index 4");
mu_assert(predy[5] == 1, "Incorrect label at index 5");
mu_assert(predy[6] == 1, "Incorrect label at index 6");
mu_assert(predy[7] == 1, "Incorrect label at index 7");
mu_assert(predy[8] == 1, "Incorrect label at index 8");
mu_assert(predy[9] == 2, "Incorrect label at index 9");
mu_assert(predy[10] == 2, "Incorrect label at index 10");
mu_assert(predy[11] == 2, "Incorrect label at index 11");
// end test code
gensvm_free_data(data);
gensvm_free_model(model);
free(predy);
return NULL;
}
char *test_gensvm_prediction_perf()
{
int i, n = 8;
struct GenData *data = gensvm_init_data();
data->n = n;
data->y = Calloc(long, n);
data->y[0] = 1;
data->y[1] = 1;
data->y[2] = 1;
data->y[3] = 1;
data->y[4] = 2;
data->y[5] = 2;
data->y[6] = 2;
data->y[7] = 3;
long *y = Calloc(long, n);
for (i=0; i<n; i++)
y[i] = 1;
mu_assert(gensvm_prediction_perf(data, y) == 50.0,
"Incorrect first time.");
for (i=0; i<n; i++)
y[i] = 2;
mu_assert(gensvm_prediction_perf(data, y) == 37.5,
"Incorrect second time.");
for (i=0; i<n; i++)
y[i] = 3;
mu_assert(gensvm_prediction_perf(data, y) == 12.5,
"Incorrect third time.");
free(y);
gensvm_free_data(data);
return NULL;
}
char *all_tests()
{
mu_suite_start();
mu_run_test(test_gensvm_predict_labels_dense);
mu_run_test(test_gensvm_predict_labels_sparse);
mu_run_test(test_gensvm_prediction_perf);
return NULL;
}
RUN_TESTS(all_tests);
|