In [1]:
import numpy as np
from scipy.misc import factorial
In [2]:
N = 45
z = 3
theta = 1/6
In [3]:
def binomial(theta, N, z):
coef = factorial(N) / factorial(N-z) / factorial(z)
p = coef * theta**z * (1 - theta)**(N-z)
return p
In [4]:
tail = np.arange(z+1)
tail
Out[4]:
In [5]:
p = binomial(theta, N, tail).sum() * 2 # left and right tail probability
p
Out[5]:
In [6]:
right_tail = np.arange(z, N)
p_right = z / right_tail * binomial(theta, right_tail, z)
p = (1 - p_right.sum()) * 2
p
Out[6]:
In [7]:
N = 45
z = 3
In [8]:
left_tail = np.arange(z+1)
theta = np.arange(0.170, 0.190, 0.001)
In [9]:
p = map(lambda t: binomial(t, N, left_tail).sum()*2, theta)
p = list(p)
In [10]:
list(zip(theta, p))
Out[10]:
In [11]:
p = np.array(p)
p_idx = np.nonzero(p > 0.05)[0][-1]
theta1 = theta[p_idx]
p[p_idx], theta1
Out[11]:
In [12]:
right_tail = np.arange(z, N)
theta = np.arange(0.005, 0.020, 0.001)
In [13]:
p = map(lambda t: binomial(t, N, right_tail).sum()*2, theta)
p = list(p)
In [14]:
p = np.array(p)
p_idx = np.nonzero(p > 0.05)[0][0]
theta2 = theta[p_idx]
p[p_idx], theta2
Out[14]:
In [15]:
theta2, theta1
Out[15]:
In [ ]:
In [16]:
theta = np.arange(0.150, 0.160, 0.001)
In [17]:
low_tail = np.arange(z, N)
def p_greated_than(theta):
p_right = z / low_tail * binomial(theta, low_tail, z)
p = (1 - p_right.sum()) * 2
return p
p = map(p_greated_than, theta)
p = list(p)
In [18]:
list(zip(theta, p))
Out[18]:
In [19]:
p_idx = np.nonzero(np.array(p) > 0.05)[0][-1]
theta1 = theta[p_idx]
p[p_idx], theta1
Out[19]:
In [20]:
theta = np.arange(0.005, 0.020, 0.001)
high_tail = np.arange(z+1)
In [ ]:
In [21]:
high_tail = np.arange(z, N+1)
def p_less_than(theta):
p = z / high_tail * binomial(theta, high_tail, z)
p = 2 * p.sum()
return p
p = map(p_less_than, theta)
p = list(p)
In [22]:
p = np.array(p)
p_idx = np.nonzero(p > 0.05)[0][0]
theta2 = theta[p_idx]
p[p_idx], theta2
Out[22]:
In [23]:
theta2, theta1
Out[23]:
In [24]:
N = 45
z = 3
theta = 1/6
In [25]:
Ns = np.arange(40, 51)
p_N = np.ones_like(Ns) / len(Ns)
In [28]:
p_total = 0
for i, n in enumerate(Ns):
# For the current `n`, determine the max z that is in the low tail:
z_max = np.arange(0, n+1) / n
z_max = np.nonzero(z_max <= z/N)[0][-1]
low_tail = np.arange(0, z_max+1)
p = 2*binomial(theta, n, low_tail).sum()
p_total += p_N[i] * p
print(n, p)
In [29]:
p_total
Out[29]:
In [ ]: