【问题标题】:`integrate_1d` syntax and interactively debugging it`integrate_1d` 语法并以交互方式调试它
【发布时间】:2020-01-24 16:13:16
【问题描述】:

我是第一次使用 rstan/Stan 的用户,尝试设置自定义可能性并使用 integrate_1d 集成它。基于documentation,我设置了我的被积函数如下:

// internal function to integrate over z 
// integrate_1d is super picky: the argument types need to be real, real, 
    real[], real[], int[]
// e.g., cannot use vectors in place of real[] or int[]

real integrand( real x,  // the observed Z-stat; value at which to evaluate integral
              real xc,
              real[] theta,
              real[] x_r,
              int[] x_i ){

    // separate the parameters
    real zeta = theta[1];
    real eta = theta[2];
    real vi = theta[3];

    // significance indicator
    int signif; 
    signif = fabs(x) > 1.96;

    return( (1/eta) * (1 - signif) + signif ) * exp( normal_lpdf( x | zeta, sqrt(vi) ) );
  }

我正在尝试测试和调试此功能,因为它最终会导致在我运行模型时评估对数似然性时出错。为此,我希望能够以交互方式运行该函数,传递我自己选择的参数。我正在尝试使用 rstan 的 expose_stan_functions 来执行此操作:

code = "functions{

  real integrand( real x,  // the observed Z-stat
                  real xc,
                  real[] theta,
                  real[] x_r,
                  int[] x_i ){

    // separate the parameters
    real zeta = theta[1];
    real eta = theta[2];
    real vi = theta[3];

    // significance indicator
    int signif; 
    signif = fabs(x) > 1.96;

    return( (1/eta) * (1 - signif) + signif ) * exp( normal_lpdf( x | zeta, sqrt(vi) ) );
  }
}"

expose_stan_functions(stanc(model_code = code))

integrand(1, {1,1,1}, real[], int[], 1e-8)

integrate_1d( integrand, negative_infinity(), positive_infinity(), {1, 1, 1}, real[], int[], 1e-8 )

但是对integrandintegrate_1d 的调用都会引发错误

错误:意外的','

就好像我用根本不正确的语法调用函数一样。 (在对integrate_1d 的调用中,我不太确定如何处理xc。我省略了它,因为文档(第9.3.2.4 节)似乎建议它应该是NaN 以获得不定积分。)

如果expose_stan_functions 不是理想的方式,我也欢迎关于交互式调试integrand 的完全不同的建议。

【问题讨论】:

    标签: integration stan rstan


    【解决方案1】:

    integrate_1d 函数是一个 Stan / C++ 函数,所以如果你想从 R 调用它,你需要指定另一个调用它的函数。在你的情况下,你可以这样做

    code = "functions{
    
      real integrand( real x,  // the observed Z-stat
                      real xc,
                      real[] theta,
                      real[] x_r,
                      int[] x_i ){
    
        // separate the parameters
        real zeta = theta[1];
        real eta = theta[2];
        real vi = theta[3];
    
        // significance indicator
        int signif; 
        signif = fabs(x) > 1.96;
    
        return( (1/eta) * (1 - signif) + signif ) * exp( normal_lpdf( x | zeta, sqrt(vi) ) );
      }
    
      real area(real[] theta, data real[] x_r) {
        int x_i[0];
        return integrate_1d(integrand, negative_infinity(), positive_infinity(),
                            theta, x_r, x_i, 1e-8);
      }
    }"
    
    library(rstan)
    expose_stan_functions(stanc(model_code = code))
    area(theta = rep(1, 3), x_r = double()) # yields 1
    

    【讨论】:

    • 这很好用。两个问题:(1)既然area 也是一个 Stan 函数,为什么 R 能够使用它自己的数据类型(例如,θ 的向量而不是实数数组)调用这个函数,即使它不能直接拨打integrate_1d? (2)声明data real[]中的data是什么意思?我似乎无法有效地通过 Google 搜索。
    • 用作标记为data 的参数的表达式不能包含任何参数、转换参数或转换参数或模型块中的局部变量。反之,它只能包含数据变量、转换数据变量或转换数据块中的局部变量。它也可以与生成的数量一起使用。对于map_rect 函数,如果在data 参数中使用局部变量,则并行执行也将不起作用。
    • 我不相信 Stan Math 库中已经有 area 函数;否则解析器会抛出关于重复符号的错误。当您调用 rstan::expose_stan_functions 时,会创建调用同名 C++ 函数的 R 包装函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 2010-09-18
    • 2011-03-19
    相关资源
    最近更新 更多