2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation. //
7 // This program is distributed in the hope that it will be useful, but //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
10 // General Public License for more details. //
12 // You should have received a copy of the GNU General Public License //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
15 // Written by Francois Fleuret //
16 // (C) Idiap Research Institute //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
19 ///////////////////////////////////////////////////////////////////////////
23 #include "fusion_sort.h"
25 scalar_t robust_sampling(int nb, scalar_t *weights, int nb_to_sample, int *sampled) {
28 for(int k = 0; k < nb_to_sample; k++) sampled[k] = 0;
31 scalar_t *pair_weights = new scalar_t[(nb+1)/2];
32 for(int k = 0; k < nb/2; k++)
33 pair_weights[k] = weights[2 * k] + weights[2 * k + 1];
35 pair_weights[(nb+1)/2 - 1] = weights[nb-1];
36 scalar_t result = robust_sampling((nb+1)/2, pair_weights, nb_to_sample, sampled);
37 for(int k = 0; k < nb_to_sample; k++) {
39 // There is a bit of a trick for the isolated sample in the odd
40 // case. Since the corresponding pair weight is the same as the
41 // one sample alone, the test is always true and the isolated
42 // sample will be taken for sure.
43 if(drand48() * pair_weights[s] <= weights[2 * s])
46 sampled[k] = 2 * s + 1;
48 delete[] pair_weights;
53 void print_roc_small_pos(ostream *out,
54 int nb_pos, scalar_t *pos_responses,
55 int nb_neg, scalar_t *neg_responses,
56 scalar_t fas_factor) {
58 scalar_t *sorted_pos_responses = new scalar_t[nb_pos];
60 fusion_sort(nb_pos, pos_responses, sorted_pos_responses);
62 int *bins = new int[nb_pos + 1];
63 for(int k = 0; k <= nb_pos; k++) bins[k] = 0;
65 for(int k = 0; k < nb_neg; k++) {
66 scalar_t r = neg_responses[k];
68 if(r < sorted_pos_responses[0])
71 else if(r >= sorted_pos_responses[nb_pos - 1])
81 if(r < sorted_pos_responses[c])
87 // Beware of identical positive responses
88 while(c < nb_pos && r >= sorted_pos_responses[c])
96 for(int k = 0; k < nb_pos; k++) {
98 if(k == 0 || sorted_pos_responses[k-1] < sorted_pos_responses[k]) {
99 (*out) << (scalar_t(s) / scalar_t(nb_neg)) * fas_factor
101 << scalar_t(nb_pos - k)/scalar_t(nb_pos)
103 << sorted_pos_responses[k]
109 delete[] sorted_pos_responses;