ROC.pm

filter: Statementstmt 83.8% 254 / 303 Branchbran 70.4% 100 / 142 Conditioncond 28.0% 21 / 75 MC/DCmcdc 8.7% 4 / 46 Subroutinesub 93.7% 15 / 16 Podpod 63.6% 7 / 11 totaltotal 67.6% 401 / 593 CC 105 SCAR 38.9
CC
105
Cov
72%
linlocate
240.0
xinbta
38.4
roc
30.9
CRAP 49.0
« roc_ui.pl ↑ Summary
1 #LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
2 #
3 # ROC.pm - A Perl module implementing receiver-operator-characteristic (ROC)
4 # curves with nonparametric confidence bounds
5 #
6 # Copyright (c) 1998 Hans A. Kestler. All rights reserved.
7 # This program is free software; you may redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 #
10 # This code implements a method for constructing nonparametric confidence
11 # for ROC curves described in
12 # R.A. Hilgers, Distribution-Free Confidence Bounds for ROC Curves,
13 # Meth Inform Med 1991; 30:96-101
14 # Additionally some auxilliary functions were ported (and corrected) from
15 # Fortran (Applied Statistics, ACM).
16 #
17 # Written in Perl by Hans A. Kestler.
18 # Bugs, comments, suggestions to:
19 # Hans A. Kestler <hans.kestler@uni-ulm.de>
20 # <h.kestler@ieee.org>
21 #
22 #
23 #LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
24  
25 package Statistics::ROC;
26 1 require 5;
27 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 require Exporter;
32  
33 1 @ISA = ('Exporter');
34 1 @EXPORT = qw(
35 roc rank loggamma betain Betain xinbta Xinbta
36 );
37 1 $VERSION = '0.01';
38  
39  
40  
41  
42 # Algorithm 291, Logarithm of the gamma function.
43 # in Collected Algorithms of the ACM, Vol II, 1980
44 # M.C. Pike and I.D. Hill with remark by M.R. Hoare
45 # see also Pike, M.C. and Hill, I.D. (1966). Algorithm 291. Logarithm of the
46 # gamma function. Commun. Ass. Comput. Mach., 9,684.
47  
48 145 ▶sub loggamma($){
Subroutine loggamma: called CC=4 SCAR=13.9
Pod documented
49 # This procedure evaluates the natural logarithm of gamma(x) for all
50 # x>0, accurate to 10 decimal places. Stirlings formula is used for the
51 # central polynomial part of the procedure
52
53 145 my $x= shift; # default arg is @_
54 145 my ($f, $z);
55
56
57 0 ▶ if($x==0){return 743.746924740801} # this is: loggamma(9.9999999999E-324)
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0) 0 145 145
58
59 145 ▶ if($x < 7)
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 7) { } 80 65 145
60 {
61 80 ▶ for($z=$x,$f=1;$z<7;$z++)
Branch: 1 branch
BranchTrueFalseTotal
if ($z < 7) 296 80 376
62 {
63 296 $x=$z; $f*=$z;
64 }
65 80 $x++;
66 80 $f= -log($f); # log returns the natural logarithm
67 }
68 65 else{ $f=0;}
69 145 $z=1/($x*$x);
70 145 return $f+($x-.5)*log($x)-$x+.918938533204673+
71 (((-.000595238095238*$z+.000793650793651)*$z-
72 .002777777777778)*$z+.083333333333333)/$x;
73 }
74  
75  
76  
77 # Algorithm AS 63 with remark AS R19,
78 # Computes incomplete beta function ratio
79 # K.L. Majumder and G.P. Bhattacharjee (1973). The Incomplete Beta Integral,
80 # Appl. Statist.,22:409:411 and
81 # G.W. Cran, K.J. Martin and G.E. Thomas (1977).Remark AS R19 and
82 # Algorithm AS109, A Remark on Algorithms AS 63: The Incomplete Beta Integral
83 # AS 64: Inverse of the Incomplete Beta Function Ratio,
84 # Appl. Statist., 26:111-114.
85 #
86 # Remarks:
87 # Complete beta function: B(p,q)=gamma(p)*gamma(q)/gamma(p+q)
88 # log(B(p,q))=ln(gamma(p))+ln(gamma(q))-ln(gamma(p+q))
89 #
90 # Incomplete beta function ratio:
91 # I_x(p,q)=1/B(p,q) * \int_0^x t^{p-1}*(1-t)^{q-1} dt
92 #
93 # --> log(B(p,q)) has to be supplied to calculate I_x(p,q)
94 # log denotes the natural logarithm
95 # $beta = log(B(p,q))
96 # $x = x
97 # $p = p
98 # $q = q
99 # The subroutine returns I_x(p,q). If an error occurs a negative value
100 # {-1,-2} is returned.
101
102 158 ▶sub betain($$$$){
Subroutine betain: called CC=11 SCAR=24.5
Pod documented
103 # Computes incomplete beta function ratio for arguments
104 # $x between zero and one, $p and $q positive.
105 # Log of complete beta function, $beta, is assumed to be known.
106
107 158 my ($x, $p, $q, $beta) = @_;
108 158 my ($xx, $psq, $cx, $pp, $qq, $index, $betain,
109 $ns, $term, $ai, $rx, $temp);
110 158 my $ACU=1.0E-14; # accuracy
111
112 # tests for admissibility of arguments
113 0 ▶ if($p<=0 || $q<=0){ return -1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($p <= 0 or $q <= 0) 0 158 158
Condition33%1/3
$p <= 0 or $q <= 0
l!l&&r!l&&!r
00158
MC/DC0%0/2
$p <= 0 or $q <= 0
$p <= 0 $q <= 0
Truth table33%1/3
A or B
A= $p <= 0B= $q <= 0
ABresult
1X1
011
000
114 0 ▶ if($x<0 || $x>1) { return -2;}
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $x > 1) 0 158 158
Condition33%1/3
$x < 0 or $x > 1
l!l&&r!l&&!r
00158
MC/DC0%0/2
$x < 0 or $x > 1
$x < 0 $x > 1
Truth table33%1/3
A or B
A= $x < 0B= $x > 1
ABresult
1X1
011
000
115  
116 # change tail if necessary and determine s
117 158 $psq=$p+$q; $cx=1-$x;
118 158 ▶ if($p<$psq*$x){ $xx=$cx; $cx=$x; $pp=$q; $qq=$p; $index=1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $psq * $x) { } 19 139 158
119 139 else{ $xx=$x; $pp=$p; $qq=$q; $index=0;}
120 158 $term=1; $ai=1; $betain=1;
121 158 $ns=$qq+$cx*$psq;
122
123 # use Soper's reduction formulae
124 158 $rx=$xx/$cx;
125 158 ▶ do{
Branch: 1 branch
BranchTrueFalseTotal
$temp <= $ACU and $temp <= $ACU * $betain or do { do { if ($ns >= 0) { $temp = $qq - $ai; if ($ns == 0) { $rx = $xx; } ; } else { $temp = $psq; ++$psq; } ; $term *= $temp * $rx / ($pp + $ai); $betain += $term; $temp = abs $term; ++$ai; --$ns } } 158 715 873
Condition66%2/3
$temp <= $ACU and $temp <= $ACU * $betain
!ll&&!rl&&r
7150158
MC/DC50%1/2
$temp <= $ACU and $temp <= $ACU * $betain
$temp <= $ACU $temp <= $ACU * $betain
Truth table66%2/3
A and B
A= $temp <= $ACUB= $temp <= $ACU * $betain
ABresult
0X0
100
111
126 0 ▶ if($ns>=0){$temp=$qq-$ai; if($ns==0){$rx=$xx;}}
Branch: 2 branches
BranchTrueFalseTotal
if ($ns >= 0) { } 783 90 873
if ($ns == 0) 0 783 783
127 90 else{ $temp=$psq; $psq++;}
128 873 $term *= $temp*$rx/($pp+$ai);
129 873 $betain+=$term;
130 873 $temp=abs($term); $ai++; $ns--;}
131 until($temp<=$ACU && $temp<=$ACU*$betain);
132
133 # calculate result
134 158 $betain *= exp($pp*log($xx)+($qq-1)*log($cx)-$beta)/$pp;
135 158 ▶ if($index){ return 1-$betain;}
Branch: 1 branch
BranchTrueFalseTotal
if ($index) { } 19 139 158
136 139 else{ return $betain;}
137 }
138
139 1 ▶sub Betain($$$){
Subroutine Betain: called CC=3 SCAR=13.3
Pod documented
140 # Computes the incomplete beta function
141 # by calling loggamma() and betain()
142 1 my ($x, $p, $q) = @_;
143
144 0 ▶ if($x==1){return 1;}
Branch: 2 branches
BranchTrueFalseTotal
if ($x == 1) { } 0 1 1
elsif ($x == 0) { } 0 1 1
145 0 elsif($x==0){return 0;}
146 1 else{ return betain($x, $p, $q,loggamma($p)+loggamma($q)-loggamma($p+$q));}
147 }
148  
149 47 ▶sub max($$){
Subroutine max: called CC=2 SCAR=7.6
Pod undocumented
150 # computes the maximum of two numbers
151 47 my ($a, $b) = @_;
152
153 47 ▶ if($a>$b){ return $a;}
Branch: 1 branch
BranchTrueFalseTotal
if ($a > $b) { } 47 0 47
154 0 else{ return $b;}
155 }
156  
157  
158 # Algorithm AS 109,
159 # Computes inverse of incomplete beta function ratio
160 # G.W. Cran, K.J. Martin and G.E. Thomas (1977).Remark AS R19 and
161 # Algorithm AS109, A Remark on Algorithms AS 63: The Incomplete Beta Integral
162 # AS 64: Inverse of the Incomplete Beta Function Ratio,
163 # Appl. Statist., 26:111-114.
164 #
165 # Remark AS R83 and the correction in vol40(1) of Appl. Statist.(1991), p.236
166 # have been incorporated in this version.
167 # K.J. Berry, P.W. Mielke, Jr and G.W. Cran (1990) Algorithm AS R83, A Remark
168 # on Algorithm AS 109: Inverse of the Incomplete Beta Function Ratio,
169 # Appl. Statist., 39:309-310.
170 #
171 # Remarks:
172 #
173 # Complete beta function: B(p,q)=gamma(p)*gamma(q)/gamma(p+q)
174 # log(B(p,q))=ln(gamma(p))+ln(gamma(q))-ln(gamma(p+q))
175 #
176 # Incomplete beta function ratio:
177 # alpha = I_x(p,q) = 1/B(p,q) * \int_0^x t^{p-1}*(1-t)^{q-1} dt
178 #
179 # --> log(B(p,q)) has to be supplied to calculate I_x(p,q)
180 # log denotes the natural logarithm
181 # $beta = log(B(p,q))
182 # $alpha= I_x(p,q)
183 # $p = p
184 # $q = q
185 # The subroutine returns x. If an error occurs a negative value {-1,-2,-3}
186 # is returned.
187
188 47 ▶sub xinbta($$$$){
Subroutine xinbta: called CC=26 SCAR=36.5
Pod documented
189 # Computes inverse of incomplete beta function ratio
190 # for given positive values of the arguments $p and $q,
191 # $alpha between zero and one.
192 # Log of complete beta function, $beta is assumed to be known.
193 #
194 # copyright by H.A. Kestler, 1998
195  
196 47 my ($p, $q, $beta, $alpha) = @_;
197 47 my ($a, $y, $pp, $qq, $index, $r, $h, $t, $w, $xinbta,
198 $yprev, $prev,$s, $sq, $tx, $adj, $g);
199 47 my $SAE=-37;
200 47 my $FPU=10**$SAE;
201 47 my $ACU;
202
203 # test for admissibility of parameters
204 0 ▶ if($p<=0 || $q<=0){ return -1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($p <= 0 or $q <= 0) 0 47 47
Condition33%1/3
$p <= 0 or $q <= 0
l!l&&r!l&&!r
0047
MC/DC0%0/2
$p <= 0 or $q <= 0
$p <= 0 $q <= 0
Truth table33%1/3
A or B
A= $p <= 0B= $q <= 0
ABresult
1X1
011
000
205 0 ▶ if($alpha<0 || $alpha>1) { return -2;}
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha < 0 or $alpha > 1) 0 47 47
Condition33%1/3
$alpha < 0 or $alpha > 1
l!l&&r!l&&!r
0047
MC/DC0%0/2
$alpha < 0 or $alpha > 1
$alpha < 0 $alpha > 1
Truth table33%1/3
A or B
A= $alpha < 0B= $alpha > 1
ABresult
1X1
011
000
206 0 ▶ if($alpha==0 || $alpha==1){ return $alpha;}
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha == 0 or $alpha == 1) 0 47 47
Condition33%1/3
$alpha == 0 or $alpha == 1
l!l&&r!l&&!r
0047
MC/DC0%0/2
$alpha == 0 or $alpha == 1
$alpha == 0 $alpha == 1
Truth table33%1/3
A or B
A= $alpha == 0B= $alpha == 1
ABresult
1X1
011
000
207
208 # change tail if necessary
209 47 ▶ if($alpha>.5){ $a=1-$alpha; $pp=$q; $qq=$p; $index=1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha > 0.5) { } 17 30 47
210 30 else{ $a=$alpha; $pp=$p; $qq=$q; $index=0;}
211
212 # calculate the initial approximation
213 47 $r=sqrt(-log($a*$a));
214 47 $y=$r-(2.30753+.27061*$r)/(1+(.99229+.04481*$r)*$r);
215 47 ▶ if($pp>1 && $qq > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($pp > 1 and $qq > 1) { } 31 16 47
Condition100%3/3
$pp > 1 and $qq > 1
!ll&&!rl&&r
61031
MC/DC100%2/2
$pp > 1 and $qq > 1
$pp > 1 $qq > 1
Truth table100%3/3
A and B
A= $pp > 1B= $qq > 1
ABresult
0X0
100
111
216 {
217 31 $r=($y*$y-3)/6; $s=1/($pp+$pp-1); $t=1/($qq+$qq-1);
218 31 $h=2/($s+$t);
219 31 $w=$y*sqrt($h+$r)/$h-($t-$s)*($r+5/6-2/(3*$h));
220 31 $xinbta=$pp/($pp+$qq*exp($w+$w));
221 }
222 else
223 {
224 16 $r=$qq+$qq; $t=1/(9*$qq);
225 16 $t=$r*(1-$t+$y*sqrt($t))**3;
226 16 ▶ if($t<=0){
Branch: 1 branch
BranchTrueFalseTotal
if ($t <= 0) { } 0 16 16
227 0 $xinbta=1-exp((log((1-$a)*$qq)+$beta)/$qq);
228 }
229 else{
230 16 $t=(4*$pp+$r-2)/$t;
231 16 ▶ if($t<=1){ $xinbta=exp((log($a*$pp)+$beta)/$pp);}
Branch: 1 branch
BranchTrueFalseTotal
if ($t <= 1) { } 4 12 16
232 12 else{ $xinbta=1-2/($t+1);}
233 }
234 }
235
236 # solve for $x by a modified newton-raphson method
237 # using subroutine betain()
238 47 $r=1-$pp; $t=1-$qq; $yprev=0; $sq=1; $prev=1;
239 0 ▶ if($xinbta<.0001){ $xinbta=.0001;}
Branch: 1 branch
BranchTrueFalseTotal
if ($xinbta < "0.0001") 0 47 47
240 0 ▶ if($xinbta>.9999){ $xinbta=.9999;}
Branch: 1 branch
BranchTrueFalseTotal
if ($xinbta > "0.9999") 0 47 47
241 47 $ACU=10**(max(-5/$pp**2-1/$a**.2-13,$SAE));
242 47 ▶ do{
Branch: 1 branch
BranchTrueFalseTotal
$adj == 0 or do { do { $y = &betain($xinbta, $pp, $qq, $beta); if ($y == -1 or $y == -2) { return -3; } ; $y = ($y - $a) * exp($beta + $r * log($xinbta) + $t * log(1 - $xinbta)); if ($y * $y <= 0) { $prev = &max($sq, $FPU); } ; $g = 1; Label10: do { do { $adj = $g * $y; $sq = $adj * $adj; $g /= 3 } while $sq >= $prev; $tx = $xinbta - $adj } until $tx >= 0 and $tx <= 1; if ($prev <= $ACU or $y * $y <= $ACU) { goto Label12; } ; if ($tx == 0 or $tx == 1) { goto Label10; } ; $xinbta = $tx; $yprev = $y } } 0 110 110
243 157 $y=betain($xinbta,$pp,$qq,$beta);
244 0 ▶ if($y==-1 || $y==-2){ return -3;} # betain returns an exception
Branch: 1 branch
BranchTrueFalseTotal
if ($y == -1 or $y == -2) 0 157 157
Condition33%1/3
$y == -1 or $y == -2
l!l&&r!l&&!r
00157
MC/DC0%0/2
$y == -1 or $y == -2
$y == -1 $y == -2
Truth table33%1/3
A or B
A= $y == -1B= $y == -2
ABresult
1X1
011
000
245 157 $y=($y-$a)*exp($beta+$r*log($xinbta)+$t*log(1-$xinbta));
246 0 ▶ if($y*$y<=0){ $prev=max($sq,$FPU);}
Branch: 1 branch
BranchTrueFalseTotal
if ($y * $y <= 0) 0 157 157
247 157 $g=1;
248 157 ▶ Label10: do{
Branch: 1 branch
BranchTrueFalseTotal
$tx >= 0 and $tx <= 1 or do { do { do { $adj = $g * $y; $sq = $adj * $adj; $g /= 3 } while $sq >= $prev; $tx = $xinbta - $adj } } 157 0 157
Condition33%1/3
$tx >= 0 and $tx <= 1
!ll&&!rl&&r
00157
MC/DC0%0/2
$tx >= 0 and $tx <= 1
$tx >= 0 $tx <= 1
Truth table33%1/3
A and B
A= $tx >= 0B= $tx <= 1
ABresult
0X0
100
111
249 157 ▶ do{ $adj=$g*$y; $sq=$adj*$adj; $g/=3;} while($sq>=$prev);
Branch: 1 branch
BranchTrueFalseTotal
$sq >= $prev and do { do { $adj = $g * $y; $sq = $adj * $adj; $g /= 3 } } 0 157 157
250 157 $tx=$xinbta-$adj;}
251 until($tx>=0 && $tx<=1);
252 157 ▶ if($prev<=$ACU || $y*$y<=$ACU){ goto Label12;}
Branch: 1 branch
BranchTrueFalseTotal
if ($prev <= $ACU or $y * $y <= $ACU) 47 110 157
Condition66%2/3
$prev <= $ACU or $y * $y <= $ACU
l!l&&r!l&&!r
047110
MC/DC50%1/2
$prev <= $ACU or $y * $y <= $ACU
$prev <= $ACU $y * $y <= $ACU
Truth table66%2/3
A or B
A= $prev <= $ACUB= $y * $y <= $ACU
ABresult
1X1
011
000
253 0 ▶ if($tx==0 || $tx==1){ goto Label10;}
Branch: 1 branch
BranchTrueFalseTotal
if ($tx == 0 or $tx == 1) 0 110 110
Condition33%1/3
$tx == 0 or $tx == 1
l!l&&r!l&&!r
00110
MC/DC0%0/2
$tx == 0 or $tx == 1
$tx == 0 $tx == 1
Truth table33%1/3
A or B
A= $tx == 0B= $tx == 1
ABresult
1X1
011
000
254 110 $xinbta=$tx; $yprev=$y;}
255 until($adj==0);
256
257 Label12:
258 47 ▶ if($index){ return 1-$xinbta;}
Branch: 1 branch
BranchTrueFalseTotal
if ($index) { } 17 30 47
259 30 else{ return $xinbta;}
260 }
261  
262  
263 47 ▶sub Xinbta($$$){
Subroutine Xinbta: called CC=3 SCAR=13.3
Pod documented
264 # Computes the inverse of the incomplete beta function
265 # by calling loggamma() and xinbta()
266 #
267 # copyright by H.A. Kestler, 1998
268  
269 47 my ($p, $q, $alpha) = @_;
270
271 0 ▶ if($alpha==1){return 1;}
Branch: 2 branches
BranchTrueFalseTotal
if ($alpha == 1) { } 0 47 47
elsif ($alpha == 0) { } 0 47 47
272 0 elsif($alpha==0){return 0;}
273 47 else{ return xinbta($p, $q,loggamma($p)+loggamma($q)-loggamma($p+$q),
274 $alpha);}
275 }
276  
277  
278  
279 9 ▶sub rank($\@){
Subroutine rank: called CC=11 SCAR=24.0
Pod documented
280 # Computes the ranks of the values specified as the second
281 # argument (an array). Returns a vector of ranks
282 # corresponding to the input vector.
283 # Different types of ranking are possible ('high', 'low', 'mean'),
284 # and are specified as first argument.
285 # These differ in the way ties of the input vector, i.e. identical
286 # values, are treated:
287 # 'high' --> replace ranks of identical values with their
288 # highest rank
289 # 'low' --> replace ranks of identical values with their
290 # lowest rank
291 # 'mean' --> replace ranks of identical values with the mean
292 # of their rank
293 #
294 # copyright by H.A. Kestler, 1998
295
296 9 my ($type, $r) = @_; # $type: type of ranking 'high', 'low' or 'mean'
297 # $r: reference to array of values to be ranked
298 9 my (@s, $s, $i, @e, @rk, $rk_m);
299
300 # calculate initial rank's
301 9 @s=sort{$$r[$a]<=>$$r[$b]} 0..$#{$r}; # sort idx num. by values of @r
302 9 ▶ for($i=0,@rk=@s;$i<@rk;$i++){ $rk[$s[$i]]=$i+1;} # set rank's
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @rk) 69 9 78
303
304 # treat ties
305 9 ▶ for($i=1,@e=(); $i<@s; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @s) 60 9 69
306 60 ▶ if($$r[$s[$i]]==$$r[$s[$i-1]]){ # test if there are ties
Branch: 2 branches
BranchTrueFalseTotal
if ($$r[$s[$i]] == $$r[$s[$i - 1]]) { } 21 39 60
elsif (@e) { } 18 21 39
307 21 push @e,$i-1;} # save index numbers of tied values (minus 1)
308 elsif(@e){ # ties have occured and are now being treated
309 18 ▶ if($type eq'mean'){ # calculate mean value of tied ranks
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "mean") 6 12 18
310 6 $rk_m=0;
311 6 for(@e,$e[-1]+1){ $rk_m+=$rk[$s[$_]];} $rk_m/=@e+1;
312 }
313 18 for(@e,$e[-1]+1){
314 39 ▶ if($type eq 'high'){ $rk[$s[$_]]=$rk[$s[$e[-1]+1]];}
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'high') { } 13 26 39
elsif ($type eq 'low') { } 13 13 26
elsif ($type eq 'mean') { } 13 0 13
315 13 elsif($type eq 'low' ){ $rk[$s[$_]]=$rk[$s[$e[0]]];}
316 13 elsif($type eq 'mean'){ $rk[$s[$_]]=$rk_m;}
317 0 else{ croak "Wrong type of ranking (high|low|mean).\n";}
318 }
319 18 @e=(); # reinitialize @e
320 }
321 }
322 9 return @rk;
323 }
324  
325  
326 18 ▶sub locate(\@$){
Subroutine locate: called CC=9 SCAR=25.1
Pod undocumented
327 # Routine to find the index for table lookup which is below
328 # the value to be interpolated.
329 # Given a reference to an array $xx and a value $x a value $j
330 # is returned such that $x is between $xx[$j] and $xx[$j+1].
331 # $xx must be monotonic, either increasing or decreasing.
332 #
333 # This routine is adapted from "Numerical Recipes in C",
334 # second edition, by Press, Teukolsky, Vetterling and Flannery,
335 # Cambridge University Press, 1992.
336 # It uses bisection to find the right place, which has a
337 # comutational complexity of O(log_2(n)).
338 #
339 # copyright by H.A. Kestler, 1998
340  
341 18 my ($xx,$x)=@_;
342 18 my ($jl,$ju)=(0,$#{$xx}); # initialize lower and upper limits
343 18 my ($jm,$ascend);
344
345 18 $ascend=$$xx[$ju] > $$xx[0];
346
347 # test if $x is inside of the array
348 18 ▶ if(($x>$$xx[$ju] || $x<$$xx[$jl]) && $ascend)
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $xx->[$ju] || $x < $xx->[$jl] and $ascend) 0 18 18
Condition33%2/6
$x > $xx->[$ju] || $x < $xx->[$jl] and $ascend
!ll&&!rl&&r
1800
$x > $xx->[$ju] or $x < $xx->[$jl]
l!l&&r!l&&!r
0018
MC/DC0%0/3
$x > $xx->[$ju] || $x < $xx->[$jl] and $ascend
$x > $xx->[$ju] $x < $xx->[$jl] $ascend
Truth table20%1/5
A or B and C
A= $x > $xx->[$ju]B= $x < $xx->[$jl]C= $ascend
ABCresult
00X0
1X00
0100
1X11
0111
349 0 { croak "Value out of range for table lookup (1): $x.\n";}
350 18 ▶ if(($x<$$xx[$ju] || $x>$$xx[$jl]) && !$ascend)
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $xx->[$ju] || $x > $xx->[$jl] and not $ascend) 0 18 18
Condition33%2/6
$x < $xx->[$ju] || $x > $xx->[$jl] and not $ascend
!ll&&!rl&&r
0180
$x < $xx->[$ju] or $x > $xx->[$jl]
l!l&&r!l&&!r
1800
MC/DC0%0/3
$x < $xx->[$ju] || $x > $xx->[$jl] and not $ascend
$x < $xx->[$ju] $x > $xx->[$jl] not $ascend
Truth table20%1/5
A or B and not(C)
A= $x < $xx->[$ju]B= $x > $xx->[$jl]C= not $ascend
ABCresult
00X0
1X00
0100
1X11
0111
351 0 { croak "Value out of range for table lookup (2): $x.\n";}
352
353 18 ▶ while(($ju-$jl)>1) { # If we are not yet done
Branch: 1 branch
BranchTrueFalseTotal
if ($ju - $jl > 1) 57 18 75
354 57 $jm=int(($ju+$jl)/2); # compute a midpoint,
355 57 ▶ if($x > $xx->[$jm] == $ascend)
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $xx->[$jm] == $ascend) { } 30 27 57
356 30 { $jl=$jm;} # and replace either the lower limit
357 else
358 27 { $ju=$jm;} # or the upper limit, as appropriate.
359 }
360 18 return $jl;
361 }
362  
363  
364 0 ▶sub linlocate(\@$$){
Subroutine linlocate: not called CC=15 SCAR=54.8
Pod undocumented
365 # Routine to find the index for table lookup which is below
366 # the value to be interpolated.
367 # Given a reference to an array $xx and a value $x a value $j
368 # is returned such that $x is between $xx[$j] and $xx[$j+1].
369 # $xx must be monotonic, either increasing or decreasing.
370 #
371 # Starts searching linearly from an initial index value
372 # provided as the third argument.
373 # If no index value can be found a negative value is
374 # returned, i.e. -1.
375 #
376 # copyright by H.A. Kestler, 1998
377
378 0 my ($xx,$x,$index)=@_;
379 0 my ($jl,$ju)=(0,$#{$xx}); # initialize lower and upper limits
380 0 my $ascend;
381
382 0 $ascend=$$xx[$ju] > $$xx[0];
383
384 # test if $x is inside of the array
385 0 ▶ if(($x>$$xx[$ju] || $x<$$xx[$jl]) && $ascend)
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $xx->[$ju] || $x < $xx->[$jl] and $ascend) 0 0 0
Condition0%0/6
$x > $xx->[$ju] || $x < $xx->[$jl] and $ascend
!ll&&!rl&&r
000
$x > $xx->[$ju] or $x < $xx->[$jl]
l!l&&r!l&&!r
000
MC/DC0%0/3
$x > $xx->[$ju] || $x < $xx->[$jl] and $ascend
$x > $xx->[$ju] $x < $xx->[$jl] $ascend
Truth table0%0/5
A or B and C
A= $x > $xx->[$ju]B= $x < $xx->[$jl]C= $ascend
ABCresult
00X0
1X00
0100
1X11
0111
386 0 { croak "Value out of range for table lookup.\n";}
387 0 ▶ if(($x<$$xx[$ju] || $x>$$xx[$jl]) && !$ascend)
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $xx->[$ju] || $x > $xx->[$jl] and not $ascend) 0 0 0
Condition0%0/6
$x < $xx->[$ju] || $x > $xx->[$jl] and not $ascend
!ll&&!rl&&r
000
$x < $xx->[$ju] or $x > $xx->[$jl]
l!l&&r!l&&!r
000
MC/DC0%0/3
$x < $xx->[$ju] || $x > $xx->[$jl] and not $ascend
$x < $xx->[$ju] $x > $xx->[$jl] not $ascend
Truth table0%0/5
A or B and not(C)
A= $x < $xx->[$ju]B= $x > $xx->[$jl]C= not $ascend
ABCresult
00X0
1X00
0100
1X11
0111
388 0 { croak "Value out of range for table lookup.\n";}
389
390 # step through the table sequentially
391 0 ▶ if($ascend && $xx->[$index]<$x){ # ascending
Branch: 2 branches
BranchTrueFalseTotal
if ($ascend and $xx->[$index] < $x) { } 0 0 0
elsif (not $ascend and $xx->[$index] > $x) { } 0 0 0
Condition0%0/3
$ascend and $xx->[$index] < $x
!ll&&!rl&&r
000
MC/DC0%0/2
$ascend and $xx->[$index] < $x
$ascend $xx->[$index] < $x
Truth table0%0/3
A and B
A= $ascendB= $xx->[$index] < $x
ABresult
0X0
100
111
392 0 ▶ while($x>$xx->[$index] and $index<=$ju) { $index++;}}
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $xx->[$index] and $index <= $ju) 0 0 0
Condition0%0/3
$x > $xx->[$index] and $index <= $ju
!ll&&!rl&&r
000
MC/DC0%0/2
$x > $xx->[$index] and $index <= $ju
$x > $xx->[$index] $index <= $ju
Truth table0%0/3
A and B
A= $x > $xx->[$index]B= $index <= $ju
ABresult
0X0
100
111
393 ▶ elsif(!$ascend && $xx->[$index]>$x){ # descending
Condition0%0/3
not $ascend and $xx->[$index] > $x
!ll&&!rl&&r
000
MC/DC0%0/2
not $ascend and $xx->[$index] > $x
not $ascend $xx->[$index] > $x
Truth table0%0/3
not(A) and B
A= not $ascendB= $xx->[$index] > $x
ABresult
0X0
100
111
394 0 ▶ while($x<$xx->[$index] and $index<=$ju) { $index++;}}
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $xx->[$index] and $index <= $ju) 0 0 0
Condition0%0/3
$x < $xx->[$index] and $index <= $ju
!ll&&!rl&&r
000
MC/DC0%0/2
$x < $xx->[$index] and $index <= $ju
$x < $xx->[$index] $index <= $ju
Truth table0%0/3
A and B
A= $x < $xx->[$index]B= $index <= $ju
ABresult
0X0
100
111
395 0 else{ return -1;} # starting index is too high
396
397 0 return $index-1;
398 }
399  
400  
401 6 ▶sub interp(\@\@\@){
Subroutine interp: called CC=3 SCAR=11.1
Pod undocumented
402 # Interpolates (table lookup) piecewise linearly an
403 # array (third argument). Returns
404 # The table is represented by the first two arguments, i.e. @xx and @yy.
405 # Assumes the @xx values to be monotonically increasing.
406 #
407 # copyright by H.A. Kestler, 1998
408
409 1 ▶ use vars ('@xx', '@yy', '@x');
Subroutine BEGIN: called CC=1 SCAR=0.0
410 6 local (*xx, *yy, *x)=@_;
411 6 my ($i, $index, @y);
412
413 # make checks
414 0 ▶ if(@xx != @yy) {croak "Sizes of xx and yy arrays are not equal.\n";}
Branch: 1 branch
BranchTrueFalseTotal
if (@Statistics::ROC::xx != @Statistics::ROC::yy) 0 6 6
415
416 6 ▶ for($i=0; $i<@x; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @Statistics::ROC::x) 18 6 24
417 {
418 18 $index=locate(@xx,$x[$i]);
419 18 $y[$i]=($yy[$index+1]-$yy[$index])/($xx[$index+1]-$xx[$index])*
420 ($x[$i]-$xx[$index]) + $yy[$index];
421 }
422 6 return @y;
423 }
424  
425  
426 1 ▶sub roc($$\@){
Subroutine roc: called CC=28 SCAR=34.3
Pod documented
427 # ROC (receiver operator characteristic) curves with confidence bounds
428 #
429 # Determines the ROC curve and its nonparametric confidence bounds.
430 # The ROC curve shows the relationship of "probability of false
431 # alarm" (x-axis) to "probability of detection" (y-axis) for a
432 # certain test.
433 # Or in medical terms: the "probability of a positive test, given no
434 # disease" to the "probability of a positive test, given disease".
435 # The ROC curve may be used to determine an "optimal" cutoff
436 # point for the test.
437 #
438 # The routine takes three arguments:
439 # (1) type of model: 'decrease' or 'increase', this states the assumption
440 # that a higher ('increase') value of the data tends to be an
441 # indicator of a positive test result or for the model 'decrease'
442 # a lower value.
443 # (2) two-sided confidence interval (usually 0.95 is chosen).
444 # (3) the data stored as a list-of-lists:
445 # each entry in this list consits of an "value / true group" pair,
446 # i.e. value / disease present. Group values are from {0,1}.
447 # 0 stands for disease (or signal) not present (prior knowledge) and
448 # 1 for disease (or signal) present (prior knowledge).
449 # Example: @s=([2, 0], [12.5, 1], [3, 0], [10, 1], [9.5, 0], [9, 1]);
450 # Notice the small overlap of the groups. The
451 # optimal cutoff point to separate the two groups would be between
452 # 9 and 9.5 if the criterion of optimality is to maximize the
453 # probability of detection and simultaneously minimize the
454 # probability of false alarm.
455 #
456 # Returns a list-of-lists with the three curves:
457 # @ROC=([@lower_b], [@roc], [@upper_b]) each of the curves is
458 # again a list-of-lists with each entry consisting of one (x,y) pair.
459 # The routine impelements the method described in:
460 # R.A. Hilgers, Distribution-Free Confidence Bounds for ROC Curves,
461 # Meth Inform Med 1991; 30:96-101
462 #
463 # copyright by H.A. Kestler, 1998
464
465 1 my $model_type = shift; # assign
466 1 my $conf = shift;
467 1 ▶ use vars '@val_grp';
Subroutine BEGIN: called CC=1 SCAR=0.0
468 1 local (*val_grp)=@_;
469
470 1 my ($cu, $cl, $elem, $n1, $n0, $i, $j);
471 1 my @grp1=();my @grp0=();
472 1 my (@f_l_1,@f_m_1,@f_h_1,@f_l_0,@f_m_0,@f_h_0,@mat,@xx,@yy,@x,@y,@index);
473 1 my (@lower_b ,@roc ,@upper_b, @ROC);
474
475 # make checks
476 0 ▶ if($conf>=1 || $conf<=0){ croak
Branch: 1 branch
BranchTrueFalseTotal
if ($conf >= 1 or $conf <= 0) 0 1 1
Condition33%1/3
$conf >= 1 or $conf <= 0
l!l&&r!l&&!r
001
MC/DC0%0/2
$conf >= 1 or $conf <= 0
$conf >= 1 $conf <= 0
Truth table33%1/3
A or B
A= $conf >= 1B= $conf <= 0
ABresult
1X1
011
000
477 "The nominal 2-sided confidence limit must be a number of [0,1].\n";}
478 0 ▶ if($model_type ne 'increase' && $model_type ne 'decrease'){ croak
Branch: 1 branch
BranchTrueFalseTotal
if ($model_type ne "increase" and $model_type ne "decrease") 0 1 1
Condition33%1/3
$model_type ne "increase" and $model_type ne "decrease"
!ll&&!rl&&r
010
MC/DC0%0/2
$model_type ne "increase" and $model_type ne "decrease"
$model_type ne "increase" $model_type ne "decrease"
Truth table33%1/3
A and B
A= $model_type ne "increase"B= $model_type ne "decrease"
ABresult
0X0
100
111
479 "Wrong model type specified!\n";}
480
481 1 $cu=(sqrt($conf)+1)/2; # calculate the one-sided upper
482 1 $cl=1-$cu; # and lower confidence limits
483
484 # extract values
485 1 ▶ for($i=0;$i<@val_grp;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @Statistics::ROC::val_grp) 14 1 15
486 14 ▶ if($val_grp[$i][1]==1) { push @grp1, $val_grp[$i][0];}
Branch: 1 branch
BranchTrueFalseTotal
if ($Statistics::ROC::val_grp[$i][1] == 1) { } 7 7 14
487 7 else { push @grp0, $val_grp[$i][0];}
488 }
489
490 # compute ranks and values of inverse incomplete beta function
491 1 @f_l_1=rank('low' ,@grp1);
492 1 @f_m_1=rank('mean',@grp1);
493 1 @f_h_1=rank('high',@grp1);
494 1 @f_l_0=rank('low' ,@grp0);
495 1 @f_m_0=rank('mean',@grp0);
496 1 @f_h_0=rank('high',@grp0);
497
498
499 1 $n1=@grp1; $n0=@grp0; # number of elements in both arrays
500 1 for $elem (@f_l_1){ $elem=Xinbta($elem,$n1+1-$elem,$cl);}
501 1 for $elem (@f_m_1){ $elem=Xinbta($elem,$n1+1-$elem,0.5);}
502 1 for $elem (@f_h_1){ $elem=Xinbta($elem,$n1+1-$elem,$cu);}
503 1 for $elem (@f_l_0){ $elem=Xinbta($elem,$n0+1-$elem,$cl);}
504 1 for $elem (@f_m_0){ $elem=Xinbta($elem,$n0+1-$elem,0.5);}
505 1 for $elem (@f_h_0){ $elem=Xinbta($elem,$n0+1-$elem,$cu);}
506  
507
508 # merge and sort
509 1 @mat=();
510 1 ▶ for($i=0;$i<$n1;$i++){ push @mat, [($grp1[$i], -1, -1, -1,
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n1) 7 1 8
511 $f_l_1[$i], $f_m_1[$i], $f_h_1[$i])];}
512 1 ▶ for($i=0;$i<$n0;$i++){ push @mat, [($grp0[$i],$f_l_0[$i], $f_m_0[$i],
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n0) 7 1 8
513 $f_h_0[$i], -1, -1, -1)];}
514 # sort numerically according to value in first column
515 1 @mat=@mat[sort{$mat[$a][0] <=> $mat[$b][0]} 0..$#mat];
516
517  
518 # for practical purposes augment @mat and fill missing data (-1)
519 # at the beginning and end of the matrix
520 1 unshift @mat,[-1, 0, 0, Xinbta(1,$n0,$cu), 0, 0, Xinbta(1,$n1,$cu)];
521 1 push @mat,[-1, Xinbta($n0,1,$cl), 1, 1, Xinbta($n1,1,$cl), 1, 1];
522 1 ▶ for($i=1;$mat[$i][1]==-1; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($mat[$i][1] == -1) 3 1 4
523 3 $mat[$i][1]=0; $mat[$i][2]=0; $mat[$i][3]=$mat[0][3];}
524 1 ▶ for($i=1;$mat[$i][4]==-1; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($mat[$i][4] == -1) 0 1 1
525 0 $mat[$i][4]=0; $mat[$i][5]=0; $mat[$i][6]=$mat[0][6];}
526 1 ▶ for($i=$#mat-1;$mat[$i][1]==-1; $i--){
Branch: 1 branch
BranchTrueFalseTotal
if ($mat[$i][1] == -1) 0 1 1
527 0 $mat[$i][1]=$mat[$#mat][1]; $mat[$i][2]=1; $mat[$i][3]=1;}
528 1 ▶ for($i=$#mat-1;$mat[$i][4]==-1; $i--){
Branch: 1 branch
BranchTrueFalseTotal
if ($mat[$i][4] == -1) 5 1 6
529 5 $mat[$i][4]=$mat[$#mat][4]; $mat[$i][5]=1; $mat[$i][6]=1;}
530  
531
532 # replace missing data (-1) with a piecewise linear interpolation
533 1 ▶ for($j=1;$j<7;$j++) # iterate thru columns
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 7) 6 1 7
534 {
535 6 ▶ for($i=1,@xx=(),@yy=(),@x=(),@index=();$i<$#mat;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#mat) 84 6 90
536 84 ▶ push @xx, $mat[$i][0] if $mat[$i][$j] !=-1;
Branch: 1 branch
BranchTrueFalseTotal
if $mat[$i][$j] != -1 66 18 84
537 84 ▶ push @yy, $mat[$i][$j] if $mat[$i][$j] !=-1;
Branch: 1 branch
BranchTrueFalseTotal
if $mat[$i][$j] != -1 66 18 84
538 84 ▶ push @x, $mat[$i][0] if $mat[$i][$j] ==-1;
Branch: 1 branch
BranchTrueFalseTotal
if $mat[$i][$j] == -1 18 66 84
539 84 ▶ push @index, $i if $mat[$i][$j] ==-1;
Branch: 1 branch
BranchTrueFalseTotal
if $mat[$i][$j] == -1 18 66 84
540 }
541 6 @y=interp(@xx,@yy,@x);
542 6 ▶ for($i=0;$i<@index;$i++){ $mat[$index[$i]][$j]=$y[$i];}
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @index) 18 6 24
543 }
544
545
546 # calculate (x,y) pairs of ROC curve and its limit curves
547 # (lower, ROC, upper) according to specified model
548 1 ▶ for($i=0,@lower_b=(),@roc=(),@upper_b=(),;$i<@mat;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @mat) 16 1 17
549 16 ▶ if($model_type eq 'decrease'){
Branch: 1 branch
BranchTrueFalseTotal
if ($model_type eq 'decrease') { } 16 0 16
550 16 push @lower_b, [($mat[$i][3], $mat[$i][4])];
551 16 push @roc, [($mat[$i][2], $mat[$i][5])];
552 16 push @upper_b, [($mat[$i][1], $mat[$i][6])];
553 }
554 else{
555 0 push @lower_b, [(1-$mat[$i][3], 1-$mat[$i][4])];
556 0 push @roc, [(1-$mat[$i][2], 1-$mat[$i][5])];
557 0 push @upper_b, [(1-$mat[$i][1], 1-$mat[$i][6])];
558 }
559 }
560
561 1 @ROC=([@lower_b], [@roc], [@upper_b]);
562 1 return @ROC;
563 }
564  
565  
566  
567  
568 # Autoload methods go after =cut, and are processed by the autosplit program.
569  
570 1 1;
571 __END__
« roc_ui.pl ↑ Summary