In [2]:
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
import numpy as np
import scipy as sp
In [3]:
np.random.uniform(size=10)>0.1
Out[3]:
In [4]:
def method_a_sim(dout, ps, nsamples=100000):
samples = [0]*nsamples
for i in range(nsamples):
if np.random.uniform() < ps:
nedges = 1 + np.random.binomial(dout-1, ps)
samples[i] = 1/float(nedges)
return np.mean(samples), np.std(samples)
In [5]:
def method_a_analysis(dout,ps):
return ( 1 - (1-ps)**dout )/float(dout)
In [12]:
dout = 3
In [13]:
samples = method_a_sim(dout, ps)
In [14]:
samples
Out[14]:
In [15]:
method_a_analysis(dout,ps)
Out[15]:
In [ ]:
In [16]:
def method_b_sim(dout, ps, nsamples=100000):
samples = [0]*nsamples
for i in range(nsamples):
if np.random.uniform() < 1/float(dout):
nedges = 1 + np.random.binomial(dout-1, ps)
samples[i] = 1/float(nedges)
else:
if np.random.uniform() < ps:
nedges = 2 + np.random.binomial(dout-2, ps)
samples[i] = 1/float(nedges)
return np.mean(samples), np.std(samples)
# return sum(samples)/float(len(samples))
In [17]:
def method_b_analysis(dout,ps):
return 1 /float(dout)
In [18]:
ps=0.9
In [19]:
method_b_sim(dout,ps)
Out[19]:
In [20]:
method_b_analysis(dout,ps)
Out[20]:
In [21]:
dout=8
allps = np.linspace(0.01, 1)
In [22]:
meanstda = [method_a_sim(dout, ps) for ps in allps]
(meana, stda) = zip(*meanstda)
meana = np.array(meana)
stda = np.array(stda)
In [23]:
analytica = [method_a_analysis(dout,ps) for ps in allps]
In [24]:
meanstdb = [method_b_sim(dout, ps) for ps in allps]
(meanb, stdb) = zip(*meanstdb)
meanb = np.array(meanb)
stdb = np.array(stdb)
In [25]:
analyticb = [method_b_analysis(dout,ps) for ps in allps]
In [26]:
fig, ax = plt.subplots(1)
ax.plot(allps, analytica, lw=2, label='Method A: analysis', color='blue')
ax.plot(allps, meana, '--', lw=2, label='Method A: analysis', color='black')
ax.plot(allps, analyticb, lw=2, label='Method B: analysis', color='yellow')
ax.plot(allps, meanb, '--', lw=2, label='Method A: analysis', color='black')
ax.fill_between(allps, meana+stda, meana-stda, facecolor='blue', alpha=0.5)
ax.fill_between(allps, meanb+stdb, meanb-stdb, facecolor='yellow', alpha=0.5)
ax.axis([0, 1, 0, 2/float(dout)]);
In [ ]:
In [ ]: