Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration in rstan

Tags:

c++

r

rcpp

rstan

Is calculating double integration possible in "RStan"? I found in this link pda-in-stan

that one dimensional integration is very slow. Please note that, the double integral is the part of the user-defined log likelihood function.

If it is not possible to calculate a double integral, how can I import my full "Rcpp" code for the whole log likelihood function inside Rstan to compute it?

EDIT

for a simple STAN model (where a one dimensional integration is used); I wrote the code in the header file ("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 */

And the Stan code looks like:

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);
 }

. But when I try the following, I get the error message:

 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

Can you tell me how to resolve this?

Edit 2

Using "verbose= TRUE"; I find the following error message:

    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

Edit 3

Digging up more, I found the following error:

    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());

Edit 4

The model is parsed successfully. But I get different kind of error this time. The code is as follows:

Inside the "guten.hpp" file:

  #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 */

And the STAN code is:
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);
} 
"

. But this time I get the error :

  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

Why is this happening?

like image 448
gultu Avatar asked Jan 24 '26 03:01

gultu


1 Answers

It can be done, but at the moment, you need to write your own C++. The steps are basically:

  1. Declare but do not define a function in the functions block of your Stan program that has a real number for its first argument (and perhaps other inputs that the integral depends on) and outputs a real number. Call that function in your model block.
  2. Define C++ functions with the same signature (the arguments will be templated and remember that a std::ostream* called pstream__ is added to the end of the arguments) that calls stan::math::integrate_1d to do the integral. There are many examples of this.
  3. Pass allow_undefined = TRUE and includes = ... when you compile the thing with rstan::stan_model

There is more information on how to do utilize external C++ functions from Stan programs.

like image 60
Ben Goodrich Avatar answered Jan 26 '26 16:01

Ben Goodrich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!