line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Text::NSP::Measures::3D::MI::tmi - Perl implementation for True Mutual |
4
|
|
|
|
|
|
|
Information for trigrams. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 SYNOPSIS |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head3 Basic Usage |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Text::NSP::Measures::3D::MI::tmi; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$tmi_value = calculateStatistic( n111=>10, |
13
|
|
|
|
|
|
|
n1pp=>40, |
14
|
|
|
|
|
|
|
np1p=>45, |
15
|
|
|
|
|
|
|
npp1=>42, |
16
|
|
|
|
|
|
|
n11p=>20, |
17
|
|
|
|
|
|
|
n1p1=>23, |
18
|
|
|
|
|
|
|
np11=>21, |
19
|
|
|
|
|
|
|
nppp=>100); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
if( ($errorCode = getErrorCode())) |
22
|
|
|
|
|
|
|
{ |
23
|
|
|
|
|
|
|
print STDERR $erroCode." - ".getErrorMessage()."\n"; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
else |
26
|
|
|
|
|
|
|
{ |
27
|
|
|
|
|
|
|
print getStatisticName."value for bigram is ".$tmi_value."\n"; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 DESCRIPTION |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
True Mutual Information (tmi) is defined as the weighted average of the |
33
|
|
|
|
|
|
|
pointwise mutual informations for all the observed and expected value pairs. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
tmi = [n111/nppp * log(n111/m111) + n112/nppp * log(n112/m112) + |
36
|
|
|
|
|
|
|
n121/nppp * log(n121/m121) + n122/nppp * log(n122/m122) + |
37
|
|
|
|
|
|
|
n211/nppp * log(n211/m211) + n212/nppp * log(n212/m212) + |
38
|
|
|
|
|
|
|
n221/nppp * log(n221/m221) + n222/nppp * log(n222/m222)] |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
PMI = log (n111/m111) |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Here n111 represents the observed value for the cell (1,1,1) and m111 |
43
|
|
|
|
|
|
|
represents the expected value for that cell. The expected values for |
44
|
|
|
|
|
|
|
the internal cells are calculated by taking the product of their |
45
|
|
|
|
|
|
|
associated marginals and dividing by the sample size, for example: |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
n1pp * np1p * npp1 |
48
|
|
|
|
|
|
|
m111= -------------------- |
49
|
|
|
|
|
|
|
nppp |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head2 Methods |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=over |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
package Text::NSP::Measures::3D::MI::tmi; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
1
|
|
4830
|
use Text::NSP::Measures::3D::MI; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
375
|
|
62
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
63
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
59
|
|
64
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
65
|
1
|
|
|
1
|
|
6
|
no warnings 'redefine'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
506
|
|
66
|
|
|
|
|
|
|
require Exporter; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
our ($VERSION, @EXPORT, @ISA); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
@EXPORT = qw(initializeStatistic calculateStatistic |
73
|
|
|
|
|
|
|
getErrorCode getErrorMessage getStatisticName); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
$VERSION = '0.97'; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item calculateStatistic($count_values) - This method calculates |
79
|
|
|
|
|
|
|
the tmi value |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
INPUT PARAMS : $count_values .. Reference of an hash containing |
82
|
|
|
|
|
|
|
the count values computed by the |
83
|
|
|
|
|
|
|
count.pl program. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
RETURN VALUES : $tmi .. TMI value for this trigram. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub calculateStatistic |
90
|
|
|
|
|
|
|
{ |
91
|
16
|
|
|
16
|
|
5658
|
my %values = @_; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# computes and returns the observed and expected values from |
94
|
|
|
|
|
|
|
# the frequency combination values. returns 0 if there is an |
95
|
|
|
|
|
|
|
# error in the computation or the values are inconsistent. |
96
|
16
|
100
|
|
|
|
323
|
if( !(Text::NSP::Measures::3D::MI::getValues(\%values)) ) { |
97
|
15
|
|
|
|
|
37
|
return(0); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
#my $marginals = $self->computeMarginalTotals(@_); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Now for the actual calculation of TMI! |
103
|
1
|
|
|
|
|
3
|
my $tmi = 0; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# dont want ($nxy / $mxy) to be 0 or less! flag error if so! |
106
|
1
|
|
|
|
|
6
|
$tmi += $n111/$nppp * Text::NSP::Measures::3D::MI::computePMI( $n111, $m111 )/ log 2; |
107
|
1
|
|
|
|
|
5
|
$tmi += $n112/$nppp * Text::NSP::Measures::3D::MI::computePMI( $n112, $m112 )/ log 2; |
108
|
1
|
|
|
|
|
4
|
$tmi += $n121/$nppp * Text::NSP::Measures::3D::MI::computePMI( $n121, $m121 )/ log 2; |
109
|
1
|
|
|
|
|
4
|
$tmi += $n122/$nppp * Text::NSP::Measures::3D::MI::computePMI( $n122, $m122 )/ log 2; |
110
|
1
|
|
|
|
|
4
|
$tmi += $n211/$nppp * Text::NSP::Measures::3D::MI::computePMI( $n211, $m211 )/ log 2; |
111
|
1
|
|
|
|
|
6
|
$tmi += $n212/$nppp * Text::NSP::Measures::3D::MI::computePMI( $n212, $m212 )/ log 2; |
112
|
1
|
|
|
|
|
4
|
$tmi += $n221/$nppp * Text::NSP::Measures::3D::MI::computePMI( $n221, $m221 )/ log 2; |
113
|
1
|
|
|
|
|
4
|
$tmi += $n222/$nppp * Text::NSP::Measures::3D::MI::computePMI( $n222, $m222 )/ log 2; |
114
|
|
|
|
|
|
|
|
115
|
1
|
|
|
|
|
6
|
return ($tmi); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item getStatisticName() - Returns the name of this statistic |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
INPUT PARAMS : none |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
RETURN VALUES : $name .. Name of the measure. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=cut |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub getStatisticName |
128
|
|
|
|
|
|
|
{ |
129
|
0
|
|
|
0
|
|
|
return "True Mutual Information"; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
135
|
|
|
|
|
|
|
__END__ |