@EricHolloway/

KLD for FI

Python

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

fork
loading
Files
  • main.py

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.5369225560158306","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"ZnJvbSByYW5kb20gaW1wb3J0IHJhbmRvbQpmcm9tIG1hdGggaW1wb3J0IGxvZwoKc3RyX2xlbiA9IDEwMDAKCnBfID0gW3JhbmRvbSgpIGZvciBfIGluIHJhbmdlKHN0cl9sZW4pXQpwID0gW2kvZmxvYXQoc3VtKHBfKSkgZm9yIGkgaW4gcF9dCnFfID0gW3JhbmRvbSgpIGZvciBfIGluIHJhbmdlKHN0cl9sZW4pXQpxID0gW2kvZmxvYXQoc3VtKHFfKSkgZm9yIGkgaW4gcV9dCnVfID0gWzEgZm9yIF8gaW4gcmFuZ2Uoc3RyX2xlbildCnUgPSBbaS9mbG9hdChzdW0odV8pKSBmb3IgaSBpbiB1X10KCmRlZiBrbGQoeCx5KToKICByZXN1bHQgPSAwCiAgZm9yIGkwLGkxIGluIHppcCh4LHkpOgogICAgcmVzdWx0ICs9IGkwICogKGxvZyhpMCwyKSAtIGxvZyhpMSwyKSkKICByZXR1cm4gcmVzdWx0CiAgCnByaW50KCJrbGQodSxwKTogIiwgcm91bmQoa2xkKHUscCksMikpCnByaW50KCJrbGQodSxxKTogIiwgcm91bmQoa2xkKHUscSksMikpCnByaW50KCJrbGQocCxxKTogIiwgcm91bmQoa2xkKHAscSksMikpCnByaW50KCJrbGQodSxwKTxrbGQocCxxKTogIiwga2xkKHUscCk8a2xkKHAscSkpCnByaW50KCJrbGQodSxxKTxrbGQocCxxKTogIiwga2xkKHUscSk8a2xkKHAscSkpCg=="},"asBuffer":null},"loaded":true}}
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))