def get_pbs_ci(x, y, alpha_targ, n_pbs_samples=1000): r2c_hat_obs = rc.r2c_n2m(x.squeeze(), y)[0].squeeze() hat_sig2 = s2_hat_obs_n2m(y) hat_mu2y = mu2_hat_unbiased_obs_n2m(y) theta = [r2c_hat_obs, hat_sig2, 1, hat_mu2y, m, n] x_new, y_new = pds_n2m_r2c(theta, n_pbs_samples, ddof=1) r2c_pbs = rc.r2c_n2m(x_new.squeeze(), y_new)[0].squeeze() ci = np.quantile(r2c_pbs, [alpha_targ / 2, 1 - alpha_targ / 2]).T return ci
def get_npbs_ci(x, y, alpha_targ, n_bs_samples=1000): y_bs = [] for k in range(n_bs_samples): _ = np.array([np.random.choice(y_obs, size=n) for y_obs in y.T]).T y_bs.append(_) y_bs = np.array(y_bs) r2c_bs = rc.r2c_n2m(x.squeeze(), y_bs)[0].squeeze() ci = np.quantile(r2c_bs, [alpha_targ / 2, 1 - alpha_targ / 2]).T return ci
def r2c_n2m_ci_from_post(trace, n, m, r2c_hat_obs, alpha_targ=0.10, nr2cs=50, nr2c_hat=1000): sig2s = trace[0] mu2ys = trace[1] * m sample_inds = np.random.choice(len(sig2s), size=nr2c_hat, replace=False) n_exps = 1 ress = [] r2s = np.linspace(0, 1, nr2cs) for r2 in r2s: res = [] for i in range(nr2c_hat): i = sample_inds[i] theta = [r2, sig2s[i], mu2ys[i], mu2ys[i], m, n] x, y = rc.pds_n2m_r2c(theta, n_exps, ddof=1) res.append(rc.r2c_n2m(x.squeeze(), y)[0]) ress.append(np.array(res).squeeze()) ress = np.array(ress) tol = alpha_targ alpha_obs = np.mean(ress > r2c_hat_obs, 1) ll_dif = np.abs(alpha_obs - alpha_targ / 2) ll_ind = np.argmin(ll_dif) if ll_dif[ll_ind] < tol: ll = r2s[ll_ind] else: ll = 0 ul_dif = np.abs(alpha_obs - (1 - alpha_targ / 2)) ul_ind = np.argmin(ul_dif) if ul_dif[ul_ind] < tol: ul = r2s[ul_ind] else: ul = 1 return ll, ul, alpha_obs
def get_emp_dist_r2er(r2c_check, r2c_hat_obs, trace, m, n, p_thresh=0.01, n_r2c_sims=100): sig2_post = trace[1] #trial-to-trial variance d2m_post = trace[0] * m #dynamic range sample_inds = np.random.choice( len(sig2_post), size=n_r2c_sims, replace=True) #randomly sample from post-dist res = np.zeros(n_r2c_sims) for j in range(n_r2c_sims): k = sample_inds[j] theta = [r2c_check, sig2_post[k], d2m_post[k], d2m_post[k], m, n] x, y = pds_n2m_r2c(theta, 1, ddof=1) res[j] = (rc.r2c_n2m(x.squeeze(), y)[0]).squeeze() return res
def get_hyb_bayes_ci(x, y, n_r2c_sims=1000, alpha_targ=0.1, p_thresh=0.01, n_splits=6, trunc_sig2=[0, np.inf], trunc_d2=[0, np.inf]): #get confidence intervals n, m = y.shape r2c_hat_obs = rc.r2c_n2m(x.squeeze(), y)[0] trace, p = sample_post_s2_d2( y, n_samps=2000, trunc_sig2=trunc_sig2, trunc_d2=trunc_d2) # get posterior dist of params ul, ul_alpha = find_cdf_pos(r2c_hat_obs, alpha_targ / 2, trace, m, n, n_splits=n_splits, p_thresh=p_thresh, n_r2c_sims=n_r2c_sims, int_l=0, int_h=1) ll, ll_alpha = find_cdf_pos(r2c_hat_obs, 1 - alpha_targ / 2, trace, m, n, n_splits=n_splits, p_thresh=p_thresh, n_r2c_sims=n_r2c_sims, int_l=0, int_h=1) return ll, ul, r2c_hat_obs, trace, ll_alpha, ul_alpha
def get_pbs_bca_ci(x, y, alpha, n_bs_samples): #need percentiles alpha and 1-alpha z_alpha = stats.norm.ppf(alpha / 2.) z_1_alpha = stats.norm.ppf(1 - alpha / 2.) m = y.shape[-1] n = y.shape[-2] r2c_hat_obs = rc.r2c_n2m(x.squeeze(), y)[0].squeeze() hat_sig2 = s2_hat_obs_n2m(y) hat_mu2y = mu2_hat_unbiased_obs_n2m(y) theta = [r2c_hat_obs, hat_sig2, 1, hat_mu2y, m, n] x_new, y_new = pds_n2m_r2c(theta, n_bs_samples, ddof=1) r2c_pbs = rc.r2c_n2m(x_new.squeeze(), y_new)[0].squeeze() #jack knife jack_r2c = [] for i in range(m): jack_y = np.array([y[:, j] for j in range(m) if i != j]).T jack_x = np.array([x[j] for j in range(m) if i != j]) _ = rc.r2c_n2m(jack_x, jack_y)[0].squeeze() jack_r2c.append(_.squeeze()) jack_r2c = np.array(jack_r2c) jack_r2c_dot = jack_r2c.mean() #need bias correction factor bias_factor = np.mean(r2c_pbs < r2c_hat_obs) z_hat_0 = stats.norm.ppf(bias_factor) #estimate of coefficient fit theta-variance relationship as linear a_hat = (np.sum((jack_r2c_dot - jack_r2c)**3) / (6. * (np.sum( (jack_r2c_dot - jack_r2c)**2))**(3 / 2.))) if z_hat_0 == np.inf: alpha_1 = 1 alpha_2 = 1 elif z_hat_0 == -np.inf: alpha_1 = 0 alpha_2 = 0 else: alpha_1 = stats.norm.cdf(z_hat_0 + (z_alpha + z_hat_0) / (1 - a_hat * (z_alpha + z_hat_0))) alpha_2 = stats.norm.cdf(z_hat_0 + (z_1_alpha + z_hat_0) / (1 - a_hat * (z_1_alpha + z_hat_0))) #ci_low = np.nanpercentile(r2c_pbs, alpha_1*100, interpolation='lower') #ci_high = np.nanpercentile(r2c_pbs, alpha_2*100, interpolation='lower') ci_low = np.nan ci_high = np.nan try: ci_low = percentile(r2c_pbs, alpha_1 * 100.) ci_high = percentile(r2c_pbs, alpha_2 * 100.) except: print('alpha_1=' + str(alpha_1)) print('alpha_2=' + str(alpha_2)) print('jack_r2c_dot=' + str(jack_r2c_dot)) print('r2c_hat_obs=' + str(r2c_hat_obs)) print('bias_factor=' + str(bias_factor)) print('z_hat_0=' + str(z_hat_0)) print('a_hat=' + str(a_hat)) print('r2c_pbs=' + str(r2c_pbs)) print('jack_r2c=' + str(r2c_pbs)) return [ci_low, ci_high]