blib/lib/AI/Calibrate.pm

filter: Statementstmt 96.5% 84 / 87 Branchbran 93.7% 15 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 92.3% 12 / 13 Podpod 50.0% 3 / 6 totaltotal 93.7% 119 / 127 CC 16 SCAR 12.3
CC
16
Cov
96%
print_vector
6.0
calibrate
5.0
PAV
5.0
CRAP 3.4
↑ Summary
1 package AI::Calibrate;
2  
3 3 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 $VERSION = "1.5";
9  
10 3 require Exporter;
11  
12 3 our @ISA = qw(Exporter);
13  
14 # This allows declaration:
15 # use AI::Calibrate ':all';
16 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
17 # will save memory.
18 3 our %EXPORT_TAGS = (
19 'all' => [
20 qw(
21 calibrate
22 score_prob
23 print_mapping
24 )
25 ]
26 );
27  
28 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
29 3 our @EXPORT = qw( );
30  
31 3 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # Structure slot names
34 3 ▶use constant SCORE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use constant PROB => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 =head1 NAME
38  
39 AI::Calibrate - Perl module for producing probabilities from classifier scores
40  
41 =head1 SYNOPSIS
42  
43 use AI::Calibrate ':all';
44 ... train a classifier ...
45 ... test classifier on $points ...
46 $calibrated = calibrate($points);
47  
48 =head1 DESCRIPTION
49  
50 Classifiers usually return some sort of an instance score with their
51 classifications. These scores can be used as probabilities in various
52 calculations, but first they need to be I<calibrated>. Naive Bayes, for
53 example, is a very useful classifier, but the scores it produces are usually
54 "bunched" around 0 and 1, making these scores poor probability estimates.
55 Support vector machines have a similar problem. Both classifier types should
56 be calibrated before their scores are used as probability estimates.
57  
58 This module calibrates classifier scores using a method called the Pool
59 Adjacent Violators (PAV) algorithm. After you train a classifier, you take a
60 (usually separate) set of test instances and run them through the classifier,
61 collecting the scores assigned to each. You then supply this set of instances
62 to the calibrate function defined here, and it will return a set of ranges
63 mapping from a score range to a probability estimate.
64  
65 For example, assume you have the following set of instance results from your
66 classifier. Each result is of the form C<[ASSIGNED_SCORE, TRUE_CLASS]>:
67  
68 my $points = [
69 [.9, 1],
70 [.8, 1],
71 [.7, 0],
72 [.6, 1],
73 [.55, 1],
74 [.5, 1],
75 [.45, 0],
76 [.4, 1],
77 [.35, 1],
78 [.3, 0 ],
79 [.27, 1],
80 [.2, 0 ],
81 [.18, 0],
82 [.1, 1 ],
83 [.02, 0]
84 ];
85  
86 If you then call calibrate($points), it will return this structure:
87  
88 [
89 [.9, 1 ],
90 [.7, 3/4 ],
91 [.45, 2/3 ],
92 [.3, 1/2 ],
93 [.2, 1/3 ],
94 [.02, 0 ]
95 ]
96  
97 This means that, given a SCORE produced by the classifier, you can map the
98 SCORE onto a probability like this:
99  
100 SCORE >= .9 prob = 1
101 .9 > SCORE >= .7 prob = 3/4
102 .7 > SCORE >= .45 prob = 2/3
103 .45 > SCORE >= .3 prob = 3/4
104 .2 > SCORE >= .7 prob = 3/4
105 .02 > SCORE prob = 0
106  
107 For a realistic example of classifier calibration, see the test file
108 t/AI-Calibrate-NB.t, which uses the AI::NaiveBayes1 module to train a Naive
109 Bayes classifier then calibrates it using this module.
110  
111 =cut
112  
113 =head1 FUNCTIONS
114  
115 =over 4
116  
117 =item B<calibrate>
118  
119 This is the main calibration function. The calling form is:
120  
121 my $calibrated = calibrate( $data, $sorted);
122  
123 $data looks like: C<[ [score, class], [score, class], [score, class]...]>
124 Each score is a number. Each class is either 0 (negative class) or 1
125 (positive class).
126  
127 $sorted is boolean (0 by default) indicating whether the data are already
128 sorted by score. Unless this is set to 1, calibrate() will sort the data
129 itself.
130  
131 Calibrate returns a reference to an ordered list of references:
132  
133 [ [score, prob], [score, prob], [score, prob] ... ]
134  
135 Scores will be in descending numerical order. See the DESCRIPTION section for
136 how this structure is interpreted. You can pass this structure to the
137 B<score_prob> function, along with a new score, to get a probability.
138  
139 =cut
140  
141 27 ▶sub calibrate {
Subroutine calibrate: called CC=5 SCAR=16.1
Pod documented
142 27 my($data, $sorted) = @_;
143  
144 27 if (DEBUG) {
145 print "Original data:\n";
146 for my $pair (@$data) {
147 my($score, $prob) = @$pair;
148 print "($score, $prob)\n";
149 }
150 }
151  
152 # Copy the data over so PAV can clobber the PROB field
153 27 my $new_data = [ map([@$_], @$data) ];
154  
155 # If not already sorted, sort data decreasing by score
156 27 ▶ if (!$sorted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sorted) 25 2 27
157 25 $new_data = [ sort { $b->[SCORE] <=> $a->[SCORE] } @$new_data ];
158 }
159  
160 27 PAV($new_data);
161  
162 27 if (DEBUG) {
163 print("After PAV, vector is:\n");
164 print_vector($new_data);
165 }
166  
167 27 my(@result);
168 27 my( $last_prob, $last_score);
169  
170 27 push(@$new_data, [-1e10, 0]);
171  
172 27 for my $pair (@$new_data) {
173 382 print "Seeing @$pair\n" if DEBUG;
174 382 my($score, $prob) = @$pair;
175 382 ▶ if (defined($last_prob) and $prob < $last_prob) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_prob and $prob < $last_prob) 114 268 382
Condition100%3/3
defined $last_prob and $prob < $last_prob
!ll&&!rl&&r
27241114
MC/DC100%2/2
defined $last_prob and $prob < $last_prob
defined $last_prob $prob < $last_prob
Truth table100%3/3
A and B
A= defined $last_probB= $prob < $last_prob
ABresult
0X0
100
111
176 114 print("Pushing [$last_score, $last_prob]\n") if DEBUG;
177 114 push(@result, [$last_score, $last_prob] );
178 }
179 382 $last_prob = $prob;
180 382 $last_score = $score;
181 }
182  
183 27 return \@result;
184 }
185  
186  
187 27 ▶sub PAV {
Subroutine PAV: called CC=5 SCAR=16.1
Pod undocumented
188 27 my ( $result ) = @_;
189  
190 27 ▶ for ( my $i = 0; $i < @$result - 1; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$result - 1) 329 27 356
191 329 ▶ if ( $result->[$i][PROB] < $result->[ $i + 1 ][PROB] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->[$i][1] < $$result[$i + 1][1]) 167 162 329
192 167 $result->[$i][PROB] =
193 ( $result->[$i][PROB] + $result->[ $i + 1 ][PROB] ) / 2;
194 167 $result->[ $i + 1 ][PROB] = $result->[$i][PROB];
195 167 print "Averaging elements $i and ", $i + 1, "\n" if DEBUG;
196  
197 167 ▶ for ( my $j = $i - 1; $j >= 0; $j-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 344 2 346
198 344 ▶ if ( $result->[$j][PROB] < $result->[ $i + 1 ][PROB] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->[$j][1] < $$result[$i + 1][1]) { } 179 165 344
199 179 my $d = ( $i + 1 ) - $j + 1;
200 179 flatten( $result, $j, $d );
201 }
202 else {
203 165 last;
204 }
205 }
206 }
207 }
208 }
209  
210 0 ▶sub print_vector {
Subroutine print_vector: not called CC=2 SCAR=17.9
Pod undocumented
211 0 my($vec) = @_;
212 0 for my $pair (@$vec) {
213 0 print join(", ", @$pair), "\n";
214 }
215 }
216  
217  
218 179 ▶sub flatten {
Subroutine flatten: called CC=3 SCAR=11.0
Pod undocumented
219 179 my ( $vec, $start, $len ) = @_;
220 179 if (DEBUG) {
221 print "Flatten called on vec, $start, $len\n";
222 print "Vector before: \n";
223 print_vector($vec);
224 }
225  
226 179 my $sum = 0;
227 179 for my $i ( $start .. $start + $len-1 ) {
228 781 $sum += $vec->[$i][PROB];
229 }
230 179 my $avg = $sum / $len;
231 179 print "Sum = $sum, avg = $avg\n" if DEBUG;
232 179 for my $i ( $start .. $start + $len -1) {
233 781 $vec->[$i][PROB] = $avg;
234 }
235 179 if (DEBUG) {
236 print "Vector after: \n";
237 print_vector($vec);
238 }
239 }
240  
241 =item B<score_prob>
242  
243 This is a simple utility function that takes the structure returned by
244 B<calibrate>, along with a new score, and returns the probability estimate.
245 Example calling form:
246  
247 $p = score_prob($calibrated, $score);
248  
249 Once you have a trained, calibrated classifier, you could imagine using it
250 like this:
251  
252 $calibrated = calibrate( $calibration_set );
253 print "Input instances, one per line:\n";
254 while (<>) {
255 chomp;
256 my(@fields) = split;
257 my $score = classifier(@fields);
258 my $prob = score_prob($score);
259 print "Estimated probability: $prob\n";
260 }
261  
262 =cut
263  
264 11 ▶sub score_prob {
Subroutine score_prob: called CC=3 SCAR=11.0
Pod documented
265 11 my($calibrated, $score) = @_;
266  
267 11 my $last_prob = 1.0;
268  
269 11 for my $tuple (@$calibrated) {
270 32 my($bound, $prob) = @$tuple;
271 32 ▶ return $prob if $score >= $bound;
Branch: 1 branch
BranchTrueFalseTotal
if $score >= $bound 9 23 32
272 23 $last_prob = $prob;
273 }
274 # If we drop off the end, probability estimate is zero
275 2 return 0;
276 }
277  
278  
279 =item B<print_mapping>
280  
281 This is a simple utility function that takes the structure returned by
282 B<calibrate> and prints out a simple list of lines describing the mapping
283 created.
284  
285 Example calling form:
286  
287 print_mapping($calibrated);
288  
289 Sample output:
290  
291 1.00 > SCORE >= 1.00 prob = 1.000
292 1.00 > SCORE >= 0.71 prob = 0.667
293 0.71 > SCORE >= 0.39 prob = 0.000
294 0.39 > SCORE >= 0.00 prob = 0.000
295  
296 These ranges are not necessarily compressed/optimized, as this sample output
297 shows.
298  
299 =back
300  
301 =cut
302 2 ▶sub print_mapping {
Subroutine print_mapping: called CC=3 SCAR=11.0
Pod documented
303 2 my($calibrated) = @_;
304 2 my $last_bound = 1.0;
305 2 for my $tuple (@$calibrated) {
306 10 my($bound, $prob) = @$tuple;
307 10 printf("%0.3f > SCORE >= %0.3f prob = %0.3f\n",
308 $last_bound, $bound, $prob);
309 10 $last_bound = $bound;
310 }
311 2 ▶ if ($last_bound != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_bound != 0) 2 0 2
312 2 printf("%0.3f > SCORE >= %0.3f prob = %0.3f\n",
313 $last_bound, 0, 0);
314 }
315 }
316  
317 =head1 DETAILS
318  
319 The PAV algorithm is conceptually straightforward. Given a set of training
320 cases ordered by the scores assigned by the classifier, it first assigns a
321 probability of one to each positive instance and a probability of zero to each
322 negative instance, and puts each instance in its own group. It then looks, at
323 each iteration, for adjacent violators: adjacent groups whose probabilities
324 locally increase rather than decrease. When it finds such groups, it pools
325 them and replaces their probability estimates with the average of the group's
326 values. It continues this process of averaging and replacement until the
327 entire sequence is monotonically decreasing. The result is a sequence of
328 instances, each of which has a score and an associated probability estimate,
329 which can then be used to map scores into probability estimates.
330  
331 For further information on the PAV algorithm, you can read the section in my
332 paper referenced below.
333  
334 =head1 EXPORT
335  
336 This module exports three functions: calibrate, score_prob and print_mapping.
337  
338 =head1 BUGS
339  
340 None known. This implementation is straightforward but inefficient (its time
341 is O(n^2) in the length of the data series). A linear time algorithm is
342 known, and in a later version of this module I'll probably implement it.
343  
344 =head1 SEE ALSO
345  
346 The AI::NaiveBayes1 perl module.
347  
348 My paper "PAV and the ROC Convex Hull" has a good discussion of the PAV
349 algorithm, including examples:
350 L<http://home.comcast.net/~tom.fawcett/public_html/papers/PAV-ROCCH-dist.pdf>
351  
352 If you want to read more about the general issue of classifier calibration,
353 here are some good papers, which are freely available on the web:
354  
355 I<"Transforming classifier scores into accurate multiclass probability estimates">
356 by Bianca Zadrozny and Charles Elkan
357  
358 I<"Predicting Good Probabilities With Supervised Learning">
359 by A. Niculescu-Mizil and R. Caruana
360  
361  
362 =head1 AUTHOR
363  
364 Tom Fawcett, E<lt>tom.fawcett@gmail.comE<gt>
365  
366 =head1 COPYRIGHT AND LICENSE
367  
368 Copyright (C) 2008-2012 by Tom Fawcett
369  
370 This library is free software; you can redistribute it and/or modify
371 it under the same terms as Perl itself, either Perl version 5.8.8 or,
372 at your option, any later version of Perl 5 you may have available.
373  
374 =cut
375 3 1;
↑ Summary