def ci_cmle_is(X, v, theta_grid, alpha_level, T = 100, verbose = False): cmle_is = np.empty_like(theta_grid) r = X.sum(1) c = X.sum(0) for l, theta_l in enumerate(theta_grid): logit_P_l = theta_l * v w_l = np.exp(logit_P_l) z = cond_a_sample(r, c, w_l, T) logf = np.empty(T) for t in range(T): logQ, logP = z[t][1], z[t][2] logf[t] = logP - logQ logkappa = -np.log(T) + logsumexp(logf) if verbose: logcvsq = -np.log(T - 1) - 2 * logkappa + \ logsumexp(2 * logabsdiffexp(logf, logkappa)) print 'est. cv^2 = %.2f (T = %d)' % (np.exp(logcvsq), T) cmle_is[l] = np.sum(np.log(w_l[X])) - logkappa crit = -0.5 * chi2.ppf(1 - alpha_level, 1) ci = invert_test(theta_grid, cmle_is - cmle_is.max(), crit) if params['plot']: plot_statistics(ax_cmle_is, theta_grid, cmle_is - cmle_is.max(), crit) cmle_is_coverage_data['cis'].append(ci) cmle_is_coverage_data['theta_grid'] = theta_grid cmle_is_coverage_data['crit'] = crit return ci
def ci_cmle_a(X, v, theta_grid, alpha_level): cmle_a = np.empty_like(theta_grid) for l, theta_l in enumerate(theta_grid): logit_P_l = theta_l * v cmle_a[l] = -cond_a_nll(X, np.exp(logit_P_l)) return invert_test(theta_grid, cmle_a - cmle_a.max(), -0.5 * chi2.ppf(1 - alpha_level, 1))
def plot_statistics(ax, theta_grid, test_val, crit): # Compute confidence interval from test statistics ci_l, ci_u = invert_test(theta_grid, test_val, crit) ci_l = max(ci_l, params['theta_l']) ci_u = min(ci_u, params['theta_u']) ax.plot(theta_grid, test_val, color = 'b') ax.hlines(crit, theta_grid[0], theta_grid[-1], linestyle = 'dotted') ax.hlines(crit, ci_l, ci_u, color = 'r') ax.vlines(ci_l, 2.0 * crit, crit, color = 'r', linestyle = 'dotted') ax.vlines(ci_u, 2.0 * crit, crit, color = 'r', linestyle = 'dotted') ax.set_ylim(2.0 * crit, 0)
def ci_cmle_a(X, v, theta_grid, alpha_level): cmle_a = np.empty_like(theta_grid) for l, theta_l in enumerate(theta_grid): logit_P_l = theta_l * v cmle_a[l] = -cond_a_nll(X, np.exp(logit_P_l)) crit = -0.5 * chi2.ppf(1 - alpha_level, 1) ci = invert_test(theta_grid, cmle_a - cmle_a.max(), crit) if params['plot']: plot_statistics(ax_cmle_a, theta_grid, cmle_a - cmle_a.max(), crit) cmle_a_coverage_data['cis'].append(ci) cmle_a_coverage_data['theta_grid'] = theta_grid cmle_a_coverage_data['crit'] = crit return ci
def ci_umle(X, v, theta_grid, alpha_level): arr = array_from_data(X, [v]) arr.offset_extremes() alpha_zero(arr) fit_model = NonstationaryLogistic() umle = np.empty_like(theta_grid) for l, theta_l in enumerate(theta_grid): fit_model.beta['x_0'] = theta_l fit_model.fit(arr, fix_beta = True) umle[l] = -fit_model.nll(arr) crit = -0.5 * chi2.ppf(1 - alpha_level, 1) ci = invert_test(theta_grid, umle - umle.max(), crit) if params['plot']: plot_statistics(ax_umle, theta_grid, umle - umle.max(), crit) umle_coverage_data['cis'].append(ci) umle_coverage_data['theta_grid'] = theta_grid umle_coverage_data['crit'] = crit return ci
def ci_cmle_is(X, v, theta_grid, alpha_level, T = 100, verbose = False): cmle_is = np.empty_like(theta_grid) r = X.sum(1) c = X.sum(0) for l, theta_l in enumerate(theta_grid): logit_P_l = theta_l * v w_l = np.exp(logit_P_l) z = cond_a_sample(r, c, w_l, T) logf = np.empty(T) for t in range(T): logQ, logP = z[t][1], z[t][2] logf[t] = logP - logQ logkappa = -np.log(T) + logsumexp(logf) if verbose: logcvsq = -np.log(T - 1) - 2 * logkappa + \ logsumexp(2 * logabsdiffexp(logf, logkappa)) print 'est. cv^2 = %.2f (T = %d)' % (np.exp(logcvsq), T) cmle_is[l] = np.sum(np.log(w_l[X])) - logkappa return invert_test(theta_grid, cmle_is - cmle_is.max(), -0.5 * chi2.ppf(1 - alpha_level, 1))
def ci_cmle_is(X, v, theta_grid, alpha_level, T=100, verbose=False): cmle_is = np.empty_like(theta_grid) r = X.sum(1) c = X.sum(0) for l, theta_l in enumerate(theta_grid): logit_P_l = theta_l * v w_l = np.exp(logit_P_l) z = cond_a_sample(r, c, w_l, T) logf = np.empty(T) for t in range(T): logQ, logP = z[t][1], z[t][2] logf[t] = logP - logQ logkappa = -np.log(T) + logsumexp(logf) if verbose: logcvsq = -np.log(T - 1) - 2 * logkappa + \ logsumexp(2 * logabsdiffexp(logf, logkappa)) print 'est. cv^2 = %.2f (T = %d)' % (np.exp(logcvsq), T) cmle_is[l] = np.sum(np.log(w_l[X])) - logkappa return invert_test(theta_grid, cmle_is - cmle_is.max(), -0.5 * chi2.ppf(1 - alpha_level, 1))