@EricHolloway/

KLD for FI

Python

Demonstrate the uniform is the minimal background assumption for calculating fuctional information.

fork
loading
Files
  • main.py
main.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
from random import random
from math import log

str_len = 1000

p_ = [random() for _ in range(str_len)]
p = [i/float(sum(p_)) for i in p_]
q_ = [random() for _ in range(str_len)]
q = [i/float(sum(q_)) for i in q_]
u_ = [1 for _ in range(str_len)]
u = [i/float(sum(u_)) for i in u_]

def kld(x,y):
  result = 0
  for i0,i1 in zip(x,y):
    result += i0 * (log(i0,2) - log(i1,2))
  return result
  
print("kld(u,p): ", round(kld(u,p),2))
print("kld(u,q): ", round(kld(u,q),2))
print("kld(p,q): ", round(kld(p,q),2))
print("kld(u,p)<kld(p,q): ", kld(u,p)<kld(p,q))
print("kld(u,q)<kld(p,q): ", kld(u,q)<kld(p,q))