在给定收敛准则的情况下实现主持人提前停车

2024-07-04 07:54:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我用emcee.EnsembleSampler来取样一些可能性。我在数百万不同的数据集上执行此操作,因此性能非常重要。在

如果一个链满足了自相关时间的一些收敛准则,就可以让它停止采样。在

在文档中,或者通过搜索,我找不到任何方法来实现这一点,所以希望有人能为它找到一个方法。在


Tags: 数据方法文档时间可能性性能集上准则
1条回答
网友
1楼 · 发布于 2024-07-04 07:54:16

虽然我在当前的emcee文档中也找不到任何内容,但最新版本似乎添加了一个教程,恰好显示了您正试图做的事情:link

如果链接中断或情况发生变化,以下是示例:

import emcee
import numpy as np
np.random.seed(42)

# The definition of the log probability function
# We'll also use the "blobs" feature to track the "log prior" for each step
def log_prob(theta):
    log_prior = -0.5 *  np.sum((theta-1.0)**2 / 100.0)
    log_prob = -0.5 * np.sum(theta**2) + log_prior
    return log_prob, log_prior

# Initialize the walkers
coords = np.random.randn(32, 5)
nwalkers, ndim = coords.shape

# Set up the backend
# Don't forget to clear it in case the file already exists
filename = "tutorial.h5"
backend = emcee.backends.HDFBackend(filename)
backend.reset(nwalkers, ndim)

# Initialize the sampler
sampler = emcee.EnsembleSampler(nwalkers, ndim, log_prob, backend=backend)

max_n = 100000

# We'll track how the average autocorrelation time estimate changes
index = 0
autocorr = np.empty(max_n)

# This will be useful to testing convergence
old_tau = np.inf

# Now we'll sample for up to max_n steps
for sample in sampler.sample(coords, iterations=max_n, progress=True):
    # Only check convergence every 100 steps
    if sampler.iteration % 100:
        continue

    # Compute the autocorrelation time so far
    # Using tol=0 means that we'll always get an estimate even
    # if it isn't trustworthy
    tau = sampler.get_autocorr_time(tol=0)
    autocorr[index] = np.mean(tau)
    index += 1

    # Check convergence
    converged = np.all(tau * 100 < sampler.iteration)
    converged &= np.all(np.abs(old_tau - tau) / tau < 0.01)
    if converged:
        break
    old_tau = tau

相关问题 更多 >

    热门问题