gpt4 book ai didi

c++ - 斯坦福一体化

转载 作者:行者123 更新时间:2023-11-30 03:18:33 24 4
gpt4 key购买 nike

是否可以在“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

使用“详细=真”;我发现以下错误消息:

    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代码是:
古腾斯坦= 函数{

    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++。这些步骤基本上是:

  1. 在 Stan 程序的 functions block 中声明但不定义函数,该函数的第一个参数是实数(可能还有积分所依赖的其他输入)并输出实数.在您的 model block 中调用该函数。
  2. 定义具有相同签名的 C++ 函数(参数将被模板化并记住一个名为 pstream__std::ostream* 被添加到参数的末尾)调用 stan::math::integrate_1d 进行积分。有很多examples
  3. 在使用 rstan::stan_model 编译时传递 allow_undefined = TRUEincludes = ...

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

关于c++ - 斯坦福一体化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54622036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com