aboutsummaryrefslogtreecommitdiff
path: root/src/gensvm_cross_validation.c
blob: d17443b2c61fc773bbff44d9cf42d71bd74733ea (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
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
/**
 * @file gensvm_cross_validation.c
 * @author G.J.J. van den Burg
 * @date 2016-10-24
 * @brief Function for running cross validation on GenModel
 *
 * @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 "gensvm_cross_validation.h"

extern FILE *GENSVM_OUTPUT_FILE;

/**
 * @brief Run cross validation with a given set of train/test folds
 *
 * @details
 * This cross validation function uses predefined train/test splits. Also, the
 * the optimal parameters GenModel::V of a previous fold as initial conditions
 * for GenModel::V of the next fold.
 *
 * @note
 * This function always sets the output stream defined in GENSVM_OUTPUT_FILE
 * to NULL, to ensure gensvm_optimize() doesn't print too much.
 *
 * @param[in] 	model 		GenModel with the configuration to train
 * @param[in] 	train_folds 	array of training datasets
 * @param[in] 	test_folds 	array of test datasets
 * @param[in] 	folds 		number of folds
 * @param[in] 	n_total 	number of objects in the union of the train
 * 				datasets
 * @return 			performance (hitrate) of the configuration on
 * 				cross validation
 */
double gensvm_cross_validation(struct GenModel *model,
		struct GenData **train_folds, struct GenData **test_folds,
		long folds, long n_total)
{
	long f;
	long *predy = NULL;
	double performance, total_perf = 0;

	// make sure that gensvm_optimize() is silent.
	FILE *fid = GENSVM_OUTPUT_FILE;
	GENSVM_OUTPUT_FILE = NULL;

	// run cross-validation
	for (f=0; f<folds; f++) {
		// reallocate model in case dimensions differ with data
		gensvm_reallocate_model(model, train_folds[f]->n,
				train_folds[f]->r);

		// initialize object weights
		gensvm_initialize_weights(train_folds[f], model);

		// train the model (surpressing output)
		gensvm_optimize(model, train_folds[f]);

		// calculate prediction performance on test set
		predy = Calloc(long, test_folds[f]->n);
		gensvm_predict_labels(test_folds[f], model, predy);
		performance = gensvm_prediction_perf(test_folds[f], predy);
		total_perf += performance * test_folds[f]->n;

		free(predy);
	}

	total_perf /= ((double) n_total);

	// reset the output stream
	GENSVM_OUTPUT_FILE = fid;

	return total_perf;
}