lib/Statistics/MaxEntropy.pm

filter: Statementstmt 80.6% 489 / 606 Branchbran 68.0% 170 / 250 Conditioncond 59.5% 25 / 42 MC/DCmcdc 25.9% 7 / 27 Subroutinesub 83.3% 40 / 48 Podpod 20.0% 8 / 40 totaltotal 72.9% 739 / 1013 CC 153 SCAR 23.3
CC
153
Cov
76%
C_func
42.0
read_parameters
30.0
write_parameters_with_names
30.0
CRAP 10.3
« script/ME.wrapper.pl ↑ Summary lib/Statistics/Candidates.pm »
1 package Statistics::MaxEntropy;
2  
3 ##---------------------------------------------------------------------------##
4 ## Author:
5 ## Hugo WL ter Doest terdoest@cs.utwente.nl
6 ## Description:
7 ## Object-oriented implementation of
8 ## Generalised Iterative Scaling algorithm,
9 ## Improved Iterative Scaling algorithm, and
10 ## Feature Induction algorithm
11 ## for inducing maximum entropy probability distributions
12 ## Keywords:
13 ## Maximum Entropy Modeling
14 ## Kullback-Leibler Divergence
15 ## Exponential models
16 ##
17 ##---------------------------------------------------------------------------##
18 ## Copyright (C) 1998, 1999 Hugo WL ter Doest terdoest@cs.utwente.nl
19 ##
20 ## This library is free software; you can redistribute it and/or modify
21 ## it under the terms of the GNU General Public License as published by
22 ## the Free Software Foundation; either version 2 of the License, or
23 ## (at your option) any later version.
24 ##
25 ## This library is distributed in the hope that it will be useful,
26 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ## GNU General Public License for more details.
29 ##
30 ## You should have received a copy of the GNU Library General Public
31 ## License along with this program; if not, write to the Free Software
32 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 ##---------------------------------------------------------------------------##
34  
35  
36 ##---------------------------------------------------------------------------##
37 ## Globals
38 ##---------------------------------------------------------------------------##
39 3 ▶use vars qw($VERSION
Subroutine BEGIN: called CC=1 SCAR=0.0
40 @ISA
41 @EXPORT
42 $VECTOR_PACKAGE
43  
44 $debug
45 $SAMPLE_size
46 $NEWTON_max_it
47 $KL_max_it
48 $KL_min
49 $NEWTON_min
50 $cntrl_c_pressed
51 $cntrl_backslash_pressed
52 3 );
53  
54  
55 ##---------------------------------------------------------------------------##
56 ## Require libraries
57 ##---------------------------------------------------------------------------##
58 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 3 ▶use diagnostics -verbose;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 3 ▶use Statistics::SparseVector;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 3 $VECTOR_PACKAGE = "Statistics::SparseVector";
62 3 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 3 require Exporter;
66 3 require AutoLoader;
67  
68 3 @ISA = qw(Exporter AutoLoader);
69 # Items to export into callers namespace by default. Note: do not export
70 # names by default without a very good reason. Use EXPORT_OK instead.
71 # Do not simply export all your public functions/methods/constants.
72 3 @EXPORT = qw($KL_min
73 $NEWTON_min
74 $debug
75 $nr_add
76 $KL_max_it
77 $NEWTON_max_it
78 $SAMPLE_size
79 );
80  
81 3 $VERSION = '1.0';
82  
83  
84 # default values for some configurable parameters
85 3 $NEWTON_max_it = 20;
86 3 $NEWTON_min = 0.001;
87 3 $KL_max_it = 100;
88 3 $KL_min = 0.001;
89 3 $debug = 0;
90 3 $SAMPLE_size = 250; # the size of MC samples
91 # binary or integer feature functions
92  
93 # for catching interrupts
94 3 $cntrl_c_pressed = 0;
95 3 $cntrl_backslash_pressed = 0;
96 3 $SIG{INT} = \&catch_cntrl_c;
97 3 $SIG{QUIT} = \&catch_cntrl_backslash;
98  
99  
100 # checks floats
101 59812 ▶sub is_float {
Subroutine is_float: called CC=1 SCAR=0.0
Pod undocumented
102 59812 my($f) = @_;
103  
104 59812 return ($f =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/);
105 }
106  
107  
108 # interrrupt routine for control c
109 0 ▶sub catch_cntrl_c {
Subroutine catch_cntrl_c: not called CC=1 SCAR=6.9
Pod undocumented
110 0 my($signame) = shift;
111  
112 0 $cntrl_c_pressed = 1;
113 0 die "<CONTROL-C> pressed\n";
114 }
115  
116  
117 # interrrupt routine for control \ (originally core-dump request)
118 0 ▶sub catch_cntrl_backslash {
Subroutine catch_cntrl_backslash: not called CC=1 SCAR=6.9
Pod undocumented
119 0 my($signame) = shift;
120  
121 0 $cntrl_backslash_pressed = 1;
122 }
123  
124  
125 # creates a new event space
126 # depending on the $arg parameter samples it or reads it from a file
127 37 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
128 37 my($this, $vectype, $filename) = @_;
129  
130 # for calling $self->new($someth):
131 37 ▶ my $class = ref($this) || $this;
Condition66%2/3
ref $this || $this
l!l&&r!l&&!r
3340
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table66%2/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
132 37 my $self = {};
133 37 bless $self, $class;
134 37 $self->{SCALER} = "gis"; # default
135 37 $self->{SAMPLING} = "corpus"; # default
136 37 $self->{NR_CLASSES} = 0;
137 37 $self->{NR_EVENTS} = 0;
138 37 $self->{NR_FEATURES} = 0;
139 37 $self->{VECTYPE} = $vectype;
140 37 ▶ if ($filename) { # hey a filename
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) 4 33 37
141 4 $self->read($filename);
142 }
143 37 $self->{FEATURE_IGNORE} = $VECTOR_PACKAGE->new($self->{NR_FEATURES});
144 37 return($self);
145 }
146  
147  
148 # decides how to sample, "enum", "mc", or "corpus"
149 332 ▶sub sample {
Subroutine sample: called CC=3 SCAR=11.0
Pod undocumented
150 332 my($self) = @_;
151  
152 332 my($sample);
153  
154 332 ▶ if ($self->{SAMPLING} eq "mc") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'SAMPLING'} eq 'mc') { } 16 316 332
elsif ($self->{'SAMPLING'} eq 'enum') { } 17 299 316
155 16 $sample = $self->new();
156 16 $sample->{VECTYPE} = "binary";
157 16 $sample->{SCALER} = $self->{SCALER};
158 16 $sample->{NR_FEATURES} = $self->{NR_FEATURES};
159 # refer to the parameters of $self
160 16 $sample->{PARAMETERS} = $self->{PARAMETERS};
161 16 $sample->{NEED_CORRECTION_FEATURE} = 1;
162 16 $sample->{CORRECTION_PARAMETER} = $self->{CORRECTION_PARAMETER};
163 16 $sample->{E_REF} = $self->{E_REF};
164 16 $sample->{THIS_IS_A_SAMPLE} = 1;
165 16 $sample->mc($self);
166 16 $sample->{CLASSES_CHANGED} = 1;
167 16 $sample->prepare_model();
168 }
169 elsif ($self->{SAMPLING} eq "enum") {
170 17 $sample = $self->new();
171 17 $sample->{SCALER} = $self->{SCALER};
172 17 $sample->{SAMPLING} = "enum";
173 17 $sample->{NR_FEATURES} = $self->{NR_FEATURES};
174 17 $sample->{PARAMETERS} = $self->{PARAMETERS};
175 17 $sample->{NEED_CORRECTION_FEATURE} = 1;
176 17 $sample->{CORRECTION_PARAMETER} = $self->{CORRECTION_PARAMETER};
177 17 $sample->{E_REF} = $self->{E_REF};
178 17 $sample->{THIS_IS_A_SAMPLE} = 1;
179 17 $sample->{M} = $self->{NR_FEATURES};
180 }
181 else { # "corpus"
182 299 $sample = $self;
183 }
184 332 $sample->prepare_sample();
185 332 return($sample);
186 }
187  
188  
189 # makes sure that when prepare_model is called, everything is recomputed
190 14 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
191 14 my($self) = @_;
192
193 14 undef $self->{PARAMETERS_INITIALISED};
194 14 $self->{PARAMETERS_CHANGED} = 1;
195 14 $self->{CLASSES_CHANGED} = 1;
196 }
197  
198  
199  
200 343 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=8.1
201 343 my($self) = @_;
202
203 343 ▶ if ($cntrl_c_pressed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Statistics::MaxEntropy::cntrl_c_pressed) 0 343 343
204 0 $self->dump();
205 }
206 }
207  
208  
209 # reads an events file, dies in case of inconsistent lines
210 # syntax first line: <name> <tab> <name> <tab> ..... <newline>
211 # syntax other lines, binary functions: <freq> <bitvector> <newline>
212 # syntax other lines, integer functions: <freq> <intvector> <newline>
213 # an intvector is a space separated list of integers
214 4 ▶sub read {
Subroutine read: called CC=7 SCAR=19.7
Pod undocumented
215 4 my($self, $file) = @_;
216  
217 4 my($features,
218 $feature_names,
219 @cols);
220  
221 4 $feature_names = "";
222 4 ▶ open(EVENTS, $file) ||
Branch: 1 branch
BranchTrueFalseTotal
open EVENTS, $file or $self->die("Could not open $file\n") 4 0 4
223 $self->die("Could not open $file\n");
224 4 print "Opened $file\n";
225  
226 # read the names of the features, skip comment
227 # note that feature name are in reverse order now
228 4 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
not $feature_names =~ /\#.*/ or do { do { $feature_names = readline EVENTS } } 4 0 4
229 4 $feature_names = <EVENTS>;
230 } until ($feature_names !~ /\#.*/);
231 4 chomp $feature_names;
232 4 $self->{FEATURE_NAMES} = [split(/\t/, $feature_names)];
233 4 $self->{NR_FEATURES} = $#{$self->{FEATURE_NAMES}} + 1;
234  
235 # read the bitvectors
236 4 ▶ while (<EVENTS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline EVENTS)) 400 4 404
237 400 ▶ if (!/\#.*/) { # skip comments
Branch: 1 branch
BranchTrueFalseTotal
unless (/\#.*/) 400 0 400
238 400 chomp;
239  
240 400 ▶ if (/\s*(\d+)\s+(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\s*(\d+)\s+(.+)/) 400 0 400
241 400 $self->{FREQ}[$self->{NR_CLASSES}] = $1;
242 400 $features = $2;
243 }
244 400 ▶ if ($self->{FREQ} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"FREQ"} == 0) 0 400 400
245 0 $self->die("Class $self->{NR_CLASSES} has zero probability\n");
246 }
247 400 $self->{NR_EVENTS} += $self->{FREQ}[$self->{NR_CLASSES}];
248 $self->{CLASSES}[$self->{NR_CLASSES}] =
249 $VECTOR_PACKAGE->new_vec($self->{NR_FEATURES},
250 400 $features, $self->{VECTYPE});
251 400 $self->{NR_CLASSES}++;
252 }
253 }
254 4 close(EVENTS);
255  
256 4 print "Read $self->{NR_EVENTS} events, $self->{NR_CLASSES} classes, " .
257 "and $self->{NR_FEATURES} features\n";
258 4 print "Closed $file\n";
259  
260 4 $self->{FILENAME} = $file;
261 4 $self->{CLASSES_CHANGED} = 1;
262 4 $self->{PARAMETERS_CHANGED} = 1;
263 }
264  
265  
266 # reads an initial distribution
267 # syntax: one parameter per line
268 0 ▶sub read_parameters {
Subroutine read_parameters: not called CC=5 SCAR=34.0
Pod undocumented
269 0 my($self, $file) = @_;
270  
271 0 my($i);
272  
273 0 $i = 0;
274 0 ▶ open(DISTR,$file) ||
Branch: 1 branch
BranchTrueFalseTotal
open DISTR, $file or $self->die("Could not open $file\n") 0 0 0
275 $self->die("Could not open $file\n");
276 0 print "Opened $file\n";
277  
278 0 ▶ while (<DISTR>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DISTR)) 0 0 0
279 0 ▶ if (!/\#.*/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\#.*/) 0 0 0
280 0 chomp;
281 0 $self->{PARAMETERS}[$i++] = $_;
282 }
283 }
284  
285 0 close(DISTR);
286 0 ▶ if ($i != $self->{NR_FEATURES}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != $self->{"NR_FEATURES"}) 0 0 0
287 0 $self->die("Initial distribution file corrupt\n");
288 }
289 0 print "Read $i parameters; closed $file\n";
290 0 $self->{PARAMETERS_CHANGED} = 1;
291 }
292  
293  
294 # writes the the current parameters
295 # syntax: <parameter> <newline>
296 1 ▶sub write_parameters {
Subroutine write_parameters: called CC=4 SCAR=14.1
Pod documented
297 1 my($self, $file) = @_;
298  
299 1 my($i);
300  
301 1 ▶ open(DISTR,">$file") ||
Branch: 1 branch
BranchTrueFalseTotal
open DISTR, ">$file" or $self->die("Could not open $file\n") 1 0 1
302 $self->die("Could not open $file\n");
303 1 print "Opened $file\n";
304  
305 1 ▶ for ($i = 0; $i < $self->{NR_FEATURES}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"NR_FEATURES"}) 18 1 19
306 18 ▶ if ($self->{FEATURE_IGNORE}->bit_test($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'FEATURE_IGNORE'}->bit_test($i)) { } 0 18 18
307 0 print DISTR "IGNORED\n";
308 }
309 else {
310 18 print DISTR "$self->{PARAMETERS}[$i]\n";
311 }
312 }
313  
314 1 close(DISTR);
315 1 print "Closed $file\n";
316 }
317  
318  
319 # writes the the current features with their parameters
320 # syntax first line: <$nr_features> <newline>
321 # syntax last line: <bitmask> <newline>
322 # syntax other lines: <name> <parameter> <newline>
323 0 ▶sub write_parameters_with_names {
Subroutine write_parameters_with_names: not called CC=5 SCAR=34.0
Pod documented
324 0 my($self, $file) = @_;
325  
326 0 my($x,
327 $bitmask);
328  
329 0 ▶ open(DISTR,">$file") ||
Branch: 1 branch
BranchTrueFalseTotal
open DISTR, ">$file" or $self->die("Could not open $file\n") 0 0 0
330 $self->die("Could not open $file\n");
331 0 print "Opened $file\n";
332  
333 # preamble
334 0 print DISTR "$self->{NR_FEATURES}\n";
335 0 print DISTR "$self->{SCALER}\n";
336 0 ▶ if ($self->{SCALER} eq "gis") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SCALER"} eq "gis") 0 0 0
337 0 print DISTR "$self->{M}\n";
338 0 print DISTR "$self->{CORRECTION_PARAMETER}\n";
339 }
340  
341 # print feature names with parameters
342 # in the meanwhile build the bitmask
343 0 $bitmask = "";
344 0 ▶ for ($x = 0; $x < $self->{NR_FEATURES}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"NR_FEATURES"}) 0 0 0
345 0 print DISTR "$self->{FEATURE_NAMES}[$x]\t$self->{PARAMETERS}[$x]\n";
346 0 ▶ if ($self->{FEATURE_IGNORE}->bit_test($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'FEATURE_IGNORE'}->bit_test($x)) { } 0 0 0
347 0 $bitmask = "0" . $bitmask;
348 }
349 else {
350 0 $bitmask = "1" . $bitmask;
351 }
352 }
353 0 print DISTR "$bitmask\n";
354  
355 0 close(DISTR);
356 0 print "Closed $file\n";
357 }
358  
359  
360 # generate random parameters
361 2 ▶sub random_parameters {
Subroutine random_parameters: called CC=3 SCAR=11.0
Pod undocumented
362 2 my($self) = @_;
363  
364 2 my($f);
365  
366 2 srand();
367 2 ▶ for ($f = 0; $f < $self->{NR_FEATURES}; $f++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f < $self->{"NR_FEATURES"}) 20 2 22
368 20 $self->{PARAMETERS}[$f] = rand() + 1;
369 }
370 2 ▶ if ($self->{SCALER} eq "gis") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SCALER"} eq "gis") 1 1 2
371 1 $self->{CORRECTION_PARAMETER} = rand();
372 }
373 2 $self->{PARAMETERS_CHANGED} = 1;
374 }
375  
376  
377 # sets parameters to $val
378 12 ▶sub set_parameters_to {
Subroutine set_parameters_to: called CC=3 SCAR=11.0
Pod undocumented
379 12 my($self, $val) = @_;
380  
381 12 my($f);
382  
383 12 ▶ for ($f = 0; $f < $self->{NR_FEATURES}; $f++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f < $self->{"NR_FEATURES"}) 132 12 144
384 132 $self->{PARAMETERS}[$f] = $val;
385 }
386 12 ▶ if ($self->{SCALER} eq "gis") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SCALER"} eq "gis") 6 6 12
387 6 $self->{CORRECTION_PARAMETER} = $val;
388 }
389 12 $self->{PARAMETERS_CHANGED} = 1;
390 }
391  
392  
393 # initialise if !$self->{PARAMETERS_INITIALISED}; subsequent calls
394 # of scale (by fi) should not re-initialise parameters
395 22 ▶sub init_parameters {
Subroutine init_parameters: called CC=4 SCAR=13.9
Pod undocumented
396 22 my($self) = @_;
397  
398 22 ▶ if (!$self->{PARAMETERS_INITIALISED}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"PARAMETERS_INITIALISED"}) 14 8 22
399 14 ▶ if ($self->{SAMPLING} eq "mc") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SAMPLING'} eq 'mc') { } 2 12 14
400 # otherwise bits will be flipped with prob 1.
401 2 $self->random_parameters();
402 }
403 else {
404 12 ▶ if ($self->{SCALER} eq "gis") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SCALER'} eq 'gis') { } 6 6 12
405 6 $self->set_parameters_to(0);
406 }
407 else {
408 6 $self->set_parameters_to(0);
409 }
410 }
411 14 $self->{PARAMETERS_INITIALISED} = 1;
412 }
413 }
414  
415  
416 # make sure \tilde{p} << q_0
417 # constant feature functions are forbidden: that is why
418 # we check whether for all features \sum_x f(x) > 0
419 # and \sum_x f(x) != $corpus_size
420 420 ▶sub check {
Subroutine check: called CC=3 SCAR=11.8
Pod undocumented
421 420 my($self) = @_;
422  
423 420 my ($x);
424  
425 420 ▶ for ($x = 0; $x < $self->{NR_CLASSES}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"NR_CLASSES"}) 41984 420 42404
426 41984 ▶ if ($self->{CLASS_EXP_WEIGHTS}[$x] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CLASS_EXP_WEIGHTS"}[$x] == 0) 0 41984 41984
427 0 print "Initial distribution not ok; class $x\n";
428 0 print $self->{CLASS_EXP_WEIGHTS}[$x], "\t", $self->{CLASSES}[$x]->to_Bin(' '),"\n";
429 }
430 }
431 }
432  
433  
434 # writes events to a file
435 # usefull in case new features have been added
436 # syntax: same as input events file
437 1 ▶sub write {
Subroutine write: called CC=3 SCAR=11.0
Pod documented
438 1 my($self, $file) = @_;
439  
440 1 my($x, $f);
441  
442 # prologue
443 1 ▶ open(EVENTS,">$file") ||
Branch: 1 branch
BranchTrueFalseTotal
open EVENTS, ">$file" or $self->die("Could not open $file\n") 1 0 1
444 $self->die("Could not open $file\n");
445 1 print "Opened $file\n";
446  
447 # write a line with the feature names
448 1 print EVENTS join("\t", @{$self->{FEATURE_NAMES}}), "\n";
449 # write the events themselves
450 1 ▶ for ($x = 0; $x < $self->{NR_CLASSES}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"NR_CLASSES"}) 100 1 101
451 100 print EVENTS $self->{FREQ}[$x],"\t";
452 100 print EVENTS $self->{CLASSES}[$x]->to_Bin(' '), "\n";
453 }
454  
455 # close the file and tell you did that
456 1 close EVENTS;
457 1 print "Wrote $self->{NR_EVENTS} events, $self->{NR_CLASSES} classes, " .
458 "and $self->{NR_FEATURES} features\n";
459 1 print "Closed $file\n";
460 }
461  
462  
463 # reads a dump, and evaluates it into an object
464 0 ▶sub undump {
Subroutine undump: not called CC=2 SCAR=17.9
Pod documented
465 0 my($class, $file) = @_;
466  
467 0 my($x,
468 $VAR1);
469  
470 # open, slurp, and close file
471 0 ▶ open(UNDUMP, "$file") ||
Branch: 1 branch
BranchTrueFalseTotal
open UNDUMP, "$file" or croak("Could not open $file\n") 0 0 0
472 croak "Could not open $file\n";
473 0 print "Opened $file\n";
474 0 undef $/;
475 0 $x = <UNDUMP>;
476 0 $/ = "\n";
477 0 close(UNDUMP);
478  
479 # and undump
480 0 eval $x;
481 0 print "Undumped $VAR1->{NR_EVENTS} events, $VAR1->{NR_CLASSES} classes, " .
482 "and $VAR1->{NR_FEATURES} features\n";
483 0 print "Closed $file\n";
484 0 return($VAR1);
485 }
486  
487  
488 # makes dump of the event space using Data::Dumper
489 3 ▶sub dump {
Subroutine dump: called CC=9 SCAR=22.5
Pod documented
490 3 my($self, $file) = @_;
491  
492 3 my(@bitvecs,
493 $dump,
494 %features,
495 $f);
496  
497 3 ▶ if (!$file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 3 3
498 0 $file = POSIX::tmpnam();
499 }
500 3 ▶ open(DUMP, ">$file") ||
Branch: 1 branch
BranchTrueFalseTotal
open DUMP, ">$file" or croak("Could not open $file\n") 3 0 3
501 croak "Could not open $file\n";
502 3 print "Opened $file\n";
503  
504 # build something that we can sort
505 # ONLY FOR CORPUS!
506 3 ▶ if (!$self->{THIS_IS_A_SAMPLE} && $self->{PARAMETERS}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"THIS_IS_A_SAMPLE"} and $self->{"PARAMETERS"}) 3 0 3
Condition33%1/3
not $self->{"THIS_IS_A_SAMPLE"} and $self->{"PARAMETERS"}
!ll&&!rl&&r
003
MC/DC0%0/2
not $self->{"THIS_IS_A_SAMPLE"} and $self->{"PARAMETERS"}
not $self->{"THIS_IS_A_SAMPLE"} $self->{"PARAMETERS"}
Truth table33%1/3
not(A) and B
A= not $self->{"THIS_IS_A_SAMPLE"}B= $self->{"PARAMETERS"}
ABresult
0X0
100
111
507 3 ▶ for ($f = 0; $f < $self->{NR_FEATURES}; $f++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f < $self->{"NR_FEATURES"}) 38 3 41
508 $features{$self->{FEATURE_NAMES}[$f]} =
509 38 $self->{PARAMETERS}[$f];
510 }
511 3 ▶ if ($self->{NEED_CORRECTION_FEATURE} && ($self->{SCALER} eq "gis")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"NEED_CORRECTION_FEATURE"} and $self->{"SCALER"} eq "gis") 2 1 3
Condition66%2/3
$self->{"NEED_CORRECTION_FEATURE"} and $self->{"SCALER"} eq "gis"
!ll&&!rl&&r
012
MC/DC50%1/2
$self->{"NEED_CORRECTION_FEATURE"} and $self->{"SCALER"} eq "gis"
$self->{"NEED_CORRECTION_FEATURE"} $self->{"SCALER"} eq "gis"
Truth table66%2/3
A and B
A= $self->{"NEED_CORRECTION_FEATURE"}B= $self->{"SCALER"} eq "gis"
ABresult
0X0
100
111
512 $features{"correction$self->{M}"} =
513 2 $self->{CORRECTION_PARAMETER};
514 }
515 # and print it into $self
516 $self->{FEATURE_SORTED} = join(' > ',
517 sort {
518 3 ▶ if ($features{$b} == $features{$a}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($features{$b} == $features{$a}) { } 1 88 89
519 1 return($b cmp $a)}
520 else {
521 88 return ($features{$b} <=> $features{$a})
522 }
523 }
524 keys(%features));
525 }
526  
527 3 $dump = Data::Dumper->new([$self]);
528 3 print DUMP $dump->Dump();
529 3 print "Dumped $self->{NR_EVENTS} events, $self->{NR_CLASSES} classes, " .
530 "and $self->{NR_FEATURES} features\n";
531  
532 3 close(DUMP);
533 3 print "Closed $file\n";
534 }
535  
536  
537 # $msg is logged, the time is logged, a dump is created, and the
538 # program dies with $msg
539 0 ▶sub die {
Subroutine die: not called CC=1 SCAR=6.9
Pod undocumented
540 0 my($self, $msg) = @_;
541  
542 0 $self->log_msg($msg);
543 0 $self->log_msg(time());
544 0 $self->dump();
545 0 croak $msg;
546 }
547  
548  
549 # prints a msg to STDOUT, and appends it to $self->{LOG}
550 # so an emergency dump will contain some history information
551 532 ▶sub log_msg {
Subroutine log_msg: called CC=1 SCAR=0.0
Pod undocumented
552 532 my($self, $x) = @_;
553  
554 532 $self->{LOG} .= $x;
555 532 print $x;
556 }
557  
558  
559 # computes f_# for alle events; results in @sample_nr_feats_on
560 # computes %$sample_m_feats_on; a HOL from m
561 428 ▶sub active_features {
Subroutine active_features: called CC=13 SCAR=26.3
Pod undocumented
562 428 my($self) = @_;
563  
564 428 my($i,
565 $j,
566 $sum);
567  
568 428 ▶ if ($self->{CLASSES_CHANGED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CLASSES_CHANGED"}) 78 350 428
569 # check for constant features
570 78 ▶ for ($i = 0; $i < $self->{NR_FEATURES}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"NR_FEATURES"}) 984 78 1062
571 984 $sum = 0;
572 984 ▶ for ($j = 0; $j < $self->{NR_CLASSES}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $self->{"NR_CLASSES"}) 98240 984 99224
573 98240 $sum += $self->{CLASSES}[$j]->bit_test($i);
574 }
575 984 ▶ if (!$sum || ($sum == $self->{NR_CLASSES})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sum or $sum == $self->{"NR_CLASSES"}) 0 984 984
Condition33%1/3
not $sum or $sum == $self->{"NR_CLASSES"}
l!l&&r!l&&!r
00984
MC/DC0%0/2
not $sum or $sum == $self->{"NR_CLASSES"}
not $sum $sum == $self->{"NR_CLASSES"}
Truth table33%1/3
not(A) or B
A= not $sumB= $sum == $self->{"NR_CLASSES"}
ABresult
1X1
011
000
576 0 print "Feature ", $i + 1, " is constant ($sum), and will be ignored\n";
577 0 $self->{FEATURE_IGNORE}->Bit_On($i);
578 }
579 }
580 # M is needed for both gis and iis
581 # NEED_CORRECTION_FEATURE is for gis only
582 78 $self->{M} = 0;
583 78 $self->{NEED_CORRECTION_FEATURE} = 0;
584 78 ▶ for ($i = 0; $i < $self->{NR_CLASSES}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"NR_CLASSES"}) 7784 78 7862
585 7784 ▶ if ($self->{CLASSES}[$i]->Norm() > $self->{M}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CLASSES"}[$i]->Norm > $self->{"M"}) 257 7527 7784
586 # higher nr_features_active found
587 257 $self->{M} = $self->{CLASSES}[$i]->Norm();
588 257 $self->{NEED_CORRECTION_FEATURE} = 1;
589 }
590 }
591 78 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Statistics::MaxEntropy::debug) 0 78 78
592 0 print "M = $self->{M}\n";
593 }
594 # set up a hash from m to classes HOL; and the correction_feature
595 # CORRECTION_FEATURE FOR gis
596 78 undef $self->{M_FEATURES_ACTIVE};
597 78 ▶ for ($i = 0; $i < $self->{NR_CLASSES}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"NR_CLASSES"}) 7784 78 7862
598 7784 ▶ if ($self->{SCALER} eq "gis") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SCALER"} eq "gis") 3694 4090 7784
599 $self->{CORRECTION_FEATURE}[$i] =
600 3694 $self->{M} - $self->{CLASSES}[$i]->Norm();
601 }
602 }
603 78 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Statistics::MaxEntropy::debug) 0 78 78
604 0 print "M = $self->{M}\n";
605 }
606 # observed feature expectations
607 78 ▶ if (!$self->{THIS_IS_A_SAMPLE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"THIS_IS_A_SAMPLE"}) 62 16 78
608 62 $self->E_reference();
609 }
610 78 undef $self->{CLASSES_CHANGED};
611 }
612 }
613  
614  
615 # compute the class probabilities according to the parameters
616 428 ▶sub prepare_model {
Subroutine prepare_model: called CC=9 SCAR=22.1
Pod undocumented
617 428 my($self) = @_;
618  
619 428 my ($x,
620 $f);
621  
622 428 $self->active_features();
623 428 ▶ if ($self->{PARAMETERS_CHANGED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PARAMETERS_CHANGED"}) 420 8 428
624 420 $self->{Z} = 0;
625 420 ▶ for ($x = 0; $x < $self->{NR_CLASSES}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"NR_CLASSES"}) 41984 420 42404
626 41984 $self->{CLASS_LOG_WEIGHTS}[$x] = 0;
627 41984 for $f ($self->{CLASSES}[$x]->indices()) {
628 $self->{CLASS_LOG_WEIGHTS}[$x] += $self->{PARAMETERS}[$f] *
629 91350 $self->{CLASSES}[$x]->weight($f);
630 91350 ▶ if ($f >= $self->{NR_FEATURES}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f >= $self->{"NR_FEATURES"}) 0 91350 91350
631 0 print "alarm: wrong index: $f\n";
632 }
633 }
634 41984 ▶ if ($self->{NEED_CORRECTION_FEATURE} && ($self->{SCALER} eq "gis")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"NEED_CORRECTION_FEATURE"} and $self->{"SCALER"} eq "gis") 27894 14090 41984
Condition66%2/3
$self->{"NEED_CORRECTION_FEATURE"} and $self->{"SCALER"} eq "gis"
!ll&&!rl&&r
01409027894
MC/DC50%1/2
$self->{"NEED_CORRECTION_FEATURE"} and $self->{"SCALER"} eq "gis"
$self->{"NEED_CORRECTION_FEATURE"} $self->{"SCALER"} eq "gis"
Truth table66%2/3
A and B
A= $self->{"NEED_CORRECTION_FEATURE"}B= $self->{"SCALER"} eq "gis"
ABresult
0X0
100
111
635 $self->{CLASS_LOG_WEIGHTS}[$x] += $self->{CORRECTION_FEATURE}[$x] *
636 27894 $self->{CORRECTION_PARAMETER};
637 }
638 41984 $self->{CLASS_EXP_WEIGHTS}[$x] = exp($self->{CLASS_LOG_WEIGHTS}[$x]);
639 41984 $self->{Z} += $self->{CLASS_EXP_WEIGHTS}[$x];
640 }
641 print "prepare_model: \$Z is not a number: $self->{Z}\n"
642 420 ▶ unless is_float($self->{Z});
Branch: 1 branch
BranchTrueFalseTotal
unless is_float($self->{"Z"}) 0 420 420
643  
644 420 ▶ if (!$self->{THIS_IS_A_SAMPLE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"THIS_IS_A_SAMPLE"}) 404 16 420
645 404 $self->entropies();
646 }
647 420 $self->check();
648 420 undef $self->{PARAMETERS_CHANGED};
649 }
650 }
651  
652  
653 332 ▶sub prepare_sample {
Subroutine prepare_sample: called CC=2 SCAR=6.9
Pod undocumented
654 332 my($self) = @_;
655  
656 # expectations
657 332 ▶ if ($self->{SCALER} eq "gis") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SCALER'} eq 'gis') { } 237 95 332
658 237 $self->E_loglinear();
659 }
660 else {
661 # A_{mj}
662 95 $self->A();
663 }
664 }
665  
666  
667 # feature expectations for the MaxEnt distribution
668 237 ▶sub E_loglinear {
Subroutine E_loglinear: called CC=9 SCAR=22.0
Pod undocumented
669 237 my($self) = @_;
670  
671 237 my($x,
672 $f,
673 $vec,
674 $weight,
675 $Z);
676  
677 237 undef $self->{E_LOGLIN};
678 237 ▶ if ($self->{SAMPLING} eq "enum") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SAMPLING'} eq 'enum') { } 9 228 237
679 9 $vec = $VECTOR_PACKAGE->new($self->{NR_FEATURES});
680 9 $self->{Z} = 0;
681 9 ▶ for ($x = 0; $x < 2 ** $self->{NR_FEATURES}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 2 ** $self->{"NR_FEATURES"}) 18432 9 18441
682 18432 $weight = $self->weight($vec);
683 18432 for $f ($vec->indices()) {
684 103424 $self->{E_LOGLIN}[$f] += $weight * $vec->weight($f);
685 }
686 $self->{E_LOGLIN}[$self->{NR_FEATURES}] += $weight *
687 18432 ($self->{M} - $vec->Norm());
688 18432 $self->{Z} += $weight;
689 18432 $vec->increment();
690 }
691 9 for $f (0..$self->{NR_FEATURES}) {
692 106 $self->{E_LOGLIN}[$f] /= $self->{Z};
693 }
694 }
695 else { # either corpus or mc sample
696 228 ▶ for ($x = 0; $x < $self->{NR_CLASSES}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"NR_CLASSES"}) 22794 228 23022
697 22794 for $f ($self->{CLASSES}[$x]->indices()) {
698 $self->{E_LOGLIN}[$f] += $self->{CLASS_EXP_WEIGHTS}[$x] *
699 48952 $self->{CLASSES}[$x]->weight($f);
700 }
701 22794 ▶ if ($self->{NEED_CORRECTION_FEATURE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"NEED_CORRECTION_FEATURE"}) 22794 0 22794
702 $self->{E_LOGLIN}[$self->{NR_FEATURES}] +=
703 $self->{CLASS_EXP_WEIGHTS}[$x] *
704 22794 ($self->{M} - $self->{CLASSES}[$x]->Norm());
705 }
706 }
707 228 for $f (0..$self->{NR_FEATURES}) {
708 2682 $self->{E_LOGLIN}[$f] /= $self->{Z};
709 }
710 }
711 }
712  
713  
714 # observed feature expectations
715 62 ▶sub E_reference {
Subroutine E_reference: called CC=6 SCAR=17.9
Pod undocumented
716 62 my($self) = @_;
717  
718 62 my($x,
719 $f,
720 @sum);
721  
722 62 ▶ for ($x = 0; $x < $self->{NR_CLASSES}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"NR_CLASSES"}) 6200 62 6262
723 6200 for $f ($self->{CLASSES}[$x]->indices()) {
724 11765 $sum[$f] += $self->{FREQ}[$x] * $self->{CLASSES}[$x]->weight($f);
725 }
726 6200 ▶ if ($self->{SCALER} eq "gis") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SCALER"} eq "gis") 3100 3100 6200
727 $sum[$self->{NR_FEATURES}] += $self->{CORRECTION_FEATURE}[$x] *
728 3100 $self->{FREQ}[$x];
729 }
730 }
731 62 for $f (0..$self->{NR_FEATURES}) {
732 886 ▶ if ($sum[$f]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sum[$f]) 855 31 886
733 855 $self->{E_REF}[$f] = $sum[$f] / $self->{NR_EVENTS};
734 }
735 }
736 }
737  
738  
739 # compute several entropies
740 404 ▶sub entropies {
Subroutine entropies: called CC=3 SCAR=11.0
Pod undocumented
741 404 my($self) = @_;
742  
743 404 my ($i,
744 $w,
745 $log_w,
746 $w_ref,
747 $log_w_ref);
748  
749 404 $self->{H_p} = 0;
750 404 $self->{H_cross} = 0;
751 404 $self->{H_p_ref} = 0;
752 404 $self->{KL} = 0;
753 404 ▶ for ($i = 0; $i < $self->{NR_CLASSES}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"NR_CLASSES"}) 40400 404 40804
754 40400 $w = $self->{CLASS_EXP_WEIGHTS}[$i];
755 # we don't know whether $p > 0
756 40400 $log_w = $self->{CLASS_LOG_WEIGHTS}[$i];
757 40400 $w_ref = $self->{FREQ}[$i];
758 # we know that $p_ref > 0
759 40400 $log_w_ref = log($w_ref);
760 # update the sums
761 40400 $self->{H_p} -= $w * $log_w;
762 40400 $self->{H_cross} -= $w_ref * $log_w;
763 40400 $self->{KL} += $w_ref * ($log_w_ref - $log_w);
764 40400 $self->{H_p_ref} -= $w_ref * $log_w_ref;
765 40400 ▶ if ($w == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w == 0) 0 40400 40400
766 0 $self->log_msg("entropies: skipping event $i (p^n($i) = 0)\n");
767 }
768 }
769 # normalise
770 404 $self->{H_p} = $self->{H_p} / $self->{Z} + log($self->{Z});
771 404 $self->{H_cross} = $self->{H_cross} / $self->{NR_EVENTS} + log($self->{Z});
772 $self->{KL} = $self->{KL} / $self->{NR_EVENTS} - log($self->{NR_EVENTS}) +
773 404 log($self->{Z});
774 404 $self->{H_p_ref} = $self->{H_p_ref} / $self->{NR_EVENTS} + log($self->{NR_EVENTS});
775 404 $self->{L} = -$self->{H_cross};
776 }
777  
778  
779 # unnormalised p(x,y)
780 # $x is required, $y is optional
781 # $x->Size()+$y->Size() == $self->{NR_FEATURES}
782 77824 ▶sub weight {
Subroutine weight: called CC=11 SCAR=31.2
Pod undocumented
783 77824 my($self, $x, $y) = @_;
784  
785 77824 my ($f,
786 $sum,
787 $norm);
788  
789 77824 $sum = 0;
790 77824 for $f ($x->indices()) {
791 498688 ▶ if (!$self->{FEATURE_IGNORE}->bit_test($f)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"FEATURE_IGNORE"}->bit_test($f)) 498688 0 498688
792 498688 $sum += $self->{PARAMETERS}[$f] * $x->weight($f);
793 498688 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Statistics::MaxEntropy::debug) 0 498688 498688
794 0 print "Current weight: $sum, current feature: $f\n";
795 }
796 }
797 }
798 77824 $norm = $x->Norm();
799 # if $y is defined,
800 # then $x->Size()+$y->Size() == $self->{NR_FEATURES} should hold:
801 77824 ▶ if (defined($y) && (($x->Size() + $y->Size()) == $self->{NR_FEATURES})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $y and $x->Size + $y->Size == $self->{"NR_FEATURES"}) 0 77824 77824
Condition33%1/3
defined $y and $x->Size + $y->Size == $self->{"NR_FEATURES"}
!ll&&!rl&&r
7782400
MC/DC0%0/2
defined $y and $x->Size + $y->Size == $self->{"NR_FEATURES"}
defined $y $x->Size + $y->Size == $self->{"NR_FEATURES"}
Truth table33%1/3
A and B
A= defined $yB= $x->Size + $y->Size == $self->{"NR_FEATURES"}
ABresult
0X0
100
111
802 0 for $f ($y->indices()) {
803 0 ▶ if (!$self->{FEATURE_IGNORE}->bit_test($f + $x->Size())) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"FEATURE_IGNORE"}->bit_test($f + $x->Size)) 0 0 0
804 0 $sum += $self->{PARAMETERS}[$f + $x->Size()] *
805 $y->weight($f);
806 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Statistics::MaxEntropy::debug) 0 0 0
807 0 print "Current weight: $sum, current feature: $f\n";
808 }
809 }
810 }
811 0 $norm += $y->Norm();
812 }
813 77824 ▶ if ($self->{NEED_CORRECTION_FEATURE} && ($self->{SCALER} eq "gis")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"NEED_CORRECTION_FEATURE"} and $self->{"SCALER"} eq "gis") 18432 59392 77824
Condition66%2/3
$self->{"NEED_CORRECTION_FEATURE"} and $self->{"SCALER"} eq "gis"
!ll&&!rl&&r
05939218432
MC/DC50%1/2
$self->{"NEED_CORRECTION_FEATURE"} and $self->{"SCALER"} eq "gis"
$self->{"NEED_CORRECTION_FEATURE"} $self->{"SCALER"} eq "gis"
Truth table66%2/3
A and B
A= $self->{"NEED_CORRECTION_FEATURE"}B= $self->{"SCALER"} eq "gis"
ABresult
0X0
100
111
814 18432 $sum += ($self->{M} - $norm) * $self->{CORRECTION_PARAMETER};
815 }
816 77824 return(exp($sum));
817 }
818  
819  
820 # computes the `a' coefficients of
821 # \sum_{m=0}^{M} a_{m,j}^{(n)} e^{\alpha^{(n)}_j m}
822 # according to the current distribution
823 95 ▶sub A {
Subroutine A: called CC=7 SCAR=19.5
Pod undocumented
824 95 my($self) = @_;
825  
826 95 my($f,
827 $m,
828 $x,
829 $weight,
830 $vec,
831 $class);
832  
833 95 undef $self->{A};
834 95 undef $self->{C};
835 95 ▶ if ($self->{SAMPLING} eq "enum") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SAMPLING'} eq 'enum') { } 8 87 95
836 8 undef $self->{Z};
837 8 $vec = $VECTOR_PACKAGE->new($self->{NR_FEATURES});
838 8 ▶ for ($x = 0; $x < 2 ** $self->{NR_FEATURES}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 2 ** $self->{"NR_FEATURES"}) 59392 8 59400
839 59392 $weight = $self->weight($vec);
840 59392 for $f ($vec->indices()) {
841 395264 $self->{A}{$vec->Norm()}{$f} += $weight * $vec->weight($f);
842 395264 $self->{C}{$vec->Norm()}{$f} += $vec->weight($f);
843 }
844 59392 $self->{Z} += $weight;
845 59392 ▶ print "Z = $self->{Z}" unless is_float($self->{Z});
Branch: 1 branch
BranchTrueFalseTotal
unless is_float($self->{"Z"}) 0 59392 59392
846 59392 $vec->increment();
847 }
848 }
849 else { # mc or corpus
850 87 ▶ for ($class = 0; $class < $self->{NR_CLASSES}; $class++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class < $self->{"NR_CLASSES"}) 8690 87 8777
851 8690 for $f ($self->{CLASSES}[$class]->indices()) {
852 $self->{A}{$self->{CLASSES}[$class]->Norm()}{$f} +=
853 $self->{CLASS_EXP_WEIGHTS}[$class] *
854 22204 $self->{CLASSES}[$class]->weight($f);
855 $self->{C}{$self->{CLASSES}[$class]->Norm()}{$f} +=
856 22204 $self->{CLASSES}[$class]->weight($f);
857 }
858 }
859 }
860 }
861  
862  
863 #
864 # Monte Carlo sampling with the Metropolis update
865 #
866  
867 # returns heads up with probability $load
868 4120 ▶sub loaded_die {
Subroutine loaded_die: called CC=2 SCAR=6.9
Pod undocumented
869 4120 my($load) = @_;
870  
871 4120 ▶ (rand() <= $load) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
rand() <= $load ? : 2153 1967 4120
872 }
873  
874  
875 # samples from the probability distribution of $other to create $self
876 # we use the so-called Metropolis update R = h(new)/h(old)
877 # Metropolis algorithm \cite{neal:probabilistic}
878 16 ▶sub mc {
Subroutine mc: called CC=8 SCAR=20.8
Pod undocumented
879 16 my($self, $other, $type) = @_;
880  
881 16 my($R,
882 $weight,
883 $state,
884 $old_weight,
885 $k,
886 %events
887 );
888  
889 16 srand();
890 # take some class from the sample space as initial state
891 16 $state = $VECTOR_PACKAGE->new($self->{NR_FEATURES});
892 # make sure there are no constant features!
893 16 $state->Fill();
894 16 $events{$state->to_Bin(' ')}++;
895 16 $state->Empty();
896 16 $weight = 0;
897 # iterate
898 16 $k = 0;
899  
900 do {
901 4120 $old_weight = $weight;
902 4120 ▶ if ($state->bit_flip($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->bit_flip($k)) { } 1143 2977 4120
903 1143 $weight += $self->{PARAMETERS}[$k];
904 }
905 else {
906 2977 $weight -= $self->{PARAMETERS}[$k];
907 }
908 4120 $R = exp($weight - $old_weight);
909 4120 ▶ if (!loaded_die(1 < $R ? 1 : $R)) { # stay at the old state
Branch: 2 branches
BranchTrueFalseTotal
if (not loaded_die(1 < $R ? 1 : $R)) { } 1967 2153 4120
1 < $R ? : 1125 2995 4120
910 1967 $state->bit_flip($k);
911 1967 $weight = $old_weight;
912 }
913 else { # add state
914 2153 $events{$state->to_Bin(' ')}++;
915 }
916 4120 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Statistics::MaxEntropy::debug) 0 4120 4120
917 0 print $state->to_Bin(' '),"\t",scalar(keys(%events)),"\t$R\n";
918 }
919 # next component
920 4120 $k = ($k + 1) % $self->{NR_FEATURES};
921 } until ((scalar(keys(%events)) == $SAMPLE_size) ||
922 16 ▶ (scalar(keys(%events)) == 2 ** $self->{NR_FEATURES}));
Branch: 1 branch
BranchTrueFalseTotal
(scalar keys %events == $Statistics::MaxEntropy::SAMPLE_size or scalar keys %events == 2 ** $self->{"NR_FEATURES"}) or do { do { $old_weight = $weight; if ($state->bit_flip($k)) { $weight += $self->{"PARAMETERS"}[$k]; } else { $weight -= $self->{"PARAMETERS"}[$k]; } ; $R = exp $weight - $old_weight; if (not loaded_die(1 < $R ? 1 : $R)) { $state->bit_flip($k); $weight = $old_weight; } else { ++$events{$state->to_Bin(" ")}; } ; if ($Statistics::MaxEntropy::debug) { print $state->to_Bin(" "), "\t", scalar keys %events, "\t$R\n"; } ; $k = ($k + 1) % $self->{"NR_FEATURES"} } } 16 4104 4120
Condition66%2/3
scalar keys %events == $Statistics::MaxEntropy::SAMPLE_size or scalar keys %events == 2 ** $self->{"NR_FEATURES"}
l!l&&r!l&&!r
1604104
MC/DC50%1/2
scalar keys %events == $Statistics::MaxEntropy::SAMPLE_size or scalar keys %events == 2 ** $self->{"NR_FEATURES"}
scalar keys %events == $Statistics::MaxEntropy::SAMPLE_size scalar keys %events == 2 ** $self->{"NR_FEATURES"}
Truth table66%2/3
A or B
A= scalar keys %events == $Statistics::MaxEntropy::SAMPLE_sizeB= scalar keys %events == 2 ** $self->{"NR_FEATURES"}
ABresult
1X1
011
000
923  
924 16 for (keys(%events)) {
925 1600 push @{$self->{CLASSES}},
926 1600 $VECTOR_PACKAGE->new_vec($self->{NR_FEATURES}, $_, $self->{VECTYPE});
927 }
928 16 $self->{NR_CLASSES} = scalar(keys(%events)) - 1;
929  
930 16 $self->{CLASSES_CHANGED} = 1;
931 16 $self->{PARAMETERS_CHANGED} = 1;
932 }
933  
934  
935 #
936 # IIS
937 #
938  
939 # Newton estimation according to (Abney 1997), Appendix B
940 0 ▶sub C_func {
Subroutine C_func: not called CC=6 SCAR=37.4
Pod undocumented
941 0 my($self, $j, $x) = @_;
942  
943 0 my($m,
944 $s0,
945 $s1,
946 $a_x_m);
947  
948 0 $s0 = - $self->{NR_EVENTS} * $self->{E_REF}[$j];
949 0 $s1 = 0;
950 0 ▶ for ($m = 1; $m <= $self->{M}; $m++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m <= $self->{"M"}) 0 0 0
951 0 ▶ if ($self->{"C"}{$m}{$j}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"C"}{$m}{$j}) 0 0 0
952 0 $a_x_m = $self->{"C"}{$m}{$j} * exp($x * $m);
953 0 $s0 += $a_x_m;
954 0 $s1 += $m * $a_x_m;
955 }
956 }
957 0 ▶ print "sum_func not a number: $s0\n"
Branch: 1 branch
BranchTrueFalseTotal
unless is_float($s0) 0 0 0
958 unless is_float($s0);
959 0 ▶ print "sum_deriv not a number: $s1\n"
Branch: 1 branch
BranchTrueFalseTotal
unless is_float($s1) 0 0 0
960 unless is_float($s1);
961  
962 0 ▶ if ($s1 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s1 == 0) { } 0 0 0
963 0 return(0);
964 }
965 else {
966 0 return($s0 / $s1);
967 }
968 }
969  
970  
971 # Newton estimation according to (Della Pietra et al. 1997)
972 3491 ▶sub A_func {
Subroutine A_func: called CC=4 SCAR=13.9
Pod undocumented
973 3491 my($self, $j, $x) = @_;
974  
975 3491 my($m,
976 $sum_func,
977 $sum_deriv,
978 $a_x_m);
979  
980 3491 $sum_func = -$self->{E_REF}[$j] * $self->{Z};
981 3491 $sum_deriv = 0;
982 3491 ▶ for ($m = 1; $m <= $self->{M}; $m++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m <= $self->{"M"}) 44809 3491 48300
983 44809 ▶ if ($self->{"A"}{$m}{$j}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"A"}{$m}{$j}) 21249 23560 44809
984 21249 $a_x_m = $self->{"A"}{$m}{$j} * exp($x * $m);
985 21249 $sum_func += $a_x_m;
986 21249 $sum_deriv += $m * $a_x_m;
987 }
988 }
989 3491 ▶ if ($sum_deriv == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sum_deriv == 0) { } 0 3491 3491
990 0 return(0);
991 }
992 else {
993 3491 return($sum_func / $sum_deriv);
994 }
995 }
996  
997  
998 # solves \alpha from
999 # \sum_{m=0}^{M} a_{m,j}^{(n)} e^{\alpha^{(n)}_j m}=0
1000 1093 ▶sub iis_estimate_with_newton {
Subroutine iis_estimate_with_newton: called CC=5 SCAR=16.2
Pod undocumented
1001 1093 my($self, $i) = @_;
1002  
1003 1093 my($x,
1004 $old_x,
1005 $deriv_res,
1006 $func_res,
1007 $k);
1008  
1009 # $x = log(0)
1010 1093 $x = 0;
1011 1093 $k = 0;
1012  
1013 # do newton's method
1014 1093 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
(abs $x - $old_x <= $Statistics::MaxEntropy::NEWTON_min or $k++ > $Statistics::MaxEntropy::NEWTON_max_it) or do { do { $old_x = $x; if ($self->{"SAMPLING"} eq "enum") { $x -= $self->A_func($i, $x); } else { $x -= $self->A_func($i, $x); } } } 1093 2398 3491
Condition66%2/3
abs $x - $old_x <= $Statistics::MaxEntropy::NEWTON_min or $k++ > $Statistics::MaxEntropy::NEWTON_max_it
l!l&&r!l&&!r
109302398
MC/DC50%1/2
abs $x - $old_x <= $Statistics::MaxEntropy::NEWTON_min or $k++ > $Statistics::MaxEntropy::NEWTON_max_it
abs $x - $old_x <= $Statistics::MaxEntropy::NEWTON_min $k++ > $Statistics::MaxEntropy::NEWTON_max_it
Truth table66%2/3
A or B
A= abs $x - $old_x <= $Statistics::MaxEntropy::NEWTON_minB= $k++ > $Statistics::MaxEntropy::NEWTON_max_it
ABresult
1X1
011
000
1015 # save old x
1016 3491 $old_x = $x;
1017 # compute new x
1018 3491 ▶ if ($self->{SAMPLING} eq "enum") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SAMPLING'} eq 'enum') { } 504 2987 3491
1019 # (DDL 1997)
1020 504 $x -= $self->A_func($i, $x);
1021 }
1022 else {
1023 # sample -> (Abney 1997)
1024 2987 $x -= $self->A_func($i, $x);
1025 }
1026 } until ((abs($x - $old_x) <= $NEWTON_min) ||
1027 ($k++ > $NEWTON_max_it));
1028 1093 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Statistics::MaxEntropy::debug) 0 1093 1093
1029 0 print "Estimated gamma_$i with Newton's method: $x\n";
1030 }
1031 1093 return($x);
1032 }
1033  
1034  
1035 # updates parameter $i
1036 332 ▶sub gamma {
Subroutine gamma: called CC=6 SCAR=18.0
Pod undocumented
1037 332 my($self, $sample) = @_;
1038  
1039 332 my($f);
1040  
1041 332 for $f (0..$self->{NR_FEATURES} - 1) {
1042 3644 ▶ if (!$self->{FEATURE_IGNORE}->bit_test($f)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"FEATURE_IGNORE"}->bit_test($f)) 3644 0 3644
1043 3644 ▶ if ($self->{SCALER} eq "gis") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SCALER'} eq 'gis') { } 2551 1093 3644
1044 $self->{PARAMETERS}[$f] +=
1045 2551 log($self->{E_REF}[$f] / $sample->{E_LOGLIN}[$f]) / $sample->{M};
1046 }
1047 else {
1048 1093 $self->{PARAMETERS}[$f] +=
1049 $sample->iis_estimate_with_newton($f);
1050 }
1051 }
1052 }
1053  
1054 332 ▶ if (($self->{SCALER} eq "gis") && ($self->{NEED_CORRECTION_FEATURE})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SCALER"} eq "gis" and $self->{"NEED_CORRECTION_FEATURE"}) 237 95 332
Condition66%2/3
$self->{"SCALER"} eq "gis" and $self->{"NEED_CORRECTION_FEATURE"}
!ll&&!rl&&r
950237
MC/DC50%1/2
$self->{"SCALER"} eq "gis" and $self->{"NEED_CORRECTION_FEATURE"}
$self->{"SCALER"} eq "gis" $self->{"NEED_CORRECTION_FEATURE"}
Truth table66%2/3
A and B
A= $self->{"SCALER"} eq "gis"B= $self->{"NEED_CORRECTION_FEATURE"}
ABresult
0X0
100
111
1055 $self->{CORRECTION_PARAMETER} +=
1056 log($self->{E_REF}[$self->{NR_FEATURES}] /
1057 237 $sample->{E_LOGLIN}[$self->{NR_FEATURES}]) / $self->{M};
1058 }
1059 }
1060  
1061  
1062 # the iterative scaling algorithms
1063 22 ▶sub scale {
Subroutine scale: called CC=14 SCAR=27.2
Pod documented
1064 22 my($self, $sampling, $scaler) = @_;
1065  
1066 22 my($k,
1067 $i,
1068 $kl,
1069 $old_kl,
1070 $diff,
1071 $sample,
1072 $old_correction_parameter,
1073 @old_parameters);
1074  
1075 22 ▶ if ($sampling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sampling) 10 12 22
1076 10 $self->{SAMPLING} = $sampling;
1077 }
1078 22 ▶ if ($scaler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scaler) 10 12 22
1079 10 $self->{SCALER} = $scaler;
1080 }
1081 22 ▶ if (($self->{SAMPLING} eq "enum") && ($self->{VECTYPE} eq "integer")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SAMPLING"} eq "enum" and $self->{"VECTYPE"} eq "integer") 0 22 22
Condition66%2/3
$self->{"SAMPLING"} eq "enum" and $self->{"VECTYPE"} eq "integer"
!ll&&!rl&&r
1480
MC/DC0%0/2
$self->{"SAMPLING"} eq "enum" and $self->{"VECTYPE"} eq "integer"
$self->{"SAMPLING"} eq "enum" $self->{"VECTYPE"} eq "integer"
Truth table66%2/3
A and B
A= $self->{"SAMPLING"} eq "enum"B= $self->{"VECTYPE"} eq "integer"
ABresult
0X0
100
111
1082 0 $self->die("Cannot enumerate integer vectors\n");
1083 }
1084 22 ▶ if (($self->{SAMPLING} eq "mc") && ($self->{VECTYPE} eq "integer")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SAMPLING"} eq "mc" and $self->{"VECTYPE"} eq "integer") 0 22 22
Condition66%2/3
$self->{"SAMPLING"} eq "mc" and $self->{"VECTYPE"} eq "integer"
!ll&&!rl&&r
2020
MC/DC0%0/2
$self->{"SAMPLING"} eq "mc" and $self->{"VECTYPE"} eq "integer"
$self->{"SAMPLING"} eq "mc" $self->{"VECTYPE"} eq "integer"
Truth table66%2/3
A and B
A= $self->{"SAMPLING"} eq "mc"B= $self->{"VECTYPE"} eq "integer"
ABresult
0X0
100
111
1085 0 $self->die("Cannot sample from integer vector space\n");
1086 }
1087  
1088 22 $self->init_parameters();
1089 22 $self->prepare_model();
1090 22 $self->log_msg("scale($self->{SCALER}, $self->{SAMPLING}, $self->{VECTYPE}): H(p_ref)=$self->{H_p_ref}\nit.\tD(p_ref||p)\t\tH(p)\t\t\tL(p_ref,p)\t\ttime\n0\t$self->{KL}\t$self->{H_p}\t$self->{L}\t" . time() . "\n");
1091 22 $k = 0;
1092 22 $kl = 1e99;
1093 22 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
($diff <= $Statistics::MaxEntropy::KL_min or $k > $Statistics::MaxEntropy::KL_max_it or $diff < 0) or do { do { @old_parameters = @{$self->{"PARAMETERS"};}; $old_correction_parameter = $self->{"CORRECTION_PARAMETER"}; if ($sample) { $sample->DESTROY; } ; $sample = $self->sample; $self->gamma($sample); $self->{"PARAMETERS_CHANGED"} = 1; $self->prepare_model; $diff = $kl - $self->{"KL"}; $kl = $self->{"KL"}; ++$k; $self->log_msg(qq[$k\t$self->{"KL"}\t$self->{"H_p"}\t$self->{"L"}\t] . time . "\n"); if ($Statistics::MaxEntropy::debug) { $self->check; } ; if ($diff < 0) { $self->log_msg("Scaling is not converging (anymore); will revert parameters!\n"); $self->{"PARAMETERS"} = \@old_parameters; $self->{"CORRECTION_PARAMETER"} = $old_correction_parameter; $self->{"PARAMETERS_CHANGED"} = 1; $self->prepare_model; } ; if ($Statistics::MaxEntropy::cntrl_backslash_pressed) { $self->dump; $Statistics::MaxEntropy::cntrl_backslash_pressed = 0; } } } 22 310 332
Condition66%4/6
$diff <= $Statistics::MaxEntropy::KL_min or $k > $Statistics::MaxEntropy::KL_max_it or $diff < 0
l!l&&r!l&&!r
220310
$diff <= $Statistics::MaxEntropy::KL_min or $k > $Statistics::MaxEntropy::KL_max_it
l!l&&r!l&&!r
220310
MC/DC33%1/3
$diff <= $Statistics::MaxEntropy::KL_min or $k > $Statistics::MaxEntropy::KL_max_it or $diff < 0
$diff <= $Statistics::MaxEntropy::KL_min $k > $Statistics::MaxEntropy::KL_max_it $diff < 0
Truth table50%2/4
A or B or C
A= $diff <= $Statistics::MaxEntropy::KL_minB= $k > $Statistics::MaxEntropy::KL_max_itC= $diff < 0
ABCresult
1XX1
01X1
0011
0000
1094 # store parameters for reverting if converging stops
1095 332 @old_parameters = @{$self->{PARAMETERS}};
1096 332 $old_correction_parameter = $self->{CORRECTION_PARAMETER};
1097 332 ▶ if ($sample) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sample) 310 22 332
1098 310 $sample->DESTROY();
1099 }
1100 332 $sample = $self->sample();
1101 332 $self->gamma($sample);
1102 332 $self->{PARAMETERS_CHANGED} = 1;
1103 332 $self->prepare_model();
1104 332 $diff = $kl - $self->{KL};
1105 332 $kl = $self->{KL};
1106  
1107 332 $k++;
1108 332 $self->log_msg("$k\t$self->{KL}\t$self->{H_p}\t$self->{L}\t" . time() . "\n");
1109 332 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Statistics::MaxEntropy::debug) 0 332 332
1110 0 $self->check();
1111 }
1112 332 ▶ if ($diff < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff < 0) 10 322 332
1113 10 $self->log_msg("Scaling is not converging (anymore); will revert parameters!\n");
1114 # restore old parameters
1115 10 $self->{PARAMETERS} = \@old_parameters;
1116 10 $self->{CORRECTION_PARAMETER} = $old_correction_parameter;
1117 10 $self->{PARAMETERS_CHANGED} = 1;
1118 10 $self->prepare_model();
1119 }
1120 332 ▶ if ($cntrl_backslash_pressed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Statistics::MaxEntropy::cntrl_backslash_pressed) 0 332 332
1121 0 $self->dump();
1122 0 $cntrl_backslash_pressed = 0;
1123 }
1124 } until ($diff <= $KL_min || ($k > $KL_max_it) || ($diff < 0));
1125 }
1126  
1127  
1128 #
1129 # Field Induction Algorithm
1130 #
1131  
1132 # add feature $g to $self
1133 28 ▶sub add_candidate {
Subroutine add_candidate: called CC=3 SCAR=11.0
Pod undocumented
1134 28 my($self, $candidates, $g) = @_;
1135  
1136 28 my($i);
1137  
1138 28 $self->{NR_FEATURES}++;
1139 28 ▶ for ($i = 0; $i < $self->{NR_CLASSES}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"NR_CLASSES"}) 2800 28 2828
1140 $self->{CLASSES}[$i]->insert_column($g,
1141 2800 $candidates->{CANDIDATES}[$i]->weight($g));
1142 }
1143 28 ▶ if ($self->{SCALER} eq "gis") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SCALER'} eq 'gis') { } 14 14 28
1144 14 $self->{PARAMETERS}[$self->{NR_FEATURES} - 1] = 1;
1145 }
1146 else {
1147 14 $self->{PARAMETERS}[$self->{NR_FEATURES} - 1] = $candidates->{ALPHA}[$g];
1148 }
1149 28 push @{$self->{FEATURE_NAMES}}, $candidates->{CANDIDATE_NAMES}[$g];
1150 28 $self->{PARAMETERS_CHANGED} = 1;
1151 28 $self->{CLASSES_CHANGED} = 1;
1152 28 $self->prepare_model();
1153 }
1154  
1155  
1156 # remove the last column
1157 20 ▶sub remove_candidate {
Subroutine remove_candidate: called CC=2 SCAR=6.9
Pod undocumented
1158 20 my($self) = @_;
1159  
1160 20 my($i);
1161  
1162 20 ▶ for ($i = 0; $i < $self->{NR_CLASSES}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"NR_CLASSES"}) 2000 20 2020
1163 # substitute offset $g length 1 by nothing
1164 2000 $self->{CLASSES}[$i]->delete_column($self->{NR_FEATURES}-1);
1165 }
1166 20 pop @{$self->{PARAMETERS}};
1167 20 pop @{$self->{FEATURE_NAMES}};
1168 20 $self->{NR_FEATURES}--;
1169 20 $self->{PARAMETERS_CHANGED} = 1;
1170 20 $self->{CLASSES_CHANGED} = 1;
1171 20 $self->prepare_model();
1172 }
1173  
1174  
1175 # checks for $event, if not there adds it, otherwise increases its {FREQ}
1176 0 ▶sub add_event {
Subroutine add_event: not called CC=4 SCAR=30.0
Pod undocumented
1177 0 my($self, $event) = @_;
1178  
1179 0 my($i,
1180 $found);
1181  
1182 0 $found = 0;
1183 0 ▶ for ($i = 0; $i < $self->{NR_CLASSES}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"NR_CLASSES"}) 0 0 0
1184 0 $found = ($event->Compare($self->{CLASSES}[$i]) == 0);
1185 0 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 0 0 0
1186 0 $self->{FREQ}[$i]++;
1187 0 last;
1188 }
1189 }
1190 0 ▶ if (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
1191 0 $self->{CLASSES}[$self->{NR_CLASSES}] = $event;
1192 0 $self->{FREQ}[$self->{NR_CLASSES}] = 1;
1193 0 $self->{NR_CLASSES}++;
1194 }
1195 0 $self->{NR_EVENTS}++;
1196 }
1197  
1198  
1199 # computes the gain for all $candidates
1200 8 ▶sub gain {
Subroutine gain: called CC=7 SCAR=19.5
Pod undocumented
1201 8 my($self, $candidates) = @_;
1202  
1203 8 my($c,
1204 $x,
1205 $kl,
1206 $below,
1207 $above,
1208 $sum_p_ref,
1209 $sum_p);
1210  
1211 8 $candidates->{MAX_GAIN} = 0;
1212 8 $candidates->{BEST_CAND} = 0;
1213 8 ▶ for ($c = 0; $c < $candidates->{NR_CANDIDATES}; $c++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $candidates->{"NR_CANDIDATES"}) 24 8 32
1214 24 ▶ if (!$candidates->{ADDED}{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($candidates->{"ADDED"}{$c}) 20 4 24
1215 20 $sum_p_ref = 0;
1216 20 $sum_p = 0;
1217 20 ▶ for ($x = 0; $x < $self->{NR_CLASSES}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"NR_CLASSES"}) 2000 20 2020
1218 2000 ▶ if ($candidates->{CANDIDATES}[$x]->bit_test($c)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($candidates->{"CANDIDATES"}[$x]->bit_test($c)) 238 1762 2000
1219 238 $sum_p += $self->{CLASS_EXP_WEIGHTS}[$x];
1220 238 $sum_p_ref += $self->{FREQ}[$x];
1221 }
1222 }
1223 20 $sum_p /= $self->{Z};
1224 20 $sum_p_ref /= $self->{NR_EVENTS};
1225 20 $above = $sum_p_ref * (1 - $sum_p);
1226 20 $below = $sum_p * (1 - $sum_p_ref);
1227 20 ▶ if ($above * $below > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($above * $below > 0) { } 20 0 20
1228 20 $candidates->{ALPHA}[$c] = log($above / $below);
1229 }
1230 else {
1231 0 $self->die("Cannot take log of negative/zero value: $above / $below\n");
1232 }
1233 # temporarily add feature to classes and compute $gain
1234 20 $kl = $self->{KL};
1235 20 $self->add_candidate($candidates, $c);
1236 20 $candidates->{GAIN}[$c] = $kl - $self->{KL};
1237 20 $self->log_msg("G($c, $candidates->{ALPHA}[$c]) = $candidates->{GAIN}[$c]\n");
1238 20 ▶ if (($candidates->{MAX_GAIN} <= $candidates->{GAIN}[$c])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($candidates->{"MAX_GAIN"} <= $candidates->{"GAIN"}[$c]) 5 15 20
1239 5 $candidates->{MAX_GAIN} = $candidates->{GAIN}[$c];
1240 5 $candidates->{BEST_CAND} = $c;
1241 }
1242 # remove the feature
1243 20 $self->remove_candidate();
1244 }
1245 }
1246 }
1247  
1248  
1249 # adds the $n best candidates
1250 4 ▶sub fi {
Subroutine fi: called CC=6 SCAR=18.2
Pod documented
1251 4 my($self, $scaler, $candidates, $n, $sample) = @_;
1252  
1253 4 my ($i,
1254 $kl);
1255  
1256 4 $self->log_msg("fi($scaler, $sample, $n, $self->{VECTYPE})\n");
1257 4 ▶ if ($scaler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scaler) 4 0 4
1258 4 $self->{SCALER} = $scaler;
1259 }
1260 4 ▶ if ($sample) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sample) 4 0 4
1261 4 $self->{SAMPLING} = $sample;
1262 }
1263  
1264 4 ▶ if ($self->{NR_CLASSES} != $candidates->{NR_CLASSES}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"NR_CLASSES"} != $candidates->{"NR_CLASSES"}) 0 4 4
1265 0 $self->die("Candidates have the wrong number of events\n");
1266 }
1267 4 $self->scale();
1268 4 $kl = $self->{KL};
1269 4 ▶ $n = ($n > $candidates->{NR_CANDIDATES}) ? $candidates->{NR_CANDIDATES} : $n;
Branch: 1 branch
BranchTrueFalseTotal
$n > $candidates->{'NR_CANDIDATES'} ? : 0 4 4
1270 4 ▶ for ($i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 8 4 12
1271 8 $self->gain($candidates);
1272 8 $self->add_candidate($candidates, $candidates->{BEST_CAND});
1273 8 $candidates->{ADDED}{$candidates->{BEST_CAND}} = 1;
1274 8 $self->log_msg("Adding candidate $candidates->{BEST_CAND}\n");
1275 8 $self->scale();
1276 8 $self->log_msg("Actual gain: " . ($self->{KL} - $kl) . "\n");
1277 8 $kl = $self->{KL};
1278 }
1279 4 return(1);
1280 }
1281  
1282  
1283 #
1284 # Routines for classification, only binary features!
1285 #
1286  
1287 # context features are 0 .. $n-1
1288 # $x is a vector, $sampling
1289 128 ▶sub classify {
Subroutine classify: called CC=4 SCAR=19.4
Pod undocumented
1290 128 my($self, $x) = @_;
1291  
1292 128 my($y,
1293 $sum,
1294 $i,
1295 $weight,
1296 $best_class,
1297 $best_weight);
1298  
1299 128 $self->log_msg("classify(" . $x->to_Bin('') . ")\n");
1300 128 $sum = 0;
1301 # use every possible completion of $x to compute $sum
1302 # allocate a class vector
1303 128 $y = $VECTOR_PACKAGE->new($self->{NR_FEATURES} - $x->Size());
1304 128 $best_weight = 0;
1305 # for every possible $y
1306 128 ▶ for ($i = 0; $i < 2 ** $y->Size(); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 2 ** $y->Size) 0 128 128
1307 # compute p(x,y) which proportional to p(y|x) (I hope)
1308 0 $weight = $self->weight($x, $y);
1309 0 ▶ if ($weight > $best_weight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weight > $best_weight) 0 0 0
1310 0 $best_class = $y;
1311 0 $best_weight = $weight;
1312 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Statistics::MaxEntropy::debug) 0 0 0
1313 0 print "$i\t", $y->to_Bin(''), "\t$weight\n";
1314 }
1315 }
1316 0 $y->increment();
1317 }
1318 128 return($best_class, $best_weight);
1319 }
1320  
1321  
1322 3 1;
1323  
1324 __END__
« script/ME.wrapper.pl ↑ Summary lib/Statistics/Candidates.pm »