In [1]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom
In [2]:
'''Computes the cumulative density function of min{X,Y}'''
def cdf_min(X,Y):
return 1 - (1-X)*(1-Y)
'''Computes the probability mass function from the cumulative density'''
def cdf_to_pmf(X):
return np.concatenate(([X[0]], np.diff(X)))
'''Constructs the cdf for the shift distance t with paramter n and p'''
def shift_distance_cdf(t, n, p):
S = binom.cdf(range(n), n-1, p)
S = np.concatenate(([0]*t*2, S))
return S
'''Computes the cdf of the minimum of several cdfs'''
def cdf_min_a(M):
m = M.shape[0]
X = M[0,:]
for i in range(1,m):
X = cdf_min(X, M[i,:])
return X
In [3]:
p = 3/4
ns = [16]
for n in ns:
Ks = range(n+1)
X = binom.cdf(range(n+1), n, p)
Z = cdf_min(X,X)
X_pmf = cdf_to_pmf(X)
Z_pmf = cdf_to_pmf(Z)
print("E[X] = {0}".format(np.sum(X_pmf*Ks)))
print("E[Z] = {0}".format(np.sum(Z_pmf*Ks)))
plt.plot(Ks, X_pmf, label='X')
plt.plot(Ks, Z_pmf, label='min{X,X}')
plt.legend()
In [4]:
ns = range(16,4099,32)
As = []
for n in ns:
Ks = range(n+1)
SDs = binom.cdf(Ks, n, p)
ts = [1,2]
for t in ts:
St = shift_distance_cdf(t, n, p)
St = St[:n+1]
Z = cdf_min(St, St)
SDs = np.vstack((SDs, Z))
St_pmf = cdf_to_pmf(St)
Z_pmf = cdf_to_pmf(Z)
SD_cdf = cdf_min_a(SDs)
SD_pmf = cdf_to_pmf(SD_cdf)
#print("n = {0}\n E[SD] = {1:.2f}\n E[X] = {2}".format(n, np.sum(Ks*SD_pmf), n*p))
a_n = np.sum(Ks*SD_pmf)/n
As.append(a_n)
#print(" alpha_n = {0}".format(a_n))
plt.plot(ns,As)
Out[4]:
In [5]:
min(As)
Out[5]:
In [ ]: