diff --git a/BayesValidRox/BayesInference/MCMC.py b/BayesValidRox/BayesInference/MCMC.py index db1c4a4a900521cbb77c5d4b6621af1bac154cbc..489f0e026ef84011d0d0d6fd3634b7d39b4a2485 100755 --- a/BayesValidRox/BayesInference/MCMC.py +++ b/BayesValidRox/BayesInference/MCMC.py @@ -60,6 +60,8 @@ class MCMC(): lower, upper = np.min(self.initsamples[:,idxDim]),np.max(self.initsamples[:,idxDim]) initsamples[:,idxDim] = st.uniform(loc=lower, scale=upper-lower).rvs(size=self.nwalkers) + print("\n>>>> Bayesian inference with MCMC started. <<<<<<") + if self.mp: with Pool() as pool: sampler = emcee.EnsembleSampler(self.nwalkers, ndim, self.log_posterior, args=[Observation, TotalSigma2], pool=pool) @@ -73,6 +75,28 @@ class MCMC(): # we'll throw-out the burn-in points and reshape: emcee_trace = sampler.chain[:, self.nburn:, :].reshape(-1, ndim).T + # Posterior diagnostics + try: + tau = sampler.get_autocorr_time(tol=0) + except emcee.autocorr.AutocorrError: + tau = 2 + burnin = int(2*np.max(tau)) + thin = int(0.5*np.min(tau)) + samples = sampler.get_chain(discard=burnin, flat=True, thin=thin) + log_prob_samples = sampler.get_log_prob(discard=burnin, flat=True, thin=thin) + + print('\n') + print('-'*15 + 'Posterior diagnostics' + '-'*15) + print("mean auto-correlation time: {0:.3f}".format(np.mean(tau))) + print("thin: {0}".format(thin)) + print("burn-in: {0}".format(burnin)) + print("flat chain shape: {0}".format(samples.shape)) + print("Mean acceptance fraction*: {0:.3f}".format(np.mean(sampler.acceptance_fraction))) + print("flat log prob shape: {0}".format(log_prob_samples.shape)) + print("\n* This value must lay between 0.2 and 0.5") + print('-'*50) + + print("\n>>>> Bayesian inference with MCMC successfully completed. <<<<<<\n") # logml_dict = self.Marginal_llk_emcee(sampler, self.nburn, logp=None, maxiter=5000) # print('\nThe Bridge Sampling Estimation is %.5f.'%(logml_dict['logml'])) diff --git a/BayesValidRox/surrogate_models/surrogate_models.py b/BayesValidRox/surrogate_models/surrogate_models.py index 4431814f0dd6a88b6b5e3e419b1420f266247fb1..56b7ff91d617d8d1d547ed51b8608e2328822cfb 100644 --- a/BayesValidRox/surrogate_models/surrogate_models.py +++ b/BayesValidRox/surrogate_models/surrogate_models.py @@ -1030,7 +1030,7 @@ class Metamodel: if self.metaModel.lower() == 'pcekriging': self.gp_poly[key]["y_"+str(idx+1)] = self.GaussianProcessEmulator(CollocationPoints, self.LCerror[key]["y_"+str(idx+1)]) - print("\n>>>> Training the {0} metamodel finished. <<<<<<\n".format(self.metaModel)) + print("\n>>>> Training the {0} metamodel sucessfully completed. <<<<<<\n".format(self.metaModel)) return self #-------------------------------------------------------------------------------------------------------- diff --git a/BayesValidRox/tests/AnalyticalFunction/Test_AnalyticalFunction.py b/BayesValidRox/tests/AnalyticalFunction/Test_AnalyticalFunction.py index af95523e8751d7cc0231693d7e51c3f4f68d7d50..1a3a3d4007d22beec52f78bd4736f61c398ad5a3 100755 --- a/BayesValidRox/tests/AnalyticalFunction/Test_AnalyticalFunction.py +++ b/BayesValidRox/tests/AnalyticalFunction/Test_AnalyticalFunction.py @@ -39,7 +39,7 @@ from BayesInference.BayesInference import BayesInference, Discrepancy if __name__ == "__main__": # Number of parameters - ndim = 10 # 2, 10 + ndim = 2 # 2, 10 #===================================================== #============= COMPUTATIONAL MODEL ================ @@ -231,7 +231,7 @@ if __name__ == "__main__": # Compute and print RMSE error PostPCE.relErrorPCEModel(nSamples=3000) - # sys.exit('STOP!!') + sys.exit('STOP!!') #===================================================== #======== Bayesian inference with Emulator ========== #===================================================== @@ -245,7 +245,7 @@ if __name__ == "__main__": # Select the inference method BayesOpts.SamplingMethod = 'MCMC' - BayesOpts.MCMCnSteps = 1000 + BayesOpts.MCMCnSteps = 5000 BayesOpts.MultiProcessMCMC = True @@ -263,7 +263,7 @@ if __name__ == "__main__": Bayes_PCE = BayesOpts.create_Inference() - + # sys.exit('STOP!!') #===================================================== #====== Bayesian inference with Forward Model ======== #=====================================================