当前位置: 首页>>代码示例>>Python>>正文


Python Trace.store方法代码示例

本文整理汇总了Python中trace.Trace.store方法的典型用法代码示例。如果您正苦于以下问题:Python Trace.store方法的具体用法?Python Trace.store怎么用?Python Trace.store使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在trace.Trace的用法示例。


在下文中一共展示了Trace.store方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: mh_query2

# 需要导入模块: from trace import Trace [as 别名]
# 或者: from trace.Trace import store [as 别名]
def mh_query2(model, pred, answer, samples_count, lag=1):
    """
    Metropolis-Hastings algorithm for sampling
    :param model: model to execute
    :param samples_count: how much samples we want
    :type samples_count: int
    :return: samples
    :rtype: list
    """
    MCMC_shared.mh_flag = True
    MCMC_shared.iteration = 0
    samples = []
    miss = True
    model()
    transitions = 0
    rejection = 0
    while len(samples) < samples_count:
        MCMC_shared.iteration += 1
        variables = MCMC_shared.trace.get_vector()
        vector_vals_drift = variables.values()
        vector = [val[0] for val in vector_vals_drift]
        drifts = [val[1] for val in vector_vals_drift]
        shifted_vector = numpy.random.multivariate_normal(vector, numpy.diag(drifts))
        new_trace = Trace(MCMC_shared.trace)
        new_trace.set_vector(dict(zip(variables.keys(), shifted_vector.tolist())), MCMC_shared.iteration)
        old_trace = MCMC_shared.trace
        MCMC_shared.trace = new_trace
        sample = model()
        while not miss and new_trace._likelihood == -float("inf"):
            new_trace.clean(MCMC_shared.iteration)
            new_trace._likelihood = 0
            for name, (chunk, iteration) in new_trace.mem.items():
                if chunk.erp.log_likelihood(chunk.x, *chunk.erp_parameters) == -float("inf"):
                    new_chunk = Chunk(chunk.erp,
                                      numpy.random.normal(old_trace.get(name).x, chunk.drift / 2),
                                      chunk.erp_parameters,
                                      drift=chunk.drift)
                    new_trace.store(name, new_chunk, iteration)
            sample = model()
        MCMC_shared.trace = old_trace
        probability = log(uniform())
        # r = erp.log_proposal_prob()
        if probability < new_trace._likelihood - old_trace._likelihood and (miss or pred(sample)):
            if miss and pred(sample):
                miss = False
                MCMC_shared.drift = 0.05
            transitions += 1
            if (transitions % lag) == 0:
                if not miss:
                    # print sample, rejection
                    samples.append(answer(sample))
            rejection = 0
            MCMC_shared.trace = new_trace
            MCMC_shared.trace.clean(MCMC_shared.iteration)
        else:
            rejection += 1

    return samples
开发者ID:chubakur,项目名称:probabilistic-python,代码行数:60,代码来源:mh.py

示例2: mh_query

# 需要导入模块: from trace import Trace [as 别名]
# 或者: from trace.Trace import store [as 别名]
def mh_query(model, pred, answer, samples_count, lag=1):
    """
    Metropolis-Hastings algorithm for sampling
    :param model: model to execute
    :param samples_count: how much samples we want
    :type samples_count: int
    :return: samples
    :rtype: list
    """
    MCMC_shared.mh_flag = True
    MCMC_shared.iteration = 0
    samples = []
    model()
    prev_name_idx = 0
    transitions = 0
    rejected = 0
    burn_in = 100
    miss = True
    while len(samples) < samples_count:
        MCMC_shared.iteration += 1
        variables = MCMC_shared.trace.names()
        # index = random.randint(0, len(variables) - 1)
        # selected_name = variables[index]
        selected_name = variables[prev_name_idx % len(MCMC_shared.trace.names())]
        prev_name_idx += 1
        current = MCMC_shared.trace.get(selected_name)
        erp, erp_params = current.erp, current.erp_parameters
        new_value = erp.proposal_kernel(current.x, *erp_params)
        # print erp_params
        fwdProb = erp.log_proposal_prob(current.x, new_value, *erp_params)
        rvsProb = erp.log_proposal_prob(new_value, current.x, *erp_params)
        # r и f для flip == 0
        # l = erp.log_likelihood(new_value, *erp_params)
        # new_trace = deepcopy(trace)
        new_trace = Trace(MCMC_shared.trace)
        new_trace.store(selected_name, Chunk(erp, new_value, erp_params), MCMC_shared.iteration)
        old_trace = MCMC_shared.trace
        MCMC_shared.trace = new_trace
        sample = model()
        MCMC_shared.trace = old_trace
        probability = log(uniform())
        # print sample
        # print new_trace._likelihood, old_trace._likelihood
        if probability < new_trace._likelihood - old_trace._likelihood + rvsProb - fwdProb and \
                (miss or pred(sample)):
            if miss and pred(sample):
                miss = False
            transitions += 1
            if not miss:
                if burn_in:
                    burn_in -= 1
                elif (transitions % lag) == 0:
                    # print len(samples), sample, new_trace._likelihood, rejected
                    samples.append(answer(sample))
            rejected = 0
            MCMC_shared.trace = new_trace
            MCMC_shared.trace.clean(MCMC_shared.iteration)
        else:
            rejected += 1

    return samples
开发者ID:chubakur,项目名称:probabilistic-python,代码行数:63,代码来源:mh.py


注:本文中的trace.Trace.store方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。