def test_autocorrelation_bad_init(self): """ Tests that the legacy and fast ac implementations produce identical output when the sampler is initialized with a bias set of samples :returns: None :rtype: None """ gaussian = MultimodalGaussian() sample_df = sample_to_df(MarkovJumpHMC, gaussian, num_steps=1000) slow_ac_df = slow_autocorrelation(sample_df) slow_ac = slow_ac_df.autocorrelation.as_matrix() fast_ac_df = autocorrelation(sample_df) fast_ac = fast_ac_df.autocorrelation.as_matrix() self.assertTrue(np.isclose(slow_ac, fast_ac, atol=TOL).all())
def test_autocorrelation_good_init_half_window(self): """ Tests that the legacy and fast ac implementations produce identical output when the sampler is not initialized in a biased manner (meaning we don't have to worry about variance mismatch) runs on the half window :returns: None :rtype: None """ gaussian = Gaussian() sample_df = sample_to_df(MarkovJumpHMC, gaussian, num_steps=1000) slow_ac_df = slow_autocorrelation(sample_df, half_window=True) slow_ac = slow_ac_df.autocorrelation.as_matrix() fast_ac_df = autocorrelation(sample_df, half_window=True) fast_ac = fast_ac_df.autocorrelation.as_matrix() self.assertTrue(np.isclose(slow_ac, fast_ac, atol=TOL).all())
nbasis=36 ndims=12 n_steps=1000 half_window=False rand_val = rand(ndims,nbasis/2,density=0.1) W = np.concatenate([rand_val.toarray(), -rand_val.toarray()],axis=1) logalpha = np.random.randn(nbasis,1) PoT_instance = ProductOfT(nbatch=100,ndims=ndims,nbasis=nbasis,W=W,logalpha=logalpha) simulate = hmc.wrapper_hmc(s_rng=s_rng,energy_fn=rw.E_val,dim=dim) a = np.zeros([1,10,n_samples]) b = np.zeros([2,10,n_samples]) for ii in np.arange(n_steps): a[:,:,ii],b[:,:,ii] = simulate() print "Acceptance Rate" print a[:,:,ii] print "Samples" print b[:,:,ii] ac_df = autocorrelation(df,half_window) #Now, we can run the same autocorrelation from the data we will extract from the data frame #but with the theano function ac= hmc.normed_autocorrelation(df) #We can compare the two plots individually and on a single plot #Drop Mic and leave. n_grad_evals = ac_df['num grad'].astype(int)
def plot_ac(distribution, control_params, mjhmc_params, lahmc_params, max_steps=3000, sample_steps=1, truncate=False, truncate_at=0.0, nuts=False, truncate_idx=None): """ distribution is an instantiated distribution object runs the sampler for max steps and then truncates the output to autocorrelation 0.5 throws an error if ac 0.5 is not reached """ from mjhmc.samplers.markov_jump_hmc import MarkovJumpHMC, ControlHMC from mjhmc.misc.autocor import calculate_autocorrelation, autocorrelation from mjhmc.misc.nutshell import sample_nuts_to_df # TODO: bring up to speed of DF-less calc autocor plt.clf() print('Calculating AutoCorrelation for ControlHMC') control_ac = calculate_autocorrelation(ControlHMC, distribution, num_steps=max_steps, sample_steps=sample_steps, half_window=True, use_cached_var=True, **control_params) print('Calculating AutoCorrelation for MJHMC') mjhmc_ac = calculate_autocorrelation(MarkovJumpHMC, distribution, num_steps=max_steps, sample_steps=sample_steps, half_window=True, resample=False, use_cached_var=True, **mjhmc_params) # lahmc_ac = calculate_autocorrelation(LAHMC, distribution, # num_steps=max_steps, # sample_steps=sample_steps, # half_window=True, # **lahmc_params) if nuts: print('Calculating AutoCorrelation for NUTS') nuts_df = sample_nuts_to_df(distribution, 100000, n_burn_in=10000) nuts_ac = autocorrelation(nuts_df, half_window=True) # find idx with autocorrelation closest to truncate_at if truncate: control_trunc = control_ac.loc[:, 'autocorrelation'] < truncate_at mjhmc_trunc = mjhmc_ac.loc[:, 'autocorrelation'] < truncate_at if truncate_idx is None: if nuts: nuts_trunc = nuts_ac.loc[:, 'autocorrelation'] < truncate_at trunc_idx = max(control_trunc[control_trunc].index[0], mjhmc_trunc[mjhmc_trunc].index[0]) # nuts_trunc[nuts_trunc].index[0]) nuts_ac = nuts_ac.loc[:trunc_idx] else: trunc_idx = max(control_trunc[control_trunc].index[0], mjhmc_trunc[mjhmc_trunc].index[0]) else: trunc_idx = truncate_idx control_ac = control_ac.loc[:trunc_idx] mjhmc_ac = mjhmc_ac.loc[:trunc_idx] control_ac.index = control_ac['num grad'] mjhmc_ac.index = control_ac['num grad'] if nuts: nuts_ac.index = nuts_ac['num grad'] nuts_ac['autocorrelation'].plot(label='NUTS') # control_ac['autocorrelation'].plot(label='Control HMC;\n {}'.format(str(control_params))) # mjhmc_ac['autocorrelation'].plot(label='Markov Jump HMC;\n {}'.format(str(mjhmc_params))) control_ac['autocorrelation'].plot(label='Control HMC') mjhmc_ac['autocorrelation'].plot(label='Markov Jump HMC') plt.xlabel("Gradient Evaluations") plt.ylabel("Autocorrelation") plt.title("{}D {}".format(distribution.ndims, type(distribution).__name__)) plt.legend() plt.show() plt.savefig("{}_{}_dim_ac_{}_steps.pdf".format( type(distribution).__name__, distribution.ndims, max_steps)) plt.show()
nbasis=36 ndims=12 n_steps=30000 half_window=False rand_val = rand(ndims,nbasis/2,density=0.1) W = np.concatenate([rand_val.toarray(), -rand_val.toarray()],axis=1) logalpha = np.random.randn(nbasis,1) #PoT_instance = ProductOfT(nbatch=100,ndims=ndims,nbasis=nbasis,W=W,lognu=logalpha) df = sample_to_df(MarkovJumpHMC,Gaussian(ndims=10),num_steps=n_steps) tm1 = time.time() ac_df = slow_autocorrelation(df,half_window) tm2 = time.time() print tm2-tm1 #Now, we can run the same autocorrelation from the data we will extract from the data frame #but with the theano function tm1 = time.time() ac= autocorrelation(df) tm2 = time.time() print tm2-tm1 #We can compare the two plots individually and on a single plot #Drop Mic and leave. n_grad_evals = ac_df['num grad'].astype(int) fig = plt.figure() plt.plot(n_grad_evals,ac['autocorrelation'],'r') plt.plot(n_grad_evals,ac_df['autocorrelation'],'g') fig.savefig('tmp_autocorr.png')
def plot_ac(distribution, control_params, mjhmc_params, lahmc_params, max_steps=3000, sample_steps=1, truncate=False, truncate_at=0.0, nuts=False, truncate_idx=None): """ distribution is an instantiated distribution object runs the sampler for max steps and then truncates the output to autocorrelation 0.5 throws an error if ac 0.5 is not reached """ plt.clf() print('Calculating AutoCorrelation for ControlHMC') control_ac = calculate_autocorrelation(ControlHMC, distribution, num_steps=max_steps, sample_steps=sample_steps, half_window=True, use_cached_var=True, **control_params) print('Calculating AutoCorrelation for MJHMC') mjhmc_ac = calculate_autocorrelation(MarkovJumpHMC, distribution, num_steps=max_steps, sample_steps=sample_steps, half_window=True, resample=False, use_cached_var=True, **mjhmc_params) # lahmc_ac = calculate_autocorrelation(LAHMC, distribution, # num_steps=max_steps, # sample_steps=sample_steps, # half_window=True, # **lahmc_params) if nuts: print('Calculating AutoCorrelation for NUTS') nuts_df = sample_nuts_to_df(distribution, 100000, n_burn_in=10000) nuts_ac = autocorrelation(nuts_df, half_window=True) # find idx with autocorrelation closest to truncate_at if truncate: control_trunc = control_ac.loc[:, 'autocorrelation'] < truncate_at mjhmc_trunc = mjhmc_ac.loc[:, 'autocorrelation'] < truncate_at if truncate_idx is None: if nuts: nuts_trunc = nuts_ac.loc[:, 'autocorrelation'] < truncate_at trunc_idx = max(control_trunc[control_trunc].index[0], mjhmc_trunc[mjhmc_trunc].index[0]) # nuts_trunc[nuts_trunc].index[0]) nuts_ac = nuts_ac.loc[:trunc_idx] else: trunc_idx = max(control_trunc[control_trunc].index[0], mjhmc_trunc[mjhmc_trunc].index[0]) else: trunc_idx = truncate_idx control_ac = control_ac.loc[:trunc_idx] mjhmc_ac = mjhmc_ac.loc[:trunc_idx] control_ac.index = control_ac['num grad'] mjhmc_ac.index = control_ac['num grad'] if nuts: nuts_ac.index = nuts_ac['num grad'] nuts_ac['autocorrelation'].plot(label='NUTS') # control_ac['autocorrelation'].plot(label='Control HMC;\n {}'.format(str(control_params))) # mjhmc_ac['autocorrelation'].plot(label='Markov Jump HMC;\n {}'.format(str(mjhmc_params))) control_ac['autocorrelation'].plot(label='Control HMC') mjhmc_ac['autocorrelation'].plot(label='Markov Jump HMC') plt.xlabel("Gradient Evaluations") plt.ylabel("Autocorrelation") plt.title("{}D {}".format(distribution.ndims, type(distribution).__name__)) plt.legend() plt.show() plt.savefig("{}_{}_dim_ac_{}_steps.pdf".format(type(distribution).__name__, distribution.ndims, max_steps)) plt.show()