line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::KullbackLeibler::Discrete; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
25909
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
29
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
37
|
|
6
|
1
|
|
|
1
|
|
666
|
use parent 'Exporter'; |
|
1
|
|
|
|
|
229
|
|
|
1
|
|
|
|
|
4
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our @EXPORT = qw(kl); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=encoding utf-8 |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Math::KullbackLeibler::Discrete - Computes Kullback-Leibler divergence for two discrete distributes. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 VERSION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Version 0.05 |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=cut |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 SYNOPSIS |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
This module computes Kullback-Leibler divergence for two discrete |
28
|
|
|
|
|
|
|
distributions, using smoothing. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use Math::KullbackLeibler::Discrete; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $P = { a => 1/2, b => 1/4, c => 1/4 }; |
33
|
|
|
|
|
|
|
my $Q = { a => 7/12, b => 2/12, d => 3/12 }; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $kl = kl( $P, $Q ); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# optionally set the smoothing epsilon |
38
|
|
|
|
|
|
|
my $kl2 = kl( $P, $Q, epsilon => 0.0001 ); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# setting epsilon to 0 results in no smoothing. |
41
|
|
|
|
|
|
|
my $kl3 = kl( $P, $Q, epsilon => 0 ); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 EXPORT |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head2 kl |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Computes smoothed KL. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Receives two mandatory arguments: two anonymous hashrefs, that map |
50
|
|
|
|
|
|
|
events to their probabilities. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Implementation based on the description presented at |
53
|
|
|
|
|
|
|
L. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
The smoothing amount can be specified as parameter: |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
kl( $P, $Q, epsilon => 0.001 ); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
This makes it possible to turn of smoothing, defined epsilon as |
60
|
|
|
|
|
|
|
zero. However, notice that this will lead to errors in case of |
61
|
|
|
|
|
|
|
divergent domains. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=cut |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub kl { |
66
|
5
|
|
|
5
|
1
|
582
|
my ($P, $Q, %opts) = @_; |
67
|
|
|
|
|
|
|
|
68
|
5
|
|
|
|
|
8
|
my $eps = 0.00001; |
69
|
|
|
|
|
|
|
|
70
|
5
|
100
|
|
|
|
17
|
$eps = $opts{epsilon} if exists $opts{epsilon}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Universe |
73
|
5
|
|
|
|
|
10
|
my $SU = {}; |
74
|
5
|
|
|
|
|
46
|
$SU->{$_}++ for (keys %$P, keys %$Q); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# | Universe - P | |
77
|
5
|
|
|
|
|
18
|
my $susp = scalar(keys %$SU) - scalar(keys %$P); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# | Universe - Q | |
80
|
5
|
|
|
|
|
10
|
my $susq = scalar(keys %$SU) - scalar(keys %$Q); |
81
|
|
|
|
|
|
|
|
82
|
5
|
|
|
|
|
15
|
my $pc = $eps * ($susp/scalar(keys %$P)); |
83
|
5
|
|
|
|
|
12
|
my $qc = $eps * ($susq/scalar(keys %$Q)); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $Pline = sub { |
86
|
28
|
|
|
28
|
|
39
|
my $i = shift; |
87
|
28
|
100
|
|
|
|
159
|
return exists($P->{$i}) ? $P->{$i} - $pc : $eps; |
88
|
5
|
|
|
|
|
26
|
}; |
89
|
|
|
|
|
|
|
my $Qline = sub { |
90
|
14
|
|
|
14
|
|
21
|
my $i = shift; |
91
|
14
|
100
|
|
|
|
106
|
return exists($Q->{$i}) ? $Q->{$i} - $qc : $eps; |
92
|
5
|
|
|
|
|
20
|
}; |
93
|
|
|
|
|
|
|
|
94
|
5
|
|
|
|
|
9
|
my $kl = 0; |
95
|
5
|
|
|
|
|
13
|
for (keys %$SU) { |
96
|
14
|
|
|
|
|
26
|
$kl += $Pline->($_) * log($Pline->($_) / $Qline->($_)); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
4
|
|
|
|
|
44
|
return $kl; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 AUTHOR |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Alberto Simoes, C<< >> |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 BUGS |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
109
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
110
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 SUPPORT |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
perldoc Math::KullbackLeibler::Discrete |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
You can also look for information at: |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=over 4 |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
L |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
L |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item * CPAN Ratings |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
L |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item * Search CPAN |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
L |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=back |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Thanks to Michael Elhadad for making his lecture on-line, so I found a |
145
|
|
|
|
|
|
|
nice and clean explanation of how this metric could be computed and |
146
|
|
|
|
|
|
|
implemented. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Copyright 2013 Alberto Simões. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
153
|
|
|
|
|
|
|
under the terms of the the Artistic License (2.0). You may obtain a |
154
|
|
|
|
|
|
|
copy of the full license at: |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
L |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Any use, modification, and distribution of the Standard or Modified |
159
|
|
|
|
|
|
|
Versions is governed by this Artistic License. By using, modifying or |
160
|
|
|
|
|
|
|
distributing the Package, you accept this license. Do not use, modify, |
161
|
|
|
|
|
|
|
or distribute the Package, if you do not accept this license. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
If your Modified Version has been derived from a Modified Version made |
164
|
|
|
|
|
|
|
by someone other than you, you are nevertheless required to ensure that |
165
|
|
|
|
|
|
|
your Modified Version complies with the requirements of this license. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
This license does not grant you the right to use any trademark, service |
168
|
|
|
|
|
|
|
mark, tradename, or logo of the Copyright Holder. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This license includes the non-exclusive, worldwide, free-of-charge |
171
|
|
|
|
|
|
|
patent license to make, have made, use, offer to sell, sell, import and |
172
|
|
|
|
|
|
|
otherwise transfer the Package with respect to any patent claims |
173
|
|
|
|
|
|
|
licensable by the Copyright Holder that are necessarily infringed by the |
174
|
|
|
|
|
|
|
Package. If you institute patent litigation (including a cross-claim or |
175
|
|
|
|
|
|
|
counterclaim) against any party alleging that the Package constitutes |
176
|
|
|
|
|
|
|
direct or contributory patent infringement, then this Artistic License |
177
|
|
|
|
|
|
|
to you shall terminate on the date that such litigation is filed. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER |
180
|
|
|
|
|
|
|
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. |
181
|
|
|
|
|
|
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
182
|
|
|
|
|
|
|
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY |
183
|
|
|
|
|
|
|
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR |
184
|
|
|
|
|
|
|
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR |
185
|
|
|
|
|
|
|
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, |
186
|
|
|
|
|
|
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=cut |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
1; # End of Math::KullbackLeibler::Discrete |