2

我在这里读到有可能(并且我解释得很简单)从 C++ 程序调用Stan例程。

我有一些复杂的对数似然函数,我用 C++ 编写了这些函数,但我真的不知道如何使用 Stan 语言对它们进行编码。是否可以使用我已经用 C++ 编写的对数似然函数在 Stan 中调用 Monte Carlo 例程?如果是这样,有什么例子吗?

这似乎是一件很自然的事情,但我找不到任何关于如何做到这一点的例子或指示。

4

2 回答 2

2

我认为您的问题与您链接的问题有点不同。他有一个完整的 Stan 程序并想从 C++ 驱动它,而您问是否可以通过调用外部 C++ 函数来评估对数似然度来规避编写 Stan 程序。但这不会让你走得太远,因为你仍然必须以 Stan 可以处理的形式传递数据,向 Stan 声明未知参数是什么(加上它们的支持)等等。所以,我认为你不能(或应该)避免学习 Stan 语言。

但是将 C++ 函数暴露给 Stan 语言是相当容易的,这基本上只涉及将 my_loglikelihood.hpp 文件${STAN_HOME}/lib/stan_math_${VERSION}/stan/math/添加到${STAN_HOME}/src/stan/lang/function_signatures.h. 那时,您的 .stan 程序可能看起来 data { // declare data like y, X, etc. } parameters { // declare parameters like theta } model { // call y ~ my_logliklihood_log(theta, X) } 像超过几分钟。Stan 语言非常类似于 C,因此更容易将 .stan 文件解析为 C++ 源文件。这是我为回归上下文中条件高斯结果的对数似然编写的 Stan 函数: functions { /** * Increments the log-posterior with the logarithm of a multivariate normal * likelihood with a scalar standard deviation for all errors * Equivalent to y ~ normal(intercept + X * beta, sigma) but faster * @param beta vector of coefficients (excluding intercept) * @param b precomputed vector of OLS coefficients (excluding intercept) * @param middle matrix (excluding ones) typically precomputed as crossprod(X) * @param intercept scalar (assuming X is centered) * @param ybar precomputed sample mean of the outcome * @param SSR positive precomputed value of the sum of squared OLS residuals * @param sigma positive value for the standard deviation of the errors * @param N integer equal to the number of observations */ void ll_mvn_ols_lp(vector beta, vector b, matrix middle, real intercept, real ybar, real SSR, real sigma, int N) { increment_log_prob( -0.5 * (quad_form_sym(middle, beta - b) + N * square(intercept - ybar) + SSR) / square(sigma) - # 0.91... is log(sqrt(2 * pi())) N * (log(sigma) + 0.91893853320467267) ); } } 这基本上只是我将原本可能是 C 语法的内容转储到 Stan 语言中的函数体中,然后可以在model.stan 程序的块中调用。

因此,简而言之,我认为将 C++ 函数重写为 Stan 函数可能是最简单的。但是,您的对数似然可能涉及一些奇异的东西,目前没有相应的 Stan 语法。在这种情况下,您可以退回到将该 C++ 函数公开给 Stan 语言,并在理想情况下向 GitHub 上 stan-dev 下的数学和 stan 存储库发出拉取请求,以便其他人可以使用它(尽管那时您还必须编写单元测试、文档等)。

于 2015-08-07T01:57:53.670 回答
2

经过进一步审查(您可能不想接受我之前的回答),您可以试试这个:在functions具有正确签名(和解析)但基本上什么都不做的块中编写一个具有用户定义函数的 .stan 程序。像这样 functions { real foo_log(real[] y, vector beta, matrix X, real sigma) { return not_a_number(); // replace this after parsing to C++ } } data { int<lower=1> N; int<lower=1> K; matrix[N,K] X; real y[N]; } parameters { vector[K] beta; real<lower=0> sigma; } model { y ~ foo(beta, X, sigma); // priors here } 然后,使用 CmdStan 编译该模型,这将生成一个 .hpp 文件作为中间步骤。编辑该 .hpp 文件foo_log以调用您的模板化 C++ 函数以及 #include 定义您的内容的头文件。然后重新编译并执行二进制文件。

这实际上可能对您有用,但如果您所做的任何事情都有广泛的用途,我们希望您贡献 C++ 的东西。

于 2015-08-07T17:21:36.913 回答