Calculating a derivative within a STAN file

Hey there! I’ve been trying to calculate a derivative in STAN using the following .stan file:


functions{
        #include "stan.h"       
        real gammaCDFderiv(real alpha, real x){
                real y = x;
                stan::math::var a = alpha;
                stan::math::var cdf = 0;
                cdf += gamma_cdf(x, a, 1);
                cdf.grad();
                return a.adj();
        }
        real lgammaJacLinf(vector x, real alpha, real lambda){
                vector[num_elements(x)] prob;
                real my_target = 0;
                real num_elem = num_elements(x);
                for (i in 2:num_elements(x)){
                        for(j in 1:i){  
                                real gam_1 = gammaCDFderiv(alpha, lambda*x[j]);
                                real gam_2 = gammaCDFderiv(alpha, lambda*x[i]);
                                real gamma_term = tgamma(alpha + 1);
                                real denom_1 = pow(lambda*x[j], alpha) * exp(-lambda*x[j]);
                                real denom_2 = pow(lambda*x[i], alpha) * exp(-lambda*x[i]);
                                real term_1 = gamma_term * gam_1 / denom_1;
                                real term_2 = gamma_term * gam_2 / denom_2;
                                
                                my_target += x[i] * x[j] * fabs(term_1 -term_2);
                        }
                }
                return log(pow(lambda*alpha, -1) * my_target);
        }
}
data{
        int<lower=1> N;
        vector[N] y;
}
parameters{
        real<lower=0>   alpha;
        real<lower=0>   lambda;
}
model{
        y ~ gamma_lpdf(alpha,1/lambda);
        target+=lgammaJacLinf(y,alpha,lambda);
}

I’m getting the error:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable “stan” does not exist.

“stan.h” is a simple header file that looks like:

#include <cmath>
#include <stan/math.hpp>

Any ideas? It looks like my complier is finding the stan/math stuff – I’m wondering why I can’t use the stan::math::etc variables. Thank you so much!

1 Like

That does not work. The #include mechanism in the Stan language is only for including other code in the Stan language. I think you need

http://mc-stan.org/rstan/articles/external.html

Thanks for the referral! I’ve now run

x.gamma = stan_model( model_code = mc, allow_undefined = TRUE,
           includes = paste0('\n#include "', 
                             file.path(getwd(), 'stan.hpp'), '"\n'))

following what was on that article. Now I’m getting the error:

running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file13543714e554.cpp 2> file13543714e554.cpp.err.txt' had status 1Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! In file included from file13543714e554.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:5:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/build_vari_array.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/l
Error in sink(type = "output") : invalid connection

Any advice would be appreciated.

Add verbose = TRUE to stan_model to see the real error message.

Hrmmmm this makes me a bit more lost. Attached is the output:
dataout.txt (80.8 KB)

The syntax is right, but it looks as if you have a Mac with Catalina.

1 Like

This needs some context :-) We don’t hate Mac users with Catalina. But Catalina seems to have broken a few things and we are currently trying to figure out what needs to be done for Stan to work seamlessly on Catalina: Dealing with Catalina

3 Likes

So after trying a number of other things, I discovered that I do not actually have Catalina, so that cannot be the issue here. Attached is the stan model I am attempting, as well as the c++ code for performing the derivative. This (https://mc-stan.org/rstan/articles/external.html) article says what I am attempting is possible, but doesn’t provide much in the way of examples. The Rcode I’m using is:

fitted_model_fid1 = stan_model( "gamma.stan", allow_undefined = TRUE,verbose = TRUE,
           includes = paste0('\n#include "', 
                             file.path(getwd(), 'lgammaJacLinf.hpp'), '"\n')) 

which gives

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! In file included from fileb73a2347bbde.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:5:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/build_vari_array.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/l

gamma.stan (253 Bytes) gammaCDFderiv.hpp (226 Bytes) lgammaJacLinf.hpp (737 Bytes)