本文整理汇总了Python中trace.Trace._likelihood方法的典型用法代码示例。如果您正苦于以下问题:Python Trace._likelihood方法的具体用法?Python Trace._likelihood怎么用?Python Trace._likelihood使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trace.Trace
的用法示例。
在下文中一共展示了Trace._likelihood方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mh_query2
# 需要导入模块: from trace import Trace [as 别名]
# 或者: from trace.Trace import _likelihood [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