Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_autocorr_time(self, window=50):
"""
Compute an estimate of the autocorrelation time for each parameter
(length: ``dim``).
:param window: (optional)
The size of the windowing function. This is equivalent to the
maximum number of lags to use. (default: 50)
"""
return autocorr.integrated_time(self.chain, axis=0, window=window)
def get_autocorr_time(self, window=50):
"""
Compute an estimate of the autocorrelation time for each parameter
(length: ``dim``).
:param window: (optional)
The size of the windowing function. This is equivalent to the
maximum number of lags to use. (default: 50)
"""
return autocorr.integrated_time(self.chain, axis=0, window=window)
f'+{ang_percen[2]-ang_percen[1]:.2f})')
print(f'Contrast (mag) = {mag_percen[1]:.2f} '
f'(-{mag_percen[1]-mag_percen[0]:.2f} '
f'+{mag_percen[2]-mag_percen[1]:.2f})')
history = f'walkers = {self.m_nwalkers}, steps = {self.m_nsteps}'
self.m_chain_out_port.copy_attributes(self.m_image_in_port)
self.m_chain_out_port.add_history('MCMCsamplingModule', history)
mean_accept = np.mean(sampler.acceptance_fraction)
print(f'Mean acceptance fraction: {mean_accept:.3f}')
self.m_chain_out_port.add_attribute('ACCEPTANCE', mean_accept, static=True)
try:
autocorr = emcee.autocorr.integrated_time(sampler.get_chain())
print(f'Integrated autocorrelation time = {autocorr}')
except emcee.autocorr.AutocorrError:
autocorr = [np.nan, np.nan, np.nan]
print('The chain is too short to reliably estimate the autocorrelation time. [WARNING]')
self.m_chain_out_port.add_attribute('AUTOCORR_0', autocorr[0], static=True)
self.m_chain_out_port.add_attribute('AUTOCORR_1', autocorr[1], static=True)
self.m_chain_out_port.add_attribute('AUTOCORR_2', autocorr[2], static=True)
self.m_chain_out_port.close_port()
f'(-{mag_percen[1]-mag_percen[0]:.2f} '
f'+{mag_percen[2]-mag_percen[1]:.2f})')
history = f'walkers = {self.m_nwalkers}, steps = {self.m_nsteps}'
self.m_chain_out_port.copy_attributes(self.m_image_in_port)
self.m_chain_out_port.add_history('MCMCsamplingModule', history)
mean_accept = np.mean(sampler.acceptance_fraction)
print(f'Mean acceptance fraction: {mean_accept:.3f}')
self.m_chain_out_port.add_attribute('ACCEPTANCE', mean_accept, static=True)
try:
autocorr = emcee.autocorr.integrated_time(sampler.get_chain())
print(f'Integrated autocorrelation time = {autocorr}')
except emcee.autocorr.AutocorrError:
autocorr = [np.nan, np.nan, np.nan]
print('The chain is too short to reliably estimate the autocorrelation time. [WARNING]')
self.m_chain_out_port.add_attribute('AUTOCORR_0', autocorr[0], static=True)
self.m_chain_out_port.add_attribute('AUTOCORR_1', autocorr[1], static=True)
self.m_chain_out_port.add_attribute('AUTOCORR_2', autocorr[2], static=True)
self.m_chain_out_port.close_port()
def check_convergence(samples):
tau = emcee.autocorr.integrated_time(samples, tol=0)
num = samples.shape[0] * samples.shape[1]
converged = np.all(tau * target_n_eff < num)
converged &= np.all(len(samples) > 50 * tau)
return converged, num / tau
def _interpret_thin(self, thin):
if thin == 'acor':
try:
thin = self.autocorrelation
except emcee.autocorr.AutocorrError:
# we seem to need to change the warning filter here, we have
# warnings as errors set elsewhere in the tests but somehow we
# can't set it back in a test specifically, I think because the
# warning -> error inside an except clause causes more
# problems. So for now just manually set the warnings filter
# here. would probably be good to do something better here
# eventually -tgd 2016-09-16
with warnings.catch_warnings():
warnings.simplefilter("always")
warnings.warn("Chain is too short for autocorrelation thinning, using whole chain")
thin = 1
elif thin is None:
thin = 1
if thin < 1:
thin = 1
else:
# Equation 24: varhat+(z) in Ford 2006
varz = (nsteps-1.0)/bz + varianceofmeans
# Equation 25: Rhat(z) in Ford 2006
gelmanrubin = np.sqrt(varEstimate/withinChainVariances)
# Equation 26: T(z) in Ford 2006
vbz = varEstimate / bz
tz = nchains*nsteps*vbz[vbz < 1]
if tz.size == 0:
tz = [-1]
chains = np.dstack(chains)
chains = np.swapaxes(chains, 0, 2)
autocorrelation = emcee.autocorr.integrated_time(chains, tol = 0)
afactor = np.divide(chains.shape[0], autocorrelation)
archange = np.divide(np.abs(np.subtract(autocorrelation, oldautocorrelation)), oldautocorrelation)
# well-mixed criteria
ismixed = min(tz) > minTz and max(gelmanrubin) < maxGR and \
np.amin(afactor) > minAfactor and np.amax(archange) < maxArchange
return (ismixed, afactor, archange, autocorrelation, gelmanrubin, tz)