def _test(p): data = d.Binomial(N, p) Sn = data.sample() posterior = prior.update(data, Sn) if posterior.cdf(s) < alpha: return 1, N, Sn # if Sn >= accept: # return 1, N, Sn return 0, N, Sn
def _test(p): n = steps Sn = 0 data = d.Binomial(steps, p) while n < N: Sn += data.sample() if accepts[n] < Sn: return 1, n, Sn n += step return 0, N, Sn
def binomial_value(x, bdy, ET, prior, nsamples=1, reset_cost=0): n = bdy.n - nsamples posterior = d.posterior(prior, d.Binomial(n, 0), [x]) if x >= bdy.accept: return 0 # distribution of next <nsample> data points new_data = d.Binomial(nsamples, posterior.mean) # compute values from borders prob_accept = 1-new_data.cdf(bdy.accept - x - 1) prob_reject = new_data.cdf(bdy.reject - x) value = prob_accept * nsamples + prob_reject * (ET + nsamples + reset_cost) # compute intermediate values value += sum(new_data.pdf(y - x) * (V + nsamples) for y, V in bdy.values.items()) return value
def binomial_acceptance(N, s, prior, X=None, alpha=0.05): if X == None: X = int(s * N)+1 if X > N: return X posterior = d.posterior(prior, d.Binomial(N, 0), [X]) if posterior.cdf(s) < alpha: return X return binomial_acceptance(N, s, prior, X=X+1, alpha=alpha)
def binomial_msprt_test(p, s, prior, alpha=0.05, N=1000, steps=1): Sn = 0 X = d.Binomial(steps, p) for n in range(steps, N + steps, steps): Sn += X.sample() pval = msprt_pval(beta_binomial_lr(Sn, n, s, prior.a, prior.b)) if pval < alpha: if Sn / n > s: return 1, n, Sn return -1, n, Sn # no outcome after Nmax steps return 0, n, Sn
def normal_msprt_test(p, s, prior, alpha=0.05, Nmax=5000, steps=1): Sn = 0 X = d.Binomial(steps, p) for n in range(steps, Nmax + 1, steps): Sn += X.sample() data = d.Gaussian(0, (Sn + p) / (n + 1) * (1 - (Sn + p) / (n + 1))) pval = normal_msprt_pval(normal_msprt(Sn / n, n, s, prior, data)) if pval < alpha: if Sn / n > s: return 1, n, Sn return -1, n, Sn # no outcome after Nmax steps return 0, n, Sn
def binomial_heuristic(s, prior, N, T, alpha=0.05, beta=0.3): boundaries = [] for n in range(1, N): accept = binomial_acceptance(n, s, prior, alpha=alpha) target = binomial_acceptance(n + T, s, prior)/(n + T) at_current = d.Binomial(n, target) reject = at_current.ppf(beta) boundaries.append(boundary(n, accept, reject, {})) boundaries.append(boundary(N, accept, accept-1, {})) return boundaries
def boundary_test(p, boundaries, slack=0): Sn = 0 n = 0 final_time = boundaries[-1].n for bdy in boundaries: # number of samples to draw: m = bdy.n - n X = d.Binomial(m, p).sample() Sn += X n += m if Sn >= bdy.accept: return 1, n, Sn if Sn <= bdy.reject - slack or n > final_time: return 0, n, Sn
def _test(p): posterior = prior Sn = 0 X = d.Binomial(steps, p) for n in range(steps, N + steps, steps): Y = X.sample() Sn += Y posterior = d.posterior(posterior, X, [Y]) # obtain at least 5 samples if posterior.cdf(s) < alpha and n > 5: return 1, n, Sn elif 1 - posterior.cdf(s) < beta: return -1, n, Sn # no outcome after Nmax steps return 0, n, Sn