在“RStan”中可以计算双重积分吗?我在这个连结中找到
pda-in-stan

一维集成非常慢。请注意,双整数是用户定义的对数似然函数的一部分。

如果无法计算双积分,如何为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

该模型已成功解析。但是这次我得到了另一种错误。代码如下:

在“guten.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 =“
职能{
    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++。这些步骤基本上是:

  • 声明但不要在Stan程序的functions块中定义一个函数,该函数的第一个参数具有实数(可能还有积分依赖的其他输入)并输出实数。在model块中调用该函数。
  • 定义具有相同签名的C++函数(参数将作为模板,并记住在参数末尾添加了一个称为std::ostream*pstream__),该函数调用stan::math::integrate_1d进行积分。有很多examples
  • 当您使用allow_undefined = TRUE编译东西时传递includes = ...rstan::stan_model

  • 关于如何利用Stan程序的外部C++函数,还有更多的information

    09-27 08:52