import numpy as np
import matplotlib.pyplot as plt
Binomial
Interactive tutorial on binomial with practical implementations and visualizations
= 'gray'
SPINE_COLOR
def format_axes(ax):
for spine in ['top', 'right']:
False)
ax.spines[spine].set_visible(
for spine in ['left', 'bottom']:
ax.spines[spine].set_color(SPINE_COLOR)0.5)
ax.spines[spine].set_linewidth(
'bottom')
ax.xaxis.set_ticks_position('left')
ax.yaxis.set_ticks_position(
for axis in [ax.xaxis, ax.yaxis]:
='out', color=SPINE_COLOR)
axis.set_tick_params(direction
return ax
def bino(n, p, k)
* p**k * (1-p)**(n-k) choose(n, k)
=21, p=0.3, k=4) stats.binom.pmf(n
0.11277578372328753
from scipy import stats
=range(21)
afor error in [0.3, 0.6]:
= plt.subplots(figsize=(4,3))
fig, ax =21, p=error, k=a), color='grey', alpha=0.3)
ax.bar(a,stats.binom.pmf(n11:],stats.binom.pmf(n=21, p=error, k=a[11:]), color='grey', alpha=0.9)
ax.bar(a[
r'$P(X=k)$')
ax.set_ylabel(r'$k$')
ax.set_xlabel(#ax.set_ylim((0,0.4))
#ax.legend()
format_axes(ax)#plt.fill_betweenx(3, 11, 20)
10.5, color='k',lw=2, label=r'$k=11, \epsilon={}$'.format(error))
plt.axvline("Probability that majority vote \n (11 out of 21) is wrong = {}".format(sum(stats.binom.pmf(n=21, p=error, k=a[11:])).round(3)))
plt.title(
plt.legend()import tikzplotlib
"test-{}.tex".format(error), ) tikzplotlib.save(
!cat test.tex
% This file was created by tikzplotlib v0.9.0.
\begin{tikzpicture}
\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
\begin{axis}[
tick align=outside,
tick pos=left,
title={Simple plot \(\displaystyle \frac{\alpha}{2}\)},
x grid style={white!69.0196078431373!black},
xlabel={time (s)},
xmin=-0.75, xmax=15.75,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ylabel={Voltage (mV)},
ymin=0, ymax=0.24892,
ytick style={color=black}
]
\draw[draw=none,fill=color0] (axis cs:0,0) rectangle (axis cs:1.5,0.00293333333333333);
\draw[draw=none,fill=color0] (axis cs:1.5,0) rectangle (axis cs:3,0.0157333333333333);
\draw[draw=none,fill=color0] (axis cs:3,0) rectangle (axis cs:4.5,0.110866666666667);
\draw[draw=none,fill=color0] (axis cs:4.5,0) rectangle (axis cs:6,0.116866666666667);
\draw[draw=none,fill=color0] (axis cs:6,0) rectangle (axis cs:7.5,0.237066666666667);
\draw[draw=none,fill=color0] (axis cs:7.5,0) rectangle (axis cs:9,0.0889333333333333);
\draw[draw=none,fill=color0] (axis cs:9,0) rectangle (axis cs:10.5,0.0760666666666667);
\draw[draw=none,fill=color0] (axis cs:10.5,0) rectangle (axis cs:12,0.012);
\draw[draw=none,fill=color0] (axis cs:12,0) rectangle (axis cs:13.5,0.00593333333333333);
\draw[draw=none,fill=color0] (axis cs:13.5,0) rectangle (axis cs:15,0.000266666666666667);
\end{axis}
\end{tikzpicture}
sum(stats.binom.pmf(n=21, p=error, k=a[11:])).round(2)
0.83