在“RStan”中计算双重积分是否可行?我发现在这个链接pda-in-stan
一维整合非常缓慢。请注意,double积分是用户定义的对数似然函数的一部分。
如果无法计算双积分,我如何导入Rstan内的整个对数似然函数的完整“Rcpp”代码来计算它?
编辑
对于简单的STAN模型(使用一维集成);我在头文件(“guten.hpp”)中编写了代码:
#ifndef guten_hpp
#define guten_hpp
#include <gtest/gtest.h>
#include <stan/math.hpp>
#include <test/unit/math/rev/mat/fun/util.hpp>
#include <test/unit/math/rev/scal/fun/nan_util.hpp>
#include <test/unit/util.hpp>
#include <vector>
#include <iostream>
#include <limits>
#include <sstream>
std::ostringstream msgs;
struct f1 {
template <typename T1, typename T2, typename T3>
inline typename stan::return_type<T1, T2, T3>::type operator()(
const T1& x, const T2& xc, const std::vector<T3>& theta, const
std::vector<double>& x_r, const std::vector<int>& x_i, std::ostream* pstream__) const {
return theta[0]*exp(-theta[0]*(x-theta[1]));
}
};
double integratetest( double beta,double lower, double upper,
std::vector<double> m, double M0)
{
std::vector<double> theta = {beta, M0};
double res = stan::math::integrate_1d( f1{}, lower,upper,theta,m,{},msgs, 1e-6);
return res;
}
#endif /* guten_hpp */
Stan代码看起来像:
gutenstan="
functions{
real integratetest( real beta,real lower, real upper,
real[] m , real M0);
real logL (int N, real[] m, real beta, real M0){
real temp=0.0;
for (i in 1:N){
temp += beta*exp(-beta*(m[i]-M0));
}
return (temp);
}
}
data{
int <lower=0> N;
real<lower=0> m[N];
real<lower=0> M0;
real<lower=0> lower;
real<lower=0> upper;
}
parameters{
real<lower=0> beta;
}
model{
//priors
beta~exponential (1);
//likelihood
target +=logL(N,m,beta,M0) - integratetest( beta,
lower, upper, m, M0);
}
。但是当我尝试以下操作时,我收到错误消息:
try(stan_model(model_code = gutenstan,model_name = "mcn", allow_undefined = TRUE,
includes = paste0('\n#include "',
file.path("/Users/myname/Desktop/guten.hpp"), '"\n')))
> Error in sink(type = "output") : invalid connection
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file2544590dc55c.cpp 2> file2544590dc55c.cpp.err.txt' had status 1
你能告诉我怎么解决这个问题吗?
编辑2
使用“verbose = TRUE”;我发现以下错误消息:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file843f28030d1f.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:531:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
#pragma clang diagnostic pop
^
In file included from file843f28030d1f.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:2:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/LU:47:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
#pragma clang diagnostic pop
^
In file included from file843f28030d1f.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:3:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Cholesky:12:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Jacobi:29:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
#pragma clang diagnostic pop
^
In file included from file843f28030d1f.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:3:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Cholesky:43:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
#pragma clang diagnostic pop
^
In file included from file843f28030d1f.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/QR:17:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Householder:27:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
#pragma clang diagnostic pop
^
In file included from file843f28030d1f.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Lib
编辑3
挖掘更多,我发现以下错误:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/prim/arr.hpp:43:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/prim/arr/functor/integrate_1d.hpp:60:14: error: no matching function for call to object of type 'const std::__1::__bind_r<double, const model86b821791915_15d5d6fb04cfa69122c6c995d8e7f4b5_namespace::f1 &, const std::__1::placeholders::__ph<1> &, const std::__1::placeholders::__ph<2> &, const std::__1::vector<double, std::__1::allocator<double> > &, const std::__1::vector<double, std::__1::allocator<double> > &, const std::__1::vector<int, std::__1::allocator<int> > &, std::__1::reference_wrapper<std::__1::basic_ostream<char> > >'
return f(x, std::numeric_limits<double>::quiet_NaN());
编辑4
模型已成功解析。但这次我得到了不同的错误。代码如下:
在“good.hpp”文件中:
#ifndef guten_hpp
#define guten_hpp
#include <stan/math.hpp>
#include <vector>
#include <iostream>
#include <limits>
#include <sstream>
std::ostringstream msgs;
struct f1 {
template <typename T1, typename T2, typename T3>
inline typename stan::return_type<T1, T2, T3>::type operator()(
const T1& x, const T2& xc, const std::vector<T3>& theta, const
std::vector<double>& x_r, const std::vector<int>& x_i, std::ostream* pstream__) const {
return theta[0]*exp(-theta[0]*(x-theta[1]));
}
};
template <typename T_a, typename T_b, typename T_theta>
double integratetest( double beta,double a, double b,
double M0)
{
T_a a_(a);
T_b b_(b);
std::vector<double> theta = {beta, M0};
std::vector<T_theta> theta_(theta.size());
for (size_t i = 0; i < theta.size(); ++i)
theta_[i] = theta[i];
double res = stan::math::integrate_1d( f1{}, a,b,theta,{},{},msgs, 1e-6);
return res;
}
#endif /* guten_hpp */
STAN代码是: gutenstan =“functions {
real integratetest( real beta,real lower, real upper,
real M0);
real logL (int N, real[] m, real beta, real M0){
real temp=0.0;
for (i in 1:N){
temp += beta*exp(-beta*(m[i]-M0));
}
return (temp);
}
}
data{
int <lower=0> N;
real<lower=0> m[N];
real<lower=0> M0;
real<lower=0> lower;
real<lower=0> upper;
}
parameters{
real<lower=0> beta;
}
model{
//priors
beta~exponential (1);
//likelihood
target +=logL(N,m,beta,M0) - integratetest( beta,
lower, upper, M0);
}
"
。但是这次我得到了错误:
Error in dyn.load(libLFile) :
unable to load shared object '/var/folders/4w/v4pl36r9475cb2fkspc3qxkm0000gp/T//RtmpP6Ytl4/file86b8620f7203.so':
dlopen(/var/folders/4w/v4pl36r9475cb2fkspc3qxkm0000gp/T//RtmpP6Ytl4/file86b8620f7203.so, 6): Symbol not found: __ZN59model86b890d5816_ee8283e8dd545f4f32b97f6348535fa6_namespace13integrat etestIN4stan4math3varEdddEEN5boost4math5tools12promote_argsIT_T0_T1_T2_ffE4typeERKS8_RKS9_RKSA_RKSB_PNSt3__113basic_ostreamIcNSM_11char_traitsIcEEEE
Referenced from: /var/folders/4w/v4pl36r9475cb2fkspc3qxkm0000gp/T//RtmpP6Ytl4/file86b8620f7203.so
Expected in: flat namespace
in /var/folders/4w/v4pl36r9475cb2fkspc3qxkm0000gp/T//RtmpP6Ytl4/file86b8620f7203.so
为什么会这样?
它可以完成,但此刻,您需要编写自己的C ++。步骤基本上是:
functions
块中定义一个函数,该函数具有第一个参数的实数(可能还有积分所依赖的其他输入)并输出实数。在model
块中调用该函数。std::ostream*
的pstream__
),它调用stan::math::integrate_1d
来执行积分。这有很多examples。allow_undefined = TRUE
编译东西时,通过includes = ...
和rstan::stan_model
有关如何利用Stan程序的外部C ++函数的更多information。