【问题标题】:Implement early stopping in emcee given a convergence criterion在给定收敛标准的情况下实现主持人提前停止
【发布时间】:2018-05-11 14:21:02
【问题描述】:

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

一旦满足自相关时间的某些收敛标准,如果能够让链停止采样,那就太好了。

我在文档中或通过我的搜索找不到任何这样做的方法,所以希望有人有它的秘诀。

【问题讨论】:

    标签: python mcmc emcee


    【解决方案1】:

    虽然我在当前的 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
    

    【讨论】:

    • 亲爱的塞巴斯蒂安,请给我您的电子邮件地址好吗?我想请教一下我在 EMCEE 中遇到的一个问题。
    猜你喜欢
    • 2021-10-25
    • 2018-07-28
    • 2021-02-09
    • 2021-12-13
    • 1970-01-01
    • 2020-04-29
    • 2020-02-03
    • 1970-01-01
    • 2016-06-24
    相关资源
    最近更新 更多