File: logsumexp.py

package info (click to toggle)
freebayes 1.3.7-1~exp
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,324 kB
  • sloc: cpp: 125,947; ansic: 4,539; sh: 1,084; python: 604; asm: 271; lisp: 75; perl: 27; makefile: 20
file content (23 lines) | stat: -rw-r--r-- 487 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import math

def logsumexp(lnv):
    """Sum exp(item) for item in lnv (log-normal vector) without overflow."""
    n = lnv[0]
    maxAbs = n
    minN = n
    maxN = n
    c = n
    for item in lnv[1:]:
        n = item
        if n > maxN:
            maxN = n
        if abs(n) > maxAbs:
            maxAbs = abs(n)
        if n < minN:
            minN = n
    if maxAbs > maxN:
        c = minN
    else:
        c = maxN
    return c + math.log(sum([math.exp(i - c) for i in lnv]))