In [52]:
def cumulative_success(p, n):
    '''
    Returns the probability of success in <=n trials
    for a p-biased coin 
    '''
    return 1 - (1 - p)**n

In [53]:
def bernoulli_se(p, n):
    '''
    Standard error of sample mean of Bernoulli distribution
    @param p - sample mean
    @param n - number of samples
    @return standard error
    '''
    return np.sqrt(p * (1 - p) / n)

In [54]:
def cumulative_success_se(p, numattempts, numsamples):
    '''
    Standard error of cumulative success probability
    @param - p - sample mean
    @param - numattempts - number of trials
    @param numsamples - number of samples to compute sample mean
    '''
    return (n * (1 - p) ** (n - 1)) * bernoulli_se(p, numsamples)

In [80]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
numattempts = np.arange(1, 11)
numsamples = 100
for p in np.linspace(0, 1.0, 10):
    prob = cumulative_success(p, numattempts)
    se = cumulative_success_se(p, numattempts, numsamples)
    plt.plot(numattempts, prob,'k', linewidth=0.2)
    plt.fill_between(numattempts, prob-se, prob+se, facecolor='lightgray', edgecolor='none')
plt.ylim([0,1])
plt.xlabel('Number of attempts')
plt.ylabel('Cumulative success probability')
plt.title('Sample mean and SE propagation for %s samples'%numsamples)


Out[80]:
<matplotlib.text.Text at 0x10e97a550>

In [ ]: