Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Args:
total_orbits (int): total number of accepted possible
orbits that are desired. This equals
``num_steps_per_walker`` x ``num_walkers``
burn_steps (int): optional paramter to tell sampler
to discard certain number of steps at the beginning
thin (int): factor to thin the steps of each walker
by to remove correlations in the walker steps
Returns:
``emcee.sampler`` object: the sampler used to run the MCMC
"""
if self.use_pt:
sampler = ptemcee.Sampler(
self.num_walkers, self.num_params, self._logl, orbitize.priors.all_lnpriors,
ntemps=self.num_temps, threads=self.num_threads, logpargs=[self.priors,]
)
else:
sampler = emcee.EnsembleSampler(
self.num_walkers, self.num_params, self._logl,
threads=self.num_threads, kwargs={'include_logp' : True}
)
for pos, lnprob, lnlike in sampler.sample(self.curr_pos, iterations=burn_steps, thin=thin):
pass
sampler.reset()
try:
self.curr_pos = pos
except UnboundLocalError: # 0 step burn-in (pos is not defined)
pass
# TODO: Need something here to pick out temperatures, just using lowest one for now
self.chain = sampler.chain
if self.use_pt:
self.post = sampler.flatchain[0, :, :]
# should also be picking out the lowest temperature logps
self.lnlikes = sampler.loglikelihood[0, :, :].flatten()
self.lnlikes_alltemps = sampler.loglikelihood
else:
self.post = sampler.flatchain
self.lnlikes = sampler.flatlnprobability
# convert posterior probability (returned by sampler objects) to likelihood (required by orbitize.results.Results)
for i, orb in enumerate(self.post):
self.lnlikes[i] -= orbitize.priors.all_lnpriors(orb, self.priors)
# include fixed parameters in posterior
self.post = self._fill_in_fixed_params(self.post)
self.results.add_samples(self.post, self.lnlikes, labels=self.system.labels)
print('Run complete')
if examine_chains:
self.examine_chains()
return sampler
documented in System() above. If M=1, this can be a 1d array.
include_logp (bool): if True, also include log prior in this function
Returns:
lnlikes (float): sum of all log likelihoods of the data given input model
"""
if include_logp:
if np.ndim(params) == 1:
logp = orbitize.priors.all_lnpriors(params, self.priors)
# escape if logp == -np.inf
if np.isinf(logp):
return -np.inf
else:
logp = np.array([orbitize.priors.all_lnpriors(pset, self.priors) for pset in params])
else:
logp = 0 # don't include prior
full_params = self._fill_in_fixed_params(params)
if np.ndim(full_params) == 2:
full_params = full_params.T
return super(MCMC, self)._logl(full_params) + logp
orbits that are desired. This equals
``num_steps_per_walker`` x ``num_walkers``
burn_steps (int): optional paramter to tell sampler
to discard certain number of steps at the beginning
thin (int): factor to thin the steps of each walker
by to remove correlations in the walker steps
examine_chains (boolean): Displays plots of walkers at each step by
running `examine_chains` after `total_orbits` sampled.
Returns:
``emcee.sampler`` object: the sampler used to run the MCMC
"""
if self.use_pt:
sampler = ptemcee.Sampler(
self.num_walkers, self.num_params, self._logl, orbitize.priors.all_lnpriors,
ntemps=self.num_temps, threads=self.num_threads, logpargs=[self.priors, ]
)
else:
sampler = emcee.EnsembleSampler(
self.num_walkers, self.num_params, self._logl,
threads=self.num_threads, kwargs={'include_logp': True}
)
# we're using args because emcee < 3.0 has three return values whereas emcee > 3.0 has
# four. We can explicitly declare 4 variables instead of args in the future.
for args in sampler.sample(self.curr_pos, iterations=burn_steps, thin=thin):
pass
sampler.reset()
try:
self.curr_pos = args[0]
Args:
params (np.array of float): MxR array
of fitting parameters, where R is the number of
parameters being fit, and M is the number of orbits
we need model predictions for. Must be in the same order
documented in System() above. If M=1, this can be a 1d array.
include_logp (bool): if True, also include log prior in this function
Returns:
lnlikes (float): sum of all log likelihoods of the data given input model
"""
if include_logp:
if np.ndim(params) == 1:
logp = orbitize.priors.all_lnpriors(params, self.priors)
# escape if logp == -np.inf
if np.isinf(logp):
return -np.inf
else:
logp = np.array([orbitize.priors.all_lnpriors(pset, self.priors)
for pset in params])
else:
logp = 0 # don't include prior
full_params = self._fill_in_fixed_params(params)
if np.ndim(full_params) == 2:
full_params = full_params.T
return super(MCMC, self)._logl(full_params) + logp
Args:
params (np.array of float): MxR array
of fitting parameters, where R is the number of
parameters being fit, and M is the number of orbits
we need model predictions for. Must be in the same order
documented in System() above. If M=1, this can be a 1d array.
include_logp (bool): if True, also include log prior in this function
Returns:
lnlikes (float): sum of all log likelihoods of the data given input model
"""
if include_logp:
if np.ndim(params) == 1:
logp = orbitize.priors.all_lnpriors(params, self.priors)
# escape if logp == -np.inf
if np.isinf(logp):
return -np.inf
else:
logp = np.array([orbitize.priors.all_lnpriors(pset, self.priors) for pset in params])
else:
logp = 0 # don't include prior
full_params = self._fill_in_fixed_params(params)
if np.ndim(full_params) == 2:
full_params = full_params.T
return super(MCMC, self)._logl(full_params) + logp
self.curr_pos = pos
# TODO: Need something here to pick out temperatures, just using lowest one for now
self.chain = sampler.chain
if self.use_pt:
self.post = sampler.flatchain[0,:,:]
self.lnlikes = sampler.loglikelihood[0,:,:].flatten() # should also be picking out the lowest temperature logps
self.lnlikes_alltemps = sampler.loglikelihood
else:
self.post = sampler.flatchain
self.lnlikes = sampler.flatlnprobability
# convert posterior probability (returned by sampler objects) to likelihood (required by orbitize.results.Results)
for i, orb in enumerate(self.post):
self.lnlikes[i] -= orbitize.priors.all_lnpriors(orb,self.priors)
# include fixed parameters in posterior
self.post = self._fill_in_fixed_params(self.post)
self.results.add_samples(self.post,self.lnlikes, labels=self.system.labels)
print('Run complete')
return sampler
documented in System() above. If M=1, this can be a 1d array.
include_logp (bool): if True, also include log prior in this function
Returns:
lnlikes (float): sum of all log likelihoods of the data given input model
"""
if include_logp:
if np.ndim(params) == 1:
logp = orbitize.priors.all_lnpriors(params, self.priors)
# escape if logp == -np.inf
if np.isinf(logp):
return -np.inf
else:
logp = np.array([orbitize.priors.all_lnpriors(pset, self.priors)
for pset in params])
else:
logp = 0 # don't include prior
full_params = self._fill_in_fixed_params(params)
if np.ndim(full_params) == 2:
full_params = full_params.T
return super(MCMC, self)._logl(full_params) + logp