pymc3 - 在 pymc3 中使用黑盒似然

标签 pymc3

我正在尝试在 pymc3 模型中包含黑盒似然函数。此似然函数仅采用参数值向量并返回似然度(所有数据已包含在函数中)。

到目前为止我一直在关注this guide并修改了代码如下,以适应我的模型只有一个参数 k 的事实。


from elsewhere import loglikelihood

# define a theano Op for our likelihood function
class LogLike(tt.Op):

    """
    Specify what type of object will be passed and returned to the Op when it is
    called. In our case we will be passing it a vector of values (the parameters
    that define our model) and returning a single "scalar" value (the
    log-likelihood)
    """

    itypes = [tt.dvector]  # expects a vector of parameter values when called
    otypes = [tt.dscalar]  # outputs a single scalar value (the log likelihood)

    def __init__(self, loglike):
        """
        Initialise the Op with various things that our log-likelihood function
        requires. Below are the things that are needed in this particular
        example.

        Parameters
        ----------
        loglike:
            The log-likelihood (or whatever) function we've defined
        """

        # add inputs as class attributes
        self.loglikelihood = loglike

    def perform(self, node, inputs, outputs):
        # the method that is used when calling the Op
        (theta,) = inputs  # this will contain my variables

        # call the log-likelihood function
        logl = self.loglikelihood(theta)

        outputs[0][0] = np.array(logl)  # output the log-likelihood


fixed_sigma_loglikelihood = lambda x: loglikelihood(np.concatenate((x,nul_sigmas)))
logl = lh.LogLike(fixed_sigma_loglikelihood)


# Create and sample 1 parameter model 
ndraws = 100
nburn  = 10
k_true = 2.5
the_model = pm.Model()
with the_model:
    k = pm.Uniform("k", lower=0, upper=15)

    # Convert the prior to a theano tensor variable
    theta = tt.as_tensor_variable([k])

    # Create density distribution for our numerical likelihood
    pm.DensityDist("likelihood", lambda y: logl(y), observed={'y':theta})

    trace = pm.sample(ndraws, tune = nburn, discard_tuned_samples=True)

只需打印输出,我就可以看到它正在正确采样。但是,完成采样后,它会失败并出现以下错误:


Only 100 samples in chain.
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Initializing NUTS failed. Falling back to elementwise auto-assignment.
Multiprocess sampling (4 chains in 4 jobs)
Slice: [k]
Could not pickle model, sampling singlethreaded.
Sequential sampling (4 chains in 1 job)
Slice: [k]
Sampling 4 chains for 10 tune and 100 draw iterations (40 + 400 draws total) took 41 seconds.████| 100.00% [110/110 00:10<00:00 Sampling chain 3, 0 divergences]
Traceback (most recent call last):
  File "~/code/integrating-data/pymc_model.py", line 54, in <module>
    trace = pm.sample(ndraws, tune = nburn, discard_tuned_samples=True)
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/pymc3/sampling.py", line 639, in sample
    idata = arviz.from_pymc3(trace, **ikwargs)
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/arviz/data/io_pymc3_3x.py", line 580, in from_pymc3
    return PyMC3Converter(
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/arviz/data/io_pymc3_3x.py", line 181, in __init__
    self.observations, self.multi_observations = self.find_observations()
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/arviz/data/io_pymc3_3x.py", line 194, in find_observations
    multi_observations[key] = val.eval() if hasattr(val, "eval") else val
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/theano/graph/basic.py", line 554, in eval
    self._fn_cache[inputs] = theano.function(inputs, self)
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/theano/compile/function/__init__.py", line 337, in function
    fn = pfunc(
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/theano/compile/function/pfunc.py", line 524, in pfunc
    return orig_function(
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/theano/compile/function/types.py", line 1970, in orig_function
    m = Maker(
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/theano/compile/function/types.py", line 1584, in __init__
    fgraph, additional_outputs = std_fgraph(inputs, outputs, accept_inplace)
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/theano/compile/function/types.py", line 188, in std_fgraph
    fgraph = FunctionGraph(orig_inputs, orig_outputs, update_mapping=update_mapping)
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/theano/graph/fg.py", line 162, in __init__
    self.import_var(output, reason="init")
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/theano/graph/fg.py", line 330, in import_var
    self.import_node(var.owner, reason=reason)
  File "/Users/llamagod/miniconda3/envs/mcmc/lib/python3.9/site-packages/theano/graph/fg.py", line 383, in import_node
    raise MissingInputError(error_msg, variable=var)
theano.graph.fg.MissingInputError: Input 0 of the graph (indices start from 0), used to compute sigmoid(k_interval__), was not provided and not given a value. Use the Theano flag exception_verbosity='high', for more information on this error.

我是 pymc3 的新手,所以这种情况让我迷失了。不幸的是,我需要某种形式的黑盒方法,因为可能性依赖于计算模拟。

最佳答案

根据我查看的评论 this thread并发现 pm. Potential 确实是实现黑盒可能性的最干净的方法。 将上面的代码修改如下即可达到目的:


# Create and sample 1 parameter model 
ndraws = 100
nburn  = 10
k_true = 2.5
the_model = pm.Model()
with the_model:
    k = pm.Uniform("k", lower=0, upper=15)

    # Convert the prior to a theano tensor variable
    theta = tt.as_tensor_variable([k])

    # Adds an arbitrary potential term to the posterior 
    pm.Potential("likelihood", logl(theta)) 

    trace = pm.sample(ndraws, tune = nburn, discard_tuned_samples=True)

关于pymc3 - 在 pymc3 中使用黑盒似然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69841994/

相关文章:

python - 使用 PYMC3 对 RV 求和

pymc3 - 无法禁用 pymc3.find_MAP() 的消息

linear-regression - 使用 PyMC3 的贝叶斯套索

python - 如何在 PyMC3 中定义一个模型,其中一个参数在多个条件下限制为相同值

python - 使用 PyMC3 计算最大似然

python - 在 pymc3 中重写用于动态系统参数估计的 pymc 脚本

bayesian - PyMC3 PK 建模。模型无法解析用于创建数据集的参数

python - 如何从 PyMC3 中的狄利克雷过程中提取无监督集群?

python - PyMC3 traceplot 不显示