def make_summary(self): """ Calculates posterior summaries for all parameters and puts them in a list """ stacked = np.vstack(self._chains) self._mean = np.mean(stacked, axis=0) self._std = np.std(stacked, axis=0) self._quantiles = np.percentile(stacked, [2.5, 25, 50, 75, 97.5], axis=0) self._ess = pints.effective_sample_size(stacked) self._ess_per_second = self._ess / self._time self._num_params = stacked.shape[1] self._num_chains = len(self._chains) # If there is more than 1 chain calculate rhat # otherwise return NA if self._num_chains > 1: self._rhat = pints.rhat_all_params(self._chains) else: self._rhat = np.repeat("NA", self._num_params) for i in range(0, self._num_params): self._summary_list.append([ "param " + str(i + 1), self._mean[i], self._std[i], self._quantiles[0, i], self._quantiles[1, i], self._quantiles[2, i], self._quantiles[3, i], self._quantiles[4, i], self._rhat[i], self._ess[i], self._ess_per_second[i] ])
def _make_summary(self): """ Calculates posterior summaries for all parameters. """ stacked = np.vstack(self._chains) # Mean, std and quantiles self._mean = np.mean(stacked, axis=0) self._std = np.std(stacked, axis=0) self._quantiles = np.percentile(stacked, [2.5, 25, 50, 75, 97.5], axis=0) # Rhat self._rhat = pints.rhat_all_params(self._chains) # Effective sample size self._ess = np.zeros(self._n_parameters) for i, chain in enumerate(self._chains): self._ess += pints.effective_sample_size(chain) if self._time is not None: self._ess_per_second = np.array(self._ess) / self._time # Create for i in range(0, self._n_parameters): row = [ self._parameter_names[i], self._mean[i], self._std[i], self._quantiles[0, i], self._quantiles[1, i], self._quantiles[2, i], self._quantiles[3, i], self._quantiles[4, i], self._rhat[i], self._ess[i], ] if self._time is not None: row.append(self._ess_per_second[i]) self._summary_list.append(row)
mcmc.set_max_iterations(sample_size) # Start adapting after 1000 iterations mcmc.set_initial_phase_iterations(sample_size // 4) # Disable logging mode mcmc.set_log_to_screen(False) # Run! #print('Running...') chains = mcmc.run() #choose the first chain chain0 = chains[0] N_effective1 = pints.effective_sample_size(chain0) N_effective1[i] = int(round(N_effective1[i])) if N_effective1[i] < L: mcmc = pints.MCMCController(log_posterior, 3, xs, method=pints.HaarioACMC) sample_size = Ldash * L // N_effective1[i] mcmc.set_max_iterations(sample_size) mcmc.set_initial_phase_iterations(sample_size // 4) mcmc.set_log_to_screen(False) chains = mcmc.run() s = sample_size // 4 + 1 #HMC: s = 1 b = False
def _run(self, result, log_path): import pints import pints.toy import numpy as np import logging log = logging.getLogger(__name__) DEBUG = False # Store method name result['method'] = self._method log.info('Using method: ' + self._method) # Get method class method = getattr(pints, self._method) # Check number of chains if isinstance(method, pints.SingleChainMCMC) and self._nchains > 1: log.warn('SingleChainMCMC run with more than 1 chain.') elif isinstance(method, pints.MultiChainMCMC) and self._nchains == 1: log.warn('MultiChainMCMC run with only 1 chain.') # Create a log pdf xtrue = np.array([2, 4]) sigma = np.diag(np.array([1, 3])) log_pdf = pints.toy.GaussianLogPDF(xtrue, sigma) # Create a log prior log_prior = pints.MultivariateGaussianLogPrior(xtrue + 1, sigma * 2) # Generate random points x0 = log_prior.sample(self._nchains) # Create a realistic sigma - for some methods only! sigma = None if method == pints.HamiltonianMCMC: sigma = np.diag(np.array([1, 3])) # Create a sampling routine mcmc = pints.MCMCController( log_pdf, self._nchains, x0, sigma0=sigma, method=method) mcmc.set_parallel(True) # Log to file if not DEBUG: mcmc.set_log_to_screen(False) mcmc.set_log_to_file(log_path) # Set max iterations n_iter = self._max_iter n_burn = int(self._max_iter * 0.5) n_init = int(self._max_iter * 0.1) mcmc.set_max_iterations(n_iter) if mcmc.method_needs_initial_phase(): mcmc.set_initial_phase_iterations(n_init) # Run chains = mcmc.run() if DEBUG: import matplotlib.pyplot as plt import pints.plot pints.plot.trace(chains) plt.show() # Combine chains (weaving, so we can see the combined progress per # iteration for multi-chain methods) chain = pfunk.weave(chains) # Calculate KLD for a sliding window n_samples = len(chain) # Total samples n_window = 500 * self._nchains # Window size n_jump = 20 * self._nchains # Spacing between windows iters = list(range(0, n_samples - n_window + n_jump, n_jump)) result['iters'] = iters result['klds'] = [ log_pdf.kl_divergence(chain[i:i + n_window]) for i in iters] # Remove burn-in # For multi-chain, multiply by n_chains because we wove the chains # together. chain = chain[n_burn * self._nchains:] log.info('Chain shape (without burn-in): ' + str(chain.shape)) log.info('Chain mean: ' + str(np.mean(chain, axis=0))) # Store kullback-leibler divergence after burn-in result['kld'] = log_pdf.kl_divergence(chain) # Store effective sample size result['ess'] = pints.effective_sample_size(chain) # Store status result['status'] = 'done'
np.savetxt('data/kCatsAllData.txt', noisy_data) # fit to this data using MCMC default_params_noise = np.hstack([default_params, 0.1]) problem = pints.MultiOutputProblem(model, times, noisy_data) log_prior = pints.UniformLogPrior(pints.RectangularBoundaries(0.1*default_params_noise, 10*default_params_noise)) log_likelihood = MultiplicativeGaussianLogLikelihood(problem) log_posterior = pints.LogPosterior(log_likelihood, log_prior) mcmc = pints.MCMCController(log_posterior, 3, [default_params_noise, default_params_noise*0.8, default_params_noise*1.25], method=pints.HaarioBardenetACMC) mcmc.set_parallel(False) mcmc.set_max_iterations(2000) chains = mcmc.run() reps = 1 while max(pints.rhat(chains[:, :, :])) > 1.10 or min(pints.effective_sample_size(chains[0, :, :-1])) < 450: mcmc = pints.MCMCController(log_posterior, 3, chains[:, -1, :], method=pints.HaarioBardenetACMC) mcmc.set_parallel(False) mcmc.set_max_iterations(2000) mcmc.set_log_to_screen(False) chain = mcmc.run() new_chains = np.zeros((chains.shape[0], chains.shape[1] + 2000, chains.shape[2])) new_chains[:, :-2000, :] = chains new_chains[:, -2000:, :] = chain[:, :, :] chains = new_chains reps += 1 print(pints.rhat(chains[:, :, :])) for i in range(3): np.savetxt('results/kCatsAll_' + str(i + 1), chains[i, :, :])
def _run(self, result): import pints import pints.toy log = logging.getLogger(__name__) # Allow slightly older pints versions to be tested try: from pints.toy import TwistedGaussianLogPDF except ImportError: from pints.toy import TwistedNormalLogPDF as TwistedGaussianLogPDF try: from pints import MultivariateGaussianLogPrior except ImportError: from pints import MultivariateNormalLogPrior \ as MultivariateGaussianLogPrior try: from pints import MCMCController except ImportError: from pints import MCMCSampling as MCMCController DEBUG = False # Show method name log.info('Using method: ' + self._method) # Get method class method = getattr(pints, self._method) # Check number of chains if isinstance(method, pints.SingleChainMCMC) and self._nchains > 1: log.warn('SingleChainMCMC run with more than 1 chain.') elif isinstance(method, pints.MultiChainMCMC) and self._nchains == 1: log.warn('MultiChainMCMC run with only 1 chain.') # Create a log pdf (use multi-modal, but with a single mode) log_pdf = TwistedGaussianLogPDF(dimension=2, b=0.1) # Generate random starting point(s) log_prior = MultivariateGaussianLogPrior([0, 0], [[10, 0], [0, 10]]) x0 = log_prior.sample(self._nchains) # Set an initial covariance matrix sigma0 = np.diag(np.array([1, 3])) # Population MCMC seems to work much better without an initial sigma if method == pints.PopulationMCMC: sigma0 = None # Set up a sampling routine mcmc = MCMCController(log_pdf, self._nchains, x0, sigma0=sigma0, method=method) mcmc.set_parallel(False) # allow external parallelisation instead # Log to file if not DEBUG: mcmc.set_log_to_screen(False) # Set max iterations n_iter = self._max_iter n_burn = int(n_iter / 2) n_init = 1000 mcmc.set_max_iterations(n_iter) if mcmc.method_needs_initial_phase(): mcmc.set_initial_phase_iterations(n_init) # Run chains = mcmc.run() if DEBUG: import matplotlib.pyplot as plt import pints.plot pints.plot.trace(chains) plt.show() # Combine chains (weaving, so we can see the combined progress per # iteration for multi-chain methods) chain = pfunk.weave(chains) # Remove burn-in # For multi-chain, multiply by n_chains because we wove the chains # together. chain = chain[n_burn * self._nchains:] log.info('Chain shape (without burn-in): ' + str(chain.shape)) log.info('Chain mean: ' + str(np.mean(chain, axis=0))) # Store kullback-leibler divergence after burn-in result['kld'] = log_pdf.kl_divergence(chain) # Store effective sample size result['ess'] = pints.effective_sample_size(chain) # Store status result['status'] = 'done'
def _run(self, result, log_path): import pints import pints.toy log = logging.getLogger(__name__) # Allow slightly older pints versions to be tested try: from pints.toy import TwistedGaussianLogPDF except ImportError: from pints.toy import TwistedNormalLogPDF as TwistedGaussianLogPDF try: from pints import MultivariateGaussianLogPrior except ImportError: from pints import MultivariateNormalLogPrior \ as MultivariateGaussianLogPrior try: from pints import MCMCController except ImportError: from pints import MCMCSampling as MCMCController DEBUG = False # Store method name result['method'] = self._method log.info('Using method: ' + self._method) # Get method class method = getattr(pints, self._method) # Check number of chains if isinstance(method, pints.SingleChainMCMC) and self._nchains > 1: log.warn('SingleChainMCMC run with more than 1 chain.') elif isinstance(method, pints.MultiChainMCMC) and self._nchains == 1: log.warn('MultiChainMCMC run with only 1 chain.') # Create a log pdf (use multi-modal, but with a single mode) log_pdf = TwistedGaussianLogPDF(dimension=2, b=0.1) # Generate a prior log_prior = MultivariateGaussianLogPrior([0, 0], [[10, 0], [0, 10]]) # Generate random starting point(s) x0 = log_prior.sample(self._nchains) # Set up a sampling routine mcmc = MCMCController(log_pdf, self._nchains, x0, method=method) mcmc.set_parallel(True) # Log to file if not DEBUG: mcmc.set_log_to_screen(False) mcmc.set_log_to_file(log_path) # Set max iterations n_iter = self._max_iter n_burn = int(n_iter / 2) n_init = 1000 mcmc.set_max_iterations(n_iter) if mcmc.method_needs_initial_phase(): mcmc.set_initial_phase_iterations(n_init) # Run chains = mcmc.run() if DEBUG: import matplotlib.pyplot as plt import pints.plot pints.plot.trace(chains) plt.show() # Combine chains (weaving, so we can see the combined progress per # iteration for multi-chain methods) chain = pfunk.weave(chains) # Calculate KLD for a sliding window n_samples = len(chain) # Total samples n_window = 500 * self._nchains # Window size n_jump = 20 * self._nchains # Spacing between windows iters = list(range(0, n_samples - n_window + n_jump, n_jump)) result['iters'] = iters result['klds'] = [ log_pdf.kl_divergence(chain[i:i + n_window]) for i in iters ] # Remove burn-in # For multi-chain, multiply by n_chains because we wove the chains # together. chain = chain[n_burn * self._nchains:] log.info('Chain shape (without burn-in): ' + str(chain.shape)) log.info('Chain mean: ' + str(np.mean(chain, axis=0))) # Store kullback-leibler divergence after burn-in result['kld'] = log_pdf.kl_divergence(chain) # Store effective sample size result['ess'] = pints.effective_sample_size(chain) # Store status result['status'] = 'done'
log_likelihood = MultiplicativeGaussianLogLikelihood(problem) log_posterior = pints.LogPosterior(log_likelihood, log_prior) mcmc = pints.MCMCController( log_posterior, 3, [ default_params_noise, default_params_noise * 0.8, default_params_noise * 1.25 ], method=pints.HaarioBardenetACMC) mcmc.set_parallel(False) mcmc.set_max_iterations(200) chains = mcmc.run() reps = 1 while max(pints.rhat(chains[:, :, :])) > 1.10 or min( pints.effective_sample_size(chains[0, :, :-1])) < 450: mcmc = pints.MCMCController(log_posterior, 3, chains[:, -1, :], method=pints.HaarioBardenetACMC) mcmc.set_parallel(False) mcmc.set_max_iterations(2000) mcmc.set_log_to_screen(False) chain = mcmc.run() new_chains = np.zeros( (chains.shape[0], chains.shape[1] + 2000, chains.shape[2])) new_chains[:, :-2000, :] = chains new_chains[:, -2000:, :] = chain[:, :, :] chains = new_chains reps += 1 print(pints.rhat(chains[:, :, :]))
def _run(self, result): import pints import pints.toy import numpy as np import logging log = logging.getLogger(__name__) DEBUG = False # Show method name log.info('Using method: ' + self._method) # Get method class method = getattr(pints, self._method) # Check number of chains if isinstance(method, pints.SingleChainMCMC) and self._nchains > 1: log.warn('SingleChainMCMC run with more than 1 chain.') elif isinstance(method, pints.MultiChainMCMC) and self._nchains == 1: log.warn('MultiChainMCMC run with only 1 chain.') # Create a log pdf xtrue = np.array([2, 4]) sigma = np.diag(np.array([1, 3])) log_pdf = pints.toy.GaussianLogPDF(xtrue, sigma) # Generate random points log_prior = pints.MultivariateGaussianLogPrior(xtrue + 1, sigma * 2) x0 = log_prior.sample(self._nchains) # Create a sampling routine mcmc = pints.MCMCController( log_pdf, self._nchains, x0, sigma0=sigma, method=method) mcmc.set_parallel(False) # allow external parallelisation instead # Set hyperparameters for selected methods if method == pints.MALAMCMC: for sampler in mcmc.samplers(): # Set MALA step size sampler.set_epsilon([1.5, 1.5]) # Log to file if not DEBUG: mcmc.set_log_to_screen(False) # Set max iterations n_iter = self._max_iter n_burn = int(self._max_iter * 0.5) n_init = int(self._max_iter * 0.1) mcmc.set_max_iterations(n_iter) if mcmc.method_needs_initial_phase(): mcmc.set_initial_phase_iterations(n_init) # Run chains = mcmc.run() if DEBUG: import matplotlib.pyplot as plt import pints.plot pints.plot.trace(chains) plt.show() # Combine chains (weaving, so we can see the combined progress per # iteration for multi-chain methods) chain = pfunk.weave(chains) # Remove burn-in # For multi-chain, multiply by n_chains because we wove the chains # together. chain = chain[n_burn * self._nchains:] log.info('Chain shape (without burn-in): ' + str(chain.shape)) log.info('Chain mean: ' + str(np.mean(chain, axis=0))) # Store kullback-leibler divergence after burn-in result['kld'] = log_pdf.kl_divergence(chain) # Store effective sample size result['ess'] = pints.effective_sample_size(chain) # Store status result['status'] = 'done'
def _run(self, result): import pints import pints.toy import numpy as np import logging log = logging.getLogger(__name__) DEBUG = False # Show method name log.info('Using method: ' + self._method) # Get method class method = getattr(pints, self._method) # Check number of chains if isinstance(method, pints.SingleChainMCMC) and self._nchains > 1: log.warn('SingleChainMCMC run with more than 1 chain.') elif isinstance(method, pints.MultiChainMCMC) and self._nchains == 1: log.warn('MultiChainMCMC run with only 1 chain.') # Create a log pdf sigma = 2 r = 4 log_pdf = pints.toy.SimpleEggBoxLogPDF(sigma=sigma, r=r) # Generate random starting point(s) d = 2 * 6 * r * sigma log_prior = pints.MultivariateGaussianLogPrior([0, 0], [[d, 0], [0, d]]) x0 = log_prior.sample(self._nchains) # Set up a sampling routine mcmc = pints.MCMCController(log_pdf, self._nchains, x0, method=method) mcmc.set_parallel(False) # allow external parallelisation instead # Log to file if not DEBUG: mcmc.set_log_to_screen(False) # Set max iterations n_iter = 50000 n_burn = 10000 n_init = 1000 mcmc.set_max_iterations(n_iter) if mcmc.method_needs_initial_phase(): mcmc.set_initial_phase_iterations(n_init) # Run chains = mcmc.run() if DEBUG: import matplotlib.pyplot as plt import pints.plot pints.plot.trace(chains) plt.show() # Combine chains (weaving, so we can see the combined progress per # iteration for multi-chain methods) chain = pfunk.weave(chains) # Remove burn-in # For multi-chain, multiply by n_chains because we wove the chains # together. chain = chain[n_burn * self._nchains:] log.info('Chain shape (without burn-in): ' + str(chain.shape)) log.info('Chain mean: ' + str(np.mean(chain, axis=0))) # Store kullback-leibler-based score after burn-in result['kld'] = log_pdf.kl_divergence(chain) # Store effective sample size result['ess'] = pints.effective_sample_size(chain) # Store status result['status'] = 'done'