是否可以根据任意后验函数定义 Stan 模型?

Is it possible to define a Stan model in terms of an arbitrary posterior function?

是否可以根据任意后验函数定义 Stan 模型?

我在想类似 MCMCPack 的 MCMCmetrop1R() 功能,用户可以在其中定义任意后验函数。如果有一个很好的例子说明如何去做,我会很好地研究 C++ API 来做到这一点。

从某种意义上说,定义一个任意的后验分布是你在 Stan 语言中所能做的全部。但是,如果您具体指的是不是已在 Stan 中定义的分布组合的后验分布,那么您可以使用 increment_log_prob 函数将项添加到 log-posterior,无论它们是部分数据的可能性或参数的先验。请参阅标题为 "Custom Probability Functions" 和 "User-Defined Functions".

的用户手册章节

在 stan-users Google 组的常见问题解答下有一个 example,尽管它使用直接操作 lp__ 符号而不是使用increment_log_prob 函数做同样的事情。现在 .stan 文件将是

data {
  int<lower=1> N;
  real<lower=0,upper=1> x[N];
}
transformed data {
  real sum_log_x; // calculate this constant only once
  sum_log_x <- 0.0;
  for (i in 1:N)
    sum_log_x <- sum_log_x + log(x[i]);
}
parameters {
  real<lower=0> a;
  real<lower=0> b;
}
model {
  real summands[N];
  // put priors on a and b here if you want

  // log-likelihood
  increment_log_prob(N * (log(a) + log(b)) + (a - 1) * sum_log_x);
  for (i in 1:N) {
    summands[i] <- (b - 1) * log1m(pow(x[i],a)); // log1m(y) := log(1 - y)
  }
  increment_log_prob(summands);
}