import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%config InlineBackend.figure_format = 'retina'
az.style.use('arviz-darkgrid')
d = {'A':[0, 0, 10, 0, 0],
'B':[0, 1, 8, 1, 0],
'C':[0, 2, 6, 2, 0],
'D':[1, 2, 4, 2, 1],
'E':[2, 2, 2, 2, 2]}
p = pd.DataFrame(data=d)
p_norm = p/p.sum(0)
def entropy(x):
y = []
for i in x:
if i == 0:
y.append(0)
else:
y.append(i*np.log(i))
h = -sum(y)
return h
H = p_norm.apply(entropy, axis=0)
H
A -0.000000 B 0.639032 C 0.950271 D 1.470808 E 1.609438 dtype: float64
ways = [1, 90, 1260, 37800, 113400]
logwayspp = np.log(ways)/10
plt.plot(logwayspp, H, 'o')
plt.plot([0.0, max(logwayspp)], [0.0, max(H)], '--k')
plt.ylabel('entropy', fontsize=14)
plt.xlabel('log(ways) per pebble');
# Build list of the candidate distributions.
p = [[1/4, 1/4, 1/4, 1/4],
[2/6, 1/6, 1/6, 2/6],
[1/6, 2/6, 2/6, 1/6],
[1/8, 4/8, 2/8, 1/8]]
# Compute expected value of each. The sum of the multiplied entries is just a dot product.
p_ev = [np.dot(i, [0, 1, 1, 2]) for i in p]
p_ev
[1.0, 0.9999999999999999, 1.0, 1.0]
# Compute entropy of each distribution
p_ent = [entropy(i) for i in p]
p_ent
[1.3862943611198906, 1.3296613488547582, 1.3296613488547582, 1.2130075659799042]
p = 0.7
A = [(1-p)**2, p*(1-p), (1-p)*p, p**2]
A
[0.09000000000000002, 0.21000000000000002, 0.21000000000000002, 0.48999999999999994]
-np.sum(A*np.log(A))
1.221728604109787
def sim_p(G=1.4):
x123 = np.random.uniform(size=3)
x4 = (G * np.sum(x123) - x123[1] - x123[2]) / (2 - G)
x1234 = np.concatenate((x123, [x4]))
z = np.sum(x1234)
p = x1234 / z
return - np.sum(p * np.log(p)), p
H = []
p = np.zeros((10**5, 4))
for rep in range(10**5):
h, p_ = sim_p()
H.append(h)
p[rep] = p_
az.plot_kde(H)
plt.xlabel('Entropy')
plt.ylabel('Density');
np.max(H)
1.2217270587669808
p[np.argmax(H)]
array([0.09035691, 0.20978912, 0.20949706, 0.49035691])
import platform
import sys
import IPython
import matplotlib
import scipy
print("""This notebook was created using:\nPython {}\nIPython {}\nArviZ {}\nNumPy {}\nMatplotlib {}\n""".format(sys.version[:5], IPython.__version__, az.__version__, np.__version__, matplotlib.__version__))
This notebook was created using: Python 3.7.3 IPython 7.5.0 ArviZ 0.4.1 NumPy 1.16.4 Matplotlib 3.1.0