respRT = helpers.load_subject_data(sub, behavdatadir) # data trials need to be in the order corresponding to the one in the # model, remember that indeces in respRT start at 1, not 0 data = respRT.loc[fitind+1, ['response', 'RT']].values #%% infer with EP-ABC and collect results print('subject: %02d, model: %s' % (sub, mname)) # the for-loop is a fail-safe procedure: try to run EP-ABC 3 times when # it unexpectedly breaks, if it didn't run through the 3rd time, just # continue with the next subject for run in range(3): try: ep_mean, ep_cov, ep_logml, nacc, ntotal, runtime = pyEPABC.run_EPABC( data, simfun, None, pars.mu, pars.cov, epsilon=epsilon, minacc=minacc, samplestep=10000, samplemax=samplemax, npass=npass, alpha=alpha, veps=veps) except KeyboardInterrupt: raise KeyboardInterrupt except: errors[sub] = 'run %d:\n' % run + traceback.format_exc() else: ep_means[si, :] = ep_mean ep_covs[si, :, :] = ep_cov ep_summary.loc[sub, 'logml'] = ep_logml ep_summary.loc[sub, 'nsamples'] = ntotal.sum() ep_summary.loc[sub, 'runtime'] = runtime.total_seconds() ep_summary.loc[sub, pars.names] = pars.get_transformed_mode( ep_mean, ep_cov) #%% preliminary extra analysis
pars.plot_param_dist() simfun = lambda data, dind, parsamples: model.gen_distances_with_params( data[0], data[1], dind, pars.transform(parsamples), pars.names) epsilon = 0.05 veps = 2 * epsilon # calling EPABC: ep_mean, ep_cov, ep_logml, nacc, ntotal, runtime = pyEPABC.run_EPABC( data[['response', 'RT']].values, simfun, None, pars.mu, pars.cov, epsilon=epsilon, minacc=2000, samplestep=10000, samplemax=6000000, npass=2, alpha=0.5, veps=veps) #%% show posterior distribution pg = pars.plot_param_dist(ep_mean, ep_cov) pg.fig.tight_layout() pg.fig.savefig('%02d_posterior.svg' % sub) #%% posterior predictive check choices_post, rts_post = model.gen_response_from_Gauss_posterior(
# epsilon; for response_dist this is: (2*epsilon for the Euclidean distance # between true and sampled RT and another factor of 2 for the two possible # responses - timed out responses don't occur here, because they are only a # point mass, i.e., one particular value of response and RT) veps = 2 * 2 * epsilon data = respRT.loc[fitind + 1, ['response', 'RT']].values # run EPABC ep_mean, ep_cov, ep_logml, nacc, ntotal, runtime = pyEPABC.run_EPABC( data, simfun, distfun, pars.mu, pars.cov, epsilon=epsilon, minacc=2000, samplestep=10000, samplemax=6000000, npass=2, alpha=0.5, veps=veps) print('logml = %8.2f' % ep_logml) #%% evaluate posterior # sample from EPABC posterior (for all trials) NP = 15000 choices, rts, samples_pos = model.gen_response_from_Gauss_posterior( np.arange(L), pars.names,
pg.fig.tight_layout() #%% fit with EP-ABC simfun = lambda data, dind, parsamples: model.gen_distances_with_params( data[0], data[1], dind, pars.transform(parsamples), pars.names) # maximum allowed difference in RT for accepting samples epsilon = 0.05 ep_mean, ep_cov, ep_logml, nacc, ntotal, runtime = pyEPABC.run_EPABC( np.c_[data.choice, data.RT], simfun, None, pars.mu, pars.cov, epsilon=epsilon, minacc=2000, samplestep=10000, samplemax=6000000, npass=2, alpha=0.5, veps=2 * epsilon) #%% investigate posterior modes = pars.get_transformed_mode(ep_mean, ep_cov) fig, axes = pars.compare_pdfs(ep_mean, ep_cov) ch_post, rt_post = model.gen_response_from_Gauss_posterior( np.arange(N), pars.names, ep_mean, ep_cov, 500, pars.transform)
data.values[:, 0].astype(dtype=int)) # maximum distance allowed for accepting samples, note that for # response_dist this is in units of RT epsilon = 0.05 # normalising constant of the uniform distribution defined by distfun and # epsilon; for response_dist this is: (2*epsilon for the Euclidean distance # between true and sampled RT and another factor of 2 for the two possible # responses - timed out responses don't occur here, because they are only a # point mass, i.e., one particular value of response and RT) veps = 2 * 2 * epsilon # run EPABC ep_mean, ep_cov, ep_logml, nacc, ntotal, runtime = run_EPABC( data.values[:, 1:], simfun, response_dist, prior_mean, prior_cov, epsilon=epsilon, minacc=500, samplestep=10000, samplemax=2000000, npass=3, alpha=0.3, veps=veps) # sample from EPABC posterior samples_pos = np.random.multivariate_normal(ep_mean, ep_cov, N) samples_pos = pd.DataFrame(paramtransform(samples_pos), columns=parnames) samples_pos['distribution'] = 'epabc_pos' samples = samples.append(samples_pos, ignore_index=True) # see what happens when you ignore RTs, epco = ep choice only distfun = lambda data, sims: response_dist(data, sims, useRT=False) epco_mean, epco_cov, epco_logml, epco_nacc, epco_ntotal, epco_runtime = run_EPABC( data.values[:, 1:], simfun, distfun, prior_mean, prior_cov, epsilon=epsilon, minacc=10000, samplestep=10000, samplemax=2000000, npass=3, alpha=0.3, veps=0.5)