-
Notifications
You must be signed in to change notification settings - Fork 4
/
SumLogProbs.py
executable file
·56 lines (39 loc) · 1.78 KB
/
SumLogProbs.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
#=========================================================================
# This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
# License (GPL) version 3, as described at www.opensource.org.
# Copyright (C)2016 William H. Majoros ([email protected]).
#=========================================================================
from __future__ import (absolute_import, division, print_function,
unicode_literals, generators, nested_scopes, with_statement)
from builtins import (bytes, dict, int, list, object, range, str, ascii,
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
# The above imports should allow this program to run in both Python 2 and
# Python 3. You might need to update your version of module "future".
import math
NEGATIVE_INFINITY=float("-inf")
def sumLogProbs_ordered(largerValue,smallerValue):
if(smallerValue==NEGATIVE_INFINITY): return largerValue
return largerValue+math.log(1+math.exp(smallerValue-largerValue))
def sumLogProbs2(logP,logQ):
if(logP>logQ): return sumLogProbs_ordered(logP,logQ)
return sumLogProbs_ordered(logQ,logP)
def sumLogProbs(x):
n=len(x)
if(n==1): return x[0]
if(n==0): return NEGATIVE_INFINITY
# Pull out the largest value
largestValue=x[0]
for v in x:
if(v>largestValue): largestValue=v
# Handle the case of all zeros separately
if(largestValue==NEGATIVE_INFINITY): return NEGATIVE_INFINITY
# Apply the Kingsbury-Raynor formula
sum=0.0;
for v in x:
if(v==NEGATIVE_INFINITY): continue
sum+=math.exp(v-largestValue)
return largestValue+math.log(sum)
#v=[math.log(0.0001),math.log(0.0002),math.log(0.0003)]
#print(math.exp(sumLogProbs2(math.log(0.1),math.log(0.2))))
#print(math.exp(sumLogProbs(v)))