Draw Two Function in Same Plot


In [2]:
import numpy as np
import matplotlib.pyplot as plt

In [2]:
n_a = 0
p = []
for i in range(1,10000):
    r = np.random.randint(2, size=1)[0]
    if r == 1:
        n_a += 1
    p.append(n_a/i)

In [58]:
from matplotlib.backends.backend_pdf import PdfPages

def tossing_experiment(pp):
    plt.figure(figsize = (12,6))
    plt.plot(p)
    plt.axhline(y=.5, xmin=0, xmax=4, linewidth=1, color = 'r', ls='dashed')
    plt.ylim([0, 1])
    plt.ylabel(r'$\frac{n_A}{n}$', size = 20)
    plt.xlabel('number of experiments', size = 15)
    plt.annotate('0.5',
                 xy=(1000, 0.5),
                 xytext=(1000, 0.2),
                 color='red',
                 size=20,
                 arrowprops=dict(facecolor='red',
                                 shrink=0.05),
                 horizontalalignment='center'
                )
    plt.annotate(r'$\frac{n_A}{n}$',
                 xy=(500, 0.55),
                 xytext=(500, 0.7),
                 color='blue',
                 size=20,
                 arrowprops=dict(facecolor='blue',
                                 shrink=0.05),
                 horizontalalignment='center'
                )
    plt.annotate(r'$\lim_{n\to\infty}\frac{n_A}{n}\approx0.5$',
                 xy=(9000, 0.5),
                 xytext=(9000, 0.65),
                 color='blue',
                 size=20,
                 arrowprops=dict(facecolor='blue',
                                 shrink=0.05),
                 horizontalalignment='center'
                )
    plt.title('flipping coin experiment')
    pp.savefig(plt.gcf())

with PdfPages('relative_frequency_interp.pdf') as pp:
    tossing_experiment(pp)

In [68]:
plt.figure(figsize = (12,6))
plt.plot(p)
plt.axhline(y=.5, xmin=0, xmax=4, linewidth=1, color = 'r', ls='dashed')
plt.ylim([0, 1])
plt.ylabel(r'$\frac{n_A}{n}$', size = 20)
plt.xlabel('number of experiments', size = 15)
plt.annotate('0.5',
             xy=(1000, 0.5),
             xytext=(1000, 0.2),
             color='red',
             size=20,
             arrowprops=dict(facecolor='red',
                             shrink=0.05),
             horizontalalignment='center'
            )
plt.annotate(r'$\frac{n_A}{n}$',
             xy=(500, 0.55),
             xytext=(500, 0.7),
             color='blue',
             size=20,
             arrowprops=dict(facecolor='blue',
                             shrink=0.05),
             horizontalalignment='center'
            )
plt.annotate(r'$\lim_{n\to\infty}\frac{n_A}{n}\approx0.5$',
             xy=(9000, 0.5),
             xytext=(9000, 0.65),
             color='blue',
             size=20,
             arrowprops=dict(facecolor='blue',
                             shrink=0.05),
             horizontalalignment='center'
            )
plt.title('flipping coin experiment')
plt.savefig('relative_frequency_interp.jpg')

In [50]:
# bernoulli distribution
p = 0.5
plt.figure(figsize=(16,4))
for p in [0.1, 0.5, 0.8]:
    for i in range(3):
        pmf = [1-p, p]
        plt.subplot(1,3,i+1)
        plt.bar([0, 1], pmf, width=0.2)
        plt.ylim(0,1)
        plt.xlim(-1,2)
        plt.xlabel(r'$x$')
        plt.ylabel(r'$pmf(probability)$')
        plt.title(r'Bernoulli Distribution $pmf$ with $p=%.2f$' % p)
plt.show()



In [5]:
param = [ [(n,p)] for n in range(5, 16, 5) for p in [0.1, 0.5, 0.8]]

In [7]:
param = []
for n in range(5, 16, 5):
    param.append([(n, p) for p in [0.1, 0.5, 0.8]])

In [41]:
import matplotlib.pyplot as plt
from math import factorial
par = (5, 0.1)
prob = [0.1, 0.5, 0.8]
num = [5, 10, 15, 20]

def comb(a,b):
    return factorial(a)/factorial(b)/factorial(a-b)

def binomial_prob(n, p):
    prob = []
    for i in range(n+1):
        #print(comb(n,i))
        prob.append(comb(n,i)*(p**i) * ((1-p)**(5-i)))
    return prob

for p in prob:
    for n in num:
        a = binomial_prob(n, p)
        plt.bar(range(n+1),a)

plt.show()



In [ ]: