def pCN(iterations, propose, phi, kappa_0, adapt_frequency=None, adapt_function=None, progress_object=None, storage=None): progress_object = progress.factory(progress_object) if adapt_frequency is not None and adapt_function is None: raise Exception( 'Adapt frequency supplied but no adapt function specified.') # create an empty numpy if the array is not supplied return_array = storage is None if storage is None: storage = st.ArrayStorage(iterations, kappa_0.shape[0]) acceptances = np.empty(iterations, dtype=np.bool) cur_kappa = kappa_0 cur_phi = as_single_number(phi(cur_kappa)) progress_object.initialise(iterations) for i in xrange(iterations): if adapt_frequency is not None and i > 0 and i % adapt_frequency == 0: propose = adapt_function(propose, storage, acceptances) try: new_kappa = propose(cur_kappa) new_phi = as_single_number(phi(new_kappa)) if np.isnan(new_phi): progress_object.report_error( i, 'About to reject proposal because new value of phi is NaN.' .format(i)) accept = False else: alpha = min(1, np.exp(cur_phi - new_phi)) accept = alpha > np.random.uniform() except Exception as ex: progress_object.report_error(i, ex) accept = False if accept: cur_kappa = new_kappa cur_phi = new_phi storage.add_sample(cur_kappa.ravel()) acceptances[i] = accept progress_object.update(i, acceptances[:i]) progress_object.update(iterations, acceptances) if return_array: return storage.array, acceptances return storage, acceptances
def rwm(iterations, propose, log_likelihood, log_prior, init_theta, progress_object=None): progress_object = progress.factory(progress_object) adapt = hasattr(propose, 'adapt') if type(init_theta) is np.ndarray: theta_shape = init_theta.shape[0] else: theta_shape = 1 samples = np.empty((iterations, theta_shape)) acceptances = np.empty(iterations, dtype=np.bool) cur_theta = init_theta cur_log_likelihood = as_single_number(log_likelihood(cur_theta)) cur_log_prior = as_single_number(log_prior(cur_theta)) progress_object.initialise(iterations) for i in xrange(iterations): new_theta = propose(cur_theta) new_log_prior = as_single_number(log_prior(new_theta)) if np.isinf(new_log_prior) and new_log_prior < 0: accept = False else: try: new_log_likelihood = log_likelihood(new_theta) new_log_likelihood = as_single_number(new_log_likelihood) if np.isnan(new_log_likelihood): progress_object.report_error(i, 'Proposal is about to be rejected because likelihood is NaN') accept = False elif np.isnan(new_log_prior): progress_object.report_error(i, 'Proposal is about to be rejected because prior is NaN') accept = False else: alpha = min(1, np.exp(new_log_likelihood + new_log_prior - cur_log_likelihood - cur_log_prior)) accept = np.random.uniform() < alpha except Exception as ex: accept = False progress_object.report_error(i, ex) if accept: cur_theta = new_theta cur_log_likelihood = new_log_likelihood cur_log_prior = new_log_prior samples[i, :] = cur_theta acceptances[i] = accept if adapt: propose.adapt(i, accept) progress_object.update(i, acceptances[:(i+1)]) progress_object.update(iterations, acceptances) return samples, acceptances
def pCN( iterations, propose, phi, kappa_0, adapt_frequency=None, adapt_function=None, progress_object=None, storage=None ): progress_object = progress.factory(progress_object) if adapt_frequency is not None and adapt_function is None: raise Exception("Adapt frequency supplied but no adapt function specified.") # create an empty numpy if the array is not supplied return_array = storage is None if storage is None: storage = st.ArrayStorage(iterations, kappa_0.shape[0]) acceptances = np.empty(iterations, dtype=np.bool) cur_kappa = kappa_0 cur_phi = as_single_number(phi(cur_kappa)) progress_object.initialise(iterations) for i in xrange(iterations): if adapt_frequency is not None and i > 0 and i % adapt_frequency == 0: propose = adapt_function(propose, storage, acceptances) try: new_kappa = propose(cur_kappa) new_phi = as_single_number(phi(new_kappa)) if np.isnan(new_phi): progress_object.report_error(i, "About to reject proposal because new value of phi is NaN.".format(i)) accept = False else: alpha = min(1, np.exp(cur_phi - new_phi)) accept = alpha > np.random.uniform() except Exception as ex: progress_object.report_error(i, ex) accept = False if accept: cur_kappa = new_kappa cur_phi = new_phi storage.add_sample(cur_kappa.ravel()) acceptances[i] = accept progress_object.update(i, acceptances[:i]) progress_object.update(iterations, acceptances) if return_array: return storage.array, acceptances return storage, acceptances
def rwm(iterations, propose, log_likelihood, log_prior, init_theta, progress_object=None): progress_object = progress.factory(progress_object) adapt = hasattr(propose, 'adapt') if type(init_theta) is np.ndarray: theta_shape = init_theta.shape[0] else: theta_shape = 1 samples = np.empty((iterations, theta_shape)) acceptances = np.empty(iterations, dtype=np.bool) cur_theta = init_theta cur_log_likelihood = as_single_number(log_likelihood(cur_theta)) cur_log_prior = as_single_number(log_prior(cur_theta)) progress_object.initialise(iterations) for i in xrange(iterations): new_theta = propose(cur_theta) new_log_prior = as_single_number(log_prior(new_theta)) if np.isinf(new_log_prior) and new_log_prior < 0: accept = False else: try: new_log_likelihood = log_likelihood(new_theta) new_log_likelihood = as_single_number(new_log_likelihood) if np.isnan(new_log_likelihood): progress_object.report_error( i, 'Proposal is about to be rejected because likelihood is NaN' ) accept = False elif np.isnan(new_log_prior): progress_object.report_error( i, 'Proposal is about to be rejected because prior is NaN' ) accept = False else: alpha = min( 1, np.exp(new_log_likelihood + new_log_prior - cur_log_likelihood - cur_log_prior)) accept = np.random.uniform() < alpha except Exception as ex: accept = False progress_object.report_error(i, ex) if accept: cur_theta = new_theta cur_log_likelihood = new_log_likelihood cur_log_prior = new_log_prior samples[i, :] = cur_theta acceptances[i] = accept if adapt: propose.adapt(i, accept) progress_object.update(i, acceptances[:(i + 1)]) progress_object.update(iterations, acceptances) return samples, acceptances