-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathloglikelihood.php
More file actions
36 lines (32 loc) · 982 Bytes
/
Copy pathloglikelihood.php
File metadata and controls
36 lines (32 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
#
# Caculate the Shannon entropy.
# Return the entropy value for the elements
#
function entropy($elements){
$sum = 0;
$result = 0;
foreach ($elements as $element){
if ($element < 0){
echo "ERROR: Should not have negative count for entropy calculation";
}
if ($element > 0){
$result += $element * log($element);
$sum += $element;
}
}
$result -= $sum * log($sum);
return -$result;
}
function logLikelihoodRatio($k11, $k12, $k21, $k22){
$rowEntropy = entropy(array($k11, $k12)) + entropy(array($k21, $k22));
$columnEntropy = entropy(array($k11, $k21)) + entropy(array($k12, $k22));
$matrixEntropy = entropy(array($k11, $k12, $k21, $k22));
if ($rowEntropy + $columnEntropy > $matrixEntropy){
return 0;
}
return 2 * ($matrixEntropy - $rowEntropy - $columnEntropy);
}
#echo logLikelihoodRatio(150, 782, 12443, 14294293);
#echo logLikelihoodRatio(276, 1002, 26036, 229785);
?>