Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
p = np.array([0.0575, 0.0128, 0.0263, 0.0285, 0.0913, 0.0173, 0.0133, 0.0313,
       0.0599, 0.0006, 0.0084, 0.0335, 0.0235, 0.0596, 0.0689, 0.0192,
       0.0008, 0.0508, 0.0567, 0.0706, 0.0334, 0.0069, 0.0119, 0.0073,
       0.0164, 0.0007, 0.1928])
entropy = np.sum(np.multiply(p, np.log2(1/p)))
entropy
4.109446190159851
# Bernoulli random variables: probability of heads = p; probability of tails = 1-p.
# Entropy = -p*log2(p) - (1-p)*log2(1-p)
prange = np.arange(0, 1.05, .1)
q = np.zeros((len(prange), 2))
for n, p in enumerate(prange):
    q[n, :] = p, -p*np.log2(p)-(1-p)*np.log2(1-p)
a = np.array(q)
plt.scatter(q[:,0], q[:,1])
plt.xlabel('p (probability)')
plt.ylabel('H (entropy)')
# Entropy is max when head-to-tail distribution is half-half.
/usr/local/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:6: RuntimeWarning: divide by zero encountered in log2
  
/usr/local/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:6: RuntimeWarning: invalid value encountered in double_scalars
  
<Figure size 432x288 with 1 Axes>