SHARE
TWEET

Untitled

a guest Mar 10th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3.  
  4. def kl(p,q):
  5.     assert(len(p)==len(q) and np.isclose(p.sum(),1) and np.isclose(q.sum(),1))
  6.     return sum(p[i]*np.log2(p[i]/q[i]) for i in range(len(p)) if p[i] != 0)
  7.  
  8. def d(p,q,Q):
  9.     pp = Q.dot(p)
  10.     qq = Q.dot(q)
  11.     return kl(p,q) - kl(pp,qq)
  12.  
  13. Q = np.array([[0.95,0.05],[1.0,0.0],[0.0,1.0]])
  14. p = np.array([0.5,.5,0])
  15. q = np.array([0.5,0.25,0.25])
  16.  
  17. alphavals = np.linspace(0,1,100, endpoint=True)
  18. plt.plot(alphavals, [d(p,a*p+(1-a)*q,Q.T) for a in alphavals])
  19. plt.xlabel(r'$\alpha$')
  20. plt.ylabel(r'$\Delta D(p, \alpha p + (1-\alpha) q)$')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top